keepalived 官方下载地址
https://www.keepalived.org/download.html
环境
nginx安装 略
keepalived安装过程
1.登陆245服务器下载解压安装
[root@k8s-nginx1 opt]# wget https://www.keepalived.org/software/keepalived-2.0.19.tar.gz [root@k8s-nginx1 opt]# tar zxvf keepalived-2.0.19.tar.gz [root@k8s-nginx1 opt]# cd keepalived-2.0.19 [root@k8s-nginx1 keepalived-2.0.19]# ./configure --sysconf=/etc [root@k8s-nginx1 keepalived-2.0.19]# make [root@k8s-nginx1 keepalived-2.0.19]# make install
2.配置
[root@k8s-nginx1 keepalived]# cat /etc/keepalived/keepalived.conf
global_defs { notification_email { nwcao@liunx.com } notification_email_from ops@liunx.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id NGINX_1 } vrrp_script nginx { script /opt/chk_nginx.sh interval 2 weight -10 } vrrp_instance VI_1 { state MASTER interface ens160 #ifconfig 查看名字,例如eth0 virtual_router_id 51 priority 100 #权重 advert_int 1 authentication { #认证信息 auth_type PASS auth_pass 1111 } track_script { nginx } virtual_ipaddress { #虚拟IP 可以多个 10.23.215.222 } }
检测nginx文件
[root@k8s-nginx1 keepalived]# cat /opt/chk_nginx.sh #!/bin/bash #check nginx server status nginxpid=$(ps -C nginx --no-header |wc -l) if [ $nginxpid -eq 0 ];then systemctl stop keepalived echo "Keepalived stopped, please check your Nginx! " |tee -a /var/log/messages fi
3.登陆246 重复安装步骤keepalived.conf修改成
[root@k8s-nginx1 keepalived]# cat /etc/keepalived/keepalived.conf global_defs { notification_email { nwcao@liunx.com } notification_email_from ops@liunx.com smtp_server 127.0.0.1 smtp_connect_timeout 30 router_id NGINX_1 } vrrp_script nginx { script /opt/chk_nginx.sh interval 2 weight -10 } vrrp_instance VI_1 { state backup interface ens160 #ifconfig 查看名字,例如eth0 virtual_router_id 51 priority 90 #权重 advert_int 1 authentication { #认证信息 auth_type PASS auth_pass 1111 } track_script { nginx } virtual_ipaddress { #虚拟IP 可以多个 10.23.215.222 } }
4.启动命令systemctl start keepalived
关闭命令 systemctl stop keepalived
开机启动命令 systemctl enable keepalived
5.测试 正常访问到245页面
关闭245服务器上nginx服务
正常切换到246服务器上页面
启动245服务器上nginx 启动 keepalived服务
页面切换到245服务器
结果符合预期。