keepalived个人简单理解就是实现一个虚IPkeepalived主从服务器之间切换的功能,当主的keeplived挂掉,从机会无缝接管该虚IP


keepalived它作为一个辅助实现高可用性工具,一般都会配合某个具体服务工作,例如mysql,drbdhaproxy等,本篇介绍的是nginx,在前期做好keepalived配置后,做一个切换脚本,当检测nginx不可用后,脚本会立即执行keepalived关闭操作,从而让从机接管,该虚IP其实会和本机的物理IP做绑定,访问虚IP其实就可以理解为访问本机物理IP,该虚IP会在脚本被触发后切换到从机,而后访问虚IP也就是在访问从机物理IP,从而实现nginx的高可用性。


一.结构以及配置:

1.nginx基本结构信息

Nginx+keepalived(两台)

192.168.1.77 nginx lb0

192.168.1.60 nginx lb1


192.168.1.80 vip0 lb0

192.168.1.81 vip1 lb1

域名:

192.168.1.80 www.crh.com

192.168.1.81 www.crh.com

2.软件安装和配置

#tar zxf keepalived-1.2.7

#cd keepalived-1.2.7

#./configure

#make && make install

安装完成之后,拷贝启动文件和配置文件等。

#cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/keepalived

#cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/keepalived

#cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf

配置文件:

/home/ytb/server/conf/nginx_main.conf (nginx主配置文件)

/etc/keepalived/keepalived.conf (keepalived主配置文件)

3.启动脚本:

/etc/init.d/nginx start

/etc/inti.d/keepalived start


二.Keepalived配置

1.Nginx双主负载lb0服务器的keepalived配置文件的配置

nginx lb0 (192.168.1.77)

#vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

router_id Nginx_Id_1

}


vrrp_script chk_nginx {

script "/root/bin/check_http.sh" #定义检测脚本

interval 2 #检测间隔

weight 2

}


vrrp_instance Nginx1 {

state MASTER

interface eth1

virtual_router_id 51

priority 150


authentication {

auth_type PASS

auth_pass w3e4r5t5

}


track_script {

chk_nginx

}


virtual_ipaddress {

192.168.1.80

}

}


vrrp_instance Nginx2 {

state BACKUP

interface eth1

virtual_router_id 52

priority 110


authentication {

auth_type PASS

auth_pass w3e4r5t5

}


virtual_ipaddress {

192.168.1.81

}

}

检测脚本:

url="http://www.kankanapp.com/test.html"

status=$(/usr/bin/curl -s --head "$url" | awk '/HTTP/ {print $2}')

if [ "$status" != "200" ];then

/etc/init.d/keepalived stop

fi


注: 以上脚本是判断如果主服务器上的nginx 不正常,则马上停止主服务器的keepalived 工作, IP 会自动切换到从服务器上,keepalive 只检测两个keepalived 是否正常工作来决定切换vip,不使用此脚本如果nginx 挂了就不会自动切换了。


2.Nginx双主负载lb0服务器的keepalived配置文件的配置.

Nginx 负载均衡备份服务器(同时此服务器也担当其中的一台源服务器)

192.168.1.60 nginx lb1

#vi /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {

router_id Nginx_Id_2

}


vrrp_script chk_nginx {

script "/root/bin/check_http.sh"

interval 2

weight 2

}


vrrp_instance Nginx1 {

state BACKUP

interface eth0

virtual_router_id 51

priority 110


authentication {

auth_type PASS

auth_pass w3e4r5t5

}


virtual_ipaddress {

192.168.1.80

}

}



vrrp_instance Nginx2 {

state MASTER

interface eth0

virtual_router_id 52

priority 150


authentication {

auth_type PASS

auth_pass w3e4r5t5

}


track_script {

chk_nginx

}


virtual_ipaddress {

192.168.1.81

}

}

LB1上面也有检测脚本,跟上面LB0的检测脚本一样,把他们放到后台运行就可以达到检测的效果了

三.切换测试

此架构是为了充分利用服务器,并负载均衡,两个vip 使用DNS 轮询的方式将域名www.crh.com 绑定到这两个IP 上

1.将nginx lb0 上的nginx 进程杀掉或down 机,vip0 会自动切换至nginx lb1 上.

2.当nginx lb0 服务器恢复以后,vip0 又切回lb0 上,为了保持负载均衡.

同理在lb1 上测试结果一样.

3.测试配图如下:

nginx+keepalived双主热备_keepalived+nginx

nginx+keepalived双主热备_keepalived+nginx_02

当lb0执行 pkill -9 nginx后

nginx+keepalived双主热备_keepalived+nginx_03

在LB0 上执行/etc/init.d/nginx start 启动nginx,并执行service keepalived start 后:

nginx+keepalived双主热备_keepalived+nginx_04

nginx+keepalived双主热备_keepalived+nginx_05


四,补充说明

1.此文档单纯是为了测试Nginx+KeepAlived 双主,不涉及nginx 配置

2.两个VIP 在应用正常的情况下一定要分布在两台机器上以达到负载均衡的目的,但是有可能在服务器故障恢复的时候出现session 不能同步的问题,是瞬间现象,如果想要不抢占,请把MASTER设成BACKUP,并添加参数nopreempt