KeepAlived系列文章:http://www.cnblogs.com/f-ck-need-u/p/7576137.htmlhtml
本文只給出幾個keepalived+lvs(VS/DR模式)的配置示例,關於keepalived的配置文件說明見:高可用之KeepAlived(一):基本概念和配置文件分析。web
在實驗開始前,須要說明幾點:vim
實驗環境以下:bash
RS上操做:服務器
yum -y install httpd echo "rs1:192.168.100.49" > /var/www/html/index.html # RS1上操做 echo "rs1:192.168.100.50" > /var/www/html/index.html # RS2上操做 service httpd start echo 1 >/proc/sys/net/ipv4/conf/all/arp_ignore echo 2 >/proc/sys/net/ipv4/conf/all/arp_announce ifconfig lo:0 192.168.100.10/32 up route add -host 192.168.100.10 dev lo route add default gw 192.168.100.51
Router上操做:負載均衡
echo 1 >/proc/sys/net/ipv4/ip_forward
Director上操做:oop
[root@xuexi ~]# route del default
[root@xuexi ~]# route add default gw 192.168.100.51
[root@xuexi ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@xuexi ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_1
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 12345678
}
virtual_ipaddress {
192.168.100.10/32 dev eth0 label eth0:0
}
}
virtual_server 192.168.100.10 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
protocol TCP
real_server 192.168.100.49 80 {
weight 2
TCP_CHECK {
connect_port 80
connect_timeout 1
nb_get_retry 2
delay_before_retry 1
}
}
real_server 192.168.100.50 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 1
nb_get_retry 2
delay_before_retry 1
}
}
}
測試是否能實現鏈接的負載均衡。性能
KeepAlived經過vrrp的冗餘路由切換協議實現高可用功能,主要用於lvs Director的高可用。測試
在配置KeepAlived高可用功能時,須要注意如下幾點:網站
實驗環境以下:
RS一、RS二、Router、director_lvs(master)和前文實驗的配置一致,不需修改。所以,只需提供director_lvs(backup)的配置文件便可。
如下是master上的操做。
[root@xuexi ~]# route del default
[root@xuexi ~]# route add default gw 192.168.100.51
[root@xuexi ~]# cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
[root@xuexi ~]# vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
router_id LVS_2 # 和master不一樣
}
vrrp_instance VI_1 { # 和master相同
state BACKUP # 和master不一樣
interface eth0
virtual_router_id 51 # 和master相同
priority 50 # 和maste不一樣
advert_int 1
authentication {
auth_type PASS # 和master相同
auth_pass 12345678 # 和master相同
}
virtual_ipaddress {
192.168.100.10/32 dev eth0 label eth0:0
}
}
virtual_server 192.168.100.10 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
protocol TCP
real_server 192.168.100.49 80 {
weight 2
TCP_CHECK {
connect_port 80
connect_timeout 1
nb_get_retry 2
delay_before_retry 1
}
}
real_server 192.168.100.50 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 1
nb_get_retry 2
delay_before_retry 1
}
}
}
啓動兩Director,查看是否只有master上設置了VIP。注意:ipvs規則在master和backup上都設置了,但因爲backup沒有VIP,所以backup設置的ipvs規則暫時是沒有意義的。當backup切換爲master狀態時,只會設置VIP。
而後查看master是否已經生效。生效後,將master斷開。觀察原來的backup切換爲master的日誌:
Mar 1 20:52:19 xuexi Keepalived_vrrp[4709]: VRRP_Instance(VI_1) Transition to MASTER STATE
Mar 1 20:52:20 xuexi Keepalived_vrrp[4709]: VRRP_Instance(VI_1) Entering MASTER STATE
Mar 1 20:52:20 xuexi Keepalived_vrrp[4709]: VRRP_Instance(VI_1) setting protocol VIPs.
Mar 1 20:52:20 xuexi Keepalived_vrrp[4709]: Sending gratuitous ARP on eth0 for 192.168.100.10
Mar 1 20:52:20 xuexi Keepalived_vrrp[4709]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.168.100.10
Mar 1 20:52:20 xuexi Keepalived_vrrp[4709]: Sending gratuitous ARP on eth0 for 192.168.100.10
Mar 1 20:52:20 xuexi Keepalived_vrrp[4709]: Sending gratuitous ARP on eth0 for 192.168.100.10
Mar 1 20:52:20 xuexi Keepalived_vrrp[4709]: Sending gratuitous ARP on eth0 for 192.168.100.10
Mar 1 20:52:20 xuexi Keepalived_vrrp[4709]: Sending gratuitous ARP on eth0 for 192.168.100.10
Mar 1 20:52:25 xuexi Keepalived_vrrp[4709]: Sending gratuitous ARP on eth0 for 192.168.100.10
Mar 1 20:52:25 xuexi Keepalived_vrrp[4709]: VRRP_Instance(VI_1) Sending/queueing gratuitous ARPs on eth0 for 192.168.100.10
Mar 1 20:52:25 xuexi Keepalived_vrrp[4709]: Sending gratuitous ARP on eth0 for 192.168.100.10
Mar 1 20:52:25 xuexi Keepalived_vrrp[4709]: Sending gratuitous ARP on eth0 for 192.168.100.10
Mar 1 20:52:25 xuexi Keepalived_vrrp[4709]: Sending gratuitous ARP on eth0 for 192.168.100.10
Mar 1 20:52:25 xuexi Keepalived_vrrp[4709]: Sending gratuitous ARP on eth0 for 192.168.100.10
能夠看到,切換速度極快(1秒之內)。
再測試將原來的master(高優先級)啓動,發現它再次成爲master,切換速度也是極快。
將RS1上的httpd中止。再查看主、備director上的ipvs規則。
[root@xuexi ~]# 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.100.10:80 wrr
-> 192.168.100.50:80 Route 1 0 0
發現master和backup兩邊都把不健康的RealServer1節點給踢出去了。由此能夠知道,健康檢查是master和backup都會循環進行的,並非只有master進行檢查。
再將RS1上的httpd啓動。而後查看ipvs規則。發現沒過幾秒鐘就把規則添加回來了。
keepalive的vrrp多實例能夠管理多個director和vip,進而能夠實現"雙主模型"的高可用。
實驗環境以下圖:
其中Director1的vrrp實例1上是R1的master,vrrp實例2是R2的backup,Director2的vrrp實例1是R1的backup,vrrp實例2是R2的master。
這裏略過4個RS的配置步驟(若有問題,參照前文配置RS一、RS2的過程)。
如下是Director1和Director2的keepalived.conf不一樣部分和相同部分的內容:
# 如下是兩臺Director上相同部分的內容
! Configuration File for keepalived |! Configuration File for keepalived
|
global_defs { |global_defs {
router_id LVS_1 | router_id LVS_2
} |}
|
vrrp_instance VI_1 { |vrrp_instance VI_1 {
state MASTER | state BACKUP
interface eth0 | interface eth0
virtual_router_id 51 | virtual_router_id 51
priority 100 | priority 50
advert_int 1 | advert_int 1
authentication { | authentication {
auth_type PASS | auth_type PASS
auth_pass 12345678 | auth_pass 12345678
} | }
virtual_ipaddress { | virtual_ipaddress {
192.168.100.10/32 dev eth0 label eth0:0 | 192.168.100.10/32 dev eth0 label eth0:0
} | }
} |}
# 不一樣vrrp實例綁定在同一接口上,vrid必須不能相同 |# 不一樣vrrp實例綁定在同一接口上,vrid必須不能相同
vrrp_instance VI_2 { |vrrp_instance VI_2 {
state BACKUP | state MASTER
interface eth0 | interface eth0
virtual_router_id 55 | virtual_router_id 55
priority 50 | priority 100
advert_int 1 | advert_int 1
authentication { | authentication {
auth_type PASS | auth_type PASS
auth_pass 12345678 | auth_pass 12345678
} | }
virtual_ipaddress { | virtual_ipaddress {
192.168.100.11/32 dev eth0 label eth0:1 | 192.168.100.11/32 dev eth0 label eth0:1
} | }
} |}
######################################################################################### # 如下是兩臺Director上相同部分的內容 virtual_server 192.168.100.10 80 { delay_loop 6 lb_algo wrr lb_kind DR protocol TCP real_server 192.168.100.49 80 { weight 2 TCP_CHECK { connect_port 80 connect_timeout 1 nb_get_retry 2 delay_before_retry 1 } } real_server 192.168.100.50 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 1 nb_get_retry 2 delay_before_retry 1 } } } virtual_server 192.168.100.11 80 { delay_loop 6 lb_algo wrr lb_kind DR protocol TCP real_server 192.168.100.57 80 { weight 2 TCP_CHECK { connect_port 80 connect_timeout 1 nb_get_retry 2 delay_before_retry 1 } } real_server 192.168.100.58 80 { weight 1 TCP_CHECK { connect_port 80 connect_timeout 1 nb_get_retry 2 delay_before_retry 1 } } }
分別測試訪問兩個VIP:http://192.168.100.10
和http://192.168.100.11
。其中前者目前只能調度R1:RS1和R1:RS2,後者只能調度R2:RS1和R2:RS2。將任一Director斷開,測試4個RS是否仍能繼續提供服務。
若是全部RS都宕了,對於外界來講就真的沒法再訪問網站了,這顯然不適合。這時能夠經過keepalived來配置一個服務頁面。例如告訴外界客戶端網站正在維護狀態,或者只提供一個網站的一個主頁面。
通常來講,由於是在全部RS都宕機的狀況下sorry server提供的臨時服務才生效,所以一般將sorry server配置在virtual_server中而非real_server中。
配置時,只需在keepalived配置文件的virtual_server段落中添加sorry_server指令便可。而且,若是啓用了vrrp的高可用,應該在master和backup節點上都加上sorry server。
virtual_server 192.168.100.10 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
protocol TCP
sorry_server 127.0.0.1 80
重啓keepalived後,再在每一個vrrp機器上配置好httpd。
yum -y install httpd echo "web Maintenancing" >/var/www/html/index.html service httpd start
而後將全部的RS節點的httpd服務停掉。再看keepalived上的ipvs規則,發現已經將VIP做爲規則添加進來了,因而下次訪問VIP時將調度這臺sorry server。當某一臺RS恢復的時候,ipvs規則又會變動爲RS的節點。
對於集羣系統不大的狀況下,LVS Director通常會比較空閒,這樣就比較浪費資源。這時一般會將LVS Director自身也做爲一個RS,一邊提供web服務,一邊提供調度功能,不過應該將它的調度權重設置低一點,以避免影響負載均衡的性能。這稱爲local RS,local RS的RIP能夠寫Director上的任意地址(127.0.0.1均可以)。例如:
real_server 127.0.0.1 80 {
weight 1
TCP_CHECK {
connect_port 80
connect_timeout 1
nb_get_retry 2
delay_before_retry 1
}
}
local RS和sorry server不該該同時設置,由於若是local RS壞了,sorry server確定沒法被調度到。