拓撲 ++++++++++++ + 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 宕機時候依然能夠繼續運行