安装
yum install keepalived -y
编写检查端口是否通的脚本
#!/bin/bash
CHK_PORT=$1
if [ -n "$CHK_PORT" ];then
PORT_PROCESS=`ss -lnt|grep $CHK_PORT|wc -l`
if [ $PORT_PROCESS -eq 0 ];then
echo "Port $CHK_PORT Is Not Used,End."
exit 1
fi
else
echo "Check Port Cant Be Empty!"
fi
测试:./check_port.sh 8080
检测8080端口是否开通.
keepalived主节点配置
! Configuration File for keepalived
global_defs {
router_id 20.153
}
vrrp_script chk_port {
script "/etc/keepalived/check_port.sh 8080"
interval 2
weight -20
}
vrrp_instance VI_1 {
state MASTER
interface ens192
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_port
}
unicast_src_ip 192.168.1.1 # 本地节点的 IP 地址
unicast_peer {
192.168.1.2 # 另一个节点的 IP 地址
192.168.1.3 # 另一个节点的 IP 地址
}
virtual_ipaddress {
172.16.20.99
}
}
keepalived备节点配置
! Configuration File for keepalived
global_defs {
router_id 20.154
}
vrrp_script chk_port {
script "/etc/keepalived/check_port.sh 8080"
interval 2
weight -20
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_port
}
unicast_src_ip 192.168.1.2 # 本地节点的 IP 地址
unicast_peer {
192.168.1.1 # 另一个节点的 IP 地址
192.168.1.3 # 另一个节点的 IP 地址
}
virtual_ipaddress {
172.16.20.99
}
}
非抢占式及通知服务
vrrp_instance VI_1 {
state BACKUP // 主备都用BACKUP
interface ens192
virtual_router_id 51
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_port
}
virtual_ipaddress {
192.168.20.99
}
nopreempt // 非抢占式
notify_master /etc/keepalived/notify_master.sh
notify_backup /etc/keepalived/notify_backup.sh
notify_fault /etc/keepalived/notify_fault.sh
notify_stop /etc/keepalived/notify_stop.sh
}