基於keepalived雙主模型的高可用LVS


背景知識:

  keepalived:Keepalived的作用是檢測web服務器的狀態,如果有一台web服務器死機,或工作出現故障,Keepalived將檢測到,並將有故障的web 服務器從系統中剔除,當web服務器工作正常后Keepalived自動將web服務器加入到服務器群中,這些工作全部自動完成,不需要人工干涉,需要人工做的只是修復故障的web服務器。

  LVS:LVS是Linux Virtual Server的簡寫,意即Linux虛擬服務器,是一個虛擬的服務器集群系統。

實驗系統:CentOS 6.6_x86_64

實驗前提:提前准備好編譯環境,防火牆和selinux都關閉

實驗說明:本實驗共有4台主機,其中keep1和keep2為2台前端的keepalived服務器,real1和real2為LVS中的realserver,IP地址對應如拓撲圖。

實驗軟件:httpd-2.2.15 keepalived-1.2.19

實驗拓撲:

    

一、配置realserver

  1.安裝httpd:

yum -y install httpd

  2.配置內核參數:

echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore           //僅在請求的地址配置在請求報文的接口進行響應
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce         //表示僅通告網絡直連的接口的地址
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce

  3.增加測試頁面並配置VIP:

    real1上:

ifconfig lo:0 192.168.19.150 netmask 255.255.255.255 broadcast 192.168.19.150 up    //配置VIP
ifconfig lo:1 192.168.19.151 netmask 255.255.255.255 broadcast 192.168.19.151 up
route add -host 192.168.19.150 dev lo:0 //配置路由
route add -host 192.168.19.151 dev lo:1 vim
/var/www/html/index.html --------------------------------------------- <h1>realserver1</h1> --------------------------------------------- service httpd start

    real2上:

 
         
ifconfig lo:0 192.168.19.150 netmask 255.255.255.255 broadcast 192.168.19.150 up
ifconfig lo:1 192.168.19.151 netmask 255.255.255.255 broadcast 192.168.19.151 up
route add -host 192.168.19.150 dev lo:0
route add -host 192.168.19.151 dev lo:1
vim /var/www/html/index.html --------------------------------------------- <h1>realserver2</h1> --------------------------------------------- service httpd start

    

二、安裝並配置keepalived

  1.編譯安裝keepalived,在keep1和keep2上操作:

wget http://www.keepalived.org/software/keepalived-1.2.19.tar.gz
tar
xf keepalived-1.2.19.tar.gz cd keepalived-1.2.19
./configure --prefix=/usr/local/keepalived --sbindir=/usr/sbin/ --sysconfdir=/etc/ --mandir=/usr/local/share/man/ --with-kernel-dir=/usr/src/kernels/2.6.32-504.30.3.el6.x86_64/ //內核版本換成自己主機的 make && make install chkconfig --add keepalived chkconfig keepalived on
yum -y install ipvsadm //安裝LVS工具

  2.配置keepalived:

    keep1上:

vim /etc/keepalived/keepalived.conf
----------------------------------------------------
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 31
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass abcd
    }
    virtual_ipaddress {
        192.168.19.150
    }
}

vrrp_instance VI_2 {
    state BACKUP
    interface eth0
    virtual_router_id 41
    priority 99
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass abcd
    }
    virtual_ipaddress {
        192.168.19.151
    }
}

virtual_server 192.168.19.150 80 {
    delay_loop 6
    lb_algo rr                         //LVS算法
    lb_kind DR                         //調度類型
    protocol TCP

    real_server 192.168.19.29 80 {
            weight 1
      
inhibit_on_failure
            TCP_CHECK {
            connect_timeout 3
            nb_get_retry 2
            delay_before_retry 1
            connect_port 80
            } } real_server
192.168.19.34 80 { weight 1
      
inhibit_on_failure
            TCP_CHECK {
            connect_timeout 3
            nb_get_retry 2
            delay_before_retry 1
            connect_port 80
            } } } virtual_server
192.168.19.151 80 { delay_loop 6 lb_algo rr lb_kind DR protocol TCP real_server 192.168.19.29 80 { weight 1
      
inhibit_on_failure
            TCP_CHECK {
            connect_timeout 3
            nb_get_retry 2
            delay_before_retry 1
            connect_port 80
            } } real_server
192.168.19.34 80 { weight 1
      
inhibit_on_failure
            TCP_CHECK {
            connect_timeout 3
            nb_get_retry 2
            delay_before_retry 1
            connect_port 80
            } } }

    keep2上:

vim /etc/keepalived/keepalived.conf 
----------------------------------------------
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 31 priority 99 advert_int 1 authentication { auth_type PASS auth_pass abcd } virtual_ipaddress { 192.168.19.150 } } vrrp_instance VI_2 { state MASTER interface eth0 virtual_router_id 41 priority 100 advert_int 1 authentication { auth_type PASS auth_pass abcd } virtual_ipaddress { 192.168.19.151 } } virtual_server 192.168.19.150 80 { delay_loop 6 lb_algo rr lb_kind DR protocol TCP real_server 192.168.19.29 80 {             weight 1 inhibit_on_failure
            TCP_CHECK {
            connect_timeout 3
            nb_get_retry 2
            delay_before_retry 1
            connect_port 80
            }
} real_server 192.168.19.34 80 { weight 1 inhibit_on_failure
            TCP_CHECK {
            connect_timeout 3
            nb_get_retry 2
            delay_before_retry 1
            connect_port 80
            } } } virtual_server 192.168.19.151 80 { delay_loop 6 lb_algo rr lb_kind DR protocol TCP real_server 192.168.19.29 80 { weight 1 inhibit_on_failure
            TCP_CHECK {
            connect_timeout 3
            nb_get_retry 2
            delay_before_retry 1
            connect_port 80
            } } real_server 192.168.19.34 80 { weight 1 inhibit_on_failure
            TCP_CHECK {
            connect_timeout 3
            nb_get_retry 2
            delay_before_retry 1
            connect_port 80
            }
} }

   3.兩台機器啟動keepalived:

service keepalived start
ipvsadm -L -n              //用LVS工具查看keepalived運行
ip addr show               //查看VIP

    

    兩台機器上LVS規則都已經生效,且2個VIP分別運行在2個節點:

      keep1上:

    

      keep2上:

    

    分別打開測試頁面進行測試:

    

    

    停掉任何一個節點,資源都會自動轉移:

    

  至此,演示完畢,謝謝!如有問題,請與我聯系,QQ:82800452


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM