欢迎使用ShowDoc!
1、安装基础包:
yum -y install libnl libnl-devel
2、上传包:
tar -zxvf keepalived-2.0.20.tar.gz -C /data/imas/base_soft
mkdir -p /data/imas/base_soft/keepalived
cd /data/imas/base_soft/keepalived-2.0.20
./configure --prefix=/data/imas/base_soft/keepalived --sysconf=/etc
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-oVTgHwVh-1681720588751)(null)]
启动 keepalived 时,默认会去 /etc/keepalived 目录下找 keepalived.conf 文件
3、编译安装:
make && make install
cd /etc/keepalived/
4、修改keepalived.conf
主节点配置:
! Configuration File for keepalived
global_defs {
router_id keep_26
}
vrrp_script check_nginx_alive {
script "/etc/keepalived/check_nginx_alive_or_not.sh"
interval 10 #每隔10秒运行上一行的脚本
weight -10 # 如果脚本运行成功,则权重-10
}
vrrp_instance VI_1 {
state MASTER
# 当前实例绑定的网卡
interface ens160
# 保证主备节点一致
virtual_router_id 51
# 优先级/权重,谁的优先级高,在MASTER挂掉以后,就能成为MASTER
priority 100
# 主备之间同步检查的时间间隔,默认1s
advert_int 1
# 认证授权的密码,防止非法节点的进入
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx_alive # 追踪 nginx脚本
}
virtual_ipaddress {
172.16.11.31
}
}
备节点配置:
! Configuration File for keepalived
global_defs {
router_id keep_26
}
vrrp_script check_nginx_alive {
script "/etc/keepalived/check_nginx_alive_or_not.sh"
interval 10 #每隔10秒运行上一行的脚本
weight -10 # 如果脚本运行成功,则权重-10
}
vrrp_instance VI_1 {
state MASTER
# 当前实例绑定的网卡
interface ens160
# 保证主备节点一致
virtual_router_id 51
# 优先级/权重,谁的优先级高,在MASTER挂掉以后,就能成为MASTER
priority 100
# 主备之间同步检查的时间间隔,默认1s
advert_int 1
# 认证授权的密码,防止非法节点的进入
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx_alive # 追踪 nginx脚本
}
virtual_ipaddress {
172.16.11.31
}
}
5、nginx 检测脚本
cd /etc/keepalived
vi check_nginx_alive_or_not.sh
脚本内容如下:
#!/bin/bash
A=ps -C nginx --no-header |wc -l
#判断nginx是否宕机,如果宕机了,尝试重启
if [ $A -eq 0 ]; then
#/data/imas/base_soft/nginx/sbin/nginx
systemctl start nginx
#等待一小会再次检查nginx,如果没有启动成功,则停止keepalived,使其启动备用机
sleep 3
if [ ps -C nginx --no-header |wc -l
-eq 0 ];then
killall keepalived
fi
fi
6、whereis keepalived
keepalived日志文件路径:/var/log/messages
7、配置keepalived开机自启:
systemctl enable keepalived
systemctl daemon-reload
systemctl start keepalived
systemctl status keepalived
systemctl restart keepalived
8、检查虚拟ip访问情况:
访问nginx地址
http://172.16.11.31