Docker+keepalived+nginx实现主从热备的方法示例

2020-06-17 06:39:03易采站长站整理


docker run --privileged -tid --name centos_master --restart=always centos_kn /usr/sbin/init

docker exec -it centos_master bash

18,修改centos_master里面nginx 欢迎页,


vim /usr/share/nginx/html/index.html

19,创建从服务器容器


docker run --privileged -tid --name centos_slave --restart=always centos_kn /usr/sbin/init
docker exec -it centos_slave bash

#修改keepalived.conf 配置文件,主要是state和priority两个参数的调整,其中master节点的priority值一定要比slave大才行

vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}

vrrp_instance VI_1 {
state SLAVE
interface eth0
virtual_router_id 121
mcast_src_ip 172.17.0.6
priority 80
nopreempt
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}

track_script {
chk_nginx
}

virtual_ipaddress {
172.17.0.100
}
}

20,修改完成之后重新加载


systemctl daemon-reload
systemctl restart keepalived.service

21,修改nginx欢迎页(若nginx没启动则执行 systemctl start nginx.service)


vim /usr/share/nginx/html/index.html

22,测试

A> 分别在宿主机,centos_master,centos_slave中进行一下命令测试,如果显示都为Master的欢迎页面,说明配置成功1/3


curl 172.17.0.100

B> 此时停止centos_master容器( docker stop centos_master ),保留centos_slave容器,执行以下命令,若切换到Slave页面,则说明keepalived配置成功2/3


curl 172.17.0.100

C> 重启centos_master 容器,此时执行以下命令,看是从Slave切换到了Master,如果切换成功,说明我们配置到此成功了。


curl 172.17.0.100

说明,测试过程中,重启容器之后,nginx没有启动,需要进入容器启动一下,不然访问不到Master页面了,但是可以Ping通。

执行下面命令,配置nginx随机启动,这样不用每次重启容器还需要手动启动nginx


chkconfig nginx on

以上就是整个配置过程,希望对大家的学习有所帮助,也希望大家多多支持软件开发网。

您可能感兴趣的文章: