LVS-DR+Keepalived 高可用集群
- Keepalived介紹
- Keepalived實作原理
- LVS-DR-Keepalived部署
- 搭建lvs-dr模式集群
- 配置lvs服務器
- 配置真實服務器
- 安裝和配置Keepalived
- 配置主機的keepalived
- 配置從機的keepalived
- 高可用測驗
Keepalived介紹
前言
在企業應用中,單臺服務器承擔應用存在單點故障的危險,在企業應用集群中,存在了至少兩處單點故障危險,單點故障一旦發生,企業服務將發生中斷,造成極大的危害
Keepalived是專為LVS和HA設計的一款健康檢查工具
- 支持故障自動切換(Failover)
- 支持節點健康狀態檢查(Health Checking)
- 官方網站:http://www.keepalived.org/
Keepalived實作原理
- Keepalived采用VRRP熱備份協議實作Linux服務器的多機熱備功能
- VRRP ,虛擬路由冗余協議,是針對路由器的一種備份解決方案
- 由多臺路由器組成一個熱備組,通過共用的虛擬IP地址對外提供服務
- 每個熱備組內同一時刻只有一臺主路由器提供服務,其他路由器處于冗余狀態
- 若當前在線的路由器失效,則其他路由器會根據設定的優先級自動接替虛擬IP地址,繼續提供服務

一主+多從,共用同一個IP地址,但優先級不同
漂移地址:就是路由器組共用的虛擬ip地址,當主路由器死了,剩下的路由會選舉出一個主路由出來 ,然后選舉出來的路由會承擔漂移地址的使用權
- Keepalived可實作多機熱備,每個熱備組可有多臺服務器,最常用的就是雙機熱備
- 雙機熱備的故障切換是由虛擬IP地址的漂移來實作,適用于各種應用服務器
LVS-DR-Keepalived部署

我還是繼續使用這個架構圖
只不過兩臺lvs現在相當于在同一個組里 使用同一個虛擬ip
搭建lvs-dr模式集群
配置lvs服務器
四臺服務器都關閉網卡的守護行程
systemctl stop NetworkManager
添加一張網卡當作VIP(虛擬ip)使用
cd /etc/sysconfig/network-scripts/
cp -a ifcfg-ens33 ifcfg-ens33:0
vim ifcfg-ens33:0
只需要修改以下四條
NAME=ens33:0
DEVICE=ens33:0
IPADDR=192.168.188.188
NETMASK=255.255.255.0
開啟新網卡
ifup ens33:0
查看網卡資訊
ifconfig
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.188.188 netmask 255.255.255.0 broadcast 192.168.188.255
ether 00:0c:29:0e:28:0d txqueuelen 1000 (Ethernet)
安裝ipvsadm
yum -y install ipvsadm
關閉網卡的廣播功能 防止ip進行沖突
vim /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
多載sysctl
sysctl -p
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
配置負載分配策略
首先清除原有策略
ipvsadm -C
添加虛擬服務器
ipvsadm -A -t 192.168.188.188:80 -s rr
添加真實服務器 作業模式為DR
ipvsadm -a -t 192.168.188.188:80 -r 192.168.188.100:80 -g
ipvsadm -a -t 192.168.188.188:80 -r 192.168.188.101:80 -g
保存
ipvsadm-save > /etc/sysconfig/ipvsadm
查看
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.188.188:80 rr
-> 192.168.188.100:80 Route 1 0 0
-> 192.168.188.101:80 Route 1 0 0
配置真實服務器
兩臺真實服務器同時操作!!!
開啟httpd
systemctl start httpd
寫一個網頁測驗
echo 'server 1' >> /var/www/html/index.html
curl localhost
server 1
添加真實服務器的介面
cp -a ifcfg-lo ifcfg-lo:0
vim ifcfg-lo:0

修改內核引數 關閉arp通訊行為
vim /etc/sysctl.conf
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
使其生效
sysctl -p
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 2
net.ipv4.conf.default.arp_ignore = 1
net.ipv4.conf.default.arp_announce = 2
net.ipv4.conf.lo.arp_ignore = 1
net.ipv4.conf.lo.arp_announce = 2
啟動網卡
ifup lo:0
添加對應的路由記錄
route add -host 192.168.188.188 dev lo:0
查看路有記錄
route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.188.2 0.0.0.0 UG 100 0 0 ens33
192.168.79.0 0.0.0.0 255.255.255.0 U 101 0 0 ens37
192.168.188.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.188.188 0.0.0.0 255.255.255.255 UH 0 0 0 lo
設定開機自啟動
echo 'route add -host 192.168.188.188 dev lo:0' >> /etc/rc.local
可以打開瀏覽器輸入VIP測驗一下

重繪

還可以通過ipvsadm查看
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.188.188:80 rr
-> 192.168.188.100:80 Route 1 0 2
-> 192.168.188.101:80 Route 1 0 1
ipvsadm -ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP 192.168.188.188:80 4 44 0 8417 0
-> 192.168.188.100:80 2 12 0 1486 0
-> 192.168.188.101:80 2 32 0 6931 0
補充:
- Conns 連接數
- InPkts 入站資料包
- OutPkts 出站資料包 dr模式只接收入站資料包,出站由真實服務器處理 因此為0
- InBytes 入站位元組
- OutBytes 出站位元組
安裝和配置Keepalived
配置主機的keepalived
可以使用原始碼包,也可以使用yum安裝
yum install -y kernel-devel openssl-devel popt-devel
yum install -y keepalived
配置keepalived
vim /etc/keepalived/keepalived.conf
# 配置如下
! Configuration File for keepalived
global_defs {
router_id LVS1 # 服務器名稱,不能重復
}
vrrp_instance VI_1 { # 定義VRRP熱備實體名稱
state MASTER # 熱備狀態MASTER為主服務器
interface ens33 # 承載VIP地址的物理介面網卡 通過這張網卡做心跳檢測
virtual_router_id 66 # 虛擬服務器的ID號,每個熱備份組保持一致
priority 100 # 權重(優先級)最大不超過150 官網建議主和從之間差距為50
advert_int 1 # 通告間隔秒數 檢測間隔1秒
authentication { # 認證資訊,每個熱備份組保持一致
auth_type PASS # 認證型別
auth_pass 123456 # 密碼字串
}
virtual_ipaddress { # 指定漂移地址VIP 可以有多個
192.168.188.188
}
}
virtual_server 192.168.188.188 80 { # 虛擬服務器地址與埠
delay_loop 6 # 健康檢測間隔時間
lb_algo rr # 指定演算法
lb_kind DR # 指定群集模式
persistence_timeout 50 # 持久化時間
protocol TCP # 采用的協議為TCP
real_server 192.168.188.100 80 { # 第一個web 節點服務器的地址和埠
weight 1 # 權重
TCP_CHECK { # 健康檢查方式
connect_port 80 # 檢查目標埠
connect_timeout 3 # 連接超時時間
nb_get_retry 3 # 重試次數
delay_before_retry 4 # 重試間隔時間
}
}
real_server 192.168.188.101 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
開啟keepalived
systemctl start keepalived
查看日志
cat /var/log/messages
Apr 2 14:13:24 lvs Keepalived_vrrp[14857]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on ens33 for 192.168.188.188
Apr 2 14:13:24 lvs Keepalived_vrrp[14857]: Sending gratuitous ARP on ens33 for 192.168.188.188
Apr 2 14:13:24 lvs Keepalived_vrrp[14857]: Sending gratuitous ARP on ens33 for 192.168.188.188
Apr 2 14:13:24 lvs Keepalived_vrrp[14857]: Sending gratuitous ARP on ens33 for 192.168.188.188
Apr 2 14:13:24 lvs Keepalived_vrrp[14857]: Sending gratuitous ARP on ens33 for 192.168.188.188
說明已經監聽到ens33 使用的192.168.188.188的VIP了
主服務器就配置完畢
配置從機的keepalived
在從機上創建網卡使用vip
cp -a ifcfg-ens33 ifcfg-ens33:0
vim !$
NAME=ens33:0
DEVICE=ens33:0
IPADDR=192.168.188.188
NETMASK=255.255.255.0
啟動網卡
[root@lvs network-scripts]# ifup ens33:0
ERROR : [/etc/sysconfig/network-scripts/ifup-eth] Error, some other host (00:0C:29:80:75:C4) already uses address 192.168
.188.188.
無法啟動,因此我們需要去設定一下網卡的啟動腳本
[root@lvs network-scripts]# vim /etc/sysconfig/network-scripts/ifup-eth
將這幾行注釋掉

再次開啟網卡
ifup ens33:0
ifconfig
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.188.188 netmask 255.255.255.0 broadcast 192.168.188.255
ether 00:0c:29:34:cf:2a txqueuelen 1000 (Ethernet)
安裝keepalived
yum install -y kernel-devel openssl-devel popt-devel
yum install -y keepalived
使用scp將主機的組態檔直接發過來
scp root@192.168.188.10:/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
vim keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS2 # 名字需要修改
}
vrrp_instance VI_1 {
state BACKUP
interface ens33
virtual_router_id 66
priority 80 # 優先級修改 其他都一樣就行
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.188.188
}
}
virtual_server 192.168.188.188 80 {
delay_loop 6
lb_algo rr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.188.100 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
real_server 192.168.188.101 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 3
nb_get_retry 3
delay_before_retry 4
}
}
}
yum install -y ipvsadm
systemctl start ipvsadm
第一次查看策略 沒有資訊
ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
systemctl start keepalived
開啟keepalived之后查看
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.188.188:80 rr persistent 50
-> 192.168.188.100:80 Route 1 0 0
-> 192.168.188.101:80 Route 1 0 0
最后關閉網卡重定向
vim /etc/sysctl.conf
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
sysctl -p
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0
net.ipv4.conf.ens33.send_redirects = 0
測驗:


使用命令查看
[root@lvs network-scripts]# 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.188.188:80 rr
-> 192.168.188.100:80 Route 1 0 5
-> 192.168.188.101:80 Route 1 0 5
[root@lvs network-scripts]# ipvsadm -ln --stats
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes
-> RemoteAddress:Port
TCP 192.168.188.188:80 34 238 0 36669 0
-> 192.168.188.100:80 17 111 0 15772 0
-> 192.168.188.101:80 17 127 0 20897 0
高可用測驗
我們將主機網斷了 然后看整個集群能否繼續作業
重繪網頁 仍然可以訪問

然后又啟動主機
重繪網頁之后 主機又繼續使用虛擬地址承擔調度的作用

至此我們的實驗還差nfs共享服務
在兩臺真實服務器上面
掛載nfs
vim /etc/fstab
192.168.79.200:/mynfs /var/www/html nfs defaults,_netdev 0 0
mount -a
df -hT
打開網頁測驗

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/272342.html
標籤:其他
