1. 程式人生 > >Keepalived單雙主模型配置

Keepalived單雙主模型配置

Keepalived單雙主模型配置 Keepalived

Keepalived單雙主模型配置

Keepalived單主配置實例:

一、安裝keepalived包

[root@keepalived-1 ~]# hostnamectl set-hostname keepalived-1
[root@keepalived-1 ~]# yum install keepalived.x86_64

主配置文件:/etc/keepalived/keepalived.conf
主程序文件:/usr/sbin/keepalived

二、進行配置主配置文件:

主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
vrrp_mcast_group4 224.0.122.22
}

vrrp_instance VI_1 {
        state MASTER
        interface ens33
virtual_router_id 11
        priority 100
        advert_int 1
authentication {
auth_type PASS
auth_pass 11112222
        }
virtual_ipaddress {
10.10.1.1/16 dev ens33

                }
}

備服務器:
抓包結果如下:

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雙主配置實例:

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
     vrrp_mcast_group4 224.0.122.22
}

vrrp_instance VI_1 {
        state MASTER
        interface ens33
        virtual_router_id 11
        priority 100
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass 11112222
        }
        virtual_ipaddress {
                 10.10.1.1/16 dev ens33

                 }
}

Keepalived-server1上配置,下邊為新添加配置:

vrrp_instance VI_2 {
        state BACKUP
        interface ens33
        virtual_router_id 21
        priority 98
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass 22221111
        }
        virtual_ipaddress {
                 10.10.1.2/16 dev ens33

                 }
}

Keepalived-server2上配置:在下邊keepalived-2單主模型基礎上添加添置:

vrrp_instance VI_22{
        state MASTER
        interface ens33
        virtual_router_id 21
        priority 100
        advert_int 1
        authentication {
                auth_type PASS
                auth_pass 22221111
        }
        virtual_ipaddress {
                 10.10.1.2/16 dev ens33

                 }
}

啟動時查看地址:
先開啟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上。)

Keepalived單雙主模型配置