CentOS6.5 LVS + KeepAlived搭建步驟

破棉襖發表於2015-02-27
測試環境架構:



  廢話不說,開始搭建:
      
     1.建立核心檔案的軟連結,如果不建立此連結編譯ipvsadm將報錯
            ln -s /usr/src/kernels/2.6.18-308.el5-x86_64/ /usr/src/linux   (2.6.18-308.el5-x86_64:核心版本

      2.下載ipvsadm
            wget
  
     3.安裝ipvsadm            
            # tar xvf ipvsadm-1.24.tar.gz -C /usr/local/src/
            # cd /usr/local/src/ipvsadm-1.24/
            # make && make install
            開啟: /etc/init.d/ipvsadm start
            如果報錯:ApplyingIPVSconfiguration:/etc/init.d/ipvsadm:line62:/etc/sysconfig/ipvsadm:沒有那個檔案或目錄
           遇到這個錯誤。執行service ipvsadm save。之後再執行service ipvsadm start

     4.
安裝keepalived的依賴包
            # yum -y install openssl-devel popt-devel libnl-devel
            # ldconfig

      5.下載keepalived
            # wget       6.
安裝keepalived
            # tar xvf keepalived-1.2.7.tar.gz -C /usr/local/src/
            # cd /usr/local/src/keepalived-1.2.7/
            # ./configure && make && make install

       7.
複製配置檔案、啟動指令碼等
            # cp /usr/local/etc/rc.d/init.d/keepalived /etc/init.d/
            # cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
            # mkdir /etc/keepalived
            # cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
            # cp /usr/local/sbin/keepalived /usr/sbin/

      8.配置keepalived主節點
            這裡使用DR模式、rr排程演算法。
            
            # vim /etc/keepalived/keepalived.conf
            ! Configuration File for keepalived
 
            #全域性定義:設定keepalived的通知機制和標識
            global_defs {
               notification_email {  #定義接收郵件的地址,一行對應一個
               qiuyx@app.moonbasagroup.com
           }
           notification_email_from admin@app.moonbasagroup.com
           smtp_server 127.0.0.1 #指定smtp伺服器地址
           smtp_connect_timeout 30
           router_id LVS_DEVEL  #定義執行keepalived機器的標識
            }
 
        #VRRP例項組定義:VRRP例項就表示在上面開啟了VRRP協議,這個例項說明了VRRP的一些特性,如主從、VRID等,可以在每個介面上開啟一個例項。
        vrrp_instance VI_1 {
                state MASTER #指定例項的初始狀態,高優先順序的會競選為master,
                interface eth0 #例項繫結的網路卡
                virtual_router_id 51 #VRID標記(0...255),MASTER和BACKUP要一致
                priority 100 #數字越高階別越高,master要高於backup至少50
                advert_int 1 #檢查間隔,預設1s
                authentication {  #設定認證
                auth_type PASS  #認證方式,支援PASS和AH
                auth_pass 1111  #認證的密碼
        }
            virtual_ipaddress {  #裡面指定漂移地址(VIP)
             192.168.0.249
        }
    }
 
    #LVS配置
    virtual_server 192.168.0.249 80 {
    delay_loop 6 #服務輪詢的延時時間
    lb_algo rr #lvs的排程演算法
    lb_kind DR #lvs叢集模式
    nat_mask 255.255.255.0
    #persistence_timeout 50 #會話保持時間(秒),測試方便取消設定
    protocol TCP #使用的協議是TCP
 
    real_server 192.168.0.235 80 {
        weight 1  #預設為1,0為失效
        TCP_CHECK { #後端伺服器健康檢查
            connect_timeout 3 #連線超時時間
            nb_get_retry 3 #重連次數
            delay_before_retry 3 #重連間隔時間(秒)
            connect_port 80
        }
    }
 
    real_server 192.168.0.244 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
      }
    }

      9.配置keepalived備節點
            
! Configuration File for keepalived
 
            global_defs {
               notification_email {
                 qiuyx@app.moonbasagroup.com
            }
           notification_email_from admin@app.moonbasagroup.com
           smtp_server 127.0.0.1
           smtp_connect_timeout 30
           router_id LVS_DEVEL
        }
 
        vrrp_instance VI_1 {
            state BACKUP  #備機使用BACKUP
            interface eth0
            virtual_router_id 51
            priority 50  #備機的優先順序要低於MASTER
            advert_int 1
            authentication {
            auth_type PASS
            auth_pass 1111
        }
        virtual_ipaddress {
             192.168.0.249
        }
    }
 
    virtual_server 192.168.0.249 80 {
    delay_loop 6
    lb_algo rr
    lb_kind DR
    nat_mask 255.255.255.0
    #persistence_timeout 50
    protocol TCP
 
    real_server 192.168.0.235 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
    }
 
    real_server 192.168.0.244 80 {
        weight 1
        TCP_CHECK {
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
            connect_port 80
        }
      }
  }

    10.重要設定
          
MASTER DR和BACKUP DR都開啟路由轉發:
             echo "1" > /proc/sys/net/ipv4/ip_forward
             固定到配置檔案:
            # vim /etc/sysctl.conf
             net.ipv4.ip_forward = 1

     11.
啟動keepalived服務(Master DR和Backup DR都啟動)
            # /etc/init.d/keepalived start
            檢視繫結的ip地址:
            # ip add show eth0
           
                  

       12.web伺服器配置
             配置虛擬ip啟動指令碼:
         #vim /etc/init.d/realserver.sh
       在檔案中輸入以下指令碼:
       #!/bin/bash
        SNS_VIP=192.168.10.100
        . /etc/rc.d/init.d/functions
        case "$1" in
        start)
         ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
         /sbin/route add -host $SNS_VIP dev lo:0
         echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore
         echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce
         echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
         echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce
         sysctl -p >/dev/null 2>&1
         echo "RealServer Start OK"
         ;;
        stop)
         ifconfig lo:0 down
         route del $SNS_VIP >/dev/null 2>&1
         echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
         echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
         echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
         echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
         echo "RealServer Stoped"
         ;;
         *)
         echo "Usage: $0 {start|stop}"
         exit 1
        esac
        exit 0

        關閉防火牆:
        service iptables stop
        啟動指令碼:
       sh /etc/init.d/realserver.sh start

 

    13.檢視lvs狀態:
            #ipvsadm -Ln
            


    至此安裝完畢,訪問192.168.0.249如期望一樣,手動停掉lvs服務 /etc/init.d/keepalived stop 測試也如期望一樣。



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29754888/viewspace-1442730/,如需轉載,請註明出處,否則將追究法律責任。

相關文章