http://freeloda.blog.51cto.com/2033581/1280962
大綱
一、前言
二、Keepalived 詳解
三、環境準備
四、LVS+Keepalived 實現高可用的前端負載均衡器
一、前言
這篇文章是前幾篇文章的總結,我們先簡單的總結一下我們前面講解的內容,前面我們講解了,LVS(負載均衡器)、Heartbeat、Corosync、Pacemaker、Web高可用集羣、MySQL高可用集羣、DRDB、iscsi、gfs2、cLVM等,唯一沒有講解的就是LVS可用,也就是前端高可用,我們這一篇博文主要講解內容。在說這個之前我們得和大家討論一個問題,也是好多博友問的問題。Heartbeat、Corosync、Keepalived這三個集羣組件我們到底選哪個好,首先我想說明的是,Heartbeat、Corosync是屬於同一類型,Keepalived與Heartbeat、Corosync,根本不是同一類型的。Keepalived使用的vrrp協議方式,虛擬路由冗餘協議 (Virtual Router Redundancy Protocol,簡稱VRRP);Heartbeat或Corosync是基於主機或網絡服務的高可用方式;簡單的說就是,Keepalived的目的是模擬路由器的高可用,Heartbeat或Corosync的目的是實現Service的高可用。所以一般Keepalived是實現前端高可用,常用的前端高可用的組合有,就是我們常見的LVS+Keepalived、Nginx+Keepalived、HAproxy+Keepalived。而Heartbeat或Corosync是實現服務的高可用,常見的組合有Heartbeat v3(Corosync)+Pacemaker+NFS+Httpd 實現Web服務器的高可用、Heartbeat v3(Corosync)+Pacemaker+NFS+MySQL 實現MySQL服務器的高可用。總結一下,Keepalived中實現輕量級的高可用,一般用於前端高可用,且不需要共享存儲,一般常用於兩個節點的高可用。而Heartbeat(或Corosync)一般用於服務的高可用,且需要共享存儲,一般用於多節點的高可用。這個問題我們說明白了,又有博友會問了,那heartbaet與corosync我們又應該選擇哪個好啊,我想說我們一般用corosync,因爲corosync的運行機制更優於heartbeat,就連從heartbeat分離出來的pacemaker都說在以後的開發當中更傾向於corosync,所以現在corosync+pacemaker是最佳組合。但說實話我對於軟件沒有任何傾向性,所以我把所有的集羣軟件都和大家說了一下,我認爲不管什麼軟件,只要它能存活下來都有它的特點和應用領域,只有把特定的軟件放在特定的位置才能發揮最大的作用,那首先我們得對這個軟件有所有了解。學習一種軟件的最好方法,就是去查官方文檔。好了說了那麼多希望大家有所收穫,下面我們來說一說keepalived。
二、Keepalived 詳解
1.Keepalived 定義
Keepalived 是一個基於VRRP協議來實現的LVS服務高可用方案,可以利用其來避免單點故障。一個LVS服務會有2臺服務器運行Keepalived,一臺爲主服務器(MASTER),一臺爲備份服務器(BACKUP),但是對外表現爲一個虛擬IP,主服務器會發送特定的消息給備份服務器,當備份服務器收不到這個消息的時候,即主服務器宕機的時候, 備份服務器就會接管虛擬IP,繼續提供服務,從而保證了高可用性。Keepalived是VRRP的完美實現,因此在介紹keepalived之前,先介紹一下VRRP的原理。
2.VRRP 協議簡介
在現實的網絡環境中,兩臺需要通信的主機大多數情況下並沒有直接的物理連接。對於這樣的情況,它們之間路由怎樣選擇?主機如何選定到達目的主機的下一跳路由,這個問題通常的解決方法有二種:
-
在主機上使用動態路由協議(RIP、OSPF等)
-
在主機上配置靜態路由
很明顯,在主機上配置動態路由是非常不切實際的,因爲管理、維護成本以及是否支持等諸多問題。配置靜態路由就變得十分流行,但路由器(或者說默認網關default gateway)卻經常成爲單點故障。VRRP的目的就是爲了解決靜態路由單點故障問題,VRRP通過一競選(election)協議來動態的將路由任務交給LAN中虛擬路由器中的某臺VRRP路由器。
3.VRRP 工作機制
在一個VRRP虛擬路由器中,有多臺物理的VRRP路由器,但是這多臺的物理的機器並不能同時工作,而是由一臺稱爲MASTER的負責路由工作,其它的都是BACKUP,MASTER並非一成不變,VRRP讓每個VRRP路由器參與競選,最終獲勝的就是MASTER。MASTER擁有一些特權,比如,擁有虛擬路由器的IP地址,我們的主機就是用這個IP地址作爲靜態路由的。擁有特權的MASTER要負責轉發發送給網關地址的包和響應ARP請求。
VRRP通過競選協議來實現虛擬路由器的功能,所有的協議報文都是通過IP多播(multicast)包(多播地址224.0.0.18)形式發送的。虛擬路由器由VRID(範圍0-255)和一組IP地址組成,對外表現爲一個周知的MAC地址。所以,在一個虛擬路由 器中,不管誰是MASTER,對外都是相同的MAC和IP(稱之爲VIP)。客戶端主機並不需要因爲MASTER的改變而修改自己的路由配置,對客戶端來說,這種主從的切換是透明的。
在一個虛擬路由器中,只有作爲MASTER的VRRP路由器會一直髮送VRRP通告信息(VRRPAdvertisement message),BACKUP不會搶佔MASTER,除非它的優先級(priority)更高。當MASTER不可用時(BACKUP收不到通告信息), 多臺BACKUP中優先級最高的這臺會被搶佔爲MASTER。這種搶佔是非常快速的(<1s),以保證服務的連續性。由於安全性考慮,VRRP包使用了加密協議進行加密。
4.VRRP 工作流程
(1).初始化:
路由器啓動時,如果路由器的優先級是255(最高優先級,路由器擁有路由器地址),要發送VRRP通告信息,併發送廣播ARP信息通告路由器IP地址對應的MAC地址爲路由虛擬MAC,設置通告信息定時器準備定時發送VRRP通告信息,轉爲MASTER狀態;否則進入BACKUP狀態,設置定時器檢查定時檢查是否收到MASTER的通告信息。
(2).Master
-
設置定時通告定時器;
-
用VRRP虛擬MAC地址響應路由器IP地址的ARP請求;
-
轉發目的MAC是VRRP虛擬MAC的數據包;
-
如果是虛擬路由器IP的擁有者,將接受目的地址是虛擬路由器IP的數據包,否則丟棄;
-
當收到shutdown的事件時刪除定時通告定時器,發送優先權級爲0的通告包,轉初始化狀態;
-
如果定時通告定時器超時時,發送VRRP通告信息;
-
收到VRRP通告信息時,如果優先權爲0,發送VRRP通告信息;否則判斷數據的優先級是否高於本機,或相等而且實際IP地址大於本地實際IP,設置定時通告定時器,復位主機超時定時器,轉BACKUP狀態;否則的話,丟棄該通告包;
(3).Backup
-
設置主機超時定時器;
-
不能響應針對虛擬路由器IP的ARP請求信息;
-
丟棄所有目的MAC地址是虛擬路由器MAC地址的數據包;
-
不接受目的是虛擬路由器IP的所有數據包;
-
當收到shutdown的事件時刪除主機超時定時器,轉初始化狀態;
-
主機超時定時器超時的時候,發送VRRP通告信息,廣播ARP地址信息,轉MASTER狀態;
-
收到VRRP通告信息時,如果優先權爲0,表示進入MASTER選舉;否則判斷數據的優先級是否高於本機,如果高的話承認MASTER有效,復位主機超時定時器;否則的話,丟棄該通告包;
5.ARP查詢處理
當內部主機通過ARP查詢虛擬路由器IP地址對應的MAC地址時,MASTER路由器回覆的MAC地址爲虛擬的VRRP的MAC地址,而不是實際網卡的 MAC地址,這樣在路由器切換時讓內網機器覺察不到;而在路由器重新啓動時,不能主動發送本機網卡的實際MAC地址。如果虛擬路由器開啓的ARP代理 (proxy_arp)功能,代理的ARP迴應也迴應VRRP虛擬MAC地址;好了VRRP的簡單講解就到這裏,我們下來講解一下Keepalived的案例。
三、環境準備
1.操作系統
-
CentOS 6.4 X86_64
2.軟件版本
-
ipvsadm.x86_64 0:1.25-10.el6
-
keepalived.x86_64 0:1.2.7-3.el6
-
httpd-2.2.15-29.el6.centos.x86_64
3.實驗拓撲
4.時間同步
node1:
1
|
[[email protected] ~]
# ntpdate 202.120.2.101
|
node2:
1
|
[[email protected] ~]
# ntpdate 202.120.2.101
|
master:
1
|
[[email protected] ~]
# ntpdate 202.120.2.101
|
slave:
1
|
[[email protected] ~]
# ntpdate 202.120.2.101
|
5.主機名互相解析
node1:
1
2
3
4
5
|
[[email protected] ~]
# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.18.201 node1.
test
.com node1
192.168.18.202 node2.
test
.com node2
|
node2:
1
2
3
4
5
|
[[email protected] ~]
# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.18.201 node1.
test
.com node1
192.168.18.202 node2.
test
.com node2
|
6.安裝yum源
node1:
1
2
|
[[email protected] ~]
# rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[[email protected] ~]
# rpm -ivh http://elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm
|
node2:
1
2
|
[[email protected] ~]
# rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[[email protected] ~]
# rpm -ivh http://elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm
|
master:
1
2
|
[[email protected] ~]
# rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[[email protected] ~]
# rpm -ivh http://elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm
|
slave:
1
2
|
[[email protected] ~]
# rpm -ivh http://download.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
[[email protected] ~]
# rpm -ivh http://elrepo.org/elrepo-release-6-5.el6.elrepo.noarch.rpm
|
四、LVS+Keepalived 實現高可用的前端負載均衡器
node1:
1.安裝httpd
1
|
[[email protected] ~]
# yum install -y httpd
|
2.配置httpd
1
2
|
[[email protected] ~]
# vim /var/www/html/index.html
<h1>RS1.
test
.com<
/h1
>
|
3.啓動httpd
1
|
[[email protected] ~]
# service httpd start
|
4.測試
5.設置開機自啓動
1
2
3
|
[[email protected] ~]
# chkconfig httpd on
[[email protected] ~]
# chkconfig httpd --list
httpd 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉
|
6.配置node1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
[[email protected] ~]
# mkdir src
[[email protected] ~]
# cd src/
[[email protected] src]
# vim realserver.sh
#!/bin/bash
#
# Script to start LVS DR real server.
# description: LVS DR real server
#
.
/etc/rc
.d
/init
.d
/functions
VIP=192.168.18.200
#修改你的VIP
host=`
/bin/hostname
`
case
"$1"
in
start)
# Start LVS-DR real server on this machine.
/sbin/ifconfig
lo down
/sbin/ifconfig
lo up
echo
1 >
/proc/sys/net/ipv4/conf/lo/arp_ignore
echo
2 >
/proc/sys/net/ipv4/conf/lo/arp_announce
echo
1 >
/proc/sys/net/ipv4/conf/all/arp_ignore
echo
2 >
/proc/sys/net/ipv4/conf/all/arp_announce
/sbin/ifconfig
lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route
add -host $VIP dev lo:0
;;
stop)
# Stop LVS-DR real server loopback device(s).
/sbin/ifconfig
lo:0 down
echo
0 >
/proc/sys/net/ipv4/conf/lo/arp_ignore
echo
0 >
/proc/sys/net/ipv4/conf/lo/arp_announce
echo
0 >
/proc/sys/net/ipv4/conf/all/arp_ignore
echo
0 >
/proc/sys/net/ipv4/conf/all/arp_announce
;;
status)
# Status of LVS-DR real server.
islothere=`
/sbin/ifconfig
lo:0 |
grep
$VIP`
isrothere=`
netstat
-rn |
grep
"lo:0"
|
grep
$VIP`
if
[ !
"$islothere"
-o !
"isrothere"
];
then
# Either the route or the lo:0 device
# not found.
echo
"LVS-DR real server Stopped."
else
echo
"LVS-DR real server Running."
fi
;;
*)
# Invalid entry.
echo
"$0: Usage: $0 {start|status|stop}"
exit
1
;;
esac
[[email protected] src]
# chmod +x realserver.sh
[[email protected] src]
# ll
總用量 4
-rwxr-xr-x 1 root root 1485 8月 22 10:18 realserver.sh
[[email protected] src]
# ./realserver.sh start
|
7.查看配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
[[email protected] src]
# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:11:92:E4
inet addr:192.168.18.201 Bcast:192.168.18.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe11:92e4
/64
Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:115061 errors:0 dropped:0 overruns:0 frame:0
TX packets:14979 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:43448483 (41.4 MiB) TX bytes:1224926 (1.1 MiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1
/128
Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:2 errors:0 dropped:0 overruns:0 frame:0
TX packets:2 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:168 (168.0 b) TX bytes:168 (168.0 b)
lo:0 Link encap:Local Loopback
inet addr:192.168.18.200 Mask:255.255.255.255
UP LOOPBACK RUNNING MTU:16436 Metric:1
[[email protected] src]
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.18.200 0.0.0.0 255.255.255.255 UH 0 0 0 lo
192.168.18.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.18.254 0.0.0.0 UG 0 0 0 eth0
[[email protected] src]
# cat /proc/sys/net/ipv4/conf/lo/arp_ignore
1
[[email protected] src]
# cat /proc/sys/net/ipv4/conf/lo/arp_announce
2
[[email protected] src]
# cat /proc/sys/net/ipv4/conf/all/arp_ignore
1
[[email protected] src]
# cat /proc/sys/net/ipv4/conf/all/arp_announce
2
|
好了,node1到這裏基本配置完成,下面我們來配置node2。
node2:
1.安裝httpd
1
|
[[email protected] ~]
# yum install -y httpd
|
2.配置httpd
1
2
|
[[email protected] ~]
# vim /var/www/html/index.html
<h1>RS2.
test
.com<
/h1
>
|
3.啓動httpd
1
|
[[email protected] ~]
# service httpd start
|
4.測試
5.設置開機自啓動
1
2
3
|
[[email protected] ~]
# chkconfig httpd on
[[email protected] ~]
# chkconfig httpd --list
httpd 0:關閉 1:關閉 2:啓用 3:啓用 4:啓用 5:啓用 6:關閉
|
6.配置node2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
[[email protected] ~]
# mkdir src
[[email protected] ~]
# cd src/
[[email protected] src]
# vim realserver.sh
#!/bin/bash
#
# Script to start LVS DR real server.
# description: LVS DR real server
#
.
/etc/rc
.d
/init
.d
/functions
VIP=192.168.18.200
host=`
/bin/hostname
`
case
"$1"
in
start)
# Start LVS-DR real server on this machine.
/sbin/ifconfig
lo down
/sbin/ifconfig
lo up
echo
1 >
/proc/sys/net/ipv4/conf/lo/arp_ignore
echo
2 >
/proc/sys/net/ipv4/conf/lo/arp_announce
echo
1 >
/proc/sys/net/ipv4/conf/all/arp_ignore
echo
2 >
/proc/sys/net/ipv4/conf/all/arp_announce
/sbin/ifconfig
lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route
add -host $VIP dev lo:0
;;
stop)
# Stop LVS-DR real server loopback device(s).
/sbin/ifconfig
lo:0 down
echo
0 >
/proc/sys/net/ipv4/conf/lo/arp_ignore
echo
0 >
/proc/sys/net/ipv4/conf/lo/arp_announce
echo
0 >
/proc/sys/net/ipv4/conf/all/arp_ignore
echo
0 >
/proc/sys/net/ipv4/conf/all/arp_announce
;;
status)
# Status of LVS-DR real server.
islothere=`
/sbin/ifconfig
lo:0 |
grep
$VIP`
isrothere=`
netstat
-rn |
grep
"lo:0"
|
grep
$VIP`
if
[ !
"$islothere"
-o !
"isrothere"
];
then
# Either the route or the lo:0 device
# not found.
echo
"LVS-DR real server Stopped."
else
echo
"LVS-DR real server Running."
fi
;;
*)
# Invalid entry.
echo
"$0: Usage: $0 {start|status|stop}"
exit
1
;;
esac
[[email protected] src]
# chmod +x realserver.sh
[[email protected] src]
# ./realserver.sh start
|
7.查看配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
[[email protected] src]
# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:B8:DF:6A
inet addr:192.168.18.202 Bcast:192.168.18.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feb8:df6a
/64
Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:110545 errors:0 dropped:0 overruns:0 frame:0
TX packets:10461 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:32853789 (31.3 MiB) TX bytes:889109 (868.2 KiB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1
/128
Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:6 errors:0 dropped:0 overruns:0 frame:0
TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:616 (616.0 b) TX bytes:616 (616.0 b)
lo:0 Link encap:Local Loopback
inet addr:192.168.18.200 Mask:255.255.255.255
UP LOOPBACK RUNNING MTU:16436 Metric:1
[[email protected] src]
# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.18.200 0.0.0.0 255.255.255.255 UH 0 0 0 lo
192.168.18.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.18.254 0.0.0.0 UG 0 0 0 eth0
[[email protected] src]
# cat /proc/sys/net/ipv4/conf/lo/arp_ignore
1
[[email protected] src]
# cat /proc/sys/net/ipv4/conf/lo/arp_announce
2
[[email protected] src]
# cat /proc/sys/net/ipv4/conf/all/arp_ignore
1
[[email protected] src]
# cat /proc/sys/net/ipv4/conf/all/arp_announce
2
|
好了,到這裏node2也基本配置完成。下面我們來配置master與slave。
masterg與slave:
1.安裝keepalived與ipvsadm
1
2
|
[[email protected] ~]
# yum install -y keepalived ipvsadm
[[email protected] ~]
# yum install -y keepalived ipvsadm
|
2.修改配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
[[email protected] ~]
# cat /etc/keepalived/keepalived.conf
! Configuration File
for
keepalived
global_defs {
notification_email {
[email protected]
#配置管理員郵箱
}
notification_email_from root
#配置發件人
smtp_server 127.0.0.1
#配置郵件服務器
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
#配置模式
interface eth0
virtual_router_id 51
priority 101
#配置優先級
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.18.200
#配置虛擬IP地址
}
}
virtual_server 192.168.18.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
#persistence_timeout 50
protocol TCP
real_server 192.168.18.201 80 {
#配置realaserver
weight 1
HTTP_GET {
#監控配置
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
real_server 192.168.18.202 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
}
|
3.將配置文件同步到slave
1
|
[[email protected] ~]
# scp /etc/keepalived/keepalived.conf [email protected]:/etc/keepalived/
|
4.簡單修改一下slave配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
[[email protected] keepalived]
# cat keepalived.conf
! Configuration File
for
keepalived
global_defs {
notification_email {
}
notification_email_from root
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
#修改爲BACKUP
interface eth0
virtual_router_id 51
priority 100
#修改優先級
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.18.200
}
}
virtual_server 192.168.18.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
#persistence_timeout 50
protocol TCP
real_server 192.168.18.201 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
real_server 192.168.18.202 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
}
|
5.啓動master與slave的keepalived服務
1
2
3
4
|
[[email protected] ~]
# service keepalived start
正在啓動 keepalived: [確定]
[[email protected] ~]
# service keepalived start
正在啓動 keepalived: [確定]
|
6.查看一下LVS狀態
1
2
3
4
5
6
7
|
[[email protected] ~]
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.18.200:80 rr
-> 192.168.18.201:80 Route 1 0 0
-> 192.168.18.202:80 Route 1 0 0
|
7.測試
8.模擬故障
(1).停止一下node1
1
2
|
[[email protected] src]
# service httpd stop
停止 httpd: [確定]
|
(2).查看一下的lvs
1
2
3
4
5
6
|
[[email protected] ~]
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.18.200:80 rr
-> 192.168.18.202:80 Route 1 0 0
|
(3).測試一下
(4).查看一下郵件
(5).重新啓動一下node1
1
2
|
[[email protected] src]
# service httpd start
正在啓動 httpd: [確定]
|
(6).再查看一下lvs狀態
1
2
3
4
5
6
7
|
[[email protected] ~]
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.18.200:80 rr
-> 192.168.18.201:80 Route 1 0 0
-> 192.168.18.202:80 Route 1 0 0
|
(7).再查看一下郵件
(8).關閉master上keepalived
1
2
3
4
5
6
|
[[email protected] ~]
# service keepalived stop
停止 keepalived: [確定]
[[email protected] ~]
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
|
(9).查看一下slave狀態
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[[email protected] ~]
# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link
/loopback
00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1
/8
scope host lo
inet6 ::1
/128
scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link
/ether
00:0c:29:f9:e6:26 brd ff:ff:ff:ff:ff:ff
inet 192.168.18.207
/24
brd 192.168.18.255 scope global eth0
inet 192.168.18.200
/32
scope global eth0
inet6 fe80::20c:29ff:fef9:e626
/64
scope link
valid_lft forever preferred_lft forever
[[email protected] ~]
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.18.200:80 rr
-> 192.168.18.201:80 Route 1 0 0
-> 192.168.18.202:80 Route 1 0 0
|
(10).再次測試一下
注,大家可以看到,經過上面的演示我們現在LVS的高可用即前端負載均衡的高可用,同時實現對後端realserver監控,也實現後端realserver宕機時會給管理員發送郵件。但還有幾個問題我們還沒有解決,問題如下:
-
所有realserver都down機,怎麼處理?是不是用戶就沒法打開,還是提供一下維護頁面。
-
怎麼完成維護模式keepalived切換?
-
如何在keepalived故障時,發送警告郵件給指定的管理員?
9.所有realserver都down機,怎麼處理?
問題:在集羣中如果所有real server全部宕機了,客戶端訪問時就會出現錯誤頁面,這樣是很不友好的,我們得提供一個維護頁面來提醒用戶,服務器正在維護,什麼時間可以訪問等,下面我們就來解決一下這個問題。解決方案有兩種,一種是提供一臺備用的real server當所有的服務器宕機時,提供維護頁面,但這樣做有點浪費服務器。另一種就是在負載均衡器上提供維護頁面,這樣是比較靠譜的,也比較常用。下面我們就來具體操作一下。
(1).master與slave安裝上httpd
1
2
|
[[email protected] ~]
# yum install -y httpd
[[email protected] ~]
# yum install -y httpd
|
(2).配置維護頁面
1
2
3
4
|
[[email protected] ~]
# vim /var/www/html/index.html
Website is currently under maintenance, please come back later!
Website is currently under maintenance, please come back later!
|
(3).啓動httpd服務並測試
1
2
3
4
|
[[email protected] ~]
# service httpd start
正在啓動 httpd: [確定]
[[email protected] ~]
# service httpd start
正在啓動 httpd: [確定]
|
(4).修改配置文件
master:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
[[email protected] ~]
# cat /etc/keepalived/keepalived.conf
! Configuration File
for
keepalived
global_defs {
notification_email {
}
notification_email_from root
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.18.200
}
}
virtual_server 192.168.18.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
protocol TCP
real_server 192.168.18.201 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
real_server 192.168.18.202 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
sorry_server 127.0.0.1 80
#增加一行sorry_server
}
|
slave:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
[[email protected] ~]
# cat /etc/keepalived/keepalived.conf
! Configuration File
for
keepalived
global_defs {
notification_email {
}
notification_email_from root
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.18.200
}
}
virtual_server 192.168.18.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
protocol TCP
real_server 192.168.18.201 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
real_server 192.168.18.202 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
sorry_server 127.0.0.1 80
#增加一行sorry_server
}
|
(5).關閉所有的real server並重新啓動一下master與slave的keepalived
1
2
3
4
5
6
7
8
9
10
|
[[email protected] ~]
# service httpd stop
停止 httpd: [確定]
[[email protected] ~]
# service httpd stop
停止 httpd: [確定]
[[email protected] ~]
# service keepalived restart
停止 keepalived: [確定]
正在啓動 keepalived: [確定]
[[email protected] ~]
# service keepalived restart
停止 keepalived: [確定]
正在啓動 keepalived: [確定]
|
(6).查看一下lvs
1
2
3
4
5
6
|
[[email protected] ~]
# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.18.200:80 rr
-> 127.0.0.1:80 Local 1 0 0
|
(7).測試
注,sorry_server測試成功,下面我們繼續。
10.怎麼完成維護模式keepalived切換?
問題:我們一般進行主從切換測試時都是關閉keepalived或關閉網卡接口,有沒有一種方法能實現在不關閉keepalived下或網卡接口來實現維護呢?方法肯定是有的,在keepalived新版本中,支持腳本vrrp_srcipt,具體如何使用大家可以man keepalived.conf查看。下面我們來演示一下具體怎麼實現。
(1).定義腳本
1
2
3
4
5
6
7
|
vrrp_srcipt chk_schedown {
script
"[ -e /etc/keepalived/down ] && exit 1 || exit 0"
interval 1
#監控間隔
weight -5
#減小優先級
fall 2
#監控失敗次數
rise 1
#監控成功次數
}
|
(2).執行腳本
1
2
3
|
track_script {
chk_schedown
#執行chk_schedown腳本
}
|
(3).修改配置文件
master:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
[[email protected] ~]
# cat /etc/keepalived/keepalived.conf
! Configuration File
for
keepalived
global_defs {
notification_email {
}
notification_email_from root
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_schedown {
#定義vrrp執行腳本
script
"[ -e /etc/keepalived/down ] && exit 1 || exit 0"
#查看是否有down文件,有就進入維護模式
interval 1
#監控間隔時間
weight -5
#降低優先級
fall 2
#失敗次數
rise 1
#成功數次
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.18.200
}
track_script {
#執行腳本
chk_schedown
}
}
virtual_server 192.168.18.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
protocol TCP
real_server 192.168.18.201 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
real_server 192.168.18.202 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
sorry_server 127.0.0.1 80
}
|
slave:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
[[email protected] ~]
# cat /etc/keepalived/keepalived.conf
! Configuration File
for
keepalived
global_defs {
notification_email {
}
notification_email_from root
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_schedown {
script
"[ -e /etc/keepalived/down ] && exit 1 || exit 0"
interval 1
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.18.200
}
track_script {
chk_schedown
}
}
virtual_server 192.168.18.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
protocol TCP
real_server 192.168.18.201 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
real_server 192.168.18.202 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
sorry_server 127.0.0.1 80
|
(4).測試
master:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
[[email protected] keepalived]
# touch down #新建一下down文件
[[email protected] keepalived]
# ll
總用量 4
-rw-r--r-- 1 root root 0 8月 22 13:39 down
-rw-r--r-- 1 root root 1317 8月 22 13:35 keepalived.conf
[[email protected] keepalived]
# tail -f /var/log/messages #查看一下日誌
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) Entering MASTER STATE
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) setting protocol VIPs.
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0
for
192.168.18.200
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) Received higher prio advert
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) Entering BACKUP STATE
Aug 22 13:43:52 master Keepalived_vrrp[12003]: VRRP_Instance(VI_1) removing protocol VIPs.
Aug 22 13:43:52 master Keepalived_healthcheckers[12002]: Netlink reflector reports IP 192.168.18.200 added
Aug 22 13:43:52 master Keepalived_healthcheckers[12002]: Netlink reflector reports IP 192.168.18.200 removed
Aug 22 13:43:52 master Keepalived_healthcheckers[12002]: SMTP alert successfully sent.
Aug 22 13:43:52 master Keepalived_healthcheckers[12002]: SMTP alert successfully sent.
^C
[[email protected] keepalived]
# ip add show #查看VIP
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link
/loopback
00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1
/8
scope host lo
inet6 ::1
/128
scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link
/ether
00:0c:29:4b:a1:85 brd ff:ff:ff:ff:ff:ff
inet 192.168.18.208
/24
brd 192.168.18.255 scope global eth0
inet6 fe80::20c:29ff:fe4b:a185
/64
scope link
valid_lft forever preferred_lft forever
|
slave:
1
2
3
4
5
6
7
8
9
10
11
12
|
[[email protected] ~]
# ip addr show #查看一下VIP已轉移到slave上
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link
/loopback
00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1
/8
scope host lo
inet6 ::1
/128
scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link
/ether
00:0c:29:f9:e6:26 brd ff:ff:ff:ff:ff:ff
inet 192.168.18.207
/24
brd 192.168.18.255 scope global eth0
inet 192.168.18.200
/32
scope global eth0
inet6 fe80::20c:29ff:fef9:e626
/64
scope link
valid_lft forever preferred_lft forever
|
好了,自寫監測腳本,完成維護模式切換,到這裏就演示成功,下面我們來解決最後一個問題,就是keepalived主從切換的郵件通告。
11.如何在keepalived故障時(或主備切換時),發送警告郵件給指定的管理員?
(1).keepalived通知腳本進階示例
下面的腳本可以接受選項,其中
-
-s, --service SERVICE,...:指定服務腳本名稱,當狀態切換時可自動啓動、重啓或關閉此服務;
-
-a, --address VIP: 指定相關虛擬路由器的VIP地址;
-
-m, --mode {mm|mb}:指定虛擬路由的模型,mm表示主主,mb表示主備;它們表示相對於同一種服務而方,其VIP的工作類型;
-
-n, --notify {master|backup|fault}:指定通知的類型,即vrrp角色切換的目標角色;
-
-h, --help:獲取腳本的使用幫助;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#!/bin/bash
# Author: freeloda
# description: An example of notify script
# Usage: notify.sh -m|--mode {mm|mb} -s|--service SERVICE1,... -a|--address VIP -n|--notify {master|backup|falut} -h|--help
contact=
'[email protected]'
helpflag=0
serviceflag=0
modeflag=0
addressflag=0
notifyflag=0
Usage() {
echo
"Usage: notify.sh [-m|--mode {mm|mb}] [-s|--service SERVICE1,...] <-a|--address VIP> <-n|--notify {master|backup|falut}>"
echo
"Usage: notify.sh -h|--help"
}
ParseOptions() {
local
I=1;
if
[ $
# -gt 0 ]; then
while
[ $I -
le
$
# ]; do
case
$1
in
-s|--service)
[ $
# -lt 2 ] && return 3
serviceflag=1
services=(`
echo
$2|
awk
-F
","
'{for(i=1;i<=NF;i++) print $i}'
`)
shift
2 ;;
-h|--help)
helpflag=1
return
0
shift
;;
-a|--address)
[ $
# -lt 2 ] && return 3
addressflag=1
vip=$2
shift
2
;;
-m|--mode)
[ $
# -lt 2 ] && return 3
mode=$2
shift
2
;;
-n|--notify)
[ $
# -lt 2 ] && return 3
notifyflag=1
notify=$2
shift
2
;;
*)
echo
"Wrong options..."
Usage
return
7
;;
esac
done
return
0
fi
}
#workspace=$(dirname $0)
RestartService() {
if
[ ${
#@} -gt 0 ]; then
if
[ -x
/etc/rc
.d
/init
.d/$I ];
then
/etc/rc
.d
/init
.d/$I restart
else
echo
"$I is not a valid service..."
fi
done
fi
}
StopService() {
if
[ ${
#@} -gt 0 ]; then
if
[ -x
/etc/rc
.d
/init
.d/$I ];
then
/etc/rc
.d
/init
.d/$I stop
else
echo
"$I is not a valid service..."
fi
done
fi
}
Notify() {
mailsubject=
"`hostname` to be $1: $vip floating"
mailbody=
"`date '+%F %H:%M:%S'`, vrrp transition, `hostname` changed to be $1."
echo
$mailbody | mail -s
"$mailsubject"
$contact
}
# Main Function
ParseOptions [email protected]
[ $? -
ne
0 ] && Usage &&
exit
5
[ $helpflag -
eq
1 ] && Usage &&
exit
0
if
[ $addressflag -
ne
1 -o $notifyflag -
ne
1 ];
then
Usage
exit
2
fi
mode=${mode:-mb}
case
$notify
in
'master'
)
if
[ $serviceflag -
eq
1 ];
then
RestartService ${services[*]}
fi
Notify master
;;
'backup'
)
if
[ $serviceflag -
eq
1 ];
then
if
[
"$mode"
==
'mb'
];
then
StopService ${services[*]}
else
RestartService ${services[*]}
fi
fi
Notify backup
;;
'fault'
)
Notify fault
;;
*)
Usage
exit
4
;;
esac
|
(2).在keepalived.conf配置文件中,其調用方法如下所示:
-
notify_master "/etc/keepalived/notify.sh -n master -a 192.168.18.200"
-
notify_backup "/etc/keepalived/notify.sh -n backup -a 192.168.18.200"
-
notify_fault "/etc/keepalived/notify.sh -n fault -a 192.168.18.200"
(3).修改配置文件
master:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
[[email protected] keepalived]
# cat keepalived.conf
! Configuration File
for
keepalived
global_defs {
notification_email {
}
notification_email_from root
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_schedown {
script
"[ -e /etc/keepalived/down ] && exit 1 || exit 0"
interval 1
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 101
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.18.200
}
track_script {
chk_schedown
}
#增加以下三行
notify_master
"/etc/keepalived/notify.sh -n master -a 192.168.18.200"
notify_backup
"/etc/keepalived/notify.sh -n backup -a 192.168.18.200"
notify_fault
"/etc/keepalived/notify.sh -n fault -a 192.168.18.200"
}
virtual_server 192.168.18.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
protocol TCP
real_server 192.168.18.201 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
real_server 192.168.18.202 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
sorry_server 127.0.0.1 80
}
|
slave:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
[[email protected] keepalived]
# cat keepalived.conf
! Configuration File
for
keepalived
global_defs {
notification_email {
}
notification_email_from root
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_script chk_schedown {
script
"[ -e /etc/keepalived/down ] && exit 1 || exit 0"
interval 1
weight -5
fall 2
rise 1
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.18.200
}
track_script {
chk_schedown
}
#增加以下三行
notify_master
"/etc/keepalived/notify.sh -n master -a 192.168.18.200"
notify_backup
"/etc/keepalived/notify.sh -n backup -a 192.168.18.200"
notify_fault
"/etc/keepalived/notify.sh -n fault -a 192.168.18.200"
}
virtual_server 192.168.18.200 80 {
delay_loop 6
lb_algo rr
lb_kind DR
nat_mask 255.255.255.0
protocol TCP
real_server 192.168.18.201 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
real_server 192.168.18.202 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 2
nb_get_retry 3
delay_before_retry 1
}
}
sorry_server 127.0.0.1 80
}
|
(4).增加腳本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
[[email protected] keepalived]
# pwd
/etc/keepalived
[[email protected] keepalived]
# vim notify.sh
[[email protected] keepalived]
# cat notify.sh
#!/bin/bash
# Author: freeloda
# description: An example of notify script
# Usage: notify.sh -m|--mode {mm|mb} -s|--service SERVICE1,... -a|--address VIP -n|--notify {master|backup|falut} -h|--help
contact=
'[email protected]'
helpflag=0
serviceflag=0
modeflag=0
addressflag=0
notifyflag=0
Usage() {
echo
"Usage: notify.sh [-m|--mode {mm|mb}] [-s|--service SERVICE1,...] <-a|--address VIP> <-n|--notify {master|backup|falut}>"
echo
"Usage: notify.sh -h|--help"
}
ParseOptions() {
local
I=1;
if
[ $
# -gt 0 ]; then
while
[ $I -
le
$
# ]; do
case
$1
in
-s|--service)
[ $
# -lt 2 ] && return 3
serviceflag=1
services=(`
echo
$2|
awk
-F
","
'{for(i=1;i<=NF;i++) print $i}'
`)
shift
2 ;;
-h|--help)
helpflag=1
return
0
shift
;;
-a|--address)
[ $
# -lt 2 ] && return 3
addressflag=1
vip=$2
shift
2
;;
-m|--mode)
[ $
# -lt 2 ] && return 3
mode=$2
shift
2
;;
-n|--notify)
[ $
# -lt 2 ] && return 3
notifyflag=1
notify=$2
shift
2
;;
*)
echo
"Wrong options..."
Usage
return
7
;;
esac
done
return
0
fi
}
#workspace=$(dirname $0)
RestartService() {
if
[ ${
#@} -gt 0 ]; then
if
[ -x
/etc/rc
.d
/init
.d/$I ];
then
/etc/rc
.d
/init
.d/$I restart
else
echo
"$I is not a valid service..."
fi
done
fi
}
StopService() {
if
[ ${
#@} -gt 0 ]; then
if
[ -x
/etc/rc
.d
/init
.d/$I ];
then
/etc/rc
.d
/init
.d/$I stop
else
echo
"$I is not a valid service..."
fi
done
fi
}
Notify() {
mailsubject=
"`hostname` to be $1: $vip floating"
mailbody=
"`date '+%F %H:%M:%S'`, vrrp transition, `hostname` changed to be $1."
echo
$mailbody | mail -s
"$mailsubject"
$contact
}
# Main Function
ParseOptions [email protected]
[ $? -
ne
0 ] && Usage &&
exit
5
[ $helpflag -
eq
1 ] && Usage &&
exit
0
if
[ $addressflag -
ne
1 -o $notifyflag -
ne
1 ];
then
Usage
exit
2
fi
mode=${mode:-mb}
case
$notify
in
'master'
)
if
[ $serviceflag -
eq
1 ];
then
RestartService ${services[*]}
fi
Notify master
;;
'backup'
)
if
[ $serviceflag -
eq
1 ];
then
if
[
"$mode"
==
'mb'
];
then
StopService ${services[*]}
else
RestartService ${services[*]}
fi
fi
Notify backup
;;
'fault'
)
Notify fault
;;
*)
Usage
exit
4
;;
esac
|
(5).給腳本增加執行權限
1
|
[[email protected] keepalived]
# chmod +x notify.sh
|
(6).將master上腳本複製到slave上
1
|
[[email protected] keepalived]
# scp -p notify.sh [email protected]:/etc/keepalived/
|
(7).測試一下腳本
1
2
3
4
5
6
7
|
[[email protected] keepalived]
# ./notify.sh -h
Usage: notify.sh [-m|--mode {mm|mb}] [-s|--service SERVICE1,...] <-a|--address VIP> <-n|--notify {master|backup|falut}>
Usage: notify.sh -h|--help
[[email protected] keepalived]
# ./notify.sh --help
Usage: notify.sh [-m|--mode {mm|mb}] [-s|--service SERVICE1,...] <-a|--address VIP> <-n|--notify {master|backup|falut}>
Usage: notify.sh -h|--help
[[email protected] keepalived]
# ./notify.sh -m mb -a 1.1.1.1 -n master
|
(8).查看一下郵件
注,大家可以看到成功收到郵件,測試成功。在模擬故障時先重啓一下keepalived服務。
(9).模擬故障
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
[[email protected] keepalived]
# ip addr show #查看一下VIP
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link
/loopback
00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1
/8
scope host lo
inet6 ::1
/128
scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link
/ether
00:0c:29:4b:a1:85 brd ff:ff:ff:ff:ff:ff
inet 192.168.18.208
/24
brd 192.168.18.255 scope global eth0
inet 192.168.18.200
/32
scope global eth0
inet6 fe80::20c:29ff:fe4b:a185
/64
scope link
valid_lft forever preferred_lft forever
[[email protected] keepalived]
# touch down #進入維護模式
[[email protected] keepalived]
# ll
總用量 8
-rw-r--r-- 1 root root 0 8月 22 14:39 down
-rw-r--r-- 1 root root 1543 8月 22 14:04 keepalived.conf
-rwxr-xr-x 1 root root 2516 8月 22 14:15 notify.sh
[[email protected] keepalived]
# ip addr show #再次查看VIP
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue state UNKNOWN
link
/loopback
00:00:00:00:00:00 brd 00:00:00:00:00:00
|