Keepalived+HAproxy
拓扑 ++++++++++++ + Client + 192.168.122.200/24 (真实机做客户端) ++++++++++++ ________________|_______________ 浮动IP eth0:1 192.168.122.199/24 | | +++++++++++++ +++++++++++++ + HAproxy master + + HAproxy backup + +++++++++++++ +++++++++++++ DIP eth0 192.168.122.201/24 DIP eth0 192.168.122.202/24 |______________________________| ______________|_______________ | | | | ++++++++++++ ++++++++++++ + HTML A + + HTML B + ++++++++++++ ++++++++++++ eth0 192.168.122.203/24 eth0 192.168.122.204/24 *分别在HAproxy master、HAproxy backup 上部署浮动IP,测试2个HAproxy单独工作都正常。测试完成后都撤掉浮动IP。 ############################################################## 解析 192.168.122.200 client.com 192.168.122.201 HAm.com 192.168.122.202 HAb.com 192.168.122.203 web1.com 192.168.122.204 web2.com ############################################################## web端 HTML A B [root@web1 ~]# yum install -y httpd 192.168.122.203/24 [root@web2 ~]# yum install -y httpd 192.168.122.204/24 [root@web1 ~]# systemctl start httpd [root@web1 ~]# echo web1111111111 > /var/www/html/index.html [root@web2 ~]# echo web2222222222 > /var/www/html/index.html ############################################################## HAproxy master & HAproxy backup # yum install -y haproxy keepalived [root@HAm ~]## cat /etc/haproxy/haproxy.cfg 注意:haproxy2配置文件一样 global log 127.0.0.1 local2 chroot /var/lib/haproxy pidfile /var/run/haproxy.pid maxconn 4000 user haproxy group haproxy daemon # turn on stats unix socket stats socket /var/lib/haproxy/stats defaults mode http log global option httplog option dontlognull option http-server-close option forwardfor except 127.0.0.0/8 option redispatch retries 3 timeout http-request 10s timeout queue 1m timeout connect 10s timeout client 1m timeout server 1m timeout http-keep-alive 10s timeout check 10s maxconn 3000 frontend main *:80 acl html url_reg -i \.html$ use_backend webserver if html backend webserver balance roundrobin option httpchk GET /index.html # cookie SERVERID insert indirect nocache server html-A 192.168.122.203:80 weight 1 check inter 2000 rise 2 fall 5 server html-B 192.168.122.204:80 weight 1 check inter 2000 rise 2 fall 5 backend app balance roundrobin server app1 127.0.0.1:5001 check server app2 127.0.0.1:5002 check server app3 127.0.0.1:5003 check server app4 127.0.0.1:5004 check [root@HAm ~]# scp /etc/haproxy/haproxy.cfg 192.168.122.202:/etc/haproxy/haproxy.cfg [root@HAm ~]# cat /etc/keepalived/keepalived.conf 注意:在haproxy2中 主要修改BACKUP和100 ! Configuration File for keepalived global_defs { notification_email { root@localhost } notification_email_from Alexandre.keepalived@localhost smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id HAproxy } vrrp_instance VI_1 { state MASTER 切换 (BACKUP) interface eth0 virtual_router_id 51 priority 150 切换 (100) advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.122.199/24 } } 注意:当haproxy1宕机时候是192.168.122.199/24切换到haproxy2下 [root@HAm ~]# ip a eth0: inet 192.168.122.201/24 brd 192.168.122.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever inet 192.168.122.199/24 scope global secondary eth0 valid_lft forever preferred_lft forever [root@HAb ~]# ip a eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:90:68:be:9e:65 brd ff:ff:ff:ff:ff:ff inet 192.168.122.202/24 brd 192.168.122.255 scope global noprefixroute eth0 valid_lft forever preferred_lft forever 自动切换脚本:(脚本主要是为了当haproxy1宕机时候切换到haproxy2上继续操作) [root@HAm ~]# vi /etc/keepalived/chk_haproxy.sh #!/bin/bash # 去掉表头定义haproxy 出现的次数为A A=`ps -C haproxy --no-header | wc -l` # 如果出现次数为0 ,则没有启动haproxy服务 if [ $A -eq 0 ] then systemctl start haproxy sleep 3 # 如果出现次数等于0,则停止haproxy服务 if [ `ps -C haproxy --no-header | wc -l ` -eq 0 ] then stop keepalived fi fi [root@HAm ~]# chmod +x /etc/keepalived/chk_haproxy.sh [root@HAm ~]# scp /etc/keepalived/chk_haproxy.sh 192.168.122.202:/etc/keepalived/ # systemctl start haproxy # systemctl start keepalived 脚本 [root@HAm ~]# vi /etc/keepalived/chk_ipvsadm.sh #!/bin/bash # 去掉表头定义ipvsadm 出现的次数为A A=`ipvsadm -L | wc -l` # 如果出现次数为0 ,则没有启动ipvsadm服务 if [ $A -lt 6 ] then systemctl start keepalived sleep 3 # 如果出现次数等于0,则停止ipvsadm服务 if [ `ipvsadm -L | wc -l ` -lt 6 ] then stop keepalived fi fi [root@HAm ~]# chmod +x /etc/keepalived/chk_ipvsadm.sh [root@HAm ~]# scp /etc/keepalived/chk_ipvsadm.sh 192.168.122.202:/etc/keepalived/chk_ipvsadm.sh ############################################################## 效果: [root@client ~]# curl 192.168.122.199/index.html web1111111111 [root@client ~]# curl 192.168.122.199/index.html web2222222222 [root@client ~]# curl 192.168.122.199/index.html web1111111111 [root@client ~]# curl 192.168.122.199/index.html web2222222222 注意:当haproxy1 宕机时候依然可以继续运行