目前,比较流行开源集群管理工具是haproxy + keepalived 是比较简单易学的组合;
主机环境:
Cent OS 6.8x64
软件版本:
haproxy-1.5.18-6.el7.x86_64.rpm
keepalived-1.3.5-1.el7.x86_64.rpm
网络配置:
web1-eth0:192.168.134.137
web1-eth0:192.168.134.139
VIP:192.168.134.200
后端web应用IP: 192.168.134.10
192.168.134.20
192.168.134.30
安装、配置、启动keepalived:
web1节点:
# yum -y install keepalived
# vim /etc/keepalived/keepalived.conf
1 ! Configuration File for keepalived 2 vrrp_script chk_http_port { 3 script "/etc/keepalived/check_haproxy.sh" 4 interval 2 5 weight 2 6 7 global_defs { 8 notification_email { 9 [email protected] 10 } 11 notification_email_from [email protected] 12 smtp_server 127.0.0.1 13 smtp_connect_timeout 120 14 router_id haproxy1 15 } 16 17 vrrp_instance web { 18 state MASTER 19 interface eth0 20 virtual_router_id 200 21 priority 100 22 advert_int 1 23 authentication { 24 auth_type PASS 25 auth_pass 1111 26 } 27 track_script { 28 chk_http_port 29 } 30 virtual_ipaddress { 31 192.168.134.200 32 } 33 } 34 }
# /etc/init.d/keepalived start
# chkconfig keepalived on
# chkconfig --list keepalived
keepalived 0:off 1:off 2:on 3:on 4:on 5:on 6:off
# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:1d:0c:d2 brd ff:ff:ff:ff:ff:ff
inet 192.168.134.137/24 brd 192.168.134.255 scope global eth0
inet 192.168.134.200/32 scope global eth0
可以看到,浮动IP配置成功!!!
web2节点:
# yum -y install keepalived
# vim /etc/keepalived/keepalived.conf
1 ! Configuration File for keepalived 2 vrrp_script chk_http_port { 3 script "/etc/keepalived/check_haproxy.sh" 4 interval 2 5 weight 2 6 } 7 8 global_defs { 9 notification_email { 10 [email protected] 11 } 12 notification_email_from [email protected] 13 smtp_server 127.0.0.1 14 smtp_connect_timeout 120 15 router_id haproxy1 16 } 17 18 vrrp_instance web { 19 state BACKUP 20 interface eth0 21 virtual_router_id 200 22 priority 50 23 advert_int 1 24 authentication { 25 auth_type PASS 26 auth_pass 1111 27 } 28 29 track_script { 30 chk_http_port 31 } 32 33 virtual_ipaddress { 34 192.168.134.200 35 } 36 }
编写状态检查脚本:
# vim /etc/keepalived/check_haproxy.sh
1 #!/bin/bash 2 B=`ip addr show |grep 192.168.134.200|wc -l` 3 if [ $B -eq 1 ];then 4 A=`ps -C haproxy --no-header |wc -l` 5 if [ $A -eq 0 ];then 6 /usr/sbin/haproxy -f /etc/haproxy/haproxy.cfg 7 sleep 3 8 if [ `ps -C haproxy --no-header |wc -l` -eq 0 ];then 9 /etc/init.d/keepalived stop 10 fi 11 fi 12 fi
# /etc/init.d/keepalived start
# chkconfig keepalived on
# chkconfig --list keepalived
keepalived 0:off 1:off 2:on 3:on 4:on 5:on 6:off
# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:1d:0c:d2 brd ff:ff:ff:ff:ff:ff inet 192.168.134.137/24 brd 192.168.134.255 scope global eth0
可以看到,备机节点上浮动IP配置成功!!!
测试VIP是否会漂移漂移:
首先关掉web1节点的keepalived:
# /etc/init.d/keepalived stop
检查web2节点是否已经拿到VIP地址
# ip addr show eth0
安装、配置、启动haproxy:web1节点和web2节点配置相同
# yum -y install haproxy
#vim /etc/haproxy/haproxy.cfg
1 global 2 log 127.0.0.1 local2 3 chroot /var/lib/haproxy 4 pidfile /var/run/haproxy.pid 5 maxconn 4000 6 user haproxy 7 group haproxy 8 daemon 9 # turn on stats unix socket 10 stats socket /var/lib/haproxy/stats 11 12 defaults 13 mode http 14 log global 15 option httplog 16 option dontlognull 17 option http-server-close 18 option forwardfor except 127.0.0.0/8 19 option redispatch 20 retries 3 21 timeout http-request 10s 22 timeout queue 1m 23 timeout connect 10s 24 timeout client 1m 25 timeout server 1m 26 timeout http-keep-alive 10s 27 timeout check 10s 28 maxconn 3000 29 30 listen stats 31 mode http 32 bind 0.0.0.0:8888 33 stats enable 34 stats uri /haproxy-status 35 stats auth haproxy:yum 36 37 frontend healthcheck 38 bind 192.168.134.200:80 39 mode http 40 option httpclose 41 log global 42 default_backend webserver 43 44 backend webserver 45 option forwardfor header X-REAL-IP 46 option httpchk HEAD / HTTP/1.0 47 balance source 48 server web1 192.168.134.10:80 inter 2000 rise 15 fall 30 check maxconn 2000 49 server web3 192.168.134.20:80 inter 2000 rise 15 fall 30 check maxconn 2000 50 server web3 192.168.134.30:80 inter 2000 rise 15 fall 30 check maxconn 2000
# /etc/init.d/haproxy start
# chkconfig haproxy on
# chkconfig --list haproxy
测试故障自愈:
停掉web1节点的haproxy;
# /etc/init.d/haproxy stop
检查服务是否自愈;
# /etc/init.d/haproxy status
停掉web1节点的keepalive的服务
# /etc/init.d/keepalived stop
检查web1节点的VIP是否漂移;
# ip addr show eth0
检查web2节点的VIP是否存在;
# ip addr show eth0