keepalived +LVS DR 雙主互備模型實驗
實驗環境介紹
操作系統:DR:centos 7.2 兩個節點,都安裝keepalived
Real Server :centos 6.7 兩個節點。都安裝上httpd
實驗環境拓撲圖
DR1 IP 192.168.36.131/24
DR2 IP 192.168.36.132/24
VIP1 IP 192.168.36.15/32
VIP2 IP 192.168.36.16/32
RIP1 IP 192.168.36.133/24
RIP2 IP 192.168.36.134/24
DR1 hostname:node1.centos7.cn
DR2 hostname:node2.centos7.cn
實驗步驟
1、兩個DR節點上需要配置host文件。能夠以主機名進行通訊。當然也可以使用DNS解析來實現,只是這種方式效率比較低,成本也高些,如果高可用節點不是很多的情況下還是使用host文件比較好。
兩個節點配置一樣,如下所示:
192.168.36.132 node2 centos7.cn
192.168.36.131 node1 centos7.cn
2、配置兩個節點相互之間的ssh認證:基于密鑰的認證。這一步不是必須的,只是方便操作而已
3、兩個節點的時間必須同步。centos7 使用chrony這個軟件進行時間同步。只需要安裝這個軟件,并將其啟動即可。當然這兩個主機要能夠上互聯網。如果不能就需要在內網搭建一個ntp服務器。centos 7 當然也支持ntp同步時間
4、兩個節點上關閉防火墻,Real Server也要關閉防火墻。如果需要都開啟,DR 上要放行組播地址224.0.0.18的流量 Real Server上需要放行tcp 80 端口,源地址是任意地址的流量。selinux不關閉似乎沒有太大的影響.
5、兩個節點上安裝keepalived和ipvsadm(這個不是必須的。安裝了方便查看ipvs相關的信息)
6、兩個Real Server安裝httpd。設置arp相關信息。提供Real Server配置腳本如下:兩個節點都運行該腳本即可。
#!/bin/bash
VIP1=192.168.36.15
VIP2=192.168.36.16
case $1 in
start_dr)
ifconfig lo:0 $VIP1 netmask 255.255.255.255 broadcast $VIP1
ifconfig lo:1 $VIP2 netmask 255.255.255.255 broadcast $VIP2
echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
;;
stop_dr)
ifdown lo
ifup lo
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
;;
*)
echo "please input parameter:start_dr or stop_dr"
;;
esac
兩個Real Server 上還需要配置httpd服務,centos 6.7上安裝完后,需要修改配置文件,否則啟動的時候總是警告信息或者直接報錯
vim /etc/httpd/conf/httpd.conf
ServerName 192.168.36.134:80。把這一行前面的"#"去掉,并修改類似這樣的即可,即使是不修改只去掉"#",就可以正常啟動了
給web服務一個測試首頁,做實驗為了分辨出負載均衡的效果故意提供內容不相同的首頁
vim /var/www/html/index.html
<h1> This is webserver1 192.168.36.134 </h>
另一個節點上
vim /var/www/html/index.html
<h1> This is webserver2 192.168.36.133 </h>
7、兩個節點上keepalived的配置如下:
node1 上keepalived 配置文
vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost.cn
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_MASTER
}
vrrp_instance VI_1 {
state MASTER
interface eno16777736
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type AH
auth_pass c87a5ba3176f
}
virtual_ipaddress {
192.168.36.15 dev eno16777736 label eno16777736:0
}
}
vrrp_instance VI_2 {
state BACKUP
interface eno16777736
virtual_router_id 52
priority 99
advert_int 1
authentication {
auth_type AH
auth_pass c87a5ba3176f
}
virtual_ipaddress {
192.168.36.16 dev eno16777736 label eno16777736:1
}
}
virtual_server 192.168.36.15 80
{
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.255
# persistence_timeout 50
protocol TCP
real_server 192.168.36.133 80
{
weight 1
TCP_CHECK
{
connect_timeout 3
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
real_server 192.168.36.134 80
{
weight 3
HTTP_GET
{
url {
path /
status_code 200
}
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
virtual_server 192.168.36.16 80
{
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.255
# persistence_timeout 50
protocol TCP
real_server 192.168.36.133 80
{
weight 1
TCP_CHECK
{
connect_timeout 3
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
real_server 192.168.36.134 80
{
weight 3
HTTP_GET
{
url {
path /
status_code 200
}
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
node 2
vim /etc/keepalived/keepalived.conf
global_defs {
notification_email {
root@localhost
}
notification_email_from keepalived@localhost.cn
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_BACKUP
}
vrrp_instance VI_1 {
state BACKUP
interface eno16777736
virtual_router_id 51
priority 99
advert_int 1
authentication {
auth_type AH
auth_pass c87a5ba3176f
}
virtual_ipaddress {
192.168.36.15 dev eno16777736 label eno16777736:0
}
}
vrrp_instance VI_2 {
state MASTER
interface eno16777736
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type AH
auth_pass c87a5ba3176f
}
virtual_ipaddress {
192.168.36.16 dev eno16777736 label eno16777736:1
}
}
virtual_server 192.168.36.15 80
{
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.255
# persistence_timeout 50
protocol TCP
real_server 192.168.36.133 80
{
weight 1
TCP_CHECK
{
connect_timeout 3
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
real_server 192.168.36.134 80
{
weight 3
HTTP_GET
{
url {
path /
status_code 200
}
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
virtual_server 192.168.36.16 80
{
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.255
# persistence_timeout 50
protocol TCP
real_server 192.168.36.133 80
{
weight 1
TCP_CHECK
{
connect_timeout 3
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
real_server 192.168.36.134 80
{
weight 3
HTTP_GET
{
url {
path /
status_code 200
}
}
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
測試客戶端瀏覽器分別訪問http:/192.168.36.15和http:/192.168.36.16
在刷新幾次會有下面的結果顯示。由于兩個Real Server權重不同,兩個Real Server服務器訪問量比大約3:1.192.168.36.134訪問理論概率75%。192.168.36.133 訪問的概率理論值25%
手動停止node1 節點上的keepalived的服務
[root@node2 keepalived]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.36.132 netmask 255.255.255.0 broadcast 192.168.36.255
inet6 fe80::20c:29ff:fe2a:96f7 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:2a:96:f7 txqueuelen 1000 (Ethernet)
RX packets 1628624 bytes 140367911 (133.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 29468 bytes 2585699 (2.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eno16777736:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.36.15 netmask 255.255.255.255 broadcast 0.0.0.0
ether 00:0c:29:2a:96:f7 txqueuelen 1000 (Ethernet)
eno16777736:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.36.16 netmask 255.255.255.255 broadcast 0.0.0.0
ether 00:0c:29:2a:96:f7 txqueuelen 1000 (Ethernet)
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 0 (Local Loopback)
RX packets 52 bytes 3805 (3.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 52 bytes 3805 (3.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
通過查看node2的IP信息即可看出node2 將node1的地址搶占過來了。通過客戶端測試。效果同剛才的一樣,兩個IP地址都能夠訪問
實際環境中一般是使用域名訪問的,由于這里是做實驗故使用IP地址訪問web服務器。
實驗總結:
1、在centos7中由于使用yum的方式安裝keepalived的,日志信息不是很詳細,排錯不是很友好
2、不知道為什么keepalived的服務重啟生效比較慢,有時候需要重啟好幾次,不知道是生效比較慢還是程序有bug,配置文件沒有修過,第一次重啟
沒有生效,再次重啟又生效。很奇怪?。?!
原創文章,作者:jslijb,如若轉載,請注明出處:http://www.www58058.com/12246
為寫標簽的細節點贊,代碼一定要格式化,不然整個篇幅外觀非常亂