lvs負載均衡

MrUse發表於2015-03-05
lvs工作在核心中,當接收客戶端請求,並沒有將接收到的請求
送到使用者程式空間,而是直接在核心進行轉發到了後端伺服器
 
lvs根據目標請求報文的目標ip和port將其轉發至後端主機叢集中的某一個
根據排程演算法來分發至後端的某個主機,響應客戶端請求
 
tcp協議應用在核心中,lvs不需要監聽在套接字上面,檔案數就沒有限制
nginx和haproxy都要監聽在套接字,有檔案套接字限制
 
lvs支援tcp、udp、AH、EST、AH_EST、SCTP等諸多協議
 
實現lvs的NAT模型:
通過修改請求報文的的目標ip地址(同時可能會修改目標埠)至挑選
出RS的RIP地址實現轉發
1、RS應該和DIP使用私網(內網)地址,且RS的閘道器要指向DIP
2、請求和響應報文都要經由director轉發,極高的負載環境中,director
可能會成為效能瓶頸
3、支援埠對映      RS可以定義為8080
4、RS可以使用任意OS
5、RS的RIP和director的DIP必須在同一ip網路中(同一網段)
director:
  VIP:客戶端請求的IP(公網提供訪問)172.16.118.249,eth0
  DIP:使用內網與RS一起,192.168.23.2,eth1
具體配置:

[root@wadeson ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:4D:19:A6
TYPE=Ethernet
UUID=e00a22ce-9c20-4495-896f-7323b984425e
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=172.16.118.249
GATEWAY=172.16.118.254
NETMASK=255.255.255.0

[root@wadeson ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
UUID=e00a22ce-9c20-4495-896f-7323b984425e
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=192.168.23.2
NETMASK=255.255.255.0

由於需要在此主機上配置負載叢集服務,需要安裝ipvsadm:

yum -y install ipvsadm

 

後端RS配置:

  RS1:192.168.23.4,eth0

  RS2:192.168.23.5,eth1

具體配置:

到這裡,所有關於ip網路的配置已經完成了,確保RS後端伺服器的web服務已經可以正常訪問了:

[root@wadeson ~]# curl http://192.168.23.4
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# curl http://192.168.23.5
<h1>test 192.168.23.5 html</h1>

然後開始在director上建立叢集服務

[root@wadeson ~]# ipvsadm -A -t 172.16.118.249:80 -s rr

  ipvsadm -A|E -t|u|f service-address [-s scheduler]

然後新增RS到叢集中:

[root@wadeson ~]# ipvsadm -a -t 172.16.118.249:80 -r 192.168.23.4 -m   這裡RS預設的埠為80,因為

支援埠對映,所以這裡可以修改為8080

  ipvsadm -a|e -t|u|f service-address -r server-address

  [-g|i|m] [-w weight] [-x upper] [-y lower]      -m為指定為NAT模式

檢視列表資訊:

[root@wadeson ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.118.249:80 rr  

-> 192.168.23.4:80 Masq 1 0 0

再次新增一個RS:

[root@wadeson ~]# ipvsadm -a -t 172.16.118.249:80 -r 192.168.23.5 -m
[root@wadeson ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.118.249:80 rr
-> 192.168.23.4:80 Masq 1 0 0
-> 192.168.23.5:80 Masq 1 0 0

叢集服務,RS配置已經完成,然後進行訪問:

[root@wadeson ~]# curl http://172.16.118.249
<h1>test 192.168.23.5 html</h1>
[root@wadeson ~]# curl http://172.16.118.249
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# curl http://172.16.118.249
<h1>test 192.168.23.5 html</h1>
[root@wadeson ~]# curl http://172.16.118.249
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# curl http://172.16.118.249
<h1>test 192.168.23.5 html</h1>
[root@wadeson ~]# curl http://172.16.118.249
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.118.249:80 rr
-> 192.168.23.4:80 Masq 1 0 5
-> 192.168.23.5:80 Masq 1 0 5

負載均衡排程演算法:

靜態方法:僅根據演算法本身進行排程
  RR:round robin輪詢(起點公平,一人一個)
  WRR:weight RR加權的RR,能者多勞(根據權重分配)
  SH:source hash源地址hash,實現session保持
    將來自於同一ip的請求始終排程至同一RS
  DH:destination hash目標地址hash(類似去快取拿資料)
    將對同一個目標的請求始終發往同一個RS
動態方法:根據演算法及後端(RS)的當前負載狀態進行排程
  Overhand=負載,較小,就被挑選
  LC:least connection(RS的連線最小就被排程到該臺RS)
    Overhand=Active*256+Inactive
  WLC:weight least connection
    Overhand=(Active*256+Inactive)/權重
  SED:shortest expect delay最短期望延遲,演算法WLC的改進
    Overhand=(Active+1)*256/weight
  NQ:never queue,SED的改進
  LBLC:locality-based LC,即為動態的DH演算法(如果請求的是
    從來沒有快取過的資料,那麼就發往RS負載較小的機子)
    正向代理情形下的cache server排程
LBLCR:可以複製的LBLC

修改排程演算法,將rr輪詢演算法改為sh:

[root@wadeson ~]# ipvsadm -E -t 172.16.118.249:80 -s sh
[root@wadeson ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.118.249:80 sh
-> 192.168.23.4:80 Masq 1 0 0
-> 192.168.23.5:80 Masq 1 0 0

然後觀察效果:

[root@wadeson ~]# curl http://172.16.118.249:80
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# curl http://172.16.118.249:80
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# curl http://172.16.118.249:80
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# curl http://172.16.118.249:80
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# curl http://172.16.118.249:80
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# curl http://172.16.118.249:80
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.118.249:80 sh
-> 192.168.23.4:80 Masq 1 0 6      只有這一臺響應
-> 192.168.23.5:80 Masq 1 0 0

將規則儲存下來:

  ipvsadm -S > /etc/sysconfig/ipvsadm

清空規則:

  ipvsadm -C

刪除定義的叢集服務:
  [root@wadeson ~]# ipvsadm -D -t 172.16.118.249:80

  [root@wadeson ~]# ipvsadm -L -n
  IP Virtual Server version 1.2.1 (size=4096)
  Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port Forward Weight ActiveConn InActConn

ipvsadm -L:
-n:基於資料格式顯示地址和埠
-c:顯示connection狀態
--states:顯示統計的資料
--rate:顯示速率
--sort:排序
--exact:顯示精確值,不做單位換算

相關文章