Docker+keepalived+nginx实现主从热备的方法示例
前言
为解决单点故障,我们需要配置主从热备方案,服务器数量有限,故使用Docker模拟安装配置。
本次配置默认已经安装了Docker。
配置环境:centos764位
docker版本:Dockerversion17.12.1-ce,build7390fc6
1,拉取centos7镜像
dockerpullcentos:7
2,创建容器
dockerrun-it-d--namecentos1-dcentos:7
3,进入容器centos1
dockerexec-itcentos1bash
4,安装常用工具
yumupdateyuminstall-yvimyuminstall-ywgetyuminstall-ygcc-c++yuminstall-ypcrepcre-develyuminstall-yzlibzlib-develyuminstall-yopensslopenssl--develyuminstall-ypopt-develyuminstall-yinitscripts yuminstall-ynet-tools
5,将容器打包成新的镜像,以后直接以该镜像创建容器
dockercommit-a'cfh'-m'centoswithcommontools'centos1centos_base
6,删除之前创建的centos1容器,重新以基础镜像创建容器,安装keepalived+nginx
dockerrm-fcentos1 #容器内需要使用systemctl服务,需要加上/usr/sbin/init dockerrun-it--namecentos_temp-d--privilegedcentos_base/usr/sbin/init dockerexec-itcentos_tempbash 作者:江湖救急 链接:https://juejin.im/post/5dc517386fb9a04a9272110b 来源:掘金 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
7,安装nginx
#使用yum安装nginx需要包括Nginx的库,安装Nginx的库 rpm-Uvhhttp://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm #使用下面命令安装nginx yuminstall-ynginx #启动nginx systemctlstartnginx.service #查看是否启动成功,出现nginx欢迎界面表示安装成功 curl172.17.0.2
8,安装keepalived
1.下载keepalivedwgethttp://www.keepalived.org/software/keepalived-1.2.18.tar.gz
2.解压安装:tar-zxvfkeepalived-1.2.18.tar.gz-C/usr/local/
3.下载插件opensslyuminstall-yopensslopenssl-devel(需要安装一个软件包)
4.开始编译keepalivedcd/usr/local/keepalived-1.2.18/&&./configure--prefix=/usr/local/keepalived
5.make一下make&&makeinstall
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/可以设置开机启动:chkconfigkeepalivedon,到此我们安装完毕! #若启动报错,则执行下面命令 cd/usr/sbin/ rm-fkeepalived cp/usr/local/keepalived/sbin/keepalived/usr/sbin/ #常用命令 systemctldaemon-reload重新加载systemctlenablekeepalived.service设置开机自动启动systemctldisablekeepalived.service取消开机自动启动systemctlstartkeepalived.service启动systemctlstopkeepalived.service停止systemctlstatuskeepalived.service查看服务状态
10,修改/etc/keepalived/keepalived.conf文件
#备份配置文件 cp/etc/keepalived/keepalived.conf/etc/keepalived/keepalived.conf.backup rm-fkeepalived.conf vimkeepalived.conf #配置文件如下 vrrp_scriptchk_nginx{ script"/etc/keepalived/nginx_check.sh" interval2 weight-20 } vrrp_instanceVI_1{ stateMASTER interfaceeth0 virtual_router_id121 mcast_src_ip172.17.0.6 priority100 nopreempt advert_int1 authentication{ auth_typePASS auth_pass1111 } track_script{ chk_nginx } virtual_ipaddress{ 172.17.0.100 } }
11,添加心跳检测文件
vimnginx_check.sh #以下是脚本内容 #!/bin/bash A=`ps-Cnginx–no-header|wc-l` if[$A-eq0];then /usr/local/nginx/sbin/nginx sleep2 if[`ps-Cnginx--no-header|wc-l`-eq0];then killallkeepalived fi fi
12,给脚本赋予执行权限
chmod+xnginx_check.sh
13,设置开机启动
systemctlenablekeepalived.service #开启keepalived systemctlstartkeepalived.service
14,检测虚拟IP是否成功,在宿主机里面执行下面命令,如果出现nginx欢迎界面表示成功
curl172.17.0.100
15,将centos_temp容器重新打包成镜像,然后利用这个新镜像再创建两个容器,实现热备效果
dockercommit-a'cfh'-m'centoswithkeepalivednginx'centos_tempcentos_kn
16,删除所有容器
dockerrm-f`dockerps-a-q`
17,用centos_kn镜像创建主服务器容器
dockerrun--privileged-tid--namecentos_master--restart=alwayscentos_kn/usr/sbin/init dockerexec-itcentos_masterbash
18,修改centos_master里面nginx欢迎页,
vim/usr/share/nginx/html/index.html
19,创建从服务器容器
dockerrun--privileged-tid--namecentos_slave--restart=alwayscentos_kn/usr/sbin/init dockerexec-itcentos_slavebash #修改keepalived.conf配置文件,主要是state和priority两个参数的调整,其中master节点的priority值一定要比slave大才行 vrrp_scriptchk_nginx{ script"/etc/keepalived/nginx_check.sh" interval2 weight-20 } vrrp_instanceVI_1{ stateSLAVE interfaceeth0 virtual_router_id121 mcast_src_ip172.17.0.6 priority80 nopreempt advert_int1 authentication{ auth_typePASS auth_pass1111 } track_script{ chk_nginx } virtual_ipaddress{ 172.17.0.100 } }
20,修改完成之后重新加载
systemctldaemon-reload systemctlrestartkeepalived.service
21,修改nginx欢迎页(若nginx没启动则执行systemctlstartnginx.service)
vim/usr/share/nginx/html/index.html
22,测试
A>分别在宿主机,centos_master,centos_slave中进行一下命令测试,如果显示都为Master的欢迎页面,说明配置成功1/3
curl172.17.0.100
B>此时停止centos_master容器(dockerstopcentos_master),保留centos_slave容器,执行以下命令,若切换到Slave页面,则说明keepalived配置成功2/3
curl172.17.0.100
C>重启centos_master容器,此时执行以下命令,看是从Slave切换到了Master,如果切换成功,说明我们配置到此成功了。
curl172.17.0.100
说明,测试过程中,重启容器之后,nginx没有启动,需要进入容器启动一下,不然访问不到Master页面了,但是可以Ping通。
执行下面命令,配置nginx随机启动,这样不用每次重启容器还需要手动启动nginx
chkconfignginxon
以上就是整个配置过程,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。