1. 程式人生 > >centos下原始碼安裝LVS+ Keepalived

centos下原始碼安裝LVS+ Keepalived

LVS安裝

ipvsadm配置過程中連結檔案下無核心,先配置出核心

cdusr/src/kernel/

[root@bogonkernels]# rpm -qa|grep kernel

kernel-headers-2.6.18-194.el5

kernel-2.6.18-194.el5

[root@bogonkernels]# yum -y install kernel-devel

1)檢查核心模組,看一下ip_vs 是否被載入 


# lsmod |grep ip_vs 

ip_vs    35009    0如果沒有顯示,則說明沒有載入,執行命令 modprobe ip_vs 就可以把ip_vs模組載入到核心 

#modprobe ip_vs  

然後再檢視就有了。
2
)安裝ipvsadm 
檢視核心版本   #uname -r  
 2.6.32-573.1.1.el6.i686

先把目錄/usr/src/kernels/2.6.32-573.1.1.el6.i686連結為/usr/src/linux,命令如下 

ln -s/usr/src/kernels/2.6.32-573.1.1.el6.i686 /usr/src/linux

在usr/local/下解壓ipvsadm的安裝包

[root@localhost ~]# cd /usr/local/

[root@localhost local]# tar –zxvf ipvsadm-1.24.tar.gz

[root@localhost local]# cd ipvsadm-1.24

[root@localhost ipvsadm-1.24]# make

解壓ipvsadm-1.24.tar.gz,執行"make;但是報錯了,類似的錯誤資訊大概如下
make -C libipvs
make[1]: Entering directory`/usr/src/ipvsadm-1.26/libipvs'
gcc -Wall -Wunused -Wstrict-prototypes -g -fPIC-DLIBIPVS_USE_NL  -DHAVE_NET_IP_VS_H -c -o libipvs.o libipvs.c
In file included from libipvs.h:13,
                from libipvs.c:23:
ip_vs.h:15:29: error: netlink/netlink.h: Nosuch file or directory
ip_vs.h:16:31: error: netlink/genl/genl.h: Nosuch file or directory
ip_vs.h:17:31: error: netlink/genl/ctrl.h: Nosuch file or directory
In file included from libipvs.h:13,
                from libipvs.c:23:
ip_vs.h:520: error: array type has incompleteelement type
ip_vs.h:521: error: array type has incompleteelement type
libipvs.c:57: warning: implicit declaration offunction ‘nlmsg_alloc’
libipvs.c:57: warning: assignment makes pointerfrom integer without a cast
libipvs.c:61: warning: implicit declaration offunction ‘genlmsg_put’
libipvs.c:61: error: ‘NL_AUTO_PID’ undeclared(first use in this function)
libipvs.c:61: error: (Each undeclaredidentifier is reported only once
libipvs.c: In function ‘ipvs_add_service’:
libipvs.c:255: error: too many arguments tofunction ‘ipvs_nl_send_message’
libipvs.c: In function ‘ipvs_update_service’:
libipvs.c:276: error: too many arguments tofunction ‘ipvs_nl_send_message’
libipvs.c: In function ‘ipvs_del_service’:
libipvs.c:296: error: too many arguments tofunction ‘ipvs_nl_send_message’
libipvs.c: In function ‘ipvs_zero_service’:
libipvs.c:473: warning: assignment makespointer from integer without a cast
libipvs.c:483: error: too many arguments to function‘ipvs_nl_send_message’
libipvs.c: In function ‘ipvs_stop_daemon’:
libipvs.c:504: warning: assignment makespointer from integer without a cast
libipvs.c:514: error: too many arguments tofunction ‘ipvs_nl_send_message’
libipvs.c: At top level:
libipvs.c:1051: error: ‘NL_OK’ undeclared(first use in this function)
libipvs.c: In function ‘ipvs_get_daemon’:
libipvs.c:1071: error: ‘NLM_F_DUMP’ undeclared(first use in this function)
libipvs.c:1072: error: too many arguments tofunction ‘ipvs_nl_send_message’
make[1]: *** [libipvs.o] Error 1
make[1]: Leaving directory`/usr/src/ipvsadm-1.26/libipvs'
make: *** [libs] Error 2


 
解決[root@localhost ipvsadm-1.24]#yuminstall libnl* libpopt*再次make,還是有報錯類似如 /usr/src/ipvsadm-1.26/ipvsadm.c:667: undefined reference to `poptStrerror'
/usr/src/ipvsadm-1.26/ipvsadm.c:667: undefinedreference to `poptBadOption'
/usr/src/ipvsadm-1.26/ipvsadm.c:670: undefinedreference to `poptFreeContext'
/usr/src/ipvsadm-1.26/ipvsadm.c:677: undefinedreference to `poptGetArg'
/usr/src/ipvsadm-1.26/ipvsadm.c:678: undefinedreference to `poptGetArg'
/usr/src/ipvsadm-1.26/ipvsadm.c:679: undefinedreference to `poptGetArg'
/usr/src/ipvsadm-1.26/ipvsadm.c:690: undefinedreference to `poptGetArg'
/usr/src/ipvsadm-1.26/ipvsadm.c:693: undefinedreference to `poptFreeContext'
collect2: ld returned 1 exit status
make: *** [ipvsadm] Error 1

解決[root@localhost ipvsadm-1.24]# yuminstall    popt-static (ftp://ftp.muug.mb.ca/mirror/centos/6.3/os/x86_64/Packages/popt-static-1.13-7.el6.x86_64.rpm再次

[root@localhost ipvsadm-1.24]# make

通過,然後

[root@localhost ipvsadm-1.24]# makeinstll 

完成安裝

安裝 配置keepalived 

./configure會出現OPENSSL錯誤,

所以首先

[root@localhost~]#yum install -y openssl openssl-devel

然後執行下面的命令:

[root@localhost~]# cd /usr/local

[root@localhostlocal]# tar –zxvf keepalived-1.2.18.tar.gz

[root@localhostlocal]# cd keepalived-1.2.18

[[email protected]]# ./configure

[[email protected]]# make

[[email protected]]# make install

完成安裝

配置keepalived

# cp/usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/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/

(1)DR1上配置keepalived:

最簡單的方法:先在本地記事本中,手工建立keepalived.conf上傳到/etc/keepalived/keepalived.conf中,替換原有即可!

!Configuration File for keepalived

global_defs{

   notification_email {

     [email protected]   #設定報警郵件地址,可以設定多個,每行1個,

     [email protected]  #需開啟郵件報警及本機的Sendmail服務。

     [email protected]

   }

   [email protected]

   smtp_server 192.168.200.1  #設定SMTPServer地址;

   smtp_connect_timeout 30

   router_id LVS_DEVEL

}

           ########VRRP Instance########

vrrp_instanceVI_1 {

    state MASTER    #指定Keepalived的角色,MASTER為主機伺服器,BACKUP為備用伺服器

    interface eth0  #BACKUP為備用伺服器

    virtual_router_id 51

    priority 100    #定義優先順序,數字越大,優先順序越高,主DR必須大於備用DR。

    advert_int 1

    authentication {

        auth_type PASS  #設定驗證型別,主要有PASS和AH兩種

        auth_pass 1111  #設定驗證密碼

    }

    virtual_ipaddress {

        192.168.0.200  #設定主DR的虛擬IP地址(virtualIP),可多設,但必須每行1個

    }

}

           ########Virtual Server########

virtual_server192.168.0.200 80 {  #注意IP地址與埠號之間用空格隔開

    delay_loop 6        #設定健康檢查時間,單位是秒

    lb_algo rr          #設定負載排程演算法,預設為rr,即輪詢演算法,最優秀是wlc演算法

    lb_kind DR          #設定LVS實現LB機制,有NAT、TUNN和DR三個模式可選

    nat_mask 255.255.255.0

    persistence_timeout 50  #會話保持時間,單位為秒

    protocol TCP        #指定轉發協議型別,有TCP和UDP兩種

    real_server 192.168.0.181 80 {

        weight 1          #配置節點權值,數字越大權值越高

        TCP_CHECK {

            connect_timeout 3     #表示3秒無響應,則超時

            nb_get_retry 3        #表示重試次數

            delay_before_retry 3  #表示重試間隔

        }

    }

    real_server 192.168.0.141 80 {  #配置伺服器節點,即Real Server2的public IP

        weight 3            #配置節點權值,數字越大權值越高

        TCP_CHECK {

            connect_timeout 3       #表示3秒無響應,則超時

            nb_get_retry 3          #表示重試次數

            delay_before_retry 3    #表示重試間隔

        }

    }

}//啟動哈keepalived、設定keepalived服務自動啟動、檢查是否生效,如下圖所示:

(2)DR2上配置keepalived:

先將上面記事本中的keepalived.conf配置檔案,僅僅修改2處,如下圖所示:

No.1:把“stateMASTER”修改為“stateBACKUP” #即備用伺服器;

No.2:把“priority 100”修改為“priority 90” #優先順序為90。

修改好後,複製到DR2/etc/keepalived/keepalived.conf中,儲存即可!

//啟動哈keepalived、設定keepalived服務自動啟動、檢查是否生效,如下圖所示:


 參考文章:http://beyondhdf.blog.51cto.com/229452/1331874

http://www.07net01.com/linux/ipvsadm_keepalivedanzhuangcuowujiejue_73067_1361863388.html

http://rensanning.iteye.com/blog/2202718