keepalived + nginx 实现双机热备

# docker run -itd  --name centos_m1 centos
# 进入容器
# docker exec -it centos_m1 /bin/bash
# 安装nginx
# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
# yum install nginx
# 安装keepalived
# yum install -y gcc openssl-devel popt-devel
# yum -y install ipvsadm
# yum install keepalived

修改keepalived.conf

! Configuration File for keepalived
global_defs {
    notification_email {
        xxxx@xx.com
    }
    notification_email_from xxxx@example.com
    smtp_server mail.example.com
    smtp_connect_timeout 30
    router_id LVS_DEVEL 
}
 
 
vrrp_script chk_nginx {
    script "/etc/keepalived/nginxcheck.sh" 
    interval 2
    weight -5
    fall 3
    rise 2
}
 
 
vrrp_instance VI_1 {
    state MASTER  
    interface eth0  
    virtual_router_id 2 
    priority 101 
    advert_int 2
    authentication {
        auth_type PASS 
        auth_pass 1111
    }
    virtual_ipaddress {
        172.17.0.200  
    }
    track_script {
       chk_nginx
    }
 
}

编辑nginxcheck.sh

A=`ps -ef | grep nginx | grep -v grep | wc -l`
if [ $A -eq 0 ];then
    #killall keepalived
    ps -ef|grep keepalived|grep -v grep|awk '{print $2}'|xargs kill
fi
# 增加执行权限
# chmod +x check_nginx.sh
# docker commit centos_m1 centos_keepalived:v1

启动master容器 

# docker run --privileged  -tid --name  keepalived_master centos_keepalived:v1 /usr/sbin/init
# 启动keepalived
# systemctl start keepalived.service

修改nginx主页

启动slave启动

# docker run --privileged  -tid --name  keepalived_slave centos_keepalived:v1 /usr/sbin/init
# 启动keepalived
# systemctl start keepalived.service

修改nginx主页

在实体主机上进行测试

关闭master容器的keepalived

 

posted @ 2018-09-09 22:05  MasterSword  阅读(156)  评论(0编辑  收藏  举报