實驗環境:html
兩臺主機均運行Apache提供Web服務;VIP爲192.168.0.5。shell
首先確認兩臺主機的Apache服務是否正常:操作系統
訪問http://192.168.0.88code
訪問http://192.168.0.110router
配置兩臺主機上的Keepalived,配置文件以下:server
! Configuration File for keepalived global_defs { notification_email { sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script check_httpd { script "/usr/bin/killall -0 httpd" interval 2 fall 2 rise 2 } vrrp_instance HTTPD { state MASTER interface ens160 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass aaaa } virtual_ipaddress { 192.168.0.5/24 } track_script { check_httpd } }
這裏沒有設置不搶佔模式,且沒有配置郵件發送功能。htm
主機B的配置文件大部分同主機A,僅有其中的state和priority與主機A不一樣blog
! Configuration File for keepalived global_defs { notification_email { sysadmin@firewall.loc } notification_email_from Alexandre.Cassen@firewall.loc smtp_server 192.168.200.1 smtp_connect_timeout 30 router_id LVS_DEVEL } vrrp_script check_httpd { script "/usr/bin/killall -0 httpd" interval 2 fall 2 rise 2 } vrrp_instance HTTPD { state BACKUP interface ens160 virtual_router_id 51 priority 80 advert_int 1 authentication { auth_type PASS auth_pass aaaa } virtual_ipaddress { 192.168.0.5/24 } track_script { check_httpd } }
配置完成Keepalived後啓動Keepalived,在主機A上經過ip addr命令能夠查看到VIP已經綁定到主機A上:ip
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:c0:e8:97 brd ff:ff:ff:ff:ff:ff inet 192.168.0.88/24 brd 192.168.0.255 scope global noprefixroute dynamic ens160 valid_lft 54444sec preferred_lft 54444sec inet 192.168.0.5/24 scope global secondary ens160 valid_lft forever preferred_lft forever inet6 fe80::ec4e:853a:570d:bf79/64 scope link noprefixroute valid_lft forever preferred_lft forever
此時訪問http://192.168.0.5get
實際訪問的是主機A提供的Web服務。
中止主機A的Apache服務,查看是否發生主備切換:
主機A上執行ip addr命令
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:c0:e8:97 brd ff:ff:ff:ff:ff:ff inet 192.168.0.88/24 brd 192.168.0.255 scope global noprefixroute dynamic ens160 valid_lft 54183sec preferred_lft 54183sec inet6 fe80::ec4e:853a:570d:bf79/64 scope link noprefixroute valid_lft forever preferred_lft forever
能夠看到VIP已經在主機A上解綁。在主機B上執行ip addr命令
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:80:6a:31 brd ff:ff:ff:ff:ff:ff inet 192.168.0.110/24 brd 192.168.0.255 scope global noprefixroute dynamic ens160 valid_lft 76521sec preferred_lft 76521sec inet 192.168.0.5/24 scope global secondary ens160 valid_lft forever preferred_lft forever inet6 fe80::91ad:f9d8:5386:ba92/64 scope link noprefixroute valid_lft forever preferred_lft forever
能夠看到VIP已經綁定到了主機B上,此時訪問http://192.168.0.5
已是主機B在提供Web服務了。
由於這裏沒有配置不搶佔模式,因此此時恢復主機A的Apache服務,查看是否會再次進行主備切換:
在主機A上執行ip addr命令
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:c0:e8:97 brd ff:ff:ff:ff:ff:ff inet 192.168.0.88/24 brd 192.168.0.255 scope global noprefixroute dynamic ens160 valid_lft 53899sec preferred_lft 53899sec inet 192.168.0.5/24 scope global secondary ens160 valid_lft forever preferred_lft forever inet6 fe80::ec4e:853a:570d:bf79/64 scope link noprefixroute valid_lft forever preferred_lft forever
此時VIP又從新綁定到主機A上了,訪問http://192.168.0.5
不搶佔模式僅在優先級高的主機上進行設置,因此主機B上的Keepalived不用進行配置。修改主機A的Keepalived配置文件(僅展現主要部分):
vrrp_instance HTTPD { state BACKUP interface ens160 virtual_router_id 51 priority 100 advert_int 1 authentication { auth_type PASS auth_pass aaaa } virtual_ipaddress { 192.168.0.5/24 } track_script { check_httpd } nopreempt
此時中止主機A上的Apache:
主機A的IP信息
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:c0:e8:97 brd ff:ff:ff:ff:ff:ff inet 192.168.0.88/24 brd 192.168.0.255 scope global noprefixroute dynamic ens160 valid_lft 53418sec preferred_lft 53418sec inet6 fe80::ec4e:853a:570d:bf79/64 scope link noprefixroute valid_lft forever preferred_lft forever
主機B的IP信息
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:80:6a:31 brd ff:ff:ff:ff:ff:ff inet 192.168.0.110/24 brd 192.168.0.255 scope global noprefixroute dynamic ens160 valid_lft 75784sec preferred_lft 75784sec inet 192.168.0.5/24 scope global secondary ens160 valid_lft forever preferred_lft forever inet6 fe80::91ad:f9d8:5386:ba92/64 scope link noprefixroute valid_lft forever preferred_lft forever
訪問http://192.168.0.5
從以上信息能夠看出,發生了一次主備切換。此時恢復主機A的Apache服務,查看是否會再次發生主備切換:
主機A的IP信息
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:c0:e8:97 brd ff:ff:ff:ff:ff:ff inet 192.168.0.88/24 brd 192.168.0.255 scope global noprefixroute dynamic ens160 valid_lft 53224sec preferred_lft 53224sec inet6 fe80::ec4e:853a:570d:bf79/64 scope link noprefixroute valid_lft forever preferred_lft forever
主機B的IP信息
2: ens160: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 link/ether 00:0c:29:80:6a:31 brd ff:ff:ff:ff:ff:ff inet 192.168.0.110/24 brd 192.168.0.255 scope global noprefixroute dynamic ens160 valid_lft 75618sec preferred_lft 75618sec inet 192.168.0.5/24 scope global secondary ens160 valid_lft forever preferred_lft forever inet6 fe80::91ad:f9d8:5386:ba92/64 scope link noprefixroute valid_lft forever preferred_lft forever
訪問http://192.168.0.5
從以上信息可見,在配置了不搶佔模式後,在主機A的Apache恢復正常以後主機A並不會再次成爲Master,不會再次發生主備切換。