lvs+keepalived是什么?
keepalived工作原理
keepalived是集群管理中保證集群高可用的一個(gè)服務(wù)軟件,其功能類(lèi)似于heartbeat,用來(lái)防止單點(diǎn)故障。 keepalived是以VRRP協(xié)議為實(shí)現(xiàn)基礎(chǔ)的,VRRP全稱(chēng)Virtual Router Redundancy Protocol,即虛擬路由冗余協(xié)議。 虛擬路由冗余協(xié)議,可以認(rèn)為是實(shí)現(xiàn)路由器高可用的協(xié)議,即將N臺(tái)提供相同功能的路由器組成一個(gè)路由器組,這個(gè)組里面有一個(gè)master和多個(gè)backup,master上面有一個(gè)對(duì)外提供服務(wù)的vip(該路由器所在局域網(wǎng)內(nèi)其他機(jī)器的默認(rèn)路由為該vip),master會(huì)發(fā)組播,當(dāng)backup收不到vrrp包時(shí)就認(rèn)為master宕掉了,這時(shí)就需要根據(jù)VRRP的優(yōu)先級(jí)來(lái)選舉一個(gè)backup當(dāng)master。這樣的話就可以保證路由器的高可用了。
LVS簡(jiǎn)介
LVS是Linux Virtual Server的簡(jiǎn)寫(xiě),意即Linux虛擬服務(wù)器,是一個(gè)虛擬的服務(wù)器集群系統(tǒng)。本項(xiàng)目在1998年5月由章文嵩博士成立,是中國(guó)國(guó)內(nèi)最早出現(xiàn)的自由軟件項(xiàng)目之一。目前有三種IP負(fù)載均衡技術(shù)(VS/NAT、VS/TUN和VS/DR),十種調(diào)度算法。
本文重點(diǎn)給大家介紹Linux利用keepalived實(shí)現(xiàn)lvs的高可用性的操作方法,感興趣的朋友參考下吧。
單主模型IPVS示例
配置keepalive
高可用的ipvs集群示例:修改keepalived配置文件
修改主機(jī):192.168.234.27的keepalived配置文件
[root@234c27 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost //接受郵件地址
}
notification_email_from keepalived@localhost //發(fā)送郵件地址
smtp_server 127.0.0.1 //發(fā)送郵件服務(wù)器IP
smtp_connect_timeout 30 //郵件連接超時(shí)時(shí)長(zhǎng)
router_id kptwo //路由id
vrrp _mcast_group4 234.10.10.10 //指定vrrp協(xié)議的多播地址
}
vrrp_instance VI_1 { //vrrp協(xié)議的
state MASTER //lvs的MASTER服務(wù)器
interface ens37 //
virtual_router_id 50 //虛擬路由
priority 100 //權(quán)重為100.越大越先
advert_int 1 //發(fā)送組博包的間隔
authentication { //驗(yàn)證
auth_type PASS //方式為pass( 明文)
auth_pass 1111 //密碼
}
virtual_ipaddress { //keepalived虛擬ip
10.0.0.100/24
}
}
virtual_server 10.0.0.100 80 {
delay_loop 6 //檢查后端服務(wù)器的時(shí)間間隔
lb_algo wrr //定義調(diào)度方法
lb_kind DR //集群的類(lèi)型
#persistence_timeout 50 //持久連接時(shí)長(zhǎng)
protocol TCP //服務(wù)協(xié)議,僅支持TCP
real_server 192.168.234.47 80 { //后端real_server服務(wù)器地址
weight 1 //權(quán)重
HTTP_GET { //應(yīng)用層檢測(cè)
url {
path / //定義要監(jiān)控的URL
status_code 200 //判斷上述檢測(cè)機(jī)制為健康狀態(tài)的響應(yīng)碼
}
connect_timeout 3 //連接請(qǐng)求的超時(shí)時(shí)長(zhǎng)
nb_get_retry 3 //重試次數(shù)
delay_before_retry 3 //重試之前的延遲時(shí)長(zhǎng)
}
}
real_server 192.168.234.57 80 {
weight 2
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
修改主機(jī):192.168.234.37的keepalived配置文件
[root@234c37 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id kptwo
vrrp _mcast_group4 234.10.10.10
}
vrrp_instance VI_1 {
state BACKUP
interface ens37
virtual_router_id 50
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.100/24
}
}
virtual_server 10.0.0.100 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
#persistence_timeout 50
protocol TCP
sorry_server 127.0.0.1:80
real_server 192.168.234.47 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.234.57 80 {
weight 2
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
查看keepalived
[root@234c37 ~]# systemctl status keepalived
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: inactive (dead)
…………
[root@234c37 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
//暫無(wú)ipvsadm
啟動(dòng)服務(wù)
[root@234c27 keepalived]# systemctl start keepalived.service
[root@234c27 keepalived]# systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2018-08-31 20:30:02 CST; 12s ago
Process: 9657 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 9658 (keepalived)
………………
[root@234c27 keepalived]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.0.0.100:80 wrr
-> 192.168.234.47:80 Route 1 0 0
-> 192.168.234.57:80 Route 2 0 0
//啟動(dòng)服務(wù)lvs vs已配置好
后端real_server準(zhǔn)備
增加ip在網(wǎng)卡上 修改限制arp通告及應(yīng)答級(jí)別 rs1 rs2都做,網(wǎng)關(guān)并指向路由
ip a a 10.0.0.100/32 dev ens37
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
route add default gw 192.168.234.17
安裝httpd服務(wù) 寫(xiě)好網(wǎng)頁(yè)文件
啟動(dòng)服務(wù)
image
多主模型IPVS示例
配置keepalive
高可用的ipvs集群示例:修改keepalived配置文件
修改主機(jī):192.168.234.27的keepalived配置文件
[root@234c27 keepalived]# vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id kpone
vrrp _mcast_group4 234.10.10.10
}
vrrp_instance VI_1 {
state MASTER
interface ens37
virtual_router_id 50
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.100/24
}
}
vrrp_instance VI_2 {
state BACKUP
interface ens37
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
10.0.0.200/24
}
}
virtual_server 10.0.0.100 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
#persistence_timeout 50
protocol TCP
#sorry_server 127.0.0.1:80
real_server 192.168.234.47 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
virtual_server 10.0.0.200 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
#persistence_timeout 50
protocol TCP
#sorry_server 127.0.0.1:80
real_server 192.168.234.57 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
修改主機(jī):192.168.234.37的keepalived配置文件
[root@234c37 ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id kptwo
vrrp _mcast_group4 234.10.10.10
}
vrrp_instance VI_1 {
state BACKUP
interface ens37
virtual_router_id 50
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
10.0.0.100/24
}
}
vrrp_instance VI_2 {
state MASTER
interface ens37
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 2222
}
virtual_ipaddress {
10.0.0.200/24
}
}
virtual_server 10.0.0.100 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
#persistence_timeout 50
protocol TCP
#sorry_server 127.0.0.1:80
real_server 192.168.234.47 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
virtual_server 10.0.0.200 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
#persistence_timeout 50
protocol TCP
#sorry_server 127.0.0.1:80
real_server 192.168.234.57 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
讓10.0.0.100的ip優(yōu)先分配至192.168.234.47 192.168.234.57備用
讓10.0.0.200的ip優(yōu)先分配至192.168.234.57 192.168.234.47備用
后端real_server準(zhǔn)備
修改192.168.234.57的vip為10.0.0.200/32
[root@234c27 keepalived]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.0.0.100:80 wrr
-> 192.168.234.47:80 Route 1 0 0
TCP 10.0.0.200:80 wrr
-> 192.168.234.57:80 Route 1 0 0
現(xiàn)在宕掉一個(gè)lvs
[root@234c27 keepalived]# systemctl stop keepalived.service
[root@234c27 keepalived]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
依然提供服務(wù)
[root@234c37 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.0.0.100:80 wrr
-> 192.168.234.47:80 Route 1 0 21
TCP 10.0.0.200:80 wrr
-> 192.168.234.57:80 Route 1 0 39
后一個(gè)實(shí)現(xiàn)基于前一個(gè)的基礎(chǔ)上修改來(lái)的
假設(shè)要實(shí)現(xiàn)sorry_server
1.把rs服務(wù)都停掉。然后在lvs上安裝apache或者nginx服務(wù)
2.將keepalived配置文件中的
virtual_server 10.0.0.200 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
#persistence_timeout 50
protocol TCP
#sorry_server 127.0.0.1:80 //這一行來(lái)修改 寫(xiě)出服務(wù)出錯(cuò)之后的頁(yè)面
real_server 192.168.234.57 80 {
weight 1
HTTP_GET {
url {
path /
status_code 200
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}