HAProxy+Keepalived 清疚 2022-08-07 14:48 160阅读 0赞 安装HAProxy请参考 [root@A local]# yum install -y gcc [root@A local]# tar zxvf haproxy-1.4.24.tar.gz [root@A local]# cd haproxy-1.4.24 [root@A haproxy-1.4.24]# make install [root@A haproxy-1.4.24]# mkdir -p /usr/local/haproxy/etc [root@A haproxy-1.4.24]# mkdir -p /usr/local/haproxy/sbin [root@A haproxy-1.4.24]# cp examples/haproxy.cfg /usr/local/haproxy/etc [root@A haproxy-1.4.24]# ln -s /usr/local/sbin/haproxy /usr/local/haproxy/sbin/haproxy [root@A haproxy-1.4.24]# vi /usr/local/haproxy/etc/haproxy.cfg ![复制代码][copycode.gif] 按 Ctrl+C 复制代码 global log 127.0.0.1 local0 maxconn 4096 \#最大连接数 chroot /usr/local/haproxy uid 99 \#所属运行的用户UID gid 99 \#所属运行用用户组 daemon \#以后台形式运行HAProxy nbproc 1 \#启动1个实例,可以启多个来提高效率 defaults log global mode http option httpclose \#每次请求完毕后主动关闭http通道 option redispatch \#当serverId对应的服务器挂掉后,强制定向到其他健康的服务器 option forwardfor \# option abortonclose \#当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接 option dontlognull \#保证HAProxy不记录上级负载均衡发送过来的用于检测状态没有数据的心跳包 retries 2 maxconn 2000 balance source \#如果想让HAProxy按照客户端的IP地址进行负载均衡策略,即同一IP地址的所有请求都发送到同一服务器时,需要配置此选项 contimeout 5000 clitimeout 50000 srvtimeout 50000 listen web\_proxy 10.10.3.163:80 mode http server web1 10.10.3.13:8817 cookie app1inst1 check inter 2000 rise 2 fall 5 server web2 10.10.3.151:8400 cookie app1inst2 check inter 2000 rise 2 fall 5 listen scoket\_proxy 10.10.3.163:12315 mode tcp server scoket1 10.10.3.158:12315 check inter 2000 rise 2 fall 5 server scoket2 10.10.3.10:12315 check inter 2000 rise 2 fall 5 listen admin\_stats bind \*:1080 \#监听端口 mode http \#http的7层模式 option httplog \#采用http日志格式 log 127.0.0.1 local0 err maxconn 10 stats refresh 30s \#统计页面自动刷新时间 stats uri /stats \#统计页面url stats realm XingCloud\\ Haproxy \#统计页面密码框上提示文本 stats auth admin:admin \#统计页面用户名和密码设置 stats hide-version \#隐藏统计页面上HAProxy的版本信息 按 Ctrl+C 复制代码 启动Haproxy haproxy -f /usr/local/haproxy/etc/haproxy.cfg 停止Haproxy killall haproxy 访问 http://10.10.3.163:1080/stats 配置直接重定向URL ![16135825-c202f836437c45548b7b0387ada4d800.jpg][] 现在HAProxy的算法也非常多,并不比专业的F5/LVS算法少,常用的算法有如下8种: balance roundrobin,表示简单的轮询,建议关注; balance static-rr,表示根据权重,建议关注; balance leastconn,表示最少连接者先处理,建议关注; balance source,表示根据请求源IP,跟Nginx的ip\_hash算法相似,建议关注; balance uri,表示根据请求的URI; balance url\_param,表示根据请求的URl参数; balance hdr(name),表示根据HTTP请求头来锁定每一次HTTP请求; balance rdp-cookie(name),表示根据据cookie(name)来锁定并哈希每一次TCP请求。 安装Keepalived 1、下载安装依赖包 yum install -y wget make gcc openssl-devel popt-devel 2、下载、安装Keepalived wget http://www.keepalived.org/software/keepalived-1.2.8.tar.gz tar zxvf keepalived-1.2.8.tar.gz cd keepalived-1.2.8 ./configure --prefix=/usr/local/keepalived make make install 3、安装成功后做成服务模式。 cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/ 4、配置文件 mkdir -p /etc/keepalived/ cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf chmod +x /etc/init.d/keepalived vi /etc/keepalived/keepalived.conf ![复制代码][copycode.gif] global_defs { router_id LVS_DEVEL } #监测haproxy进程状态,每2秒执行一次 vrrp_script chk_haproxy { script "/etc/keepalived/chk_haproxy.sh" interval 2 weight 2 } vrrp_instance VI_1 { state MASTER #从服务器为BACKUP interface eth0 virtual_router_id 51 priority 100 #从服务器要低于100 advert_int 1 mcast_src_ip 10.10.3.171 #MASTER服务器IP,从服务器写从服务器的IP authentication { auth_type PASS auth_pass 111111 } track_script { chk_haproxy #监测haproxy进程状态 } virtual_ipaddress { 10.10.3.170 #虚拟IP } } ![复制代码][copycode.gif] chk\_haproxy.sh内容 vi /etc/keepalived/chk_haproxy.sh ![复制代码][copycode.gif] status=$(ps aux|grep haproxy | grep -v grep | grep -v bash | wc -l) if [ "${status}" = "0" ]; then /etc/init.d/haproxy start status2=$(ps aux|grep haproxy | grep -v grep | grep -v bash |wc -l) if [ "${status2}" = "0" ]; then /etc/init.d/keepalived stop fi fi ![复制代码][copycode.gif] 启动和关闭服务 service keepalived start service keepalived stop 高可用测试 ![复制代码][copycode.gif] 1、将Master上的keepalived停止 查看Master上的日志 tail -f /var/log/messages Sep 23 01:24:34 181 Keepalived[3125]: Stopping Keepalived v1.2.8 (09/23,2013) Sep 23 01:24:34 181 Keepalived_vrrp[3127]: VRRP_Instance(VI_1) sending 0 priority Sep 23 01:24:34 181 Keepalived_vrrp[3127]: VRRP_Instance(VI_1) removing protocol VIPs. 发现Master释放了VIP 再查看Backup上的日志 Sep 23 01:20:26 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) Transition to MASTER STATE Sep 23 01:20:27 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) Entering MASTER STATE Sep 23 01:20:27 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) setting protocol VIPs. Sep 23 01:20:27 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.10.3.180 Sep 23 01:20:27 182 Keepalived_healthcheckers[2886]: Netlink reflector reports IP 10.10.3.180 added 发现Backup绑定了VIP 2、将Master上的keepalived启动 查看Master上的日志 tail -f /var/log/messages Sep 23 01:27:56 181 Keepalived_vrrp[3450]: VRRP_Instance(VI_1) Entering MASTER STATE Sep 23 01:27:56 181 Keepalived_vrrp[3450]: VRRP_Instance(VI_1) setting protocol VIPs. Sep 23 01:27:56 181 Keepalived_vrrp[3450]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 10.10.3.180 Sep 23 01:27:56 181 Keepalived_healthcheckers[3449]: Netlink reflector reports IP 10.10.3.180 added 发现Master绑定了VIP 再查看Backup上的日志 Sep 23 01:21:15 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) Received higher prio advert Sep 23 01:21:15 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) Entering BACKUP STATE Sep 23 01:21:15 182 Keepalived_vrrp[2887]: VRRP_Instance(VI_1) removing protocol VIPs. Sep 23 01:21:15 182 Keepalived_healthcheckers[2886]: Netlink reflector reports IP 10.10.3.180 removed 发现Backup释放了VIP [copycode.gif]: /images/20220805/ca8e028a120742bf8ee643e4ff344cf0.png [16135825-c202f836437c45548b7b0387ada4d800.jpg]: /images/20220805/5ad4eb3e26a3491f92a50fcc7291307b.png
还没有评论,来说两句吧...