准备工作
镜像使用alpine,我们需要添加apk源,进行apk add安装
[root@linuxea-com ~]# echo "http://nl.alpinelinux.org/alpine/edge/main" >> /etc/apk/repositories
[root@linuxea-com ~]# apk --update -t add keepalived curl iproute2
dockerfile参考:https://raw.githubusercontent.com/LinuxEA-Mark/Docker-keepalived/master/Dockerfile
其中,在run脚本中启动keepalived时用tail放一个前台进程确保容器up
Master配置文件:
[root@linuxea-com ~]# cat keeplived.conf
! Configuration File for keepalived
global_defs {
}
router_id {
LVS_DEVEL_1
}
#vrrp_script chk_nginx_service {
# script "/root/check.sh"
# interval 3
# weight -50
# fall 3
# rise 2
# user keepalived
#}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.1.50/8
}
}
Backup配置文件:
[root@linuxea-com ~]# cat keeplived.conf
! Configuration File for keepalived
global_defs {
}
router_id {
LVS_DEVEL_2
}
#vrrp_script chk_nginx_service {
# script "/root/check.sh"
# interval 3
# weight -50
# fall 3
# rise 2
# user keepalived
#}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
riority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.1.50/8
}
}
脑裂判断:
在单个keepalived在容器中没有好的办法来判断脑裂的,我们可以写计划任务,每分钟运行一次即可,脚本如下:
[root@linuxea-com ~]# cat 1.sh
#!/bin/bash
if [ "$(ss -tlnp|grep -e 1180|wc -l)" -ne '1' ]||[ "$(ss -tlnp|grep -E 8765|wc -l)" -ne '2' ];
then
docker rm -f keepalived1.3.2
ip addr del 10.0.1.50/8 dev eth0
fi
安装脚本:
[root@linuxea-com ~]# curl -Lks4 https://raw.githubusercontent.com/LinuxEA-Mark/Docker-keepalived/master/create_docker-keepalived.sh |bash
延伸阅读
- linuxea: 三小时快速入门docker指南
- linuxea:白话容器之Registry与Harbor的构建和使用 (26)
学习更多
学习如何使用Docker CLI命令,Dockerfile命令,使用这些命令可以帮助你更有效地使用Docker应用程序。查看Docker文档和我的其他帖子以了解更多信息。
- docker目录
- 白话容器
- docker-compose