Welcome to ShowDoc!
1. Install the basic package:
yum -y install libnl libnl-devel
2. Upload package:
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
[External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly (img-oVTgHwVh-1681720588751)(null)]
start When keepalived, the default will go to the /etc/keepalived directory to find the keepalived.conf file
3. Compile and install:
make && make install
cd /etc/keepalived/
4. Modify keepalived.conf
Master node configuration:
! 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
}
}
Standby node configuration:
! 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 detection script
cd /etc/keepalived
vi check_nginx_alive_or_not.sh
The content of the script is as follows:
#!/bin/bash
A= ps -C nginx --no-header |wc -l
#Determine whether nginx is down, if it is down, try to restart
if [ $A -eq 0 ]; then
#/data/imas /base_soft/nginx/sbin/nginx
systemctl start nginx
#Wait for a while and check nginx again. If it does not start successfully, stop keepalived and make it start the standby machine
sleep 3
if [ ps -C nginx --no-header |wc -l
-eq 0 ];then
killall keepalived
fi
fi
6、whereis keepalived
keepalived log file path: /var/log/messages
7. Configure keepalived to boot automatically:
systemctl enable keepalived
systemctl daemon-reload
systemctl start keepalived
systemctl status keepalived
systemctl restart keepalived
8. Check the virtual ip access status:
Visit nginx address
http://172.16.11.31