nginx + keepalive
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx + keepalive相关的知识,希望对你有一定的参考价值。
目的: 学习nginx + keepalive结合实现双机热备。
【实现图】
【环境】
master系统配置:
[[email protected] html]# ifconfig |grep -A 1 eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:65:14:0F
inet addr:192.168.100.10 Bcast:192.168.100.255 Mask:255.255.255.0
[[email protected] html]# hostname
master
[[email protected] html]# ifconfig |grep -A 1 eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:65:14:0F
inet addr:192.168.100.10 Bcast:192.168.100.255 Mask:255.255.255.0
[[email protected] html]# cat /etc/issue | head -1
entOS release 6.4 (Final)
master nginx提前web环境:
[[email protected] html]# curl 192.168.100.10
<!DOCTYPE html>
<html>
<h1>192.168.100.10 -- lnmp master -- bbs.test.com</h1>
</html>
slave系统配置:
[[email protected] ~]# ifconfig |grep -A 2 eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:65:14:0F
inet addr:192.168.100.10 Bcast:192.168.100.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe65:140f/64 Scope:Link
[[email protected] ~]# hostname
master
[[email protected] ~]# ifconfig |grep -A 1 eth0
eth0 Link encap:Ethernet HWaddr 00:0C:29:65:14:0F
inet addr:192.168.100.10 Bcast:192.168.100.255 Mask:255.255.255.0
[[email protected] ~]# head -1 /etc/issue
entOS release 6.4 (Final)
slave nginx配置:
[[email protected] ~]# curl 192.168.100.13
<!DOCTYPE html>
<html>
<h1>192.168.100.13 -- lnmp slave -- bbs.test.com</h1>
</html>
【安装keepalive】
yum install keepalived
keepalived-1.2.13-5.el6_6.x86_64
版本号:
[[email protected] html]# keepalived -v
Keepalived v1.2.13 (03/19,2015)
安装的重要文件:
/etc/keepalived #配置文件目录
/etc/keepalived/keepalived.conf #配置文件
/etc/rc.d/init.d/keepalived #启动文件
/etc/sysconfig/keepalived #keepalived的系统初识化文件
/usr/bin/genhash #不知道 hash相关的吧
/usr/sbin/keepalived #keepalived的可执行文件
【配置文件的编写】
去除默认的配置文件
> /etc/keepalived/keepalived.conf
master配置配置文件
! Configuration File for keepalived
#core的定义
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
#定义检查脚本
vrrp_script check_http {
script "/root/check_nginx.sh" # verify the pid existance
interval 2 # check every 2 seconds
weight 2 # add 2 points of prio if OK 如果检测监本是成功的则优先级加2
}
#配置实例
vrrp_instance VI_1 {
state MASTER #主机为MASTER,备用机为BACKUP
interface eth0 #interface to monitor #HA监测网络接口
virtual_router_id 51 # Assign one ID for this route 主、备机的virtual_router_id必须相同
priority 101 # 101 on master,100 on backup
nopreempt #非抢占(因为默认如果master挂了,backup顶上去,即使master恢复也不抢占!
debug
authentication {
auth_type PASS ##VRRP认证方式
auth_pass mynginx #密码为mynginx
}
#VIP地址
virtual_ipaddress {
192.168.100.12
}
track_script {
check_http (调用nginx进程检测脚本)
}
}
###############check_http的脚本
[[email protected] html]# cat /root/check_nginx.sh
#!/bin/bash A=`ps -C nginx --no-header | wc -l` if [ $A -eq 0 ];then /etc/init.d/nginx start sleep 3 if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then killall keepalived fi fi
当检测到nginx进程不存在的时候,就干掉所有的keepalived,这时候,请求将会由keepalived的backup接管!!
注意: 脚本一定要有执行权限
chmod +x /root/check_nginx.sh
slave配置配置文件:
......
......
state BACKUP #主机为MASTER,备用机为BACKUP
...
priority 100
【启动】
master和slave 的keepalive和nginx都启动
/etc/init.d/keepalived start
/etc/init.d/nignx start
此时可以看到vip在master机器行,因为优先级高
[[email protected] html]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:65:14:0f brd ff:ff:ff:ff:ff:ff
inet 192.168.100.10/24 brd 192.168.100.255 scope global eth0
inet 192.168.100.12/32 scope global eth0
inet6 fe80::20c:29ff:fe65:140f/64 scope link
valid_lft forever preferred_lft forever
【会导致切换的情况】
1 master挂了(机器挂了或者keepalive进程没了),终归到底keepalive进程没了,此时会vrrp检查对端没包,此时backup接管VIP。
2 check_nginx.sh检查脚本,当检测到nginx进程挂了,且起不来的时候,就把keepalived全部杀掉。
这样当然,就切换到了backup咯。
【疑问】
1 抓包如何抓到vrrp包?
2 keepalive的日志如何弄?
tail -f /var/log/messages
3 非强制好像不起作用。先关掉master的keepalivd再启动master 的keepalived和nginx,master依然会把VIP抢占古来
4 weight像没啥用?
5 检查时间
advert_int 1 #检查间隔,默认1秒
6 track_script其实就是用来检测nginx的。
本文出自 “学通信,第一份工作运维” 博客,请务必保留此出处http://cuidehua.blog.51cto.com/5449828/1844976
以上是关于nginx + keepalive的主要内容,如果未能解决你的问题,请参考以下文章
keepalived入门之keepalive+nginx实例部署
keepalive笔记之二:keepalive+nginx(自定义脚本实现,上述例子也可以实现)