1. 程式人生 > 實用技巧 >LVS+keepalive

LVS+keepalive

LVS+keepalive

什麼是keepalive

Keepalived是Linux下一個輕量級別的高可用解決方案。高可用(High Avalilability,HA),其實兩種不同的含義:廣義來講,是指整個系統的高可用行;狹義的來講就是指主機的冗餘和接管。它與HeartBeat、RoseHA實現類似的功能,都可以實現服務或者網路的高可用,但是又有差別。HeartBeat是一個專業的、功能完善的高可用軟體,它提供了HA 軟體所需的基本功能。比如:心跳檢測、資源接管,檢測叢集中的服務,在叢集節點轉移共享IP地址的所有者等等。HeartBeat功能強大,但是部署和使用相對比較麻煩。與HeartBeat相比,Keepalived主要是通過虛擬路由冗餘(VRRP)來實現高可用功能,雖然它沒有HeartBeat功能強大,但是Keepalived部署和使用非常的簡單,所有配置只需要一個配置檔案即可以完成。   keepalived主要有三個模組,分別是core、check和vrrp。core模組為keepalived的核心,負責主程序的啟動、維護以及全域性配置檔案的載入和解析。check負責健康檢查,包括常見的各種檢查方式。vrrp模組是來實現VRRP協議的

實驗環境

關閉selinux和iptables

主機centos7.6
192.168.47.47 VIP
192.168.47.191 lvs-master
192.168.47.193 lvs-slave
192.168.47.189 RealServer02
192.168.47.188 RealServer01

1、在LVS上:

yum install ipvsadm keepalived -y

Master節點配置檔案

[root@lvs ~]# cat /etc/keepalived/keepalived.conf
! Configuration File for keepalived

global_defs {
router_id LVS_DEVEL # 設定lvs的id,在一個網路內應該是唯一的
}
vrrp_instance VI_1 {
state MASTER #指定Keepalived的角色,MASTER為主,BACKUP為備
interface ens33 # 繫結的網絡卡名
virtual_router_id 51 #虛擬路由編號,主備要一致
priority 100 #定義優先順序,數字越大,優先順序越高,主DR必須大於備用DR
advert_int 1 #檢查間隔,預設為1s
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.47.16 #定義虛擬IP(VIP),可多設,每行一個
}
}
# 定義對外提供服務的LVS的VIP以及port
virtual_server 192.168.47.47 80 {
delay_loop 6 # 設定健康檢查時間,單位是秒
lb_algo wrr # 設定負載排程的演算法為wrr
lb_kind DR # 設定LVS實現負載的機制,有NAT、TUN、DR三個模式
nat_mask 255.255.255.0
persistence_timeout 0
protocol TCP
real_server 192.168.47.188 80 { # 指定real server1的IP地址
weight 3 # 配置節點權值,數字越大權重越高
TCP_CHECK {
connect_timeout 10
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.47.189 80{ # 指定real server2的IP地址
weight 3 # 配置節點權值,數字越大權重越高
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}

 Slave 配置

No.1處:把“state MASTER”修改為“state BACKUP” #即備用伺服器;

No.2處:把“priority 100”修改為“priority 90” #優先順序為90。

2、配置RealServer(我安裝的是httpd,也可以用nginx或者其他的)

[root@t1 ~]# cat realserver.sh 
#add for chkconfig
#chkconfig: 2345 70 30
#description: RealServer's script
#processname: realserver.sh
#!/bin/bash
VIP=192.168.47.47
source /etc/rc.d/init.d/functions
case "$1" in
start)
#繫結VIP,抑制ARP廣播
ifconfig lo:0 $VIP netmask 255.255.255.255 broadcast $VIP
/sbin/route add -host $VIP dev lo:0
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
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $VIP >/dev/null 2>&1
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
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0

3、啟動httpd服務、執行指令碼

[root@t1 ~]# systemctl start httpd
[root@t1 ~]# ./realserver.sh start

4、啟動keeplived,檢視lvs規則

[root@lvs ~]# systemctl restart keepalived
#可以看到已經成功添加了DR、WRR的規則
[root@lvs ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.47.47:80 wrr
-> 192.168.47.188:80 Route 3 0 0
-> 192.168.47.189:80 Route 3 0 0

5、驗證

[root@lvs ~]# curl 192.168.47.47
”I am RealServer1 192.168.47.189”
[root@lvs ~]# curl 192.168.47.47
”I am RealServer1 192.168.47.188”
[root@lvs ~]# curl 192.168.47.47
”I am RealServer1 192.168.47.189”
[root@lvs ~]# curl 192.168.47.47
”I am RealServer1 192.168.47.188”
[root@lvs ~]# curl 192.168.47.47
”I am RealServer1 192.168.47.189”
[root@lvs ~]# curl 192.168.47.47
”I am RealServer1 192.168.47.188”

6、將後端的realserver其中一個節點服務停掉,並測試

#停了192.168.47.188的節點
[root@t1 ~]# systemctl stop httpd
#可以看到LVS規則中,已經沒有了192.168.47.188的節點
[root@lvs ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.47.47:80 wrr
-> 192.168.47.189:80 Route 3 0 0
#再把他起來
[root@t1 ~]# systemctl start httpd
#可以看到LVS規則中,又有了
[root@lvs ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.47.47:80 wrr
-> 192.168.47.188:80 Route 3 0 0
-> 192.168.47.189:80 Route 3 0 0

7、把keepalived、master節點宕掉。看看是否能切換到slave

[root@lvs ~]# systemctl stop keepalived

可以看到VIP漂移了過來

再測試是否能正常訪問

[root@lvss ~]# ipvsadm -Ln 
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.47.47:80 wrr
-> 192.168.47.188:80 Route 3 0 0
-> 192.168.47.189:80 Route 3 0 0
[root@lvss ~]# curl 192.168.47.47
”I am RealServer1 192.168.47.189”
[root@lvss ~]# curl 192.168.47.47
”I am RealServer1 192.168.47.188”
[root@lvss ~]# curl 192.168.47.47
”I am RealServer1 192.168.47.189”
[root@lvss ~]# curl 192.168.47.47
”I am RealServer1 192.168.47.188”
[root@lvss ~]# curl 192.168.47.47
”I am RealServer1 192.168.47.189”