keepalived安装---VIP

keepalived的切换可以是自动的,但是却做不到毫秒级别,他怎么都需要几秒钟的时间进行切换。
这就有一个问题,虽然在主节点出现问题我们转向备份节点时,这个延时无可避免,但是在我们修复主节点后,实际上并没有必要再马上做一次切换,所以Keepalived提供了一种非抢占模式,来满足这个要求。所以,生产环境中建议采用非抢占模式

一、Keepalived 抢占模式

1.keepalived安装

在所有worker节点上安装

1.1 安装keepalived

yum -y install keepalived

1.2 keepalived配置

1.2.1 worker-01 的keepalived配置文件

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf,输入如下内容:

! Configuration File for keepalived
global_defs {
   router_id worker-01.techzsun.com
}
vrrp_instance VI_1 {
    state MASTER 
    interface ens192
    virtual_router_id 50
    priority 150
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.7.207
    }
}

1.2.2 worker-02 的keepalived配置文件

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf,输入如下内容:

! Configuration File for keepalived
global_defs {
   router_id worker-02.techzsun.com
}
vrrp_instance VI_1 {
    state BACKUP 
    interface ens192
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.7.207
    }
}

1.2.3 worker-03 的keepalived配置文件

mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf,输入如下内容:

! Configuration File for keepalived
global_defs {
   router_id worker-03.techzsun.com
}
vrrp_instance VI_1 {
    state BACKUP 
    interface ens192
    virtual_router_id 50
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        172.16.7.207
    }
}

1.3 测试VIP

1.3.1 查看VIP

worker-01 节点上查看

[root@worker-01 ~]# ip a |grep ens192
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    inet 172.16.7.204/24 brd 172.16.7.255 scope global noprefixroute ens192
    inet 172.16.7.207/32 scope global ens192

worker-02 节点上查看

[root@worker-02 ~]# ip a | grep ens192
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    inet 172.16.7.205/24 brd 172.16.7.255 scope global noprefixroute ens192

worker-03 节点上查看

[root@worker-03 ~]# ip a |grep ens192
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    inet 172.16.7.206/24 brd 172.16.7.255 scope global noprefixroute ens192

可以看出,现在的VIP(172.16.7.204)上。
测试VIP是否会飘:
只要把VIP所在主机172.16.7.204关机,VIP就会飘到另外的主机上。

1.3.2 测试VIP

[root@worker-01 ~]# ping 172.16.7.207 -c 4
PING 172.16.7.207 (172.16.7.207) 56(84) bytes of data.
64 bytes from 172.16.7.207: icmp_seq=1 ttl=64 time=0.046 ms
64 bytes from 172.16.7.207: icmp_seq=2 ttl=64 time=0.038 ms
64 bytes from 172.16.7.207: icmp_seq=3 ttl=64 time=0.040 ms

二、Keepalived 非抢占模式

下面我们就来介绍一下Keepalived的非抢占模式的配置(无MASTER节点,全部依据优先级确定哪个节点进行工作):
此模式下keepalived 的配置

! Configuration File for keepalived
global_defs {
    # 每个节点主机名不一样
    router_id worker-01.techzsun.com
}
vrrp_instance VI_1 {
    # 所有节点都是BACKUP
    state BACKUP
    # 每个节点对应的网卡名称
    interface ens192
    # 同一个keepalived组,节点的设置必须一样,这样才会被识别
    virtual_router_id 50  
    # 这个关键配置项,设置为“非抢占”模式
    nopreempt
    # 每个节点的优先级一定要不一样
    priority 120
    # 组播信息发送间隔,每个节点设置必须一样
    advert_int 1
    # 验证信息,只有验证信息相同,才能被加入到一个组中。
    authentication {
        auth_type PASS
        auth_pass 310012
    }
    # 虚拟地址和绑定的端口,如果有多个,就绑定多个
    # dev 是指定浮动IP要绑定的网卡设备号
    virtual_ipaddress {
        172.16.7.207
    }
    # keepalived 在某些环境(如:OpenStack)下一直脑裂无法选举。
    # 在不支持组播的场景下,可以使用单播模式
    unicast_peer {
        172.16.7.204
        172.16.7.205
        172.16.7.206
    }
}

参考资料:

  1. https://www.cnblogs.com/shangsharon/p/11557587.html
  2. https://www.cnblogs.com/zhaoya2019/p/13032218.html
  3. https://blog.csdn.net/yinwenjie/article/details/47130609 详解
  4. keepalived 的 unicast 单播模式+结合秒云
    最近遇到一个问题,keepalived 在 OpenStack 环境下一直脑裂无法选举。在不支持组播的场景下,可以使用单播模式。
    https://www.cnblogs.com/keithtt/p/12758731.html