Keepalived单双主模型配置

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Keepalived单双主模型配置相关的知识,希望对你有一定的参考价值。

Keepalived单双主模型配置

Keepalived单主配置实例:

一、安装keepalived包

[[email protected] ~]# hostnamectl set-hostname keepalived-1
[[email protected] ~]# yum install keepalived.x86_64

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

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

主keepalived服务器:

[[email protected] ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
notification_email {
[email protected]
                }
     notification_email_from      [email protected]
     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 {
     [email protected]
                 }
     notification_email_from [email protected]
     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单双主模型配置的主要内容,如果未能解决你的问题,请参考以下文章

LVS+Keepalived双主模型的实现

VRRP笔记三:配置keepalived为实现haproxy高可用的双主模型配置文件示例:

实现双主模型NGINX架构

单主模型的IPVS高可用和nginx+keepalived的双主高可用模型

Keepalived实现双主模型的ipvs高可用集群

keepalived高可用nginx/htttpd,双主模型