安装keepalived
安装命令
yum install keepalived -y
查看是否安装成功,使用如下命令能看到版本号,表示成功
rpm -q -a keepalived
使用yum命令安装keepalived成功后,在/etc/keepalived目录下有keepalived.conf配置文件,高可用配置就是主要配置该文件
cd /etc/keepalived
vi keepalived.conf
主机的keepalived.conf的配置内容如下
主监控MASTER配置
!Configuration File for keepalived
global_defs {
   
   router_id keep_01
   notification_email {
     qj1314520@126.com
   }
   notification_email_from root@xuad.com
   smtp_server mail.xuad.com
   smtp_connect_timeout 30
   vrrp_skip_check_adv_addr
   
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   script_user root
   enable_script_security
}
vrrp_script chk_nginx {
    script "/kdata/keepalived/nginx_check.sh"   
    interval 2
    weight 3
}
vrrp_instance VI_1 {
    
    state MASTER     
   
    interface ens33	
   
    virtual_router_id 51
    mcast_src_ip 192.168.132.131	
   
    priority 100     
    
    advert_int 1
    
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    
	
    virtual_ipaddress {
        192.168.132.120    
    }
   
    track_script {
        chk_nginx
    }
}
vrrp_instance VI_2 {
    state MASTER
    interface ens33
    virtual_router_id 52
    priority 101
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.132.121 
    }
}
备用监控BACKUP配置
!Configuration File for keepalived
global_defs {
   
   router_id keep_01
   notification_email {
     qj1314520@126.com
   }
   notification_email_from root@xuad.com
   smtp_server mail.xuad.com
   smtp_connect_timeout 30
   vrrp_skip_check_adv_addr
   
   vrrp_garp_interval 0
   vrrp_gna_interval 0
   script_user root
   enable_script_security
}
vrrp_script chk_nginx {
    script "/kdata/keepalived/nginx_check.sh"   
    interval 2
    weight 3
}
vrrp_instance VI_1 {
    
    state MASTER     
   
    interface ens33	
   
    virtual_router_id 51
    mcast_src_ip 192.168.132.132	
   
    priority 100     
    
    advert_int 1
    
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    
	
    virtual_ipaddress {
        192.168.132.120    
    }
   
    track_script {
        chk_nginx
    }
}
切换到当前用户目录空间,建立一个keepalived目录,这个目录存放的是nginx是否正常启动的检测脚本
进入keepalived目录,创建nginx_check.sh文件,文件内容如下
counter=`ps -C nginx --no-header |wc -l`
if [ $counter -eq 0 ];then
     systemctl restart docker
      sleep 3
            if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
	  
	  echo "重新启动失败" >> /kdata/keepalived/logs
                  systemctl stop keepalived
	  exit 1
           else
	
                 echo "重新启动成功" >> /kdata/keepalived/logs
                 exit 0
           fi 
else
     
     echo "nginx运行正常" >> /kdata/keepalived/logs;
     exit 0
fi
脚本创建完成后,必须给脚本赋予可以执行的权限,这里为了方便直接给所有权限
chmod 777 nginx_check.sh
启动keepalived并查看其状态
systemctl start keepalived
systemctl status keepalived
启动keepalived服务,并开机自启
systemctl start keepalived
systemctl enable keepalived
但是查看日志
 tail -f /var/log/messages
-------------docker部署,暂时未启动成功,标注------------------------------
docker pull osixia/keepalived:2.0.20
docker run --cap-add=NET_ADMIN --cap-add=NET_BROADCAST --cap-add=NET_RAW --net=host -d osixia/keepalived:2.0.20
docker run --name keepalived  \
-v /kdata/keepalived/conf/keepalived.conf:/container/service/keepalived/assets/keepalived.conf \
-d osixia/keepalived:2.0.20 
docker run  --name keepalived01  --restart=always  \
--net mynetwork --ip 172.18.0.10 --privileged=true  \
-e TZ="Asia/Shanghai" -e "I18N_LOCALE=zh-CN"  \
-v /kdata/keepalived/keepalived.conf:/container/service/keepalived/assets/keepalived.conf \
-v /kdata/keepalived/conf/nginx_check.sh:/container/service/nginx_check.sh \
-e KEEPALIVED_VIRTUAL_IPS="#PYTHON2BASH:['192.168.132.120']" \
-e KEEPALIVED_UNICAST_PEERS="#PYTHON2BASH:['192.168.132.131','192.168.132.132','192.168.132.133']" \
-e KEEPALIVED_PRIORITY="100" \
-e KEEPALIVED_INTERFACE="ens33"  \
-e KEEPALIVED_PASSWORD="111111" \
-d osixia/keepalived:2.0.20
docker run  --name keepalived01  --restart=always  \
--rm -v /kdata/keepalived/keepalived.conf:/container/service/keepalived/assets/keepalived.conf \
-v /kdata/keepalived/conf/nginx_check.sh:/container/service/nginx_check.sh \
-d osixia/keepalived:2.0.20
service keepalived start
ps -aux |grep keepalived
docker logs -f --tail=1000 keepalived01