天天看點

Linux伺服器負載均衡LB Cluster的LVS(NAT,DR)

一 基礎知識:

1 群集種類:

1.1 LB 負載均衡;解決大量的并發的連接配接請求;

    scale on(單機處理能力的增強)

    scale out

1.2 HA 高可用性群集;應用于企業網中高可用性體系,提供時時的線上服務99.999%能夠正常運作;

1.3 HP 高性能群集;應用于大量的複雜的計算時使用;

2 群集分發裝置(director dispather):将請求分發到群集中的各個主機中

2.1 從表現形式上劃分:

2.1.1 通過硬體實作(F5)

2.1.2 通過軟體(比如Linux中的2.4、2.6、ipvs[ipvsdm]-->lvs)

2.2 從層次上劃分:

2.2.1 四層交換:協定  端口号【包括:F5、lvs】

2.2.2 七層交換:squid  nginx

3 為了實作LVS-DR模型需要解決相關問題:

3.1 每個real server上都需要配置vip;vip配置在lookback接口上;

3.2 解決多個相同vip導緻的arp應答混亂;

3.3 通過arp_arpannounce和arp_ignore來控制realserver的arp應答;

3.4 解決realserver的應答封包的源位址,源位址應該為VIP;

4 LVS三種模型:

4.1 LVS-NAT模型:

    特點:VS-NAT 最易于配置 ; 可擴充性差 ;所有流量都要經過分發裝置,容易形成瓶頸;

    資料包流向:CIP-DGW-VIP-RIP-DIP-DGW-CIP

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349794993Inr2.png"></a>

4.2 LVS-DR模型:直接路由模型;

    特點:能夠處理比nat模型更過的節點;

    資料包流向:CIP-DGW-VIP-RIP-DGW-CIP

    Rip和dip需在同一個網絡中;

    Realserver的網關不再是rip;

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_13497949933n4K.png"></a>

4.3 LVS-TUN:隧道模型;

    特點:可以誇廣播域,即不再同一區域網路内;可擴充性好;僅修改請求進入IP;

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349794994OzYv.png"></a>

5 ipvsadmin 常用指令: 

           ipvsadm -Ln           檢視分發規測和分發情況 

           ipvsadm -Ln -c         檢視連接配接狀态 

           ipvsadm -L -n --rate    檢視目前連接配接數 

           ipvsadm -L -n --stats    檢視總連接配接數 

           ipvsadm -C            清空規則

6 用戶端的arp控制:

6.1 詳解arp_arpannounce:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349794994l2N7.png"></a>

    //将使用2  integer;

6.2 詳解arp_ignore:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_13497949953Nt7.png"></a>

    //将使用1  integer;

二 案例一:實作基于LVS-NAT模型的伺服器群集

1 拓撲圖:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349794995Mwxj.png"></a>

2 案例說明:

    實作基于LVS-NAT模型的伺服器群集,采用輪訓排程(RR);

3 配置步驟:

3.1 配置realserver1

3.1.1 網絡參數:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349794996xYIf.png"></a>

3.1.2 重新開機網絡服務:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349794996X9vb.png"></a>

3.1.3 挂在CD光牒:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_134979499661D7.png"></a>

3.1.4 安裝realserver1的httpd服務:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349794997Y6VA.png"></a>

3.1.5 建立realserver1的測試頁:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_13497949979ogg.png"></a>

3.1.6 啟動httpd服務:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349794997H9Xw.png"></a>

3.2 配置realserver2

3.2.1 網絡參數:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_134979499877eU.png"></a>

3.2.2 重新開機網絡服務:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349794998WcmG.png"></a>

3.2.3 挂在CD光牒:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349794999oEtZ.png"></a>

3.2.4 安裝realserver1的httpd服務:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_13497949998NuH.png"></a>

3.2.5 建立realserver1的測試頁:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349794999Tj3v.png"></a>

3.2.6 啟動httpd服務:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795000q5vU.png"></a>

3.3 配置director群集伺服器

3.3.1 網絡參數:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795000mrNI.png"></a>

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795001bHBW.png"></a>

3.3.2 重新開機網絡服務:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795001iEPs.png"></a>

3.3.3 打開路由轉發功能:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795004EcV6.png"></a>

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_13497950050giF.png"></a>

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795005yGeA.png"></a>

3.3.4 挂在CD光牒:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795006N65a.png"></a>

3.3.5 安裝ipvs管理工具:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795006AqPo.png"></a>

3.3.6 執行一下指令産生ipvsadm規則檔案:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795006mt7B.png"></a>

3.3.7 啟動ipvsadm:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795007WlyS.png"></a>

3.3.8 添加ipvs規則:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795007nh8s.png"></a>

3.3.9 将ipvsadm規則寫入ipvsadm檔案中:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795008UTqY.png"></a>

3.4 測試:

3.4.1 使用http通路192.168.10.100

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795008hVdR.png"></a>

3.4.2 重新整理後:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795012gSux.png"></a>

三 案例二“實作基于LVS-DR模型的伺服器群集

1 拓撲圖

<a href="http://blog.51cto.com/attachment/201210/204649975.jpg" target="_blank"></a>

2 案例說明

實作基于LVS-DR模型的伺服器群集,采用輪訓排程(RR);

3 配置步驟

3.1.1 在系統控制檔案sysctl.conf中添加arp核心參數:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795016RQIm.png"></a>

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795016UnX8.png"></a>

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795017YxdA.png"></a>

3.1.2 在eth0網卡上配置RIP:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795023gsKQ.png"></a>

3.1.3 在loopback接口上配置VIP:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795026dusf.png"></a>

3.1.4 重新開機網絡服務:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795027YzZQ.png"></a>

3.1.5 添加路由:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795027Qk1c.png"></a>

//為了實作使用vip來對通路進行應答,需要使從realserver1到任何位址的資料包從lo:0的接口使用vip發送;

3.1.6 挂在CD光牒:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795027e5qG.png"></a>

3.1.7 安裝realserver1的httpd服務:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795028wJlP.png"></a>

3.1.8 建立realserver1的測試頁:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795037aXjk.png"></a>

3.1.9 啟動httpd服務:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_13497950411Sjb.png"></a>

3.2.1 在系統控制檔案sysctl.conf中添加arp核心參數:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795041wyQm.png"></a>

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795042b57A.png"></a>

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795042Ubd9.png"></a>

3.2.2 在eth0網卡上配置RIP:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795042689U.png"></a>

3.2.3 在loopback接口上配置VIP:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795043h8jk.png"></a>

3.2.4 重新開機網絡服務:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795043hv2Z.png"></a>

3.2.5 添加路由:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795044dV7I.png"></a>

3.2.6 挂在CD光牒:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795047v52C.png"></a>

3.2.7 安裝realserver1的httpd服務:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_13497950538prY.png"></a>

3.2.8 建立realserver1的測試頁:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795053rTea.png"></a>

3.2.9 啟動httpd服務:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_13497950573RH2.png"></a>

3.3.1 配置director的RIP:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795060IhPI.png"></a>

3.3.2 配置director的VIP:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795060GMac.png"></a>

3.3.3 重新開機網絡服務:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795061p4BC.png"></a>

3.3.4 挂載CD光牒:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795064ienL.png"></a>

3.3.5 安裝ipvsadm服務:

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795064VN0g.png"></a>

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795065ev0Q.png"></a>

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795065Qvg9.png"></a>

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795066Gvsc.png"></a>

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795066YhhP.png"></a>

3.4.1 使用http通路192.168.10.10

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795066RXaY.png"></a>

<a href="http://cexpert.blog.51cto.com/attachment/201210/9/5251990_1349795067hDlm.png"></a>

四 案例三“實作基于LVS-DR模型的伺服器群集,并實作基于NFS的簡單存儲“

1 拓撲圖

<a href="http://blog.51cto.com/attachment/201210/205146740.jpg" target="_blank"></a>

2 案例說明

Web伺服器的頁面檔案從NFS網絡檔案系統中讀取

3 配置步驟:基于案例2的群集模型,隻是在web服務中加入了存儲;

3.1 配置nfs伺服器:

3.1.1 網絡配置:

# vim /etc/sysconfig/network-scripts/ifcfg-eth0

  1 # Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]

  2 DEVICE=eth0

  3 BOOTPROTO=none

  4 ONBOOT=yes

  5 HWADDR=00:0c:29:58:6b:94

  6 NETMASK=255.255.255.0

  7 IPADDR=192.168.10.103

  8 TYPE=Ethernet

3.1.2 重新開機網絡服務:

# service network restart

3.2 NFS服務是預設安裝的,使用時直接啟動即可;

3.2.1 啟動nfs服務:

# service nfs start

3.2.2 建立一個共享檔案和測試頁面:

# mkdir /public

# echo 'from nfs server!' &gt;/public/index.html

3.2.3 編寫共享清單:

# vim /etc/exports

    1 /public 192.168.10.0/24(ro)

3.2.4 導出共享清單:

# exportfs -rv

exporting 192.168.10.0/24:/public

-r 導出;

-v 詳細資訊;

3.3 在realserver1伺服器上進行nfs用戶端配置:

3.3.1 編輯自動挂載檔案:

# vim /etc/auto.master

  9 /misc   /etc/auto.misc

 10 /var/www /etc/auto.htmount --timeout

3.3.2 建立并編輯nfs共享檔案挂載點的監控檔案:

# cp -p /etc/auto.misc /etc/auto.htmount

# vim /etc/auto.htmount

 18 html            -ro,soft,intr           192.168.10.103:/public

3.3.3 重新開機autofs服務:

# service autofs restart

3.3.4 重新開機httpd服務:

# service httpd restart

3.3.5 檢視目前挂載情況:

[root@localhost ~]# mount

/dev/sda2 on / type ext3 (rw)

proc on /proc type proc (rw)

sysfs on /sys type sysfs (rw)

devpts on /dev/pts type devpts (rw,gid=5,mode=620)

/dev/sda1 on /boot type ext3 (rw)

tmpfs on /dev/shm type tmpfs (rw)

none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)

sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)

/dev/hdc on /mnt/cdrom type iso9660 (ro)

192.168.10.103:/public on /var/www/html type nfs (ro,timeout,soft,intr,addr=192.168.10.103)

//此時httpd的主配置檔案成功挂載;

3.4 在realserver2伺服器上進行nfs用戶端配置:

3.4.1 編輯自動挂載檔案:

3.4.2 建立并編輯nfs共享檔案挂載點的監控檔案:

3.4.3 重新開機autofs服務:

3.4.4 重新開機httpd服務:

3.4.5 檢視目前挂載情況:

3.5 測試:

<a href="http://blog.51cto.com/attachment/201210/205210771.png" target="_blank"></a>

測試成功!!

本文轉自 cexpert 51CTO部落格,原文連結:http://blog.51cto.com/cexpert/1020033

繼續閱讀