Just Do IT !

HA高可用与负载均衡入门到实战(六)---- 基于Docker配置KeepAlive支持Nginx高可用

字数统计: 922阅读时长: 4 min
2020/04/21 Share

网站架构

基于Docker容器里构建高并发网站

拓扑图:
在这里插入图片描述
上文讲述了简单的基于Docker的配置Nginx反向代理和负载均衡

本文讲述Keepalived与Nginx共同实现高可用实例
|IP地址 | 容器名 |功能|
|–|–|–|
| 172.18.0.11| nginx1| nginx+keepalived |
| 172.18.0.12|nginx2| nginx+keepalived |
| 172.18.0.10|VIP| |

安装配置keepalived

使用nginx镜像生成nginx-keep镜像

1) 启动nginx容器并进入
docker run -d --privileged nginx /usr/sbin/init

2) 在nginx容器中使用yum方式安装keepalived
yum install -y keepalived
3) 保存容器为镜像
docker commit 容器ID nginx-keep

使用nginx-keep镜像启动nginx1和nginx2两个容器

1) 创建docker网络
docker network create --subnet=172.18.0.0/16 cluster
2) 查看宿主机上的docker网络类型种类
docker network ls
3) 启动容器nginx1,设定地址为172.18.0.11
docker run -d --privileged --net cluster --ip 172.18.0.11 --name nginx1 nginx-keep /usr/sbin/init
4) 启动容器nginx2,设定地址为172.18.0.12
docker run -d --privileged --net cluster --ip 172.18.0.12 --name nginx2 nginx-keep /usr/sbin/init

5) 配置容器nginx1, nginx2的web服务,编辑首页内容为“nginx1”,“nginx2”, 在宿主机访问

1
2
3
4
5
6
[root@localhost ~]# curl 172.18.0.12
nginx2

[root@localhost ~]# curl 172.18.0.11
nginx1
[root@localhost ~]#

在nginx1和nginx2两个容器配置keepalived

1) 在nginx1编辑 /etc/keepalived/keepalived.conf ,启动keepalived服务

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
 ! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id nginx1
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}

vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}

virtual_ipaddress {
172.18.0.10
}
}

2) 在nginx2编辑 /etc/keepalived/keepalived.conf ,启动keepalived服务

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
 ! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id nginx2
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}

vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.18.0.10
}
}

注意:
/etc/keepalived/keepalived.conf配置文件中将#vrrp_strict注释掉, 否则会出现ping VIP不通的现象

1
2
3
4
5
6
vrrp_strict
#严格遵守VRRP协议。 这将禁止:

0 VIPs
unicast peers (单播对等体)
IPv6 addresses in VRRP version 2(VRRP版本2中的IPv6地址)

即vrrp_strict:严格遵守VRRP协议。下列情况将会阻止启动Keepalived:1. 没有VIP地址。2. 单播邻居。3. 在VRRP版本2中有IPv6地址。

3) 在宿主机使用浏览器访问虚拟地址
curl http:// 172.18.0.10

1
2
[root@localhost ~]# curl 172.18.0.10
nginx1

4) 在nginx1上当掉网卡
ifconfig eth0 down

5) 在宿主机使用浏览器访问虚拟地址
curl http:// 172.18.0.10

1
2
[root@localhost ~]# curl 172.18.0.10
nginx2

配置keepalived 支持nginx高可用

编写 Nginx 状态检测脚本

1) 在nginx1上编写 Nginx 状态检测脚本/etc/keepalived/nginx_check.sh

1
2
3
4
5
6
7
8
9
10
#!/bin/bash
if [ `ps -C nginx --no-header |wc -l` -eq 0 ]
then
systemctl start nginx
sleep 2
if [ `ps -C nginx --no-header |wc -l` -eq 0 ]
then
kill keepalived
fi
fi

脚本说明: 当检测nginx没有进程时选择启动nginx, 如果启动失败则关闭keepalived
2) 赋予/etc/keepalived/nginx_check.sh执行权限
chmod a+x /etc/keepalived/nginx_check.sh

配置keepalived 支持nginx高可用

1) 在nginx1上编辑/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
39
! Configuration File for keepalived

global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id nginx1
vrrp_skip_check_adv_addr
#vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_script chk_nginx{
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script{
chk_nginx
}
virtual_ipaddress {
172.18.0.10
}
}

2) 重新启动keepalived,在主机使用浏览器访问虚拟地址

1
2
[root@localhost ~]# curl 172.18.0.10
nginx1

3) 在nginx1停止nginx服务,在主机使用浏览器访问虚拟地址

1
2
[root@localhost ~]# curl 172.18.0.10
nginx2

原因: weight -20 每当运行一次vrrp_script chk_nginx脚本, 本机的权重减20

CATALOG
  1. 1. 网站架构
  2. 2. 安装配置keepalived
    1. 2.1. 使用nginx镜像生成nginx-keep镜像
    2. 2.2. 使用nginx-keep镜像启动nginx1和nginx2两个容器
    3. 2.3. 在nginx1和nginx2两个容器配置keepalived
  3. 3. 配置keepalived 支持nginx高可用
    1. 3.1. 编写 Nginx 状态检测脚本
    2. 3.2. 配置keepalived 支持nginx高可用