nginx+keepalived实现高可用性
一、随便测试了下nginx+keepalived组成高可用性 如下图:
二、
1、 当用户访问test.abc.com 时,通过DNS 轮循机制,用户的请求会分别分发到两个keepalived 为master 的服务器上。 实现负载均衡的功能
2、 keepalived 通过发送和接收组播包中的同一个virtual_router_id 的中的成员的存活,来确定对方的不可用,一旦检测到对方的不可用,即会切换他的备份角色为主,
即:当真实机192.168.200.143上的keepalived 检测到 真实机192.168.200.144上的keepalived 不可用时,143上对
192.168.200.144的VIP 156的backup 会变为master
3、 keepalived 只能实现针对网络层的一个高可用,即
一台装有keepalived的进程检测不到另一台装有keepalived 的存活是,才会发生切换。
4、 针对应用层的一个检测keepalived 需要通过vrrp_scripts 来实现
三、安装请参考
http://blog.chinaunix.net/u3/116418/showart_2278032.html
四、配置
服务器A:
vi /etc/keepalived/keepalie.conf
# Configuration File for keepalived
# by chibiaowu@gmail.com
# vrrp master master
global_defs {
router_id LVS_DEVEL
}
vrrp_script chk_nginx {
script "/tmp/check.sh"
#script " interval 2
weight 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx 10
}
virtual_ipaddress {
192.168.200.145
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth0
virtual_router_id 53
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx 10
}
virtual_ipaddress {
192.168.200.146
}
}
服务器B:
# Configuration File for keepalived
# by chibiaowu@gmail.com
# vrrp master master
global_defs {
router_id LVS_DEVEL2
}
vrrp_script chk_nginx {
script "/tmp/check.sh"
interval 2
weight 2
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 52
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.200.145
}
}
vrrp_instance VI_2 {
state MASTER
interface eth0
virtual_router_id 53
priority 180
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx 10
}
virtual_ipaddress {
192.168.200.146
}
}
五、check.sh
#!/bin/sh
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ]
then
/usr/local/nginx/sbin/nginx
sleep 1
if [ `ps -C nginx --no-header |wc -l` -eq 0 ]
then
killall keepalived
fi
fi
阅读(2595) | 评论(0) | 转发(0) |