Haproxy

1.ospf协议:用来做lvs的水平括扩展(高可用)
2.Fnat模式:
解决了多vlan|session共享
特性:
    LIP:多VL,支持后端RS不在同一个网络中(TUN实施成本过高)
    lvs不具备对后端RS健康检查     
性能:DR>TUN>NAT>FNAT
3.DDOS攻击|
4.TCP三次握手

Haproxy

源码编译&&制作rpm包
(1)源码编译
[root@server1 ~]# ls
haproxy-1.6.11.tar.gz
[root@server1 ~]# cd haproxy-1.6.11
[root@server1 haproxy-1.6.11]# ls
CHANGELOG     doc       include      Makefile  src      VERDATE
contrib       ebtree    LICENSE      README    SUBVERS  VERSION
CONTRIBUTING  examples  MAINTAINERS  ROADMAP   tests
[root@server1 haproxy-1.6.11]# less README 
[root@server1 haproxy-1.6.11]# yum install pcre-devel openssh-devel zlib-devel -y
[root@server1 haproxy-1.6.11]# make TARGET=linux26 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 PREFIX=/usr/local/haproxy
[root@server1 haproxy-1.6.11]# make TARGET=linux26 USE_PCRE=1 USE_OPENSSL=1 USE_ZLIB=1 PREFIX=/usr/local/haproxy install
[root@server1 haproxy-1.6.11]# cd examples/
[root@server1 examples]# ls
acl-content-sw.cfg     debug2ansi    haproxy.spec           ssl.cfg
auth.cfg               debug2html    haproxy.vim            stats_haproxy.sh
check                  debugfind     init.haproxy           transparent_proxy.cfg
check.conf             errorfiles    option-http_proxy.cfg
content-sw-sample.cfg  haproxy.init  seamless_reload.txt
[root@server1 examples]# cp haproxy.init /etc/init.d/haproxy
[root@server1 examples]# chmod +x /etc/init.d/haproxy 


(2)制作源码包

[root@server1 examples]# yum install rpm-build -y
[root@server1 examples]# rpmbuild -bb haproxy.spec 
error: File /root/rpmbuild/SOURCES/haproxy-1.6.11.tar.gz: No such file or directory
[root@server1 examples]# cp ~/haproxy-1.6.11.tar.gz  ~/rpmbuild/SOURCES/
[root@server1 examples]# cd
[root@server1 ~]# cd rpmbuild/RPMS/
[root@server1 RPMS]# ls
x86_64
[root@server1 RPMS]# cd x86_64/
[root@server1 x86_64]# ls
haproxy-1.6.11-1.x86_64.rpm
[root@server1 x86_64]# rpm -qpl haproxy-1.6.11-1.x86_64.rpm 
/etc/haproxy
/etc/rc.d/init.d/haproxy
/usr/sbin/haproxy
/usr/share/doc/haproxy-1.6.11
/usr/share/doc/haproxy-1.6.11/CHANGELOG
/usr/share/doc/haproxy-1.6.11/README
/usr/share/doc/haproxy-1.6.11/architecture.txt
/usr/share/doc/haproxy-1.6.11/configuration.txt
/usr/share/doc/haproxy-1.6.11/intro.txt
/usr/share/doc/haproxy-1.6.11/management.txt
/usr/share/doc/haproxy-1.6.11/proxy-protocol.txt
/usr/share/man/man1/haproxy.1.gz
[root@server1 x86_64]# 

源码安装

[root@server1 x86_64]# rm -fr /usr/local/haproxy/
[root@server1 x86_64]# ls
haproxy-1.6.11-1.x86_64.rpm
[root@server1 x86_64]# rpm -ivh haproxy-1.6.11-1.x86_64.rpm 
Preparing...                ########################################### [100%]
   1:haproxy                ########################################### [100%]
[root@server1 ~]# cd haproxy-1.6.11/
[root@server1 haproxy-1.6.11]# cd examples/
[root@server1 examples]# cp content-sw-sample.cfg /etc/haproxy/haproxy.cfg
[root@server1 x86_64]# 

实现负载均衡

[root@server1 ~]# groupadd -g 200 haproxy
[root@server1 ~]# useradd -u 200 -g 200 -M -s /sbin/nologin haproxy
[root@server1 ~]# id haproxy
uid=200(haproxy) gid=200(haproxy) groups=200(haproxy)
[root@server1 ~]# vim /etc/haproxy/haproxy.cfg
.....
    10  global
    11          maxconn         10000
    12          stats socket    /var/run/haproxy.stat mode 600 level admin
    13          log             127.0.0.1 local0
    14          uid             200
    15          gid             200
    16          chroot          /var/empty
    17          daemon
    18  defaults
    19          mode            http
    20      log     global
    21      option      httplog
    22      #option     dontlogull
    23      monitor-uri /monitoruri
    24      maxconn     8000
    25          timeout client  30s
    26  
    27          stats uri       /admin/stats
    28  
    29          option prefer-last-server
    30          retries         2
    31          option redispatch
    32          timeout connect 5s
    33          timeout server  5s
    34  
    35  frontend public
    36          bind            172.25.66.1:80 name clear
    37  
    38  #        use_backend     static if { hdr_beg(host) -i img }
    39          default_backend dynamic
    40  
    41  backend dynamic     #定义算法(haproxy的8种调度算法)
    42          server          statsrv1 172.25.66.2:80 check inter 1000    weight 2    #定义后端服务器及权重
    43          server          statsrv2 172.25.66.3:80 check inter 1000
    44  
.....
[root@server1 ~]# /etc/init.d/haproxy start
Starting haproxy:                                          [  OK  ]
[root@server1 ~]#
[root@server1 ~]# vim /etc/security/limits.conf 
 51 haproxy         -       nodile          8000

测试

[root@foundation66 Desktop]# curl 172.25.66.1/monitoruri
<html><body><h1>200 OK</h1>
Service ready.
</body></html>
[root@foundation66 Desktop]# curl 172.25.66.1/admin/stats
[kiosk@foundation66 Desktop]$ for i in range {1..5};do curl 172.25.66.1;done
Server2
Server3
Server2
Server3
Server2
Server3
[kiosk@foundation66 Desktop]$ 

添加日志

**haproxy算法(8种负载均衡算法)

实现重定向

[root@server1 ~]# vim /etc/haproxy/haproxy.cfg 
.....
 37         acl badip src 172.25.66.250     #禁IP
 38         #http-request deny if badip
 39         redirect location http://172.25.66.4 if badip   #重定向到4上
.....

测试

浏览器访问:
172.25.66.1--->http://172.25.66.4/  #自动实现跳转

支持动静页面

[root@server1 ~]# vim /etc/haproxy/haproxy.cfg 
.....
    35  frontend public
    36          bind            172.25.66.1:80 name clear
    37      acl badip src 172.25.66.250
    38      acl url_static path_end -i .png .gpeg .gif
    39      #http-request deny if badip
    40      #redirect location http://172.25.66.4 if badip
    41      acl write method POST
    42      acl write method PUT
    43  
    44      use_backend     static if write
    45          default_backend dynamic
    46      
    48  backend dynamic     #动态页面定义到2
    49      balance leastconn
    50          server          statsrv1 172.25.66.2:80 check inter 1000
    51  backend static  #静态页面定义到3
    52          server          statsrv2 172.25.66.3:80 check inter 1000
 .....

RS端:

必须支持php
yum install php #重启httpd
通过上传照片实现
**先下载upload目录|修改权限|移动文件|修改文件大小
[root@server2 html]# chmod 777 upload
[root@server2 ~]# cd /var/www/html/upload/
[root@server2 html]# ls
index.html  index.php  upload  upload_file.php
[root@server2 html]# vim  upload_file.php   #修改图片大小
[root@server2 upload]# 

浏览器访问:

http://172.25.66.1  先broswer,然后submit

POST测试

[root@server3 ~]# cd /var/www/html/upload
[root@server3 upload]# ls
OSI.gif     #上传成功
[root@server3 upload]# 

Pacemaker+haproxy

Server1|Server2安装和配置pacmaker|haproxy

添加VIP

[root@server1 ~]# crm
crm(live)# configure 
crm(live)configure#  property stonith-enabled=false
crm(live)configure# primitive vip ocf:heartbeat:IPaddr2 params ip=172.25.66.100 cidr_netmask=24 op monitor interval=1min    #添加vip
crm(live)configure# commit
crm(live)configure# bye
bye
[root@server1 ~]#

添加haproxy服务

[root@server1 ~]# crm 
crm(live)# configure 
crm(live)configure# primitive haproxy lsb:haproxy op monitor interval=1min  #添加服务
crm(live)configure# property no-quorum-policy=ignore
crm(live)configure# show
node server1
node server4
primitive haproxy lsb:haproxy \
    op monitor interval="1min"
primitive vip ocf:heartbeat:IPaddr2 \
    params ip="172.25.66.100" cidr_netmask="24" \
    op monitor interval="1min"
property $id="cib-bootstrap-options" \
    dc-version="1.1.10-14.el6-368c726" \
    cluster-infrastructure="classic openais (with plugin)" \
    expected-quorum-votes="2" \
    stonith-enabled="false"
crm(live)configure# commit 
crm(live)configure# bye 
bye
[root@server1 ~]#


&&&crm_mon监控
**在添加完VIP和haproxy之后,commit,集群自动启动服务,会发现vip和haproxy都开启
    在一个节点(Server1)down掉之后,另一个(Server2)接管
06-05
### HAProxy 配置使用指南 HAProxy 是一个高性能的 TCP/HTTP 负载均衡器,能够帮助用户实现高可用性和负载分发。以下是关于 HAProxy 的配置和使用指南。 #### 1. 基本配置文件结构 HAProxy 的配置文件通常位于 `/etc/haproxy/haproxy.cfg`,其结构分为多个部分,每个部分都有特定的功能[^1]: - **global**: 全局配置,定义日志、用户权限等。 - **defaults**: 默认设置,应用于所有前端和后端,除非被覆盖。 - **frontend**: 定义监听的 IP 和端口,以及请求匹配规则。 - **backend**: 定义后端服务器池,指定如何分发流量。 - **listen**: 同时包含 frontend 和 backend 的功能,适合简单的场景。 以下是一个典型的 HAProxy 配置示例: ```plaintext global log /dev/log local0 log /dev/log local1 notice chroot /var/lib/haproxy stats socket /run/haproxy/admin.sock mode 660 level admin expose-fd listeners stats timeout 30s user haproxy group haproxy daemon defaults log global mode http option httplog option dontlognull retries 3 timeout connect 5000ms timeout client 50000ms timeout server 50000ms frontend http_front bind *:80 default_backend http_back backend http_back balance roundrobin server web1 192.168.1.10:80 check server web2 192.168.1.11:80 check ``` #### 2. 配置项详解 - **global**: 定义全局参数,如日志路径、运行用户等。 - **defaults**: 设置默认行为,例如超时时间、重试次数等。 - **frontend**: 定义监听的端口和流量匹配规则。`bind *:80` 表示监听所有 IP 的 80 端口。 - **backend**: 定义后端服务器池,`balance roundrobin` 表示使用轮询算法分发流量[^1]。 #### 3. 测试配置文件 在修改配置文件后,可以通过以下命令测试配置文件是否正确: ```bash haproxy -c -f /etc/haproxy/haproxy.cfg ``` 如果配置无误,将显示 `Configuration file is valid`。 #### 4. 启动和重启服务 启动或重启 HAProxy 服务可以使用以下命令: ```bash systemctl start haproxy systemctl restart haproxy ``` #### 5. 高可用集群配置 为了实现高可用性,可以结合 Keepalived 使用。通过 Keepalived 提供虚拟 IP (VIP),确保主备切换时流量不会中断[^2]。以下是 Keepalived 的基本配置示例: ```plaintext vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1234 } virtual_ipaddress { 192.168.1.100 } } ``` #### 6. Kubernetes 中的 HAProxy Ingress 在 Kubernetes 环境中,可以通过 ConfigMap 自定义 HAProxy Ingress 的行为[^4]。以下是一个 ConfigMap 示例: ```yaml apiVersion: v1 kind: ConfigMap metadata: name: haproxy-ingress-config data: global: | log /dev/log local0 chroot /var/lib/haproxy user haproxy group haproxy daemon defaults: | mode http timeout connect 5s timeout client 50s timeout server 50s ``` #### 7. 测试 HAProxy 是否正常工作 可以通过访问 HAProxy 的 VIP 地址来测试其是否正常工作。例如,使用 `curl` 命令发送 HTTP 请求: ```bash curl http://192.168.0.100 ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值