铁雪资源网 Design By www.gsvan.com

前言

为解决单点故障,我们需要配置主从热备方案,服务器数量有限,故使用Docker模拟安装配置。

本次配置默认已经安装了Docker。

配置环境:centos7 64位

docker版本:Docker version 17.12.1-ce, build 7390fc6

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 initscripts
yum 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/init
docker run -it --name centos_temp -d --privileged centos_base /usr/sbin/init
docker 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
# 使用下面命令安装nginx
yum install -y nginx
#启动nginx
systemctl 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.backup

rm -f keepalived.conf
vim 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/bash
A=`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
  fi
fi

12,给脚本赋予执行权限

chmod +x nginx_check.sh

13,设置开机启动

systemctl enable keepalived.service

#开启keepalived
systemctl 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/init

docker exec -it centos_master bash

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

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

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

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

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

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,主从热备,Docker,keepalived,nginx

铁雪资源网 Design By www.gsvan.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
铁雪资源网 Design By www.gsvan.com

评论“Docker+keepalived+nginx实现主从热备的方法示例”

暂无Docker+keepalived+nginx实现主从热备的方法示例的评论...