nginx + keepalive 实现HA
主nginx负载均衡器 192.168.166.203
辅nginx负载均衡器 192.168.166.177
VIP地址 192.168.166.178
tar -zxvf keepalived-1.2.1.tar.gz
cd keepalived-1.2.1
./configure --prefix=/usr/local/keepalived --sysconf=/etc
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/sbin/keepalived /bin/
chkconfig --add keepalived
chmod 755 /etc/init.d/keepalived
chkconfig keepalived on
service keepalived start
MASTER
vim /etc/keepalived/keepalived.conf
smtp_server xxx.smtp.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
script "/root/monitor_nginx.sh"
interval 2
weight 2
}
state MASTER #(主机为MASTER,备用机为BACKUP)
interface eth0 #(HA监测网络接口)
virtual_router_id 61 #(主、备机的virtual_router_id必须相同)
#mcast_src_ip 192.168.166.203 #(多播的源IP,设置为本机外网IP,与VIP同一网卡)此项可不设置
priority 90 #(主、备机取不同的优先级,主机值较大,备份机值较小,值越大优先级越高)
advert_int 1 #(VRRP Multicast广播周期秒数)
authentication {
auth_type PASS #(VRRP认证方式)
auth_pass 1234 #(密码)
}
track_script {
Monitor_Nginx #(调用nginx进程检测脚本)
}
virtual_ipaddress {
192.168.166.178 #(VRRP HA虚拟地址)
}
}
###BACKUP方面只需要修改state为BACKUP , priority比MASTER稍低就OK了
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
service keepalived start
或
/usr/local/keepalived/sbin/keepalived -D -f /etc/keepalived/keepalived.conf
2. 停止其中一个web看是否能将访问转到另一台上
3. 停止两台nginx上任何一个nginx进程看监控进程脚本是否会自动启动nginx
4. 停止任何一台nginx上的keepalived进程看另一台是否接管vip
track_interface的意思是将Linux中你想监控的网络接口卡监控起来,当其中的一块出现故障是keepalived都将视为路由器出现故障。
这里请注意: virtual_router_id 61,同一组master/backup中,也就是如果你设置了多个VRRP,同一组这个ID必须相同,不然启动的时候主从都会把VIP给启动了
即:当真实机192.168.166.177上的keepalived 检测到 真实机192.168.166.203上的keepalived 不可用时,177上将使用vip:192.168.166.178对外服务并由backup角色转变为master
keepalived+nginx实现 HA与负载均衡中keepalived负责实现HA功能,控制VIP(虚拟网络地址)转移功能,nginx实现对realserver七层负载均衡功能,nginx提供代理服务和cache功能,DNS服务可以负责前段VIP的负载均衡。
keepalived的配置
192.168.200.1 keepalived配置
! Configuration File for keepalived
global_defs {
notification_email {
[email protected]
}
notification_email_from
[email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_TEST_YDQ
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 61
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.101/24 dev eth0 label eth0:0
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 62
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
192.168.200.102/24 dev eth0 label eth0:1
}
}
vrrp_instance VI_3 {
state BACKUP
interface eth0
virtual_router_id 63
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 3333
}
virtual_ipaddress {
192.168.200.103/24 dev eth0 label eth0:1
}
}