docker 安装keepalived

1、拉取keepalived镜像

能够拉取镜像的前提是已经安装了docker,如未安装请参考docker安装手册

搜素镜像

先搜索所有keepalived镜像,根据实际环境拉取所需要的镜像

docker search keepalived

部分结果如下:

NAME        DESCRIPTION        STARS          OFFICIAL                         AUTOMATED
alterway/keepalived                    Keepalived manage virtual ip between servers.   24                                      [OK]
osixia/keepalived                      Keepalived with quick start configuration vi…   12                                      
arcts/keepalived                       Alpine based container for keepalived           7                                       [OK]
solnetcloud/keepalived                 Routing software designed to provide facilit…   3                                       [OK]
gloppenhosting/keepalived              keepalived for virtual ip between kamailio s…   2                                       [OK]
goldenroute/keepalived-haproxy         Keepalived and HAProxy                          1                                       [OK]
indigodatacloud/keepalived             Docker image for Keepalived                     0                                       [OK]
osixia/keepalived-confd                Keepalived that get unicast peers from any k…   0                                       
weikinhuang/keepalived-kube            keepalived: Loadbalancing & High-Availabilit…   0                                       
clearent/keepalived                    Dockerized keepalived to ease HA in deployme…   0                                       
aramase/keepalived                     Docker image for running keepalived             0                                       
interlegis/keepalived                  Docker container for keepalived                 0                                       [OK]
randomcoww/keepalived                  Keepalived                                      0                                       [OK]
mesosphere/keepalived                                                                  0                                       
mesosphere/keepalived-snmp             Dockerized keepalived with snmp support         0                                       
azenk/keepalived                       Apline based Keepalived                         0                                       [OK]
angelnu/keepalived                     Keepalived for multiple architectures           0                                       [OK]
alogoc/keepalived                                                                      0                                       
osism/keepalived                                                                       0                                       
bara/keepalived                        keepalived based on alpine linux with netsta…   0                                       [OK]
jckasper/keepalived                                                                    0                                       
djannot/keepalived                                                                     0                                       
nexinto/keepalived                                                                     0                                       
brainloop/keepalived                                                                   0                                       
拉取镜像
docker pull arcts/keepalived

2、启动容器

因为keepalived需要创建vip,其对网络权限要求较高,我们在启动容器的时候需要增加权限,启动方式如下。

docker run -it --name keepalived_master --net=host --cap-add NET_ADMIN  [image id] /bin/bash

例:

docker run -it --name keepalived_master --net=host --cap-add NET_ADMIN  124d8d1c0dc2 /bin/bash

3、编辑keepalived配置文件

vim /etc/keepalived/keepalived.conf

以下为master部分示例,安装时请根据自身情况更改或者添加配置文件信息

vrrp_instance VI_1 {
    state MASTER  			#主为MASTER 备为BACKUP
    interface ens33  		#ens33为绑定的网卡名,此处请根据现实情况更改
    virtual_router_id 2 	#虚拟路由ID,负责相同虚拟IP的keepalived集群最好定义为相同的id
    priority 101 			#优先级,MASTER的优先级必须比BACKUP的高
    advert_int 2
    authentication {
        auth_type PASS 
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.11.96  		#虚拟IP
    }
}

4、测试配置文件

keepalived -t -f /etc/keepalived/keepalived.conf

以上操作在主备节点都进行

5、启动

keepalived -l -f /etc/keepalived/keepalived.conf
在主节点启动之后命令行无返回

使用ip addr验证是否绑定虚拟ip

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:b8:e5:cf brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.172/24 brd 192.168.11.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet 192.168.11.96/32 scope global ens33		#虚拟ip已绑定
       valid_lft forever preferred_lft forever
主节点正常运行的情况下,备节点启动之后会有返回
bash-4.4# keepalived -l -f /etc/keepalived/keepalived.conf
Keepalived[9]: Starting Keepalived v2.0.5 (06/30,2018), git commit v3.8.1-6-g8fc23ec3d0
Keepalived[9]: WARNING - keepalived was build for newer Linux 4.4.6, running on Linux 3.10.0-1062.4.3.el7.x86_64 #1 SMP Wed Nov 13 23:58:53 UTC 2019
Keepalived[9]: Opening file '/etc/keepalived/keepalived.conf'.
bash-4.4# Keepalived[10]: Starting VRRP child process, pid=11
Keepalived_vrrp[11]: Registering Kernel netlink reflector
Keepalived_vrrp[11]: Registering Kernel netlink command channel
Keepalived_vrrp[11]: Opening file '/etc/keepalived/keepalived.conf'.
Keepalived_vrrp[11]: Registering gratuitous ARP shared channel
Keepalived_vrrp[11]: (VI_1) Entering BACKUP STATE (init)

最后一行可以看到备节点就位

查看备节点ip信息

ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:b8:e5:cf brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.181/24 brd 192.168.11.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::8e86:ffc0:f6ed:bce7/64 scope link 
       valid_lft forever preferred_lft foever

备节点虚拟ip未绑定

6、验证

关闭主节点

在主节点上查看ip地址

ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:0c:29:b8:e5:cf brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.172/24 brd 192.168.11.255 scope global ens33
       valid_lft forever preferred_lft forever
    inet6 fe80::8e86:ffc0:f6ed:bce7/64 scope link 
       valid_lft forever preferred_lft foever

可以看到vip已解绑

查看备节点
bash-4.4# keepalived -l -f /etc/keepalived/keepalived.conf
Keepalived[9]: Starting Keepalived v2.0.5 (06/30,2018), git commit v3.8.1-6-g8fc23ec3d0
Keepalived[9]: WARNING - keepalived was build for newer Linux 4.4.6, running on Linux 3.10.0-1062.4.3.el7.x86_64 #1 SMP Wed Nov 13 23:58:53 UTC 2019
Keepalived[9]: Opening file '/etc/keepalived/keepalived.conf'.
bash-4.4# Keepalived[10]: Starting VRRP child process, pid=11
Keepalived_vrrp[11]: Registering Kernel netlink reflector
Keepalived_vrrp[11]: Registering Kernel netlink command channel
Keepalived_vrrp[11]: Opening file '/etc/keepalived/keepalived.conf'.
Keepalived_vrrp[11]: Registering gratuitous ARP shared channel
Keepalived_vrrp[11]: (VI_1) Entering BACKUP STATE (init)
Keepalived_vrrp[11]: (VI_1) Entering MASTER STATE

可以看到最后一行增加信息,备节点变为主节点

查看备节点ip信息

2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:bf:0d:e7 brd ff:ff:ff:ff:ff:ff
    inet 192.168.11.181/24 brd 192.168.11.255 scope global noprefixroute dynamic ens33
       valid_lft 1241sec preferred_lft 1241sec
    inet 192.168.11.96/32 scope global ens33		#虚拟ip
       valid_lft forever preferred_lft forever

可以看到虚拟ip绑定到了备节点上

重新启动主节点,备节点会发生变化

bash-4.4# keepalived -l -f /etc/keepalived/keepalived.conf
Keepalived[9]: Starting Keepalived v2.0.5 (06/30,2018), git commit v3.8.1-6-g8fc23ec3d0
Keepalived[9]: WARNING - keepalived was build for newer Linux 4.4.6, running on Linux 3.10.0-1062.4.3.el7.x86_64 #1 SMP Wed Nov 13 23:58:53 UTC 2019
Keepalived[9]: Opening file '/etc/keepalived/keepalived.conf'.
bash-4.4# Keepalived[10]: Starting VRRP child process, pid=11
Keepalived_vrrp[11]: Registering Kernel netlink reflector
Keepalived_vrrp[11]: Registering Kernel netlink command channel
Keepalived_vrrp[11]: Opening file '/etc/keepalived/keepalived.conf'.
Keepalived_vrrp[11]: Registering gratuitous ARP shared channel
Keepalived_vrrp[11]: (VI_1) Entering BACKUP STATE (init)
Keepalived_vrrp[11]: (VI_1) Entering MASTER STATE
Keepalived_vrrp[11]: (VI_1) Master received advert from 192.168.11.172 with higher priority 101, ours 100
Keepalived_vrrp[11]: (VI_1) Entering BACKUP STATE

最后两行可以看到状态发生改变,收到主节点消息重新变回备节点

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐