目的: 學習nginx + keepalive結合實作雙機熱備。
【實作圖】
<a href="http://s5.51cto.com/wyfs02/M01/86/AA/wKioL1fG1n6ieUYGAABEkJkEL8A459.png" target="_blank"></a>
【環境】
master系統配置:
[root@master 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
[root@master html]# hostname
master
[root@master html]# cat /etc/issue | head -1
entOS release 6.4 (Final)
master nginx提前web環境:
[root@master html]# curl 192.168.100.10
<!DOCTYPE html>
<html>
<h1>192.168.100.10 -- lnmp master -- bbs.test.com</h1>
</html>
slave系統配置:
[root@master ~]# ifconfig |grep -A 2 eth0
inet6 addr: fe80::20c:29ff:fe65:140f/64 Scope:Link
[root@master ~]# hostname
[root@master ~]# ifconfig |grep -A 1 eth0
[root@master ~]# head -1 /etc/issue
slave nginx配置:
[root@slave ~]# curl 192.168.100.13
<h1>192.168.100.13 -- lnmp slave -- bbs.test.com</h1>
【安裝keepalive】
yum install keepalived
keepalived-1.2.13-5.el6_6.x86_64
版本号:
[root@master 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的腳本
[root@master html]# cat /root/check_nginx.sh
<code>#!/bin/bash</code>
<code>A=`</code><code>ps</code> <code>-C nginx --no-header | </code><code>wc</code> <code>-l`</code>
<code>if</code> <code>[ $A -</code><code>eq</code> <code>0 ];</code><code>then</code>
<code> </code><code>/etc/init</code><code>.d</code><code>/nginx</code> <code>start</code>
<code> </code><code>sleep</code> <code>3</code>
<code> </code><code>if</code> <code>[ `</code><code>ps</code> <code>-C nginx --no-header | </code><code>wc</code> <code>-l` -</code><code>eq</code> <code>0 ];</code><code>then</code>
<code> </code><code>killall keepalived</code>
<code> </code><code>fi</code>
<code>fi</code>
當檢測到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機器行,因為優先級高
[root@master 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包?
<code>tcpdump -vvn |</code><code>grep</code> <code>-i vrrp</code>
可以看到優先級的詳細資訊。
或者通過指定vrr協定抓取
<code>tcpdump -vnn vrrp</code>
2 keepalive的日志如何弄?
tail -f /var/log/messages
3 非強制nopreempt好像不起作用。先關掉master的keepalivd再啟動master 的keepalived和nginx,master依然會把VIP搶占古來
據說是非強占,隻能在backup中起作用,即要将初識狀态都設定成為backup,但是實驗未成功。 這裡不細細研究。
4 weight像沒啥用?
在這個地方,确實沒用。但是weight的隻要腳本執行傳回值為0(即echo $?為0就表示腳本執行成功),是以以上的哪個實驗,master的pri優先級為101+2=103,backup的pri優先級為100+2=102。
其實上面切換的主要關鍵,是用到關閉keepalived。進行切換。
messgages可以看到的資訊。
VRRP_Script(check_http) succeeded
5 檢查時間
6 track_script其實就是用來檢測nginx的。
簡單VRRP的原理:
看vrrp包資訊:
21:47:12.591586 IP (tos 0xc0, ttl 255, id 431, offset 0, flags [none], proto VRRP (112), length 40)
192.168.100.10 > 224.0.0.18: VRRPv2, Advertisement, vrid 51, prio 103, authtype simple, intvl 1s, length 20, addrs: 192.168.100.12 auth "mynginx^@"
是master通過多點傳播位址224.0.0.18通告自己的route id和優先級。收到包的節點,并且認證通過的,比較自己和收到的優先級,進行選舉新的mater。
本文轉自殘劍部落格51CTO部落格,原文連結http://blog.51cto.com/cuidehua/1844976如需轉載請自行聯系原作者
cuizhiliang