keepalived高可用集羣介紹:node
keepalived起初是專爲LVS設計的,專門用來監控LVS集羣系統中各個服務節點的狀態,後來又加入了VRRP的功能,所以除了配置LVS服務外,也能夠做爲其餘服務的高可用軟件(Nginx Haproxy,MySQL,HTTP),VRRP是Virtual Redundancy Protocol(虛擬路由器冗餘協議)linux
的縮寫,VRRP出現的目的就是爲了解決靜態路由出現單點故障問題,它可以保證網絡的不間斷、穩定的運行。因此,keepalived一方面具備lvs cluster nodes healthchecks功能,另外一方面也具備lvsdirectors failover功能nginx
keepalived服務兩大用途:healthcheck & failoverweb
LVS directors failover功能:算法
實現LB master主機和backup主機之間故障轉移和自動切換,這是針對有兩個負載均衡器director同時工做而採起的故障轉移錯誤,當主負載均衡器失效或故障時,備份負載均衡器將自動接管主的全部工做,一旦主的故障修復,master又會接管回他原來處理的工做,而備會釋放master失效時它接管的工做,此時二者將恢復到最初各自的角色狀態。vim
LVS cluster nodes heathcheck功能:後端
負載均衡按期檢查RS的可用性決定是否給其分發請求,當虛擬服務器中的某一個甚至是幾個真實服務器同時發生故障沒法提供服務時,負載均衡器會自動將失效的RS從隊列中清除,從而保證用戶訪問不受影響,當故障修復後,系統又會自動把他們加入到隊列。bash
keepalived實現方式及原理服務器
VRRP協議介紹:網絡
VRRP協議,全稱Virtual Redundancy Protocol(虛擬路由器冗餘協議),VRRP出現的目的就是爲了解決靜態路由出現單點故障,VRRP是經過一種競選協議機制來將路由任務交給某臺VRRP路由器在一個VRRP虛擬路由器中,有多臺物理的VRRP路由器,可是這多臺物理的機器並不一樣時工做,而是由一臺稱爲master的負責路由工做,其餘的都是backup,master並不是一成不變,VRRP協議讓每一個VRRP路由器參與競選,最終獲勝的就是master,master有一些特權,好比擁有VIP地址,咱們的主機就是用這個IP地址做爲靜態路由的,擁有特權的master要負責轉發發送給網關地址的包,和響應ARP請求。只有做爲master的vrrp路由器會一直髮送VRRP廣告包,backup不會搶佔master,除非它的優先級更高,當master不可用時,backup收不到廣告包,多臺backup中優先級最高的這臺會搶佔爲master
總結:
一、VRRP協議,全稱Virtual Redundancy Protocol(虛擬路由器冗餘協議),VRRP出現的目的就是爲了解決靜態路由出現單點故障
二、VRRP是經過一種競選協議機制來將路由任務交給某臺VRRP路由器
三、VRRP是經過用IP多播的方式實現通訊
四、主發包,備接收包,備接不到包的時候,接管主的資源,備能夠有多個,經過優先級競選
安裝配置keepalived實現服務高可用
網絡拓撲:
本實驗環境使用4臺虛擬機CentOS 6.5操做系統
虛擬機1:192.168.10.55 後端RS服務器,web服務器:nginx1.6.3
虛擬機2:192.168.10.56 後端RS服務器2 web服務器:nginx1.6.3
虛擬機3:192.168.10.57 LVS+keepalived-MASTER服務器:LVS-1.26 keepalived-1.1.19
虛擬機4:192.168.10.58 LV+keepalived-BACKUP服務器:LVS-1.26 keepalived-1.1.19
VIP:192.168.10.2/24
nginx的安裝和配置這裏不在寫了,如下是lvs+keepalived的安裝配置實現lvs的高可用功能
1、部署lvs
一、安裝LVS模塊
wget ln -s /usr/src/kernels/2.6.18-194.el5-x86_64 /usr/src/linux tar -zxvf ipvsadm-1.26.tar.gz cd ipvsadm-1.26 make && make install modprobe ip_vs lsmod |grep "ip_vs"
若是顯示下面的結果,表示ipvs安裝成功
[root@LVS-2 keepalived-1.1.19]# lsmod |grep "ip_vs" ip_vs_rr 1420 1 ip_vs 125220 3 ip_vs_rr libcrc32c 1246 1 ip_vs ipv6 317340 156 ip_vs,ip6t_REJECT,nf_conntrack_ipv6,nf_defrag_ipv6
make報錯解決:
libipvs.c:1051: error: ‘NL_OK’ undeclared (first use in this function)
libipvs.c: In function ‘ipvs_get_daemon’:
libipvs.c:1071: error: ‘NLM_F_DUMP’ undeclared (first use in this function)
libipvs.c:1072: error: too many arguments to function ‘ipvs_nl_send_message’
make[1]: *** [libipvs.o] Error 1
make[1]: Leaving directory `/usr/local/src/ipvsadm-1.26/libipvs'
make: *** [libs] Error 2
緣由:ipvsadm1.26適用於kernel2.6.28及以後的內核版本,若是符合系統環境後,一樣須要先安裝依賴包:
yum install libnl* popt*
二、配置LVS
2.1配置LVS的VIP地址
ifconfig eth0:0 192.168.10.2/24 up
2.2配置LVS服務
ipvsadm -A -t 192.168.10.2:80 -s rr
ipvsadm -a -t 192.168.10.2:80 -r 192.168.10.55 -g -w 1
ipvsadm -a -t 192.168.10.2:80 -r 192.168.10.56 -g -w 1
2、在每臺LVS服務器上安裝部署keepalived
一、下載keepalived並安裝依賴
cd /usr/local/src/
wget http://www.keepalived.org/software/keepalived-1.1.19.tar.gz
yum install openssl oepnssl-devel popt*
二、安裝keepalived
ln -s /usr/src/kernels/2.6.32-358.el6.x86-64/ /usr/src/linux
configure --sysconf=/etc
cp /usr/local/sbin/keepalived /usr/sbin/
三、檢查啓動結果
ps -ef |grep keepalived
四、配置文件介紹
vim /etc/keepalived
! Configuration File for keepalived
global_defs { #全局設置,設置報警的收件人
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc #設置發件人的郵箱
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL#這臺keepalived的ID,至關於MySQL的serverid,不一樣的機器不一樣的ID
}
vrrp_instance VI_1 { #定義keepalived的一個實例
state MASTER #當前keepalived的狀態,MASTER/BACKUP
interface eth0 #keepalived提供服務在哪一個網絡接口
virtual_router_id 51 #虛擬的路由ID,在兩個keepalived之間相同的實例必須同樣,在同一個keepalived裏,和別的實例不能同樣
priority 150 #優先級,建議主備之間相差間隔爲50,優先級越高,在主故障的時候,誰就會優先競選爲master
advert_int 1 #高可用對之間接管的間隔,若是超過1秒沒收到廣告包,就接管master
authentication { #認證配置
auth_type PASS
auth_pass 1111
}
virtual_ipaddress { #這裏的VIP地址就會綁定在上面設置的網卡地址上
192.168.10.2/24
}
}
virtual_server 10.0.0.2 80 { #一個virtual_server是一個LVS實例,IP爲LVS的VIP。
delay_loop 6
lb_algo wrr #指定負載均衡算法爲wrr
lb_kind DR #指定LVS的模式爲DR
nat_mask 255.255.255.0
persistence_timeout 300 #會話保持時間
protocol TCP
real_server 192.168.10.55 80 {#後端的RS設置
weight 1#權重
TCP_CHECK {#keepalived健康檢查
connect_timeout 8#超時時間
nb_get_retry 3#重試次數
delay_before_retry 3 #失敗的重試次數
connect_port 80#檢查的端口
}
}
real_server 192.168.10.56 80 { #第二個RS設置
weight 1#權重
TCP_CHECK {connect_timeout 8
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
五、啓動keepalived
service keepalived start
ps -ef |grep keepalived
root 3687 1 0 00:41 ? 00:00:00 keepalived -D
root 3689 3687 0 00:41 ? 00:00:00 keepalived -D
root 3690 3687 0 00:41 ? 00:00:00 keepalived -D
root 3961 1720 0 01:33 pts/0 00:00:00 grep keepalived
注意:另外192.168.10.58的lvs和keepalived安裝步驟相同,就再也不寫安裝步驟了,下面是192.168.10.58keepalived的配置文件
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.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.10.2/24
}
}
到此,LVS+KEEPALIVED安裝配置完畢。下面是後端的RS,RS我使用的是nginx,也不在寫安裝配置步驟了。各位請自行配置好web軟件
3、後端RS配置過程
在每臺RS服務器上綁定虛擬IP
ifconfig lo:0 192.168.10.2/32 up
添加路由
route add -host 192.168.10.2 dev lo
抑制ARP操做
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
RS配置完畢,下面是測試。
啓動keepalived
service keepalived start
使用命令查看master的VIP
[root@LVS ~]# ip add 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:25:66:25 brd ff:ff:ff:ff:ff:ff inet 192.168.10.57/24 brd 192.168.10.255 scope global eth0 inet 192.168.10.2/24 scope global secondary eth0 inet6 fe80::20c:29ff:fe25:6625/64 scope link valid_lft forever preferred_lft forever [root@LVS ~]#
這時,master服務正常,而backup的keepalived服務處於監聽狀態,因此backup上不會應用VIP,下面是backup,並無192.168.10.2/24的VIP。
[root@LVS-2 keepalived-1.1.19]# ip add 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:00:07:69 brd ff:ff:ff:ff:ff:ff inet 192.168.10.58/24 brd 192.168.10.255 scope global eth0 inet6 fe80::20c:29ff:fe00:769/64 scope link valid_lft forever preferred_lft forever
中止master的keepalived以後,觀察網頁是否能正常打開,backup上的VIP狀態
[root@LVS ~]# service keepalived stop Stopping keepalived: [ OK ] [root@LVS ~]# ip add 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:25:66:25 brd ff:ff:ff:ff:ff:ff inet 192.168.10.57/24 brd 192.168.10.255 scope global eth0 inet6 fe80::20c:29ff:fe25:6625/64 scope link valid_lft forever preferred_lft forever [root@LVS ~]#
master上的VIP:192.168.10.2已經沒有了,下面的ping只超時了一個,能夠知道keepalived在故障切換時的時間是很短的,對於用戶來講更是感受不到的。
下面是backup的keepalived狀態
[root@LVS-2 keepalived-1.1.19]# ip add 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:00:07:69 brd ff:ff:ff:ff:ff:ff inet 192.168.10.58/24 brd 192.168.10.255 scope global eth0 inet 192.168.10.2/24 scope global secondary eth0 inet6 fe80::20c:29ff:fe00:769/64 scope link valid_lft forever preferred_lft forever [root@LVS-2 keepalived-1.1.19]#