5.Lvs+Keepalived健康检查
1. Nginx+keepalived对后端服务器心跳检查(需要自定义脚本)
原理:Keepalived并不跟nginx耦合,它俩完全不是一家人但是keepalived提供一个机制:让用户自定义一个shell脚本去检测用户自己的程序,返回状态给keepalived就可以了
#MASTER节点
global_defs {
}
vrrp_script chk_health {
script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1"
interval 1 #每隔1秒执行上述的脚本,去检查用户的程序ngnix
weight -2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 1
priority 100
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_health
}
virtual_ipaddress {
10.0.0.10/24
}
notify_master "/usr/local/keepalived/sbin/notify.sh master"
notify_backup "/usr/local/keepalived/sbin/notify.sh backup"
notify_fault "/usr/local/keepalived/sbin/notify.sh fault"
}
#添加切换通知脚本:vi /usr/local/keepalived/sbin/notify.sh
#!/bin/bash
case "$1" in
master)
/usr/local/nginx/sbin/nginx
exit 0
;;
backup)
/usr/local/nginx/sbin/nginx -s stop
/usr/local/nginx/sbin/nginx
exit 0
;;
fault)
/usr/local/nginx/sbin/nginx -s stop
exit 0
;;
*)
echo 'Usage: notify.sh {master|backup|fault}'
exit 1
;;
esac
#添加执行权限:chmod +x /usr/local/keepalived/sbin/notify.sh
global_defs {
}
vrrp_script chk_health {
script "[[ `ps -ef | grep nginx | grep -v grep | wc -l` -ge 2 ]] && exit 0 || exit 1"
interval 1
weight -2
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 1
priority 99
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_health
}
virtual_ipaddress {
10.0.0.10/24
}
notify_master "/usr/local/keepalived/sbin/notify.sh master"
notify_backup "/usr/local/keepalived/sbin/notify.sh backup"
notify_fault "/usr/local/keepalived/sbin/notify.sh fault"
}
#在第二台机器上添加notify.sh脚本
#分别在两台机器上启动keepalived
service keepalived start
chkconfig keepalived on
2. Lvs+keepalived对后端服务器心跳检查(不需要自定义脚本)
1. 在两台(192.168.232.201和192.168.232.205) Lvs转发服务器上安装ipvsadm和Keepalived:yum -y install ipvsadm/keepalived
2. 修改配置文件:vi /etc/keepalived/keepalived.conf
主机:Lvs+keepalived(MASTER)
global_defs { #指定keepalived在发生切换时需要发送email到的对象 notification_email { root@localhost #邮件接收者 } notification_email_from lvs@localhost #邮件发送者 smtp_server 127.0.0.1 #邮件服务器地址,如未安装,使用本地 smtp_connect_timeout 30 router_id LVS_DEVEL } #IP漂移协议 vrrp_instance VI_1 { state MASTER #指定A节点为主节点 备分机上设置为BACKUP即可 interface eth0 #绑定IP漂移的网络接口,在该接口上绑定VIP virtual_router_id 51 #VRRP组名,两个节点的设置必须一样 priority 100 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.232.206/24 dev eth0 label eth0:1 #绑定VIP } } #只接收80端口过来的请求 virtual_server 192.168.232.206 80 { delay_loop 6 lb_algo wlc #轮循 lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP #RS服务器1 real_server 192.168.232.200 80 { weight 1 #健康检查 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } #RS服务器2 real_server 192.168.232.204 80 { weight 1 #健康检查 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
备机:Lvs+keepalived(BACKUP)
global_defs { #指定keepalived在发生切换时需要发送email到的对象 notification_email { root@localhost #邮件接收者 } notification_email_from lvs@localhost #邮件发送者 smtp_server 127.0.0.1 #邮件服务器地址,如未安装,使用本地 smtp_connect_timeout 30 router_id LVS_DEVEL } #IP漂移协议 vrrp_instance VI_1 { state BACKUP #指定A节点为主节点 备分机上设置为BACKUP即可 interface eth0 #绑定IP漂移的网络接口,在该接口上绑定VIP virtual_router_id 51 #VRRP组名,两个节点的设置必须一样 priority 99 advert_int 1 authentication { auth_type PASS auth_pass 1111 } virtual_ipaddress { 192.168.232.206/24 dev eth0 label eth0:1 #绑定VIP } } #只接收80端口过来的请求 virtual_server 192.168.232.206 80 { delay_loop 6 lb_algo wlc #轮循 lb_kind DR nat_mask 255.255.255.0 persistence_timeout 50 protocol TCP #RS服务器1 real_server 192.168.232.200 80 { weight 1 #健康检查 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } #RS服务器2 real_server 192.168.232.204 80 { weight 1 #健康检查 HTTP_GET { url { path / status_code 200 } connect_timeout 3 nb_get_retry 3 delay_before_retry 3 } } }
3. 启动两台Lvs转发服务器的Keepalived服务:service keepalived start
4.通过ifconfig命令查看主从Lvs转发服务器的网络接口:
5.查看地址和端口号:ipvsadm -Ln
6.使用浏览器进行访问:http://192.168.232.206:80/
7.主机挂掉:init 6
你情我愿,我们就在一起!