keepalived +LVS DR 雙主互備模型實驗

keepalived +LVS DR 雙主互備模型實驗

 實驗環境介紹

    操作系統:DR:centos 7.2 兩個節點,都安裝keepalived

              Real Server :centos 6.7 兩個節點。都安裝上httpd

    實驗環境拓撲圖

blob.png

    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

blob.png在刷新幾次會有下面的結果顯示。由于兩個Real Server權重不同,兩個Real Server服務器訪問量比大約3:1.192.168.36.134訪問理論概率75%。192.168.36.133 訪問的概率理論值25%

blob.png

blob.pngblob.png

手動停止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

(1)
jslijbjslijb
上一篇 2016-03-09 09:55
下一篇 2016-03-09 09:59

相關推薦

  • DNS常用配置解析反向解析

    什么是域名解析庫文件?     是指將我們訪問的域名解析成IP返回給本主機,然后本機拿著解析后IP去訪問服務器,而解析格式就放在域名解析庫文件中。DNS解析分為正向解析和反向解析    正向解析就是將域或域名解析成對應的IP地址,反之則相反,通過IP解析成域名 &nbs…

    Linux干貨 2017-05-31
  • rpm命令詳解及和yum之間的關系

        RPM 全名是“ RedHat Package Manager ”簡稱則為 RPM 顧名思義,當初這個軟件管理的機制是由 Red Hat 這家公司發展出來的。 RPM 是以一種數據庫記錄的方式來將你所需要的軟件安裝到你的 Linux 系統的一套管理機制。    &nbsp…

    Linux干貨 2016-08-21
  • 每日一練 —8.4 文本處理工具,正則表達式

    練習 1 、找出ifconfig 命令結果中本機的所有IPv4 地址 2 、查出分區空間使用率的最大百分比值 3 、查出用戶UID 最大值的用戶名、UID 及shell 類型 4 、查出/tmp 的權限,以數字方式顯示 5 、統計當前連接本機的每個遠程主機IP 的連接數,并按從大到小排序   6,顯示/proc/meminfo文件中以大寫或小寫s開…

    Linux干貨 2016-08-15
  • iptables初探

    iptables 簡述 基礎知識 命令使用 簡述 iptables是什么?netfilter又是什么? iptables是位于用戶控件的一個防火墻規則控制管理工具。netfilter是位于內核中的真正的防火墻,由五個鉤子函數(hooks)而組成。 iptables的作用是什么? 用來添加,刪除,管理netfilter規則。 netfilter的作用是什么? …

    2016-05-31
  • 關于man的幾個重要命令

    接觸Linux有很多年了,以前對于linux我覺得自己懂得的還算一般吧,可是聽了馬哥的關于linux的視頻課程,發現自己就是一個還沒入門的菜鳥,所以果斷的狠心的報了馬哥的網絡班,拜在馬哥旗下,以后就打著馬哥的旗號到外面混,哈哈最初的這幾天學習,因為工作和時間的等等等的關系,所以才到現在還在趕著寫我的博客作業,現在就最有感觸的幾個命令串聯一下。學習linux肯…

    Linux干貨 2016-10-31
  • 基本腳本編譯

                                  …

    2017-08-05

評論列表(1條)

  • stanley
    stanley 2016-03-09 09:57

    為寫標簽的細節點贊,代碼一定要格式化,不然整個篇幅外觀非常亂

欧美性久久久久