Keepalived+Nginx****实现热备份
思路说明:
-
利用linux镜像容器,在其中安装keepalived和nginx, 制作新镜像支持热备份,这样便于直接使用,但在linux镜像中安装keepalived无法实现虚拟IP,也可能是网卡等设置不对。
-
在linux主机内,启动keepalived容器和nginx容器,实现热备份。实现难点:如何利用keepalived容器来监测nginx容器的运行状态
,这涉及到容器与容器,容器与主机间的通信问题,尝试过容器链接等方式,没有实现。Docker hub上有alterway/keepalived/,应该可以实现这一功能,链接如下:
https://hub.docker.com/r/alterway/keepalived/
- 建立两个linux主机,在两个linux主机内分别安装keepalived软件和启动nginx容器,这样比较便于实现热备份,和keepalived监测nginx的运行状态。这里采用这种方式实现热备份。
整体结构说明:
如上所示:建立两个主机iyangcong-master, iyangcong-backup ,ip分别为10.104.15.62,10.104.12.170。在master上安装keepalived-master,启动nginx-master, 同样在backup上安装keepalived-backup和启动nginx-backup。通过keepalived的虚拟IP,把两主机的IP君化为VIP10.104.1.1。这样便实现了热备份。
首先主机占据VIP,由keepalived-master 监测nginx-master的运行状态,若发现nginx-master出现异常,则尝试重启nginx-master,若重启失败,则关闭keepalived-master;此时VIP转移到iyangcong-backup上面,由keepalived-backup启动nginx-backup并监测其运行状态,从而实现了VIP的“飘移”。
iyangcong 10.104.15.62 master
iyangcong2 10.104.12.170 backup
实现步骤:
- 在iyangcong(10.104.15.62)master主机上,搭建docker私服,实现镜像的共享,便于实现已经拥有的镜像从master转移到backup(10.104.12.170)上。
- 在master安装keepalived-master和启动nginx-master容器
- 在backup安装keepalived-backup和启动nginx-backup容器
- 测试VIP的漂移功能
具体细节:
- master主机搭建docker****私服。
docker search registry
docker pull registry
启动registry
dcker run –d – name iyangcong-registry
–p 5000:5000 –v /opt/data/registry:/mp/registry registry
docker logs iyangcong-registry查看启动日志
给master已有nginx镜像重命名:
Docker tag nginx 10.104.15.62:5000/nginx
将镜像推送到私服:
Docker push 10.104.15.62:5000/nginx
因为Docker从1.3.X之后,与docker registry交互默认使用的是https,然而此处搭建的私有仓库只提供http服务,所以当与私有仓库交互时就会报上面的错误。为了解决这个问题需要在启动docker server时增加启动参数为默认使用http访问。修改docker启动配置文件Ubuntu下配置文件地址为:/etc/init/docker.conf,在其中增加–insecure-registry 10.104.15.62:5000如下所示:
$ sudo vi /etc/init/docker.conf
修改完之后,重启Docker服务。
$ sudo restart docker
Systemctl restart docker
推送成功。
同样在backup上修改配置文件,重启docker
在backup主机上拉取nginx私服镜像
Docker pull 10.104.15.62:5000/nginx
这样backup主机上有了master的镜像。
-
master安装keepalived-master和启动nginx-master
-
安装****keepalived
关于keepalived的版本,在这里安装成功的1.1.19,其余版本遇见了各种问题。
具体可参考链接:
http://www.bubuko.com/infodetail-265205.html
http://www.keepalived.org/documentation.html
apt-get install libssl-dev
apt-get install openssl
apt-get install libpopt-dev
apt-get install gcc
apt-get install daemon
tar –zxvf keepalived-1.1.19.tar.gz
cd keepalived-1.1.19
./configure –prefix=/usr/local/keepalived
make
make install
mkdir /etc/sysconfig
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/sbin/keepalived /sbin
mkdir /etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf /etc/keepalived/
Mkdir –p /etc/rc.d/init.d
cp /usr/share/initramfs-tools/scripts/functions /etc/rc.d/init.d/
vi /etc/init.d/keepalived +23
daemon keepalived start
Mkdir /var/lock/subsys
Service keepalived start
Vi /etc/rc.local
Mkdir /var/lock/subsys
Service keepalived start
/etc/keepalived/keepalived.conf keepalived-master核心配置文件:
编写nginx监测脚本;
/data/chk_nginx.sh
cmod 755 /data/chk_nginx.sh
sh/data/chk_nginx.sh
- 启动nginx-master容器
** **/data/index.html nginx-master!!!
Docker run –p 180:80 –v /data/index.html:/usr/share/nginx/html/index.html
–name nginx-master –d nginx.
启动keepalived
Cd /etc/init.d/
Service keepalived start
Keepalived启动成功:
Nginx-master启动成功:
VIP绑定成功:
关闭nginx-master,可知nginx-master自动启动(keepalived监测脚本chk_nginx.sh实现)
访问VIP成功
- backup安装keepalived-backup和nginx-backup
安装keepalived步骤同理:keepalived-backup核心配置文件:
Keepalived-backup监测脚本:
现在backup并没有VIP
- 测试VIP的转移功能
master的ip配置情况:
4.1关闭master的nginx-master,可知nginx又自动重启
4.2关闭master的keepalived,可知VIP转到bakcup主机上,
并且,nginx-backup已经自动启动
4.3现在master主机恢复keepalived,
可知若主机keepalived恢复正常,则VIP又回归到master主机上,并且master主机的nginx-master自动重启。Backup主机失去VIP绑定功能。
总结:
- alterway/keepalived容器可以实现宿主机的VIP,但是来监测其他容器的运行状态没有实现,可以继续学习一下,应该能够实现这一功能。
https://hub.docker.com/r/alterway/keepalived/
-
容器与容器,容器与主机之间的通信,容器链接等问题。
-
keepalived.conf配置文件的使用
-
网卡,路由,IP配置等问题。