Keepalived+nginx 高可用配置 - 双主模式(双机互为主备)
發表於 : 週二 9月 05, 2017 11:30 am
安裝
yum -y install keepalived
兩台要做的設定
cd /etc/keepalived/
vim nginx.sh
# 监控nginx进程,若nginx主进程不存在则启动nginx
# 若5s后nginx进程还是不存在的话kill掉keepalived进程,防止nginx没运行该主机的keepalived还接管虚拟IP
#!/bin/bash
status=$(ss -lnp | grep -c 'nginx')
if [ ${status} == 0 ]; then
systemctl nginx restart
sleep 2
status=$(ss -lnp | grep -c 'nginx')
if [ ${status} == 0 ]; then
systemctl stop keepalived
fi
fi
chmod +x nginx.sh
cp keepalived.conf keepalived.conf.bak
vi keepalived.conf
vrrp_script nginx {
script "/etc/keepalived/nginx.sh"
interval 2 # 检测间隔2s
weight -5 # 若检测失败权重减低5
fall 3 # 检测失败3次就定义为down状态
rise 2 # 检测失败后,检测成功超过2次就定义为up状态
}
172.16.20.137
代碼: 選擇全部
vrrp_instance VI_1 {
state MASTER # master_server
interface ens160
virtual_router_id 51
priority 100 # 权重值,值大的优先级高
advert_int 2 # 检测时间间隔2s
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.20.111 # VIP
}
track_script {
nginx # 检测脚本
}
}
vrrp_instance VI_2 {
state BACKUP # backup_server
interface ens160
virtual_router_id 52
priority 100 # 权重值,值大的优先级高
advert_int 2 # 检测时间间隔2s
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.20.110 # VIP
}
track_script {
nginx # 检测脚本
}
}
172.16.20.138
代碼: 選擇全部
vrrp_script nginx {
script "/etc/keepalived/nginx.sh"
interval 2 # 检测间隔2s
weight -5 # 若检测失败权重减低5
fall 3 # 检测失败3次就定义为down状态
rise 2 # 检测失败后,检测成功超过2次就定义为up状态
}
vrrp_instance VI_1 {
state BACKUP # backup_server
interface ens160
virtual_router_id 51
priority 100 # 权重值,值大的优先级高
advert_int 2 # 检测时间间隔2s
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.20.111 # VIP
}
track_script {
nginx # 检测脚本
}
}
vrrp_instance VI_2 {
state MASTER # master_server
interface ens160
virtual_router_id 52
priority 100 # 权重值,值大的优先级高
advert_int 2 # 检测时间间隔2s
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.20.110 # VIP
}
track_script {
nginx # 检测脚本
}
}
172.16.20.135
代碼: 選擇全部
vrrp_script nginx {
script "/etc/keepalived/nginx.sh"
interval 2 # 检测间隔2s
weight -5 # 若检测失败权重减低5
fall 3 # 检测失败3次就定义为down状态
rise 2 # 检测失败后,检测成功超过2次就定义为up状态
}
vrrp_instance VI_1 {
state MASTER # master_server
interface ens160
virtual_router_id 50
priority 100 # 权重值,值大的优先级高
advert_int 2 # 检测时间间隔2s
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.20.222 # VIP
}
track_script {
nginx # 检测脚本
}
}
vrrp_instance VI_2 {
state BACKUP # backup_server
interface ens160
virtual_router_id 49
priority 100 # 权重值,值大的优先级高
advert_int 2 # 检测时间间隔2s
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.20.220 # VIP
}
track_script {
nginx # 检测脚本
}
}
172.16.20.136
代碼: 選擇全部
vrrp_script nginx {
script "/etc/keepalived/nginx.sh"
interval 2 # 检测间隔2s
weight -5 # 若检测失败权重减低5
fall 3 # 检测失败3次就定义为down状态
rise 2 # 检测失败后,检测成功超过2次就定义为up状态
}
vrrp_instance VI_1 {
state BACKUP # backup_server
interface ens160
virtual_router_id 50
priority 100 # 权重值,值大的优先级高
advert_int 2 # 检测时间间隔2s
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.20.222 # VIP
}
track_script {
nginx # 检测脚本
}
}
vrrp_instance VI_2 {
state MASTER # master_server
interface ens160
virtual_router_id 49
priority 100 # 权重值,值大的优先级高
advert_int 2 # 检测时间间隔2s
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
172.16.20.220 # VIP
}
track_script {
nginx # 检测脚本
}
}
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --in-interface ens160 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=112/tcp --permanent
firewall-cmd --reload
setenforce 0 ##设置SELinux 成为permissive模式
##setenforce 1 设置SELinux 成为enforcing模式
systemctl start keepalived
systemctl restart keepalived
虛擬IP查詢
ip addr # 查看虚拟IP
[驗證測試]
打開瀏覽器: http://172.16.20.111 http://172.16.20.110 http://172.16.20.222 http://172.16.20.220 可以看到畫面
到 其中一台主機 執行: systemctl stop keepalived
再打開瀏覽器: http://172.16.20.111 http://172.16.20.110 http://172.16.20.222 http://172.16.20.220 一樣可以看到畫面
查看log : tail -f /var/log/messages