Redhat5.8 64位LVS實例環境講解【一】

更多內容請訪問我的個人網站【Linux札記】

一、服務器環境

1、拓撲圖

lvs_01

OS:Redhat 5.8 64位系統

LVS-Master : 192.168.19.60

LVS-Backup : 192.168.19.61

LVS-VIP : 192.168.19.65

Realserver-1 : 192.168.19.62 (Apache)

Realserver-2 : 192.168.19.63 (Nginx)

Realserver-2 : 192.168.19.64 (IIS)

2、安裝LVS依賴軟件

[root@xunbang_master ~]# yum install -y gcc gcc-c++ make openssl-devel kernel-devel

注:系統採用最少化方法安裝

所有服務器關閉Selinux

[root@xunbang_master ~]# vim /etc/sysconfig/selinux
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#       enforcing - SELinux security policy is enforced.
#       permissive - SELinux prints warnings instead of enforcing.
#       disabled - SELinux is fully disabled.
SELINUX=disabled
# SELINUXTYPE= type of policy in use. Possible values are:
#       targeted - Only targeted network daemons are protected.
#       strict - Full SELinux protection.
SELINUXTYPE=targeted


 

把SELINUX=disabled保存,然後重啓服務器即可。

二、安裝ipvsadm

1、建立編譯時必須的一個軟鏈接

[root@xunbang_master ~]# ln -s /usr/src/kernels/2.6.18-308.el5-x86_64/ /usr/src/linux

(這裏如果找不到以上內核目錄,是因爲kernerl-devel沒有裝,先去安裝上就有了)

[root@xunbang_master ~]# ll /usr/src/linux

lvs_02

2、下載 ipvsadm

[root@xunbang_master ~]# wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz

lvs_03

3、解壓縮

[root@xunbang_master ~]# tar -zxvf ipvsadm-1.24.tar.gz
 
[root@xunbang_master ~]# cd ipvsadm-1.24

4、編譯安裝

[root@xunbang_master ipvsadm-1.24]# make && make install

5、確認安裝成功

[root@xunbang_master ipvsadm-1.24]# whereis ipvsadm

lvs_10

1
[root@xunbang_master ipvsadm-1.24]# ipvsadm --version

lvs_05

三、安裝keepalived

1、下載keepalived

[root@xunbang_master ~]# wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz

lvs_04

2、解壓keepalived

[root@xunbang_master ~]# tar -zxvf keepalived-1.2.2.tar.gz
[root@xunbang_master ~]# cd keepalived-1.2.2
[root@xunbang_master keepalived-1.2.2]# ./configure

lvs_09

lvs_06

(注意這個步驟要看到以上字樣纔是正常的)

 

3、安裝

[root@xunbang_master keepalived-1.2.2]# make && make install

如果報錯,如圖所示這樣

lvs_15

注意:

把#include linux/types.h /* For __beXX types in userland */

移到#include sys/types.h 這行的下面

再編譯就可以通過了。(這個可能是keepalived版本問題)

[root@xunbang_master keepalived-1.2.2]# vim keepalived/libipvs-2.6/ip_vs.h

lvs_07

lvs_08

 

4. 配置

先看下有沒有文件

lvs_14

[root@xunbang_master ~]# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
 
[root@xunbang_master ~]# cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
 
[root@xunbang_master ~]# mkdir /etc/keepalived
 
[root@xunbang_master ~]# cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
 
[root@xunbang_master ~]# cp /usr/local/sbin/keepalived /usr/sbin/
 
[root@xunbang_master ~]# chkconfig  keepalived on


[root@xunbang_master ~]# service keepalived restart

lvs_12

查看keepalived進程在不在

lvs_13

5、從 keepalived跟主keepalived服務器同樣的安裝方法,唯一不同的就是keepalived.conf配置文件不同。

四、keepalived.conf配置文件解釋

1、keepalived.conf主配置文件內容

! Configuration File for keepalived
 
global_defs {
   notification_email {
         [email protected] #表示發送通知郵件時郵件源地址是誰
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1 #表示發送email時使用的smtp服務器地址,這裏可以用本地的sendmail來實現
  # smtp_connect_timeout 30 #連接smtp連接超時時間
   router_id LVS_DEVEL
}
 
vrrp_instance VI_1 {
    state MASTER  # 備份LB將MASTER改爲BACKUP
    interface eth0 #實例綁定的網卡,因爲在配置虛擬IP的時候必須是在已有的網卡上添加的
    virtual_router_id 51  #這裏設置VRID,這裏非常重要,相同的VRID爲一個組,他將決定多播的MAC地址
    priority 100 # 優先級,備份LB值改爲比這個值小
    advert_int 1 #檢查間隔,默認爲1秒
    authentication { #這裏設置認證
        auth_type PASS #認證方式,可以是PASS或AH兩種認證方式
        auth_pass 1111 #認證密碼
    }
    virtual_ipaddress {
        192.168.19.65 # 多個VIP換行寫
    }
}
 
    virtual_server 192.168.19.65 80 {
        delay_loop 6  #每隔6秒查詢realserver狀態
        lb_algo wlc # LVS 算法
        lb_kind DR  # LVS模式 DR是直接路由
        persistence_timeout 60 #同一IP連接60秒內分配到同一臺realserver
        protocol TCP #TCP協議檢測realserver狀態
 
    real_server 192.168.19.62 80 {
        weight 3 #權重
        TCP_CHECK {
        connect_timeout 10 #10秒無響應超時
        nb_get_retry 3 #重連次數
        delay_before_retry 3 #重連間隔
        connect_port 80 #監控檢查的端口
        }
    }
    real_server 192.168.19.63 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
    real_server 192.168.19.64 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}

2、keepalived.conf從配置文件內容

! Configuration File for keepalived
 
global_defs {
   notification_email {
         [email protected]
   }
   notification_email_from [email protected]
   smtp_server 127.0.0.1
  # smtp_connect_timeout 30
   router_id LVS_DEVEL
}
 
vrrp_instance VI_1 {
    state BACKUP
    interface eth0
    virtual_router_id 51
    priority 90
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.19.65 
    }
}
 
    virtual_server 192.168.19.65 80 {
        delay_loop 6 
        lb_algo wlc 
        lb_kind DR 
        persistence_timeout 60
        protocol TCP 
 
    real_server 192.168.19.62 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 10 
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
    }
    real_server 192.168.19.63 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
    real_server 192.168.19.64 80 {
        weight 3
        TCP_CHECK {
        connect_timeout 10
        nb_get_retry 3
        delay_before_retry 3
        connect_port 80
        }
     }
}

到此主從LVS配置完成,下一篇講物理機的配置(也就是real_server)


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章