上一篇:【 Keepalived 】Nginx or Http 主-备模式
在此基础上进行修改并实现 Keepalived主-主模式
首先,需要理解的是:主-备模式是一个VIP在工作,主-主模式则需要两个VIP来工作。一旦其中一台主机出现问题,则两个VIP都会绑定到同一台主机上,待到故障解除,重新启动httpd服务,恢复正常。
ka1 配置文件:
! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server localhost smtp_connect_timeout 30 router_id NodeA } vrrp_script check_nginx { script "/etc/keepalived/bash/check_nginx.sh" interval 5 weight -10 } vrrp_instance VI_1 { state MASTER interface eth0 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_nginx } virtual_ipaddress { 192.168.2.200/24 } } vrrp_instance VI_2 { state BUCKUP interface eth0 virtual_router_id 52 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_nginx } virtual_ipaddress { 192.168.2.201/24 } }
ka2 配置文件:
! Configuration File for keepalived global_defs { notification_email { [email protected] } notification_email_from [email protected] smtp_server localhost smtp_connect_timeout 30 router_id NodeB } vrrp_script check_nginx { script "/etc/keepalived/bash/check_nginx.sh" interval 5 weight -10 } vrrp_instance VI_1 { state BACKUP interface eth1 virtual_router_id 51 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_nginx } virtual_ipaddress { 192.168.2.200/24 } } vrrp_instance VI_2 { state MASTER interface eth1 virtual_router_id 52 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } track_script { check_nginx } virtual_ipaddress { 192.168.2.201/24 } }
测试:
正常情况下:
ka1:
[ka1 [email protected]192.168.2.10 ~]#ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:39:92:4f brd ff:ff:ff:ff:ff:ff inet 192.168.2.10/24 brd 192.168.2.255 scope global eth0 inet 192.168.2.200/24 scope global secondary eth0 inet6 fe80::20c:29ff:fe39:924f/64 scope link valid_lft forever preferred_lft forever
ka2:
[ka2 [email protected]192.168.2.11 ~]#ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:7b:9f:8c brd ff:ff:ff:ff:ff:ff inet 192.168.2.11/24 brd 192.168.2.255 scope global eth1 inet 192.168.2.201/24 scope global secondary eth1 inet6 fe80::20c:29ff:fe7b:9f8c/64 scope link valid_lft forever preferred_lft forever
当ka1 httpd出现错误时:
ka1 VIP被转移 [ka1 [email protected]192.168.2.10 ~]#ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:39:92:4f brd ff:ff:ff:ff:ff:ff inet 192.168.2.10/24 brd 192.168.2.255 scope global eth0 inet6 fe80::20c:29ff:fe39:924f/64 scope link valid_lft forever preferred_lft forever ka2 被绑定了两个VIP [ka2 [email protected]192.168.2.11 ~]#ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:7b:9f:8c brd ff:ff:ff:ff:ff:ff inet 192.168.2.11/24 brd 192.168.2.255 scope global eth1 inet 192.168.2.201/24 scope global secondary eth1 inet 192.168.2.200/24 scope global secondary eth1 inet6 fe80::20c:29ff:fe7b:9f8c/64 scope link valid_lft forever preferred_lft forever
再三强调:在keepalived使用中,脚本很重要很重要。要经过严格的测试调试,才能使用,我这里在贴一遍脚本,上个主-备中也有脚本,需要灵活修改的地方我红色标识出来:
#!/bin/bash pidfile=/var/lock/subsys/`basename $0`.pid if [ -f $pidfile ] && [ -e /proc/`cat $pidfile` ] ; then exit 1 fi trap "rm -rf $pidfile ; exit 0" 1 2 3 15 echo $$ > $pidfile maxfails=3 fails=0 success=0 while [ 1 ] do /usr/bin/wget --timeout=3 --tries=1 http://192.168.2.11/ -q -O /dev/null && ping -c1 192.168.2.1 &> /dev/null if [ $? -ne 0 ] ; then let fails=$[$fails+1] success=0 else fails=0 let success=$[$success+1] fi if [ $fails -ge $maxfails ] ; then fails=0 success=0 #check keepalived is running ? try to stop it /etc/init.d/keepalived status | grep 正在运行 if [ $? -eq 0 ] ; then /usr/bin/logger -is "local service fails $maxfails times ... try to stop keepalived." /etc/init.d/keepalived stop 2>&1 | /usr/bin/logger fi fi if [ $success -gt $maxfails ] ; then #check keepalived is stopped ? try to start it /etc/init.d/keepalived status | grep 已停 if [ $? -eq 0 ] ; then /usr/bin/logger -is "service changes normal, try to start keepalived ." /etc/init.d/keepalived start fi success=0 fi sleep 3 done
时间: 2024-10-10 04:47:58