[root@keepalived-1 ~]# hostnamectl set-hostname keepalived-1 [root@keepalived-1 ~]# yum install keepalived.x86_64
主配置文件:/etc/keepalived/keepalived.conf
主程序文件:/usr/sbin/keepalivedvim
主keepalived服務器:服務器
[root@keepalived-1 ~]# vim /etc/keepalived/keepalived.conf ! Configuration File for keepalived global_defs { notification_email { root@localhost #定義收件人地址 } notification_email_from keepalived@localhost #郵件發件人地址 smtp_server 127.0.0.1 #發送郵件服務器地址 smtp_connect_timeout 5 #鏈接郵件服務器的超時時間 router_id 10.10.10.101 #當前路由的id信息網絡中的節點標識(物理IP地址或主機名) vrrp_mcast_group4 224.0.122.22 #組播地址 } vrrp_instance VI_1 { #一個虛擬路由器的標識, state MASTER #重要選項,初始或狀態;只能有一個是MASTER,餘下的都應該爲BACKUP; interface ens33 # 綁定爲當前虛擬路由器使用的物理接口;通常爲網卡地址; virtual_router_id 11 #當前虛擬路由器的唯一標識(相同組的兩臺機器必須一致) priority 100 #當前主機在此虛擬路徑器中的優先級;範圍1-254; advert_int 1 #vrrp通告的時間間隔;心跳間隔時間。 authentication { #認證方式爲:域共享密鑰 auth_type PASS auth_pass 11112222 #最多不要超過8位 } virtual_ipaddress { 10.10.1.1/16 dev ens33 #(接口輔助地址,vip地址,虛擬地址) } }
備服務器:網絡
更改項以下:tcp
[root@keepalived-2 ~]# vim /etc/keepalived/keepalived.conf global_defs {} 中: router_id 10.10.10.101 #當前路由的id信息網絡中的節點標識(物理IP地址或主機名) vrrp_instance VI_1 {} 中: state BACKUP #重要選項,初始或狀態;只能有一個是MASTER,餘下的都應該爲BACKUP; priority 98 #當前主機在此虛擬路徑器中的優先級;範圍1-254
命令參考以下: 啓動服務器 [root@keepalived-2 ~]# systemctl start keepalived.service 中止服務 [root@keepalived-1 ~]# systemctl stop keepalived.service 查看服務狀態和日誌信息 [root@keepalived-1 ~]# systemctl status keepalived.service 查看ip地址信息 [root@keepalived-1 ~]# ip a l 裝包工具使用: [root@localhost etc]# tcpdump -i ens33 host 224.0.122.22
一、先啓動備keepalived-server抓包並查看日誌信息ide
抓包結果以下:工具
13:39:51.734593 IP 10.10.10.102 > 224.0.122.22: VRRPv2, Advertisement, vrid 11, prio 98, authtype simple, intvl 1s, length 20 13:39:52.737100 IP 10.10.10.102 > 224.0.122.22: VRRPv2, Advertisement, vrid 11, prio 98, authtype simple, intvl 1s, length 20
日誌結果以下:性能
May 25 13:39:57 keepalived-2 Keepalived_vrrp[1742]: Sending gratuitous ARP on ens33 for 10.10.1.1 May 25 13:39:57 keepalived-2 Keepalived_vrrp[1742]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on en....1.1 #<-- May 25 13:39:57 keepalived-2 Keepalived_vrrp[1742]: Sending gratuitous ARP on ens33 for 10.10.1.1
ip 信息ui
ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:92:37:53 brd ff:ff:ff:ff:ff:ff inet 10.10.10.102/16 brd 10.10.255.255 scope global ens33 valid_lft forever preferred_lft forever inet 10.10.1.1/16 scope global secondary ens33#<-- valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe92:3753/64 scope link
再啓動主keepalived-server抓包並查看日誌信息日誌
抓包結果以下:code
13:47:53.028784 IP 10.10.10.102 > 224.0.122.22: VRRPv2, Advertisement, vrid 11, prio 98, authtype simple, intvl 1s, length 20 13:47:54.030591 IP 10.10.10.102 > 224.0.122.22: VRRPv2, Advertisement, vrid 11, prio 98, authtype simple, intvl 1s, length 20 13:47:55.032137 IP 10.10.10.102 > 224.0.122.22: VRRPv2, Advertisement, vrid 11, prio 98, authtype simple, intvl 1s, length 20 13:47:56.033873 IP 10.10.10.102 > 224.0.122.22: VRRPv2, Advertisement, vrid 11, prio 98, authtype simple, intvl 1s, length 20 13:47:56.033883 IP 10.10.10.101 > 224.0.122.22: VRRPv2, Advertisement, vrid 11, prio 100, authtype simple, intvl 1s, length 20#<-- 13:47:57.040529 IP 10.10.10.101 > 224.0.122.22: VRRPv2, Advertisement, vrid 11, prio 100, authtype simple, intvl 1s, length 20 13:47:58.041992 IP 10.10.10.101 > 224.0.122.22: VRRPv2, Advertisement, vrid 11, prio 100, authtype simple, intvl 1s, length 20 13:47:59.043760 IP 10.10.10.101 > 224.0.122.22: VRRPv2, Advertisement, vrid 11, prio 100, authtype simple, intvl 1s, length 20
日誌結果以下:
主: May 25 13:41:31 keepalived-1 Keepalived_vrrp[1872]: Sending gratuitous ARP on ens33 for 10.10.1.1 May 25 13:41:31 keepalived-1 Keepalived_vrrp[1872]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 10.10.1.1#<-- May 25 13:41:31 keepalived-1 Keepalived_vrrp[1872]: Sending gratuitous ARP on ens33 for 10.10.1.1 備: May 25 13:41:25 keepalived-2 Keepalived_vrrp[1761]: VRRP_Instance(VI_1) Received advert with higher priority 100, ours 98 May 25 13:41:25 keepalived-2 Keepalived_vrrp[1761]: VRRP_Instance(VI_1) Entering BACKUP STATE May 25 13:41:25 keepalived-2 Keepalived_vrrp[1761]: VRRP_Instance(VI_1) removing protocol VIPs.
使用ip a l 命令查看ip地址
主 ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:98:88:45 brd ff:ff:ff:ff:ff:ff inet 10.10.10.101/16 brd 10.10.255.255 scope global ens33 valid_lft forever preferred_lft forever inet 10.10.1.1/16 scope global secondary ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe98:8845/64 scope link 備 ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:92:37:53 brd ff:ff:ff:ff:ff:ff inet 10.10.10.102/16 brd 10.10.255.255 scope global ens33#<-- valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe92:3753/64 scope link
再次中止主keepalived-server查看
日誌:
May 25 13:48:29 keepalived-2 Keepalived_vrrp[1781]: Sending gratuitous ARP on ens33 for 10.10.1.1 May 25 13:48:29 keepalived-2 Keepalived_vrrp[1781]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 10.10.1.1 #<-- May 25 13:48:29 keepalived-2 Keepalived_vrrp[1781]: Sending gratuitous ARP on ens33 for 10.10.1.1
抓包:
13:48:22.073929 IP 10.10.10.101 > 224.0.122.22: VRRPv2, Advertisement, vrid 11, prio 100, authtype simple, intvl 1s, length 20 13:48:22.701737 IP 10.10.10.101 > 224.0.122.22: VRRPv2, Advertisement, vrid 11, prio 0, authtype simple, intvl 1s, length 20#<-- 13:48:23.320348 IP 10.10.10.102 > 224.0.122.22: VRRPv2, Advertisement, vrid 11, prio 98, authtype simple, intvl 1s, length 20 13:48:24.321473 IP 10.10.10.102 > 224.0.122.22: VRRPv2, Advertisement, vrid 11, prio 98, authtype simple, intvl 1s, length 20
Ip地址信息:
ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:92:37:53 brd ff:ff:ff:ff:ff:ff inet 10.10.10.102/16 brd 10.10.255.255 scope global ens33 valid_lft forever preferred_lft forever inet 10.10.1.1/16 scope global secondary ens33#<-- valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe92:3753/64 scope link
實驗結果:(搶佔模式分析)
備keepalived先啓動是會發起arp免費廣播,當局域網中已有且只有它一個的話,他會將模式更改成:MASTER,將ip地址添加到本身的路由中,同時也會繼續向局域中發送arp廣播,(只在組播地址中發送,抓包監聽也只能監聽組播地址。)
當主keepalived-server開啓服務器後(宕機恢復後也是如此),同時也接受道備keepalived發送的ARP廣播中附帶的優先級的值98,與本身的優先級進行比對,本身的優先級高於備keepalived-server的優先級,發送arp廣播,他會向組播網絡中發送免費的arp廣播並附帶優先級100的值,備keepalived-server收到後,與本身的優先級比對,沒有100高則轉換模式進入BACKUP模式;主keepalived-server開始發起免費的ARP廣播;模式切換完成。
當主keepalived-server宕機或關閉是會發送一個優先級爲0的arp廣播,備keepalived-server接收到主keepalived的廣播後,發起arp廣播並將本身的模式改成MASTER模式(若是組內擁有三臺以上主機優先級最低的不會發起arp廣播,也沒有日誌信息。只有全部高於最低優先級的keepalived-server都宕機了,纔會搶佔,不然不會。)
非搶佔模式:優先級高的主機宕機恢復後,不會搶佔,只有當現運行的keepalived-server出現宕機,它纔會搶佔MASTER,稱爲主keepalived-server,(通常不設置此模式,)
若是主server的性能高於備server,建議設置成搶佔模式或配置成雙主,非搶佔模式不適用雙主模式。
Keepalived-server1上配置:在上邊單主模型基礎上添加添置:
下邊爲單主模型的配置,只是copy一份,沒有更改。
! Configuration File for keepalived global_defs { notification_email { root@localhost #定義收件人地址 } notification_email_from keepalived@localhost #郵件發件人地址 smtp_server 127.0.0.1 #發送郵件服務器地址 smtp_connect_timeout 5 #鏈接郵件服務器的超時時間 router_id 10.10.10.101 #當前路由的id信息網絡中的節點標識(物理IP地址或主機名) vrrp_mcast_group4 224.0.122.22 #組播地址 } vrrp_instance VI_1 { #一個虛擬路由器的標識, state MASTER #重要選項,初始或狀態;只能有一個是MASTER,餘下的都應該爲BACKUP; interface ens33 # 綁定爲當前虛擬路由器使用的物理接口;通常爲網卡地址; virtual_router_id 11 #當前虛擬路由器的唯一標識(相同組的兩臺機器必須一致) priority 100 #當前主機在此虛擬路徑器中的優先級;範圍1-254; advert_int 1 #vrrp通告的時間間隔;心跳間隔時間。 authentication { #認證方式爲:域共享密鑰 auth_type PASS auth_pass 11112222 #最多不要超過8位 } virtual_ipaddress { 10.10.1.1/16 dev ens33 #(接口輔助地址,vip地址,虛擬地址) } }
Keepalived-server1配置,下邊爲新添加配置:
vrrp_instance VI_2 { #一個虛擬路由器的標識, state BACKUP # 重要選項,初始或狀態;只能有一個是MASTER,餘下的都應該爲BACKUP; interface ens33 # 綁定爲當前虛擬路由器使用的物理接口;通常爲網卡地址; virtual_router_id 21 #當前虛擬路由器的唯一標識(相同組的兩臺機器必須一致) priority 98 #當前主機在此虛擬路徑器中的優先級;範圍1-254; advert_int 1 #vrrp通告的時間間隔;心跳間隔時間。 authentication { #認證方式爲:域共享密鑰 auth_type PASS auth_pass 22221111· #最多不要超過8位 } virtual_ipaddress { 10.10.1.2/16 dev ens33 #(接口輔助地址,vip地址,虛擬地址) } }
Keepalived-server2上配置:在上邊單主模型基礎上添加添置:
vrrp_instance VI_2 { #一個虛擬路由器的標識, state MASTER # 重要選項,初始或狀態;只能有一個是MASTER,餘下的都應該爲BACKUP; interface ens33 # 綁定爲當前虛擬路由器使用的物理接口;通常爲網卡地址; virtual_router_id 21 #當前虛擬路由器的唯一標識(相同組的兩臺機器必須一致) priority 100 #當前主機在此虛擬路徑器中的優先級;範圍1-254; advert_int 1 #vrrp通告的時間間隔;心跳間隔時間。 authentication { #認證方式爲:域共享密鑰 auth_type PASS auth_pass 22221111· #最多不要超過8位 } virtual_ipaddress { 10.10.1.2/16 dev ens33 #(接口輔助地址,vip地址,虛擬地址) } }
啓動時查看地址:
先開啓server-2(兩個地址都在server2上)
ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:72:5b:f2 brd ff:ff:ff:ff:ff:ff inet 10.10.10.102/16 brd 10.10.255.255 scope global ens33 valid_lft forever preferred_lft forever inet 10.10.1.2/16 scope global secondary ens33 #<-- valid_lft forever preferred_lft forever inet 10.10.1.1/16 scope global secondary ens33 #<-- valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe72:5bf2/64 scope link
再開啓server-1(server-1將1.1的地址搶佔回去,server1和server2每一個設備上各一個地址,互爲主同時也互爲備)
Server-1: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:98:88:45 brd ff:ff:ff:ff:ff:ff inet 10.10.10.101/16 brd 10.10.255.255 scope global ens33 #<-- valid_lft forever preferred_lft forever inet 10.10.1.1/16 scope global secondary ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe98:8845/64 scope link Server-2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:72:5b:f2 brd ff:ff:ff:ff:ff:ff inet 10.10.10.102/16 brd 10.10.255.255 scope global ens33 valid_lft forever preferred_lft forever inet 10.10.1.2/16 scope global secondary ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe72:5bf2/64 scope link
關閉server-2(當server2關閉服務器,兩個地址全都在server1上。)
ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:98:88:45 brd ff:ff:ff:ff:ff:ff inet 10.10.10.101/16 brd 10.10.255.255 scope global ens33 valid_lft forever preferred_lft forever inet 10.10.1.1/16 scope global secondary ens33 #<-- valid_lft forever preferred_lft forever inet 10.10.1.2/16 scope global secondary ens33 #<-- valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fe98:8845/64 scope link
結果:先開啓server-2(兩個地址都在server2上)再開啓server-1(server-1將1.1的地址搶佔回去,server1和server2每一個設備上各一個地址,互爲主同時也互爲備)關閉server-2(當server2關閉服務器,兩個地址全都在server1上。)