Docker+keepalived+nginx实现主从热备

前言

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

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

配置环境:centos7 64位c++

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



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 update
yum install -y vim
yum install -y wget
yum install -y  gcc-c++  
yum install -y pcre pcre-devel  
yum install -y zlib zlib-devel  
yum install -y  openssl-devel
yum install -y popt-devel
yum 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复制代码

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.开始编译keepalived 
cd  /usr/local/keepalived-1.2.18/ && ./configure --prefix=/usr/local/keepalived

5.make一下
make && make install
复制代码

9,将keepalived 安装成系统服务

mkdir /etc/keepalived
cp /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
复制代码


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/3vim

curl 172.17.0.100
复制代码

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

curl 172.17.0.100
复制代码

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

curl 172.17.0.100
复制代码

说明,测试过程当中,重启容器以后,nginx没有启动,须要进入容器启动一下,否则访问不到Master页面了,可是能够Ping通。服务器

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

chkconfig nginx on
复制代码

以上就是整个配置过程工具

相关文章
相关标签/搜索