我的本地环境,一台 Unbuntu 18.04 系统的物理机 想实践下 虚拟 ip,keepalive 就一台机器,所以用 docker 来实践一下。
关于虚拟 ip, 可以看下这篇文章https://www.gitos.org/2019/08/19/vip-ha.html
关于 docker, keepalive 基本按这篇文章https://www.jianshu.com/p/6a8f38b8076d
修改 dockerfile,减少重复下载环境
1 2 3 4 5 6 7 8 FROM centos:latestMAINTAINER relengxing<relengxing@outlook.com>RUN yum install -y gcc openssl-devel popt-devel RUN yum install -y net-tools RUN yum install -y vim RUN yum install -y keepalived RUN yum install -y nginx
启动 centos 容器 –privileged=true 获取特权 /sbin/init:这个地方是实测 /bin/bash,会有权限没有。因为我还实验了下手动加虚拟 ip
1 2 docker run --name centos_master -itd --privileged =true relengxing/centos:v1 /sbin/init docker run --name centos_slave -itd --privileged =true relengxing/centos:v1 /sbin/init
可以进容器查看 ip 地址
Master 节点ifconfig
1 2 3 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255 ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
在容器外则通过 docker network docker network ls docker network inspect d8c58097a4c3
ifconfig eth0:0 172.17.0.88 netmask 255.255.0.0 up ifconfig eth0:0 down
修改/etc/keepalived/keepalived.conf
的内容
主节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 ! Configuration File for keepalived global_defs { notification_email { xuad@xuad.com } notification_email_from root@xuad.com smtp_server mail.xuad.com smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script chk_nginx { script "/etc/keepalived/nginx_pid.sh" # 检查nginx状态的脚本 interval 2 weight 3 } vrrp_instance VI_1 { state MASTER #备份服务器上将MASTER改为BACKUP interface eth0 virtual_router_id 51 priority 101 #备份服务上将100改为小于100,可配置成90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.17.0.99 #有多个vip可在下面继续增加 } track_script { chk_nginx } }
备份节点
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 ! Configuration File for keepalived global_defs { notification_email { xuad@xuad.com } notification_email_from root@xuad.com smtp_server mail.xuad.com smtp_connect_timeout 30 router_id LVS_DEVEL vrrp_skip_check_adv_addr vrrp_strict vrrp_garp_interval 0 vrrp_gna_interval 0 } vrrp_script chk_nginx { script "/etc/keepalived/nginx_pid.sh" # 检查nginx状态的脚本 interval 2 weight 3 } vrrp_instance VI_1 { state BACKUP #备份服务器上将MASTER改为BACKUP interface eth0 virtual_router_id 51 priority 99 #备份服务上将100改为小于100,可配置成90 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.17.0.99 #有多个vip可在下面继续增加 } track_script { chk_nginx } }
nginx_pid.sh
1 2 3 4 5 6 7 8 9 10 11 A=`ps -ef | grep nginx | grep -v grep | wc -l`if [ $A -eq 0 ];then nginx sleep 2 if [ `ps -ef | grep nginx | grep -v grep | wc -l` -eq 0 ];then ps -ef|grep keepalived|grep -v grep|awk '{print $2}' |xargs kill -9 fi fi
修改 nginx 显示的页面,我就修改了这一行,标示是主机还是备份机
主节点
1 2 <strong> nginx master Node </strong> HTTP server after it has been
从节点
1 <strong> nginx Slave Node </strong > HTTP server after it has been
然后在主机运行curl 172.17.0.99
显示
1 2 3 4 ... <strong>nginx master Node</strong> HTTP server after it has been ...
关闭主节点后 然后在主机运行curl 172.17.0.99
显示
1 2 3 ... <strong> nginx slave Node </strong > HTTP server after it has been ...
遇到一个问题没有解决
在主节点运行nginx -s stop
推出 nginx 后,没有切换到从节点,必须关闭主节点。
更新: 这个问题看了下 是 nginx 检测脚本的问题 修改为
1 2 3 4 5 6 #!/bin/bash A=`ps -C nginx --no-header |wc -l` if [ $A -eq 0 ];then systemctl stop keepalived.service fi
成功了