平时在网上看到的大部分都是用keepalived对服务器的一个ip做热切换,在虚拟机做的测试也是如此,不过在去新浪面试云平台系统工程师的过程中,面试官针对实际应用问了我一个问题,问题是用keepalived如何做对双网卡服务器网卡检测与web服务检测,为没遇到过这种情况不知道该如何回答,后来在网上搜索资料,看了keepalived权威指南,有这么一段
如果不使用 VRRP Sync Groups 如果keepalived 主机有两个网段,每个网段开启一个VRRP 实例,如果对外的网段出现问题,VRRPD认为自己仍然认为健康,因此 Master和Backup 相互切换,从而导致服务不能正常使用,同时高可用集群也不能正常运行,Sync group 就是为了解决该问题,可以把两个实例放进同一个Sync Group 中
在vmware虚拟机中安装了两个centos5.5系统进行测试了一下,先安装keepalived ,具体安装过程可以参考
master主机配置如下
global_defs { ####全局定义
notification_email { #####指定keepalived在发生事件时(比如切换),需要发送的email对象,可以多个,每行一个
sdj@qq.com
}
notification_email_from sdj@qq.com
smtp_server 127.0.0.1 #####指定发送email的smtp服务器
smtp_connect_timeout 30
router_id LVS_DEVEL #######运行keepalived的机器的一个标识
}
vrrp_sync_group VG1 {
group {
VI_1
VI_2
}
}
vrrp_script chk_http_port {
#script "/opt/nginx_pid.sh" ###监控脚本
script "</dev/tcp/127.0.0.1/80" ####监控本地机器80端口,可以换为其他需要监控的端口,也可以监控进程例如监控haproxy可以写为 script "killall -0 haproxy"
interval 2 ###监控时间
#weight -2 ###经测试keepalived 1.17版本加这个配置不能切换,最新版本1.21加上这个则可以
}
vrrp_instance VI_1 {
state MASTER
interface eth0
track_interface {
eth0
eth1
}
virtual_router_id 51 #VRID标记 master和backup必须一致
mcast_src_ip 192.168.2.113 #发送多播包的地址,如果不设置默认使用绑定的网卡的primary ip
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port ### 就是上面vrrp_script后面的实例名称 执行监控的服务
}
virtual_ipaddress {
192.168.2.116
}
}
vrrp_instance VI_2 {
state MASTER
interface eth1
track_interface {
eth0
eth1
}
virtual_router_id 52
mcast_src_ip 192.168.100.113
priority 150
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port ### 就是上面vrrp_script后面的实例名称 执行监控的服务
}
virtual_ipaddress {
192.168.100.117
}
}
backup主机的配置如下
global_defs {
notification_email {
sdj@qq.com
}
notification_email_from sdj@qq.com
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_sync_group VG1 {
group {
VI_1
VI_2
}
}
vrrp_script chk_http_port {
#script "/opt/nginx_pid.sh" ###监控脚本
script "</dev/tcp/127.0.0.1/80"
interval 2 ###监控时间
weight -2 ###在keepalived 1.17中需要注释,适用于新版本
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
track_interface {
eth0
eth1
}
virtual_router_id 51
mcast_src_ip 192.168.100.114
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port ### 就是上面vrrp_script后面的实例名称 执行监控的服务
}
virtual_ipaddress {
192.168.100.116
}
}
vrrp_instance VI_2 {
state BACKUP
interface eth1
track_interface { ####设置额外的监控,里面的任何一个网卡出现问题,都会进入FAULT状态
eth0
eth1
}
virtual_router_id 52
mcast_src_ip 192.168.2.114
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_http_port ### 就是上面vrrp_script后面的实例名称 执行监控的服务
}
virtual_ipaddress {
192.168.2.117
}
}
配置完keepalived文件后 测试两个主机的VIP转移
把master主机网卡或者keepalived或者是nginx进程停止 都可以瞬间切换到backup主机
启动master主机的网卡 keepalived nginx master又抢占回vip
在业务应用如果master发生故障,切换回backup,master主机online后,又切换回到master,这样频繁的切换不能容忍的,可以设置不抢占的方法
具体方法是将master主机的state也设置为BACKUP,在state BACKUP下面加入nopreempt
backup主机不需要加入,两个主机根据priority的高低进行抢占,高的为MASTER主机
二、安装nginx(master和slave安装步骤一样)
64位操作系统请先安装libunwind库,32位操作系统不要安装。libunwind库为基于64位CPU和操作系统的程序提供了基本的堆栈辗转开解功能,其中包括用于输出堆栈跟踪的API、用于以编程方式辗转开解堆栈的API以及支持C++异常处理机制的API。
1.Tcmalloc 优化Nginx性能
# tar zxvf libunwind-0.99-alpha.tar.gz
# cd libunwind-0.99-alpha/
# CFLAGS=-fPIC ./configure
# make CFLAGS=-fPIC
# make CFLAGS=-fPIC install
2、安装google-perftools:
# tar zxvf google-perftools-0.97.tar.gz
# cd google-perftools-0.97/
# ./configure
# make && make install
# echo "/usr/local/lib" > /etc/ld.so.conf.d/usr_local_lib.conf
# /sbin/ldconfig
# lsof -n|grep tcmalloc
在编译Nginx时添加参数--with-google_perftools_module
3.安装 pcre
#tar zxvf pcre-7.8.tar.gz
#cd pcre-7.8
#./configure
#make && make install
4、安装nginx
# tar zxvf nginx-0.7.62.tar.gz
# cd nginx-0.7.62
# ./configure --with-http_stub_status_module --with-google_perftools_module --prefix=/usr/local/nginx
# make && make install
启动 nginx
# /usr/local/nginx/sbin/nginx然后我们用IE测试一下http://IP地址或域名 就可以看到nginx的默认的页面证明nginx已经启动
关闭 nginx
# kill -QUIT `cat /usr/local/nginx/logs/nignx.pid`
重启 nginx
# kill -HUP `cat /usr/local/nginx/logs/nignx.pid`