首页 > 代码库 > KeepAlived+Nginx多活配置示例
KeepAlived+Nginx多活配置示例
#程序安装,如果使用tar包安装,这里暂不做说明
yum install keepalived
#修改sysctl.conf,DR模式需要在RIP设备上操作:
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.ip_forward = 1
#配置RS端的VIP信息
ifconfig lo:1 VIP netmask 255.255.255.255 brodcast VIP up
route add -host VIP dev lo:1
如果有多个VIP,添加多条策略即可
#需要在keepalived服务器上配置:
Keepalived.conf配置:
global_defs {
notification_email {
<receive_user1@test.com>
<receive_user2@test.com>
}
notification_email_from <send_user@test.com>
smtp_server <IP>
smtp_connect_timeout <INT>
router_id <HOSTNAME>
}
vrrp_script check_nginx {
script "/opt/lvs/script/check_nginx.sh"
interval 2
weight -2
}
#第一个VRRP实例配置
vrrp_instance VSV_1 {
state MASTER|BACKUP #分别在主从配置
interface <NET_DEVICE_NAME>
virtual_router_id <ID_NUM> #注意,这里的ID和VSV_2的不能一样
priority 100|99 #分别在主从配置
advert_int 1
authentication {
auth_type PASS
auth_pass <PASSWORD>
}
vitual_ipaddress {
VIP/MASK dev <NET_DEVICE_NAME> #第一个VIP地址
}
track_script {
check_nginx
}
}
virtual_server VIP PORT {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server RIP1 PORT {
weight 1
TCP_CHECK {
connect_timeout 5
nb_get_retry 2
delay_before_retry 2
connect_port 80
}
}
real_server RIP2 PORT {
weight 1
TCP_CHECK {
connect_timeout 5
nb_get_retry 2
delay_before_retry 2
connect_port 80
}
}
}
#第二个VRRP配置
vrrp_instance VSV_2 {
state BACKUP|MASTER
interface <NET_DEVICE_NAME>
virtual_router_id <ID_NUM_2>
priority 99|100
advert_int 1
authentication {
auth_type PASS
auth_pass <PASSWORD_2>
}
vitual_ipaddress {
VIP_2/MASK dev <NET_DEVICE_NAME>
}
track_script {
check_nginx
}
}
virtual_server VIP_2 PORT {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 60
protocol TCP
real_server RIP1 PORT {
weight 1
TCP_CHECK {
connect_timeout 5
nb_get_retry 2
delay_before_retry 2
connect_port 80
}
}
real_server RIP2 PORT {
weight 1
TCP_CHECK {
connect_timeout 5
nb_get_retry 2
delay_before_retry 2
connect_port 80
}
}
}
注意:红色部分分别在两台机器上做不同配置
#check_nginx.sh脚本
#!/bin/bash
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
/usr/local/nginx/sbin/nginx
sleep 5
if [ "$(ps -ef | grep "nginx: master process"| grep -v grep )" == "" ]
then
killall keepalived
fi
fi
#注意,在实际使用过程中,应将所有RS和DS的配置参数做成服务脚本
本文出自 “致信” 博客,谢绝转载!
KeepAlived+Nginx多活配置示例