keepalive_docker_nginx实践

我的本地环境,一台 Unbuntu 18.04 系统的物理机
想实践下 虚拟 ip,keepalive
就一台机器,所以用 docker 来实践一下。

关于虚拟 ip, 可以看下这篇文章
https://www.gitos.org/2019/08/19/vip-ha.html

关于 docker, keepalive 基本按这篇文章
https://www.jianshu.com/p/6a8f38b8076d

修改 dockerfile,减少重复下载环境

1
2
3
4
5
6
7
8
FROM centos:latest
MAINTAINER relengxing<relengxing@outlook.com>

RUN yum install -y gcc openssl-devel popt-devel
RUN yum install -y net-tools
RUN yum install -y vim
RUN yum install -y keepalived
RUN yum install -y nginx

启动 centos 容器
–privileged=true 获取特权
/sbin/init:这个地方是实测 /bin/bash,会有权限没有。因为我还实验了下手动加虚拟 ip

1
2
docker run --name centos_master -itd --privileged=true relengxing/centos:v1 /sbin/init
docker run --name centos_slave -itd --privileged=true relengxing/centos:v1 /sbin/init

可以进容器查看 ip 地址

Master 节点
ifconfig

1
2
3
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)

在容器外则通过 docker network
docker network ls
docker network inspect d8c58097a4c3

ifconfig eth0:0 172.17.0.88 netmask 255.255.0.0 up
ifconfig eth0:0 down

修改/etc/keepalived/keepalived.conf的内容

主节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
! Configuration File for keepalived
global_defs {
notification_email {
xuad@xuad.com
}
notification_email_from root@xuad.com
smtp_server mail.xuad.com
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}

vrrp_script chk_nginx {
script "/etc/keepalived/nginx_pid.sh" # 检查nginx状态的脚本
interval 2
weight 3
}

vrrp_instance VI_1 {
state MASTER #备份服务器上将MASTER改为BACKUP
interface eth0
virtual_router_id 51
priority 101 #备份服务上将100改为小于100,可配置成90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.0.99 #有多个vip可在下面继续增加
}
track_script {
chk_nginx
}
}

备份节点

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
! Configuration File for keepalived

global_defs {
notification_email {
xuad@xuad.com
}
notification_email_from root@xuad.com
smtp_server mail.xuad.com
smtp_connect_timeout 30
router_id LVS_DEVEL
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}

vrrp_script chk_nginx {
script "/etc/keepalived/nginx_pid.sh" # 检查nginx状态的脚本
interval 2
weight 3
}

vrrp_instance VI_1 {
state BACKUP #备份服务器上将MASTER改为BACKUP
interface eth0
virtual_router_id 51
priority 99 #备份服务上将100改为小于100,可配置成90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.17.0.99 #有多个vip可在下面继续增加
}
track_script {
chk_nginx
}
}

nginx_pid.sh

1
2
3
4
5
6
7
8
9
10
11
#检测nginx是否存活的脚本
A=`ps -ef | grep nginx | grep -v grep | wc -l`
if [ $A -eq 0 ];then
nginx
sleep 2
if [ `ps -ef | grep nginx | grep -v grep | wc -l` -eq 0 ];then
#killall keepalived
ps -ef|grep keepalived|grep -v grep|awk '{print $2}'|xargs kill -9
fi

fi

修改 nginx 显示的页面,我就修改了这一行,标示是主机还是备份机

主节点

1
2
<strong>nginx  master Node</strong> HTTP server after it has been

从节点

1
<strong>nginx  Slave Node</strong> HTTP server after it has been

然后在主机运行
curl 172.17.0.99
显示

1
2
3
4
...
<strong>nginx master Node</strong> HTTP server after it has been
...

关闭主节点后
然后在主机运行
curl 172.17.0.99
显示

1
2
3
...
<strong>nginx slave Node</strong> HTTP server after it has been
...

遇到一个问题没有解决

在主节点运行
nginx -s stop 推出 nginx 后,没有切换到从节点,必须关闭主节点。

更新:
这个问题看了下
是 nginx 检测脚本的问题
修改为

1
2
3
4
5
6
#!/bin/bash
# 如果进程中没有nginx则将keepalived进程kill掉
A=`ps -C nginx --no-header |wc -l` ## 查看是否有 nginx进程 把值赋给变量A
if [ $A -eq 0 ];then ## 如果没有进程值得为 零
systemctl stop keepalived.service ## 则结束 keepalived 进程
fi

成功了


本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!