千家信息网

如何在Docker中使用keepalived+nginx实现主从热备

发表于:2025-01-30 作者:千家信息网编辑
千家信息网最后更新 2025年01月30日,如何在Docker中使用keepalived+nginx实现主从热备?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。1,拉取cento
千家信息网最后更新 2025年01月30日如何在Docker中使用keepalived+nginx实现主从热备

如何在Docker中使用keepalived+nginx实现主从热备?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

1,拉取centos7镜像

docker pull centos:7

2,创建容器

docker run -it -d --name centos1 -d centos:7

3,进入容器centos1

docker exec -it centos1 bash

4,安装常用工具

yum updateyum install -y vimyum install -y wgetyum install -y gcc-c++ yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum install -y openssl openssl--develyum install -y popt-develyum install -y initscriptsyum install -y net-tools

5,将容器打包成新的镜像,以后直接以该镜像创建容器

docker commit -a 'cfh' -m 'centos with common tools' centos1 centos_base

6,删除之前创建的centos1 容器,重新以基础镜像创建容器,安装keepalived+nginx

docker rm -f centos1#容器内需要使用systemctl服务,需要加上/usr/sbin/initdocker run -it --name centos_temp -d --privileged centos_base /usr/sbin/initdocker exec -it centos_temp bash作者:江湖救急链接:https://juejin.im/post/5dc517386fb9a04a9272110b来源:掘金著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

7,安装nginx

#使用yum安装nginx需要包括Nginx的库,安装Nginx的库rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm# 使用下面命令安装nginxyum install -y nginx#启动nginxsystemctl start nginx.service#查看是否启动成功,出现nginx欢迎界面表示安装成功curl 172.17.0.2

8,安装keepalived

1.下载keepalived wget http://www.keepalived.org/software/keepalived-1.2.18.tar.gz

2.解压安装:tar -zxvf keepalived-1.2.18.tar.gz -C /usr/local/

3.下载插件openssl yum install -y openssl openssl-devel(需要安装一个软件包)

4.开始编译keepalivedcd /usr/local/keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalived

5.make一下 make && make install

9,将keepalived 安装成系统服务

mkdir /etc/keepalivedcp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/然后复制keepalived脚本文件:cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/ln -s /usr/local/sbin/keepalived /usr/sbin/可以设置开机启动:chkconfig keepalived on,到此我们安装完毕!#若启动报错,则执行下面命令cd /usr/sbin/ rm -f keepalived cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ #常用命令systemctl daemon-reload 重新加载systemctl enable keepalived.service 设置开机自动启动systemctl disable keepalived.service 取消开机自动启动systemctl start keepalived.service 启动systemctl stop keepalived.service停止systemctl status keepalived.service 查看服务状态

10,修改/etc/keepalived/keepalived.conf文件

#备份配置文件cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.backuprm -f keepalived.confvim keepalived.conf#配置文件如下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 121  mcast_src_ip 172.17.0.6  priority 100  nopreempt  advert_int 1  authentication {    auth_type PASS    auth_pass 1111  }  track_script {    chk_nginx  }  virtual_ipaddress {    172.17.0.100  }}

11,添加心跳检测文件

vim nginx_check.sh#以下是脚本内容#!/bin/bashA=`ps -C nginx -no-header |wc -l`if [ $A -eq 0 ];then  /usr/local/nginx/sbin/nginx  sleep 2  if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then    killall keepalived  fifi

12,给脚本赋予执行权限

chmod +x nginx_check.sh

13,设置开机启动

systemctl enable keepalived.service#开启keepalivedsystemctl start keepalived.service

14,检测虚拟IP是否成功,在宿主机里面执行下面命令,如果出现nginx欢迎界面表示成功

curl 172.17.0.100

15,将centos_temp 容器重新打包成镜像,然后利用这个新镜像再创建两个容器,实现热备效果

docker commit -a 'cfh' -m 'centos with keepalived nginx' centos_temp centos_kn

16,删除所有容器

docker rm -f `docker ps -a -q`

17,用centos_kn 镜像创建主服务器容器

docker run --privileged -tid --name centos_master --restart=always centos_kn /usr/sbin/initdocker 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/initdocker 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-reloadsystemctl 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

关于如何在Docker中使用keepalived+nginx实现主从热备问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注行业资讯频道了解更多相关知识。

0