dnf install -y keepalived
MASTER /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id nginx-ha-21
}
vrrp_script nginx_check
{
script "/usr/libexec/keepalived/check-nginx-status"
interval 3
}
vrrp_instance vi_nginx {
state MASTER
interface eth0
virtual_router_id 18
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass h3m5Ewvk
}
track_script {
nginx_check weight 0
}
virtual_ipaddress {
192.168.20.18 dev eth0
}
}
BACKUP /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id nginx-ha-22
}
vrrp_script nginx_check
{
script "/usr/libexec/keepalived/check-nginx-status"
interval 3
}
vrrp_instance vi_nginx {
state BACKUP
interface eth0
virtual_router_id 18
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass h3m5Ewvk
}
track_script {
nginx_check weight 0
}
virtual_ipaddress {
192.168.20.18 dev eth0
}
}
配置检测脚本 /usr/libexec/keepalived/check-nginx-status
#! /bin/bash
http_url=" localhost/nginx-status"
http_code=$( curl -sq -m 5 -o /dev/null $http_url -w %{http_code})
test $http_code = 200 && exit 0 || exit 1
chmod 755 /usr/libexec/keepalived/check-nginx-status
chcon -u system_u -t keepalived_unconfined_script_exec_t /usr/libexec/keepalived/check-nginx-status
firewall-cmd --add-rich-rule=' rule protocol value="vrrp" family=ipv4 destination address=224.0.0.18 accept' --permanent
firewall-cmd --reload
systemctl start keepalived
systemctl enable keepalived
systemctl status keepalived