docker构建keepalived思路


准备工作

镜像使用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

2 分享

您可以选择一种方式赞助本站

支付宝扫码赞助

支付宝扫码赞助

日期: 2017-01-04分类: Docker

标签: docker

发表评论