簡(jiǎn)介
KeepAlived是網(wǎng)絡(luò)協(xié)議VRRP(Virtual Route Redundancy Protocol 虛擬路由冗余協(xié)議)協(xié)議的實(shí)現(xiàn)。
KeepAlived是Linux集群管理中保證集群高可用的一個(gè)服務(wù)軟件相味,其功能是用來(lái)防止單點(diǎn)故障物独。具體來(lái)說(shuō),其作用是檢測(cè)服務(wù)器的狀態(tài)娜谊,如果有一臺(tái)web服務(wù)器宕機(jī)谍珊,或工作出現(xiàn)故障谊囚,KeepAlived將會(huì)檢測(cè)到搀别,并將有故障的服務(wù)器從系統(tǒng)中剔除丹擎,同時(shí)使用其他服務(wù)器代替該服務(wù)器的工作,當(dāng)服務(wù)器工作正常后KeepAlived自動(dòng)將服務(wù)器加入到服務(wù)器群中歇父,這些工作全部自動(dòng)完成蒂培,不需要人工干涉,需要人工做的只是修復(fù)故障的服務(wù)器榜苫。
KeepAlived的工作原理:
通過(guò)了解VRRP協(xié)議來(lái)了解KeepAlived的工作原理护戳。
VRRP協(xié)議:Virtual Route Redundancy Protocol 虛擬路由冗余協(xié)議。是一種容錯(cuò)協(xié)議垂睬,保證當(dāng)主機(jī)的下一跳路由出現(xiàn)故障時(shí)灸异,由另一臺(tái)備用路由器來(lái)代替出現(xiàn)故障的路由器進(jìn)行工作,從而保持網(wǎng)絡(luò)通信的連續(xù)性和可靠性羔飞。這些路由器組成了一個(gè)虛擬路由器,其中包含一個(gè)Master 路由器和多個(gè) Backup 路由器檐春。主機(jī)將虛擬路由器當(dāng)作默認(rèn)網(wǎng)關(guān)逻淌。一個(gè)虛擬路由器可以擁有一個(gè)或多個(gè)IP 地址。
實(shí)驗(yàn)實(shí)現(xiàn)基于KeepAlived雙主模型的高可用LVS
需求分析
如下圖所示
FQDN | IP |
---|---|
images.king.com | VIP1 192.168.1.100 |
app.king.com | VIP2 192.168.1.200 |
當(dāng)客戶端訪問(wèn)images.king.com的時(shí)候疟暖,是通過(guò)VIP1訪問(wèn)卡儒,最終后端提供服務(wù)的是WEBSRV1和WEBSRV2田柔;當(dāng)客戶端訪問(wèn)app.king.com的時(shí)候,是通過(guò)VIP2訪問(wèn)骨望,后端提供服務(wù)的是WEBSRV3和WEBSRV4硬爆。當(dāng)用戶訪問(wèn)其中一個(gè)站點(diǎn)的時(shí)候,單個(gè)LVS服務(wù)器可以提供負(fù)載均衡擎鸠,將前端的請(qǐng)求調(diào)度到后端的兩臺(tái)WEBSRV上缀磕,但是一臺(tái)LVS無(wú)法避免單點(diǎn)失敗的問(wèn)題,所以考慮用兩臺(tái)LVS服務(wù)器配合KeepAlived來(lái)實(shí)現(xiàn)高可用性劣光。同時(shí)袜蚕,企業(yè)提供兩個(gè)站點(diǎn)的服務(wù),如果每個(gè)站點(diǎn)都采用兩臺(tái)LVS來(lái)實(shí)現(xiàn)高可用性绢涡,那么需要四臺(tái)LVS牲剃,成本比較高,而且正常情況下雄可,有兩臺(tái)LVS服務(wù)器處于空閑中凿傅。可以考慮采用KeepAlived + LVS 雙主模型的方式來(lái)實(shí)現(xiàn)這種需求数苫。
雙主模型聪舒。即針對(duì)VIP1,LVS1是MASTER文判,LVS2是BACKUP过椎,此時(shí),VIP1漂在LVS1上面戏仓,LVS1負(fù)責(zé)調(diào)度后端的WEBSRV1和WEBSRV2疚宇。針對(duì)VIP2,LVS2是MASTER赏殃,LVS1是BACKUP敷待,此時(shí),VIP2漂在LVS2上面仁热,LVS2負(fù)責(zé)調(diào)度后端的WEBSRV3和WEBSRV4榜揖。當(dāng)LVS1或者LVS2服務(wù)器出現(xiàn)故障的時(shí)候,那么此時(shí)出故障的LVS上面的VIP將漂移到另一臺(tái)LVS上面抗蠢,并且原來(lái)負(fù)責(zé)的兩臺(tái)后端WEBSRV將由正常的LVS接管举哟。這就是大致的實(shí)現(xiàn)過(guò)程。
實(shí)驗(yàn)環(huán)境準(zhǔn)備
確保所有虛擬機(jī)(centos7.3)上面的防火墻和selinux處于關(guān)閉狀態(tài)迅矛。
各節(jié)點(diǎn)時(shí)間必須同步
找一臺(tái)機(jī)器A與國(guó)內(nèi)一臺(tái)常用的NTP服務(wù)器同步時(shí)間妨猩,企業(yè)里面其他的機(jī)器與A機(jī)器同步時(shí)間。
各節(jié)點(diǎn)之間通過(guò)主機(jī)名互相通信秽褒,建議使用/etc/hosts文件實(shí)現(xiàn)
各節(jié)點(diǎn)之間的root用戶可以基于密鑰認(rèn)證的ssh服務(wù)完成互相通信
實(shí)驗(yàn)過(guò)程
一壶硅、配置客戶端CLIENT
vim /etc/hosts
# 添加下面兩行
192.168.1.100 images.king.com
192.168.1.200 app.king.com
二威兜、在WEBSRV1和WEBSRV2上面
WEBSRV2的配置和WEBSRV1的配置大致相同,下面以WEBSRV1為例介紹一下庐椒。
# 如果沒(méi)有安裝httpd就安裝
yum install httpd
# 啟動(dòng)服務(wù)
systemctl start httpd
# 準(zhǔn)備頁(yè)面椒舵,如果是WEBSRV2,就把WEBSRV1 改為WEBSRV2
echo "WEBSRV1 images" > /var/www/html/index.html
# 準(zhǔn)備RS腳本
vim lvs_dr_rs.sh
#!/bin/bash
vip=192.168.1.100
mask='255.255.255.255'
dev=lo:1
case $1 in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig $dev $vip netmask $mask broadcast $vip up
route add -host $vip dev $dev
echo "The RS Server is Ready!"
;;
stop)
ifconfig $dev down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "The RS Server is Canceled!"
;;
*)
echo "Usage: $(basename $0) start|stop"
exit 1
;;
esac
# 執(zhí)行RS腳本
bash lvs_dr_rs.sh start
# 查看回環(huán)網(wǎng)卡上面是否綁定了vip 192.168.1.100
ip a
三约谈、在WEBSRV3和WEBSRV4上面
WEBSRV4的配置和WEBSRV3的配置大致相同笔宿,下面以WEBSRV3為例介紹一下。
# 如果沒(méi)有安裝httpd就安裝
yum install httpd
# 啟動(dòng)服務(wù)
systemctl start httpd
# 準(zhǔn)備頁(yè)面窗宇,如果是WEBSRV4措伐,就把WEBSRV3 改為WEBSRV4
echo "WEBSRV3 app" > /var/www/html/index.html
# 準(zhǔn)備RS腳本
vim lvs_dr_rs.sh
#!/bin/bash
vip=192.168.1.200
mask='255.255.255.255'
dev=lo:1
case $1 in
start)
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
ifconfig $dev $vip netmask $mask broadcast $vip up
route add -host $vip dev $dev
echo "The RS Server is Ready!"
;;
stop)
ifconfig $dev down
echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce
echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo "The RS Server is Canceled!"
;;
*)
echo "Usage: $(basename $0) start|stop"
exit 1
;;
esac
# 執(zhí)行RS腳本
bash lvs_dr_rs.sh start
# 查看回環(huán)網(wǎng)卡上面是否綁定了vip 192.168.1.200
ip a
四、配置LVS1的KeepAlived
# 安裝ipvsadm军俊,用于查看ipvs規(guī)則
yum install ipvsadm
# 查看一下侥加,目前來(lái)說(shuō)是空的
ipvsadm -Ln
# 安裝KeepAlived
yum install keepalived
# 配置keepalived
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from node1@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node1
vrrp_mcast_group4 224.100.100.100
}
vrrp_instance VI_1 {
state MASTER
interface ens33
virtual_router_id 88
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 411fa9f6
}
virtual_ipaddress {
192.168.1.100/24
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
virtual_server 192.168.1.100 80 {
delay_loop 3
lb_algo wrr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.1.14 80 {
weight 2
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.15 80 {
weight 1
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
vrrp_instance VI_2 {
state BACKUP
interface ens33
virtual_router_id 66
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 123fa9f6
}
virtual_ipaddress {
192.168.1.200/24
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
virtual_server 192.168.1.200 80 {
delay_loop 3
lb_algo rr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.1.16 80 {
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.66 80 {
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
# MASTER和BACKUP切換的郵件通知腳本
cat /etc/keepalived/notify.sh
#!/bin/bash
contact='root@localhost'
notify() {
mailsubject="$(hostname) to be $1, vip floating"
mailbody="$(date +'%F %T'): vrrp transition, $(hostname) changed to be $1"
echo "$mailbody" | mail -s "$mailsubject" $contact
}
case $1 in
master)
notify master
;;
backup)
notify backup
;;
fault)
notify fault
;;
*)
echo "Usage: $(basename $0) {master|backup|fault}"
exit 1
;;
esac
# keepalived記錄日志 ,修改/etc/sysconfig/keepalived對(duì)應(yīng)的行
KEEPALIVED_OPTIONS="-D -S 2"
# 在/etc/rsyslog.conf添加一行
local2.* /var/log/keepalived.log
# 重啟日志服務(wù)
systemctl restart rsyslog
# 準(zhǔn)備sorry server
yum install httpd
systemctl start httpd
echo sorry,server > /var/www/html/index.html
# 啟動(dòng)keepalived服務(wù)
systemctl start keepalived
五粪躬、配置LVS2的KeepAlived
LVS2的配置大致和LVS1相同担败,主要是KeepAlived的配置不同,現(xiàn)將KeepAlived的配置說(shuō)明如下镰官。
cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from node2@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id node2
vrrp_mcast_group4 224.100.100.100
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 88
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 411fa9f6
}
virtual_ipaddress {
192.168.1.100/24
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
virtual_server 192.168.1.100 80 {
delay_loop 3
lb_algo wrr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.1.14 80 {
weight 2
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.15 80 {
weight 1
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
vrrp_instance VI_2 {
state MASTER
interface ens33
virtual_router_id 66
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123fa9f6
}
virtual_ipaddress {
192.168.1.200/24
}
notify_master "/etc/keepalived/notify.sh master"
notify_backup "/etc/keepalived/notify.sh backup"
notify_fault "/etc/keepalived/notify.sh fault"
}
virtual_server 192.168.1.200 80 {
delay_loop 3
lb_algo rr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
real_server 192.168.1.16 80 {
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.66 80 {
HTTP_GET {
url {
path /index.html
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
六提前、在CLIENT上進(jìn)行測(cè)試
測(cè)試一:
for i in {1..15};do curl images.king.com; done
for i in {1..15};do curl app.king.com; done
按照調(diào)度算法正常進(jìn)行服務(wù)調(diào)度。
測(cè)試二:
for i in {1..50};do sleep 0.5; curl images.king.com;done
for i in {1..50};do sleep 0.5; curl app.king.com;done
斷開(kāi)LVS1或者LVS2的網(wǎng)絡(luò)泳唠,兩個(gè)站點(diǎn)依然正常訪問(wèn)狈网。
此時(shí),VIP1和VIP2集中在一臺(tái)正常工作的LVS上面笨腥。
測(cè)試三:
for i in {1..50};do sleep 0.5; curl images.king.com;done
停掉WEBSRV1或者WEBSRV2拓哺,images.king.com站點(diǎn)依然可以正常訪問(wèn)。
for i in {1..50};do sleep 0.5; curl app.king.com;done
停掉WEBSRV3或者WEBSRV4脖母,app.king.com站點(diǎn)依然可以正常訪問(wèn)士鸥。
測(cè)試四:
for i in {1..50};do sleep 0.5; curl images.king.com;done
把WEBSRV1和WEBSRV2的httpd服務(wù)都停掉,發(fā)現(xiàn)顯示的是sorry,server谆级,前端調(diào)度器充當(dāng)了WEBSRV烤礁。
for i in {1..50};do sleep 0.5; curl app.king.com;done
把WEBSRV3和WEBSRV4的httpd服務(wù)都停掉,發(fā)現(xiàn)顯示的是sorry,server肥照,前端調(diào)度器充當(dāng)了WEBSRV脚仔。