基於LVS-DR的WEB服務的負載均衡實現

LVS-DR架構圖html

clip_p_w_picpath002

都是一塊網卡前端

Director: DIP:eth0: 192.168.1.3 網關web

VIP:eth0:1: 192.168.1.6算法

RealServer1:RIP1:eth0:192.168.1.7apache

VIP:lo:0:192.168.1.6vim

RealServer2:RIP2:eth0:192.168.1.8bash

VIP:lo:0:192.168.1.6服務器

Client:eth0: 192.168.1.250cookie

原理以下:網絡

1. 用戶請求直達Director的VIP地址,Director根據算法從Realserver列表中選取下一個Realserver,並將數據包轉發給它, 整個過程當中源地址CIP,目標地址VIP不變;

2. Realserver響應數據包再也不通過Director直接返回客戶端電腦,同時返回數據包的源地址VIP,目標地址CIP;

3.每一臺Realserver都必須配置同一VIP地址,所以在同一網絡中,全部廣播包會到任意一臺主機,但數據包經過路由器的內網關及VIP地址時是經過MAC地址實現的,兩者通訊時必須發起一次ARP廣播以解析VIP的MAC地址, 因此任何一臺配置VIP地址的主機都能收到一份ARP廣播請求,對於Linux而言,地址屬於系統,而非網卡。只要系統上有VIP地址,就會響應ARP廣播。所以路由器內網關會收到4個不一樣MAC地址的響應,響應最快的可能正確包但被後面的錯誤包給沖刷了,沒法獲知哪一個是正確的響應包。形成誰響應最慢誰的數據包反而生效,在必定時間內再也不發起ARP廣播,這段時間內只響應來自此MAC地址的數據包,失去負載均衡的意義。

4. realserver應該以VIP爲源地址響應數據包,但不響應對VIP地址的ARP廣播請求。這樣才能保證前端客戶端請求一旦被路由器內網關ARP廣播,只有director的VIP響應。director在轉發數據包時IP地址不變,源MAC修改成DIP,直接修改目標MAC地址爲RIP MAC。realserver解包發現源地址CIP,目標地址VIP,處理完成返回數據包時,源IP是VIP,目標IP是CIP,源MAC是RIP MAC,目標MAC是CIP MAC。CIP MAC沒法接受來自內網的RIP MAC的響應。因此realserver上需另做額外配置,訪問時以那個VIP作目標地址,響應時必須以此地址作響應。

5.對realserver所須要配置:

   1. 隔離對VIP的ARP請求作響應,修改內核參數 lo:0接口。

   2. 任何對目標地址爲VIP,響應的源地址也必須是VIP 。設置特定路由。

6.對Director所須要配置:

   1. 只開放對VIP的ARP請求作響應

   2. 肯定只有用戶請求的地址是VIP eth0:1 時才轉發,對DIP eth0 不轉發。VIP:80 和DIP:80 是兩個徹底不一樣套接字,能夠共存。特殊路由

1.1 Director配置

圖形界面setup配置直觀但沒有文本有思路。再也不累述

DIP:eth0: 192.168.1.3 網關

[root@director ~]# vim /etc/hosts

127.0.0.1 localhost.localdomain localhost

192.168.1.3 director.example.com director

::1 localhost6.localdomain6 localhost6

[root@director ~]# vim /etc/sysconfig/network

NETWORKING=yes

NETWORKING_IPV6=no

HOSTNAME=director.example.com

GATEWAY=192.168.1.3 網關

[root@director ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 

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

DEVICE=eth0

BOOTPROTO=static

BROADCAST=192.168.1.255

HWADDR=00:0C:29:BB:02:FD

IPADDR=192.168.1.3

NETMASK=255.255.255.0

NETWORK=192.168.1.0

ONBOOT=yes

VIP:eth0:1: 192.168.1.6

[root@director ~]# cd /etc/sysconfig/network-scripts/

[root@director network-scripts]# cp ifcfg-eth0 ifcfg-eth0:1 複製網卡別名配置文件

[root@director network-scripts]# vim ifcfg-eth0:1

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

DEVICE=eth0:1 系統是照DEVICE名稱識別,因此必須是eth0:1,圖形化setup中也必須區別設置

BOOTPROTO=static

BROADCAST=192.168.1.255

HWADDR=00:0C:29:BB:02:FD

IPADDR=192.168.1.6

NETMASK=255.255.255.0

NETWORK=192.168.1.0

ONBOOT=yes

[root@director network-scripts]# service network restart

[root@director network-scripts]# ifconfig

eth0 Link encap:Ethernet HWaddr 00:0C:29:BB:02:FD

inet addr:192.168.1.3 Bcast:192.168.1.255 Mask:255.255.255.0

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:959 errors:0 dropped:0 overruns:0 frame:0

TX packets:758 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000 

RX bytes:86349 (84.3 KiB) TX bytes:114489 (111.8 KiB)

Interrupt:67 Base address:0x2024 

eth0:1 Link encap:Ethernet HWaddr 00:0C:29:BB:02:FD

inet addr:192.168.1.6 Bcast:192.168.1.255 Mask:255.255.255.0

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

Interrupt:67 Base address:0x2024 

lo Link encap:Local Loopback

inet addr:127.0.0.1 Mask:255.0.0.0

UP LOOPBACK RUNNING MTU:16436 Metric:1

RX packets:34 errors:0 dropped:0 overruns:0 frame:0

TX packets:34 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0 

RX bytes:2616 (2.5 KiB) TX bytes:2616 (2.5 KiB)

容許數據包轉發

[root@director ~]# vim /etc/sysctl.conf

net.ipv4.ip_forward = 1

[root@director ~]# service network restart 重啓網絡生效

[root@director ~]# sysctl -p

net.ipv4.ip_forward = 1

net.ipv4.conf.default.rp_filter = 1

net.ipv4.conf.default.accept_source_route = 0

kernel.sysrq = 0

kernel.core_uses_pid = 1

net.ipv4.tcp_syncookies = 1

kernel.msgmnb = 65536

kernel.msgmax = 65536

kernel.shmmax = 4294967295

kernel.shmall = 268435456

檢查默認路由

[root@director ~]# route -n

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0 路由進入

169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0

0.0.0.0 192.168.1.3 0.0.0.0 UG 0 0 0 eth0 默認路由出去

添加特殊路由

肯定只有用戶請求的地址是VIP eth0:1 時才轉發,對DIP eth0 不轉發。VIP:80 和DIP:80 是兩個徹底不一樣套接字,能夠共存。

[root@director ~]# route add -host 192.168.1.6 dev eth0:1 臨時添加的靜態路由,重啓網絡後無效

[root@director ~]# route -n

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

192.168.1.6 0.0.0.0 255.255.255.255 UH 0 0 0 eth0

192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0

0.0.0.0 192.168.1.3 0.0.0.0 UG 0 0 0 eth0

[root@director ~]# service network restart

[root@director ~]# route -n

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0

0.0.0.0 192.168.1.3 0.0.0.0 UG 0 0 0 eth0

保存永久路由

保存路由設置,使其在網絡重啓後任然有效

[root@director ~]# vim /etc/sysconfig/static-routes

any host 192.168.1.6 gw 192.168.1.6 

[root@director sysconfig]# service network restart

[root@director sysconfig]# route -n

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

192.168.1.6 192.168.1.6 255.255.255.255 UGH 0 0 0 eth0

192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0

0.0.0.0 192.168.1.3 0.0.0.0 UG 0 0 0 eth0

安裝集羣服務

[root@director ~]# yum -y install ipvsadm

ipvsadm.i386 0:1.24-13.el5

在director上配置集羣服務

[root@director ~]# ipvsadm –C 清空ipvs表

[root@director ~]# ipvsadm –ln 十進制地址顯示錶

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

[root@director ~]# ipvsadm -A -t 192.168.1.6:80 -s rr

-A 添加虛擬服務 -t tcp $ip:$port -s scheduler 調度算法 rr 輪調

[root@director ~]# ipvsadm -a -t 192.168.1.6:80 -r 192.168.1.7 -w 1 -g

[root@director ~]# ipvsadm -a -t 192.168.1.6:80 -r 192.168.1.8 -w 1 -g

-a 添加真實服務器 -t tcp $ip:$port –r 真實服務器 –w 權重

-g --gateway 指定LVS 的工做模式爲直接路由模式(也是LVS 默認的模式),不寫也不要緊

[root@director ~]# ipvsadm -ln

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP 192.168.1.6:80 rr

-> 192.168.1.8:80 Route 1 0 0

-> 192.168.1.7:80 Route 1 0 0

[root@director ~]# service ipvsadm save

ipvsadm: Saving IPVS table to /etc/sysconfig/ipvsadm: [ OK ]

[root@director ~]# service ipvsadm restart

ipvsadm: Clearing the current IPVS table: [ OK ]

ipvsadm: Unloading modules: [ OK ]

ipvsadm: Clearing the current IPVS table: [ OK ]

ipvsadm: Applying IPVS configuration: [ OK ]

配置realserver, 對於任何一臺realserver都要先隔離arp廣播,再配置地址, 次序必定不能亂。不然失效。

配置RIP1

先清除原始網卡配置

[root@rip1 ~]# vim /etc/hosts

#192.168.1.7 rip1.example.com rip1

[root@rip1 ~]# vim /etc/sysconfig/network

#GATEWAY=192.168.1.3

[root@rip1 ~]# cd /etc/sysconfig/network-scripts/

[root@rip1 network-scripts]# mv ifcfg-eth0 ifcfg-eth0.bak

[root@rip1 network-scripts]# mv ifcfg-lo ifcfg-lo.bak

配置arp通告與忽略規則隔離arp廣播

[root@rip1 ~]# echo "net.ipv4.conf.all.arp_announce = 2" >> /etc/sysctl.conf 

[root@rip1 ~]# echo "net.ipv4.conf.lo.arp_announce = 2" >> /etc/sysctl.conf 

[root@rip1 ~]# echo "net.ipv4.conf.all.arp_ignore = 1" >> /etc/sysctl.conf 

[root@rip1 ~]# echo "net.ipv4.conf.lo.arp_ignore = 1" >> /etc/sysctl.conf

[root@rip1 ~]# sysctl -p

net.ipv4.conf.all.arp_announce = 2

#對查詢目標使用最適當的本地地址響應。在此模式下將忽略這個IP數據包的源地址並嘗試選擇與能與該地址通訊的本地地址.首要是選擇全部的網絡接口的子網中外出訪問子網中包含該目標IP地址的本地地址. 若是沒有合適的地址被發現,將選擇當前的發送網絡接口或其餘的有可能接受到該ARP迴應的網絡接口來進行發送。

net.ipv4.conf.lo.arp_announce = 2

net.ipv4.conf.all.arp_ignore = 1 #只響應目標IP地址是來訪網絡接口本地地址的ARP查詢請求

net.ipv4.conf.lo.arp_ignore = 1

**************************************************************************************

3.1

echo "1" >/proc/sys/net/ipv4/conf/lo/arp_ignore

echo "2" >/proc/sys/net/ipv4/conf/lo/arp_announce 

這兩條是能夠不用的,由於arp對邏輯接口沒有意義。

3.2 若是你的RealServer的外部網絡接口是eth0,那麼

echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore

echo "2" >/proc/sys/net/ipv4/conf/all/arp_announce 

其實真正要執行的是:

echo "1" >/proc/sys/net/ipv4/conf/eth0/arp_ignore

echo "2" >/proc/sys/net/ipv4/conf/eth0/arp_announce 

因此我我的建議把上面兩條也加到你的腳本里去,由於萬一系統裏上面兩條默認的值不是0,那有多是會出問題。

************************************************************************************

配置地址

[root@rip1 ~]# vim /etc/hosts

# Do not remove the following line, or various programs

# that require network functionality will fail.

127.0.0.1 localhost.localdomain localhost

192.168.1.7 rip1.example.com rip1

::1 localhost6.localdomain6 localhost6

[root@rip1 ~]# vim /etc/sysconfig/network

NETWORKING=yes

NETWORKING_IPV6=no

HOSTNAME=rip1.example.com

GATEWAY=192.168.1.3

[root@rip1 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 

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

DEVICE=eth0

BOOTPROTO=static

BROADCAST=192.168.1.255

HWADDR=00:0C:29:BB:02:F1

IPADDR=192.168.1.7

NETMASK=255.255.255.0

NETWORK=192.168.1.0

ONBOOT=yes

配置VIP:lo:0 IP地址:

[root@r1~]# ifconfig lo:0 192.168.1.6 broadcast 192.168.1.6 netmask 255.255.255.255 up 臨時性配置

永久配置

[root@rip1 network-scripts]# cp ifcfg-lo ifcfg-lo:0

[root@rip1 network-scripts]# vim ifcfg-lo:0

DEVICE=lo:0

IPADDR=192.168.1.6

NETMASK=255.255.255.255

# If you're having problems with gated making 127.0.0.0/8 a martian,

# you can change this to something else (255.255.255.255, for example)

BROADCAST=192.168.1.6

GATEWAY=192.168.1.6

ONBOOT=yes

NAME=loopback

[root@rip1 ~]# service network restart

[root@rip1 ~]# ifconfig

eth0 Link encap:Ethernet HWaddr 00:0C:29:BB:02:F1

inet addr:192.168.1.7 Bcast:192.168.1.255 Mask:255.255.255.0

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:2597 errors:0 dropped:0 overruns:0 frame:0

TX packets:1759 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000 

RX bytes:230523 (225.1 KiB) TX bytes:230795 (225.3 KiB)

Interrupt:67 Base address:0x2024 

lo Link encap:Local Loopback

inet addr:127.0.0.1 Mask:255.0.0.0

UP LOOPBACK RUNNING MTU:16436 Metric:1

RX packets:12 errors:0 dropped:0 overruns:0 frame:0

TX packets:12 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0 

RX bytes:760 (760.0 b) TX bytes:760 (760.0 b)

lo:0 Link encap:Local Loopback

inet addr:192.168.1.6 Mask:255.255.255.255

UP LOOPBACK RUNNING MTU:16436 Metric:1

添加特殊路由,臨時設置

[root@rip1 ~]# route add -host 192.168.1.6 dev lo:0

等於realserver上對虛擬vip的路由經過lo:0環回給本身,不對外作響應

[root@rip1 ~]# route –n 尚未顯示在路由表

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

192.168.1.6 0.0.0.0 255.255.255.255 UH 0 0 0 lo

192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0

0.0.0.0 192.168.1.3 0.0.0.0 UG 0 0 0 eth0

保存永久路由

保存路由設置,使其在網絡重啓後任然有效

[root@rip1 ~]# vim /etc/sysconfig/static-routes

any host 192.168.1.6 gw 192.168.1.6

[root@rip1~]# service network restart

[root@rip1 ~]# route -n

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

192.168.1.6 192.168.1.6 255.255.255.255 UGH 0 0 0 lo

192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0

0.0.0.0 192.168.1.3 0.0.0.0 UG 0 0 0 eth0

[root@rip1 ~]# echo "web1" > /var/www/html/index.html

[root@rip1 ~]# service httpd start

Starting httpd: [ OK ]

[root@rip1 ~]# netstat -ntlp | grep 80

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7802/httpd

clip_p_w_picpath002[5]

配置RIP2

先清除原始網卡配置

[root@rip2 ~]# vim /etc/hosts

#192.168.1.8 rip2.example.com rip2

[root@rip2 ~]# vim /etc/sysconfig/network

#GATEWAY=192.168.1.3

[root@rip2 ~]# cd /etc/sysconfig/network-scripts/

[root@rip2 network-scripts]# mv ifcfg-eth0 ifcfg-eth0.bak

[root@rip2 network-scripts]# mv ifcfg-lo ifcfg-lo.bak

配置arp通告與忽略規則隔離廣播

[root@rip2 ~]# echo "net.ipv4.conf.all.arp_announce = 2" >> /etc/sysctl.conf 

[root@rip2 ~]# echo "net.ipv4.conf.lo.arp_announce = 2" >> /etc/sysctl.conf 

[root@rip2 ~]# echo "net.ipv4.conf.all.arp_ignore = 1" >> /etc/sysctl.conf 

[root@rip2 ~]# echo "net.ipv4.conf.lo.arp_ignore = 1" >> /etc/sysctl.conf

[root@rip2 ~]# sysctl -p

net.ipv4.conf.all.arp_announce = 2

net.ipv4.conf.lo.arp_announce = 2

net.ipv4.conf.all.arp_ignore = 1

net.ipv4.conf.lo.arp_ignore = 1

配置地址

[root@rip2 ~]# vim /etc/hosts

# Do not remove the following line, or various programs

# that require network functionality will fail.

127.0.0.1 localhost.localdomain localhost

192.168.1.8 rip2.example.com rip2

::1 localhost6.localdomain6 localhost6

[root@rip2 ~]# vim /etc/sysconfig/network

NETWORKING=yes

NETWORKING_IPV6=no

HOSTNAME=rip2.example.com

GATEWAY=192.168.1.3

[root@rip2 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 

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

DEVICE=eth0

BOOTPROTO=static

BROADCAST=192.168.1.255

HWADDR=00:0C:29:BB:02:F2

IPADDR=192.168.1.8

NETMASK=255.255.255.0

NETWORK=192.168.1.0

ONBOOT=yes

配置VIP:lo:0 IP地址:

[root@rip2~]# ifconfig lo:0 192.168.1.6 broadcast 192.168.1.6 netmask 255.255.255.255 up 臨時性配置

永久配置

[root@rip2 network-scripts]# cp ifcfg-lo ifcfg-lo:0

[root@rip2 network-scripts]# vim ifcfg-lo:0

DEVICE=lo:0

IPADDR=192.168.1.6

NETMASK=255.255.255.255

# If you're having problems with gated making 127.0.0.0/8 a martian,

# you can change this to something else (255.255.255.255, for example)

BROADCAST=192.168.1.6

GATEWAY=192.168.1.6

ONBOOT=yes

NAME=loopback

[root@rip2 ~]# service network restart

[root@rip2 ~]# ifconfig

eth0 Link encap:Ethernet HWaddr 00:0C:29:BB:02:F1

inet addr:192.168.1.7 Bcast:192.168.1.255 Mask:255.255.255.0

UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1

RX packets:2597 errors:0 dropped:0 overruns:0 frame:0

TX packets:1759 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:1000 

RX bytes:230523 (225.1 KiB) TX bytes:230795 (225.3 KiB)

Interrupt:67 Base address:0x2024 

lo Link encap:Local Loopback

inet addr:127.0.0.1 Mask:255.0.0.0

UP LOOPBACK RUNNING MTU:16436 Metric:1

RX packets:12 errors:0 dropped:0 overruns:0 frame:0

TX packets:12 errors:0 dropped:0 overruns:0 carrier:0

collisions:0 txqueuelen:0 

RX bytes:760 (760.0 b) TX bytes:760 (760.0 b)

lo:0 Link encap:Local Loopback

inet addr:192.168.1.6 Mask:255.255.255.255

UP LOOPBACK RUNNING MTU:16436 Metric:1

添加特殊路由,臨時設置

[root@rip2 ~]# route add -host 192.168.1.6 dev lo:0

[root@rip2 ~]# route –n 尚未顯示在路由表

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

192.168.1.6 0.0.0.0 255.255.255.255 UH 0 0 0 lo

192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0

0.0.0.0 192.168.1.3 0.0.0.0 UG 0 0 0 eth0

保存永久路由

保存路由設置,使其在網絡重啓後任然有效

[root@rip2 ~]# vim /etc/sysconfig/static-routes

any host 192.168.1.6 gw 192.168.1.6

[root@rip2~]# service network restart

[root@rip2 ~]# route -n

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

192.168.1.6 192.168.1.6 255.255.255.255 UGH 0 0 0 lo

192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0

0.0.0.0 192.168.1.3 0.0.0.0 UG 0 0 0 eth0

[root@rip2 ~]# echo "web2" > /var/www/html/index.html

[root@rip2~]# service httpd start

Starting httpd: [ OK ]

[root@rip2 ~]# netstat -ntlp | grep 80

tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 7706/httpd

clip_p_w_picpath004

在客戶端192.168.1.250打開http://192.168.1.6, 客戶端不斷刷新,發現web2和web1交替出現,比率爲1:1,說明依次輪詢模式爲RR

clip_p_w_picpath006

director檢查狀態

[root@director ~]# ipvsadm -ln

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP 192.168.1.6:80 rr

-> 192.168.1.8:80 Route 1 0 11

-> 192.168.1.7:80 Route 1 0 11

[root@director ~]# ipvsadm -ln –stats 統計數據

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Conns InPkts OutPkts InBytes OutBytes

-> RemoteAddress:Port

TCP 192.168.1.6:80 3093 15459 0 1140059 0

-> 192.168.1.8:80 1670 8343 0 617015 0

-> 192.168.1.7:80 1423 7116 0 523044 0

[root@director ~]# ipvsadm -ln –rate 查速率

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port CPS InPPS OutPPS InBPS OutBPS

-> RemoteAddress:Port

TCP 192.168.1.6:80 1 4 0 652 0

-> 192.168.1.8:80 1 2 0 340 0

-> 192.168.1.7:80 0 2 0 312 0

不能在director自己測試壓力

[root@director ~]# ab -c 100 -n 1000 http://192.168.1.6/index.html

This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.1.6 (be patient)

apr_socket_recv: Connection refused (111)

和打開文件數無關,沒有報錯鏈接過多

[root@director ~]# ulimit -a

core file size (blocks, -c) 0

data seg size (kbytes, -d) unlimited

scheduling priority (-e) 0

file size (blocks, -f) unlimited

pending signals (-i) 16384

max locked memory (kbytes, -l) 32

max memory size (kbytes, -m) unlimited

open files (-n) 1024

pipe size (512 bytes, -p) 8

POSIX message queues (bytes, -q) 819200

real-time priority (-r) 0

stack size (kbytes, -s) 10240

cpu time (seconds, -t) unlimited

max user processes (-u) 16384

virtual memory (kbytes, -v) unlimited

file locks (-x) unlimited

[root@station250 ~]# ab -c 100 -n 1000 http://192.168.1.6/index.html

This is ApacheBench, Version 2.0.40-dev <$Revision: 1.146 $> apache-2.0

Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/

Copyright 2006 The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.1.6 (be patient)

Completed 100 requests

Completed 200 requests

Completed 300 requests

Completed 400 requests

Completed 500 requests

Completed 600 requests

Completed 700 requests

Completed 800 requests

Completed 900 requests

Finished 1000 requests

Server Software: Apache/2.2.3

Server Hostname: 192.168.1.6

Server Port: 80

Document Path: /index.html

Document Length: 5 bytes

Concurrency Level: 100

Time taken for tests: 0.174614 seconds

Complete requests: 1000

Failed requests: 0

Write errors: 0

Total transferred: 264528 bytes

HTML transferred: 5010 bytes

Requests per second: 5726.92 [#/sec] (mean)

Time per request: 17.461 [ms] (mean)

Time per request: 0.175 [ms] (mean, across all concurrent requests)

Transfer rate: 1477.54 [Kbytes/sec] received

Connection Times (ms)

min mean[+/-sd] median max

Connect: 0 2 3.2 1 21

Processing: 5 13 4.3 13 29

Waiting: 4 12 4.3 12 28

Total: 6 15 6.3 15 45

Percentage of the requests served within a certain time (ms)

50% 15

66% 16

75% 18

80% 18

90% 23

95% 30

98% 36

99% 39

100% 45 (longest request)

[root@director ~]# ipvsadm -ln

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP 192.168.1.6:80 rr

-> 192.168.1.7:80 Route 1 0 507

-> 192.168.1.8:80 Route 1 0 495

腳本:

[root@director ~]# vim director.sh

#!/bin/bash

#

# LVS script for LVS/DR

#

. /etc/rc.d/init.d/functions

#

VIP=192.168.1.6

RIP1=192.168.1.7

RIP2=192.168.1.8

PORT=80

#

case"$1"in

start)

/sbin/ifconfig eth0:1 $VIP broadcast $VIP netmask 255.255.255.255 up

/sbin/route add -host $VIP dev eth0:1

# Since thisis the Director we must be able to forward packets

echo 1 > /proc/sys/net/ipv4/ip_forward

# Clear all iptables rules.

/sbin/iptables -F

# Reset iptables counters.

/sbin/iptables -Z

# Clear all ipvsadm rules/services.

/sbin/ipvsadm -C

# Add an IP virtual service for VIP port 80

# In this recipe, we will use the round-robin scheduling method. 

# In production, however, you should use a weighted, dynamic scheduling method. 

/sbin/ipvsadm -A -t $VIP:80 -s wlc

# Now direct packets forthis VIP to

# the real server IP (RIP) inside the cluster

/sbin/ipvsadm -a -t $VIP:80 -r $RIP1 -g -w 1

/sbin/ipvsadm -a -t $VIP:80 -r $RIP2 -g -w 2

/bin/touch /var/lock/subsys/ipvsadm &> /dev/null

;;

stop)

# Stop forwarding packets

echo 0 > /proc/sys/net/ipv4/ip_forward

# Reset ipvsadm

/sbin/ipvsadm -C

# Bring down the VIP interface

/sbin/ifconfig eth0:1 down

/sbin/route del $VIP

/bin/rm -f /var/lock/subsys/ipvsadm

echo "ipvs is stopped..."

;;

status)

if [ ! -e /var/lock/subsys/ipvsadm ]; then

echo "ipvsadm is stopped ..."else

echo "ipvs is running ..."

ipvsadm -L -n

fi

;;

*)

echo "Usage: $0 {start|stop|status}"

;;

esac

[root@director ~]# chmod +x director.sh

[root@director ~]# ll director.sh

-rwxr-xr-x 1 root root 1470 Jun 16 01:48 director.sh

[root@director ~]# ./director.sh start

[root@director ~]# ./director.sh status

ipvs is running ...

IP Virtual Server version 1.2.1 (size=4096)

Prot LocalAddress:Port Scheduler Flags

-> RemoteAddress:Port Forward Weight ActiveConn InActConn

TCP 192.168.1.6:80 wlc

-> 192.168.1.8:80 Route 2 0 1

-> 192.168.1.7:80 Route 1 0 0

[root@rip1 ~]# vim realserver.sh

#!/bin/bash

#

# Script to start LVS DR real server.

# description: LVS DR real server

#

. /etc/rc.d/init.d/functions

VIP=192.168.1.6

host=`/bin/hostname`

case "$1" in

start)

# Start LVS-DR real server on this machine.

/sbin/ifconfig lo down

/sbin/ifconfig lo up

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

/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up

/sbin/route add -host $VIP dev lo:0

;;

stop)

# Stop LVS-DR real server loopback device(s).

/sbin/ifconfig lo:0 down

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

;;

status)

# Status of LVS-DR real server.

islothere=`/sbin/ifconfig lo:0 | grep $VIP`

isrothere=`netstat -rn | grep "lo:0" | grep $VIP`

if [ ! "$islothere" -o ! "isrothere" ];then

# Either the route or the lo:0 device

# not found.

echo "LVS-DR real server Stopped."

else

echo "LVS-DR real server Running."

fi

;;

*)

# Invalid entry.

echo "$0: Usage: $0 {start|status|stop}"

exit 1

;;

esac

[root@rip1 ~]# chmod +x realserver.sh

[root@rip1 ~]# ll realserver.sh

-rwxr-xr-x 1 root root 1481 Jun 16 01:50 realserver.sh

[root@rip1 ~]# ./realserver.sh start

[root@rip1 ~]# ./realserver.sh status

LVS-DR real server Running.

[root@rip2 ~]# vim realserver.sh

#!/bin/bash

#

# Script to start LVS DR real server.

# description: LVS DR real server

#

. /etc/rc.d/init.d/functions

VIP=192.168.1.6

host=`/bin/hostname`

case "$1" in

start)

# Start LVS-DR real server on this machine.

/sbin/ifconfig lo down

/sbin/ifconfig lo up

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

/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up

/sbin/route add -host $VIP dev lo:0

;;

stop)

# Stop LVS-DR real server loopback device(s).

/sbin/ifconfig lo:0 down

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

;;

status)

# Status of LVS-DR real server.

islothere=`/sbin/ifconfig lo:0 | grep $VIP`

isrothere=`netstat -rn | grep "lo:0" | grep $VIP`

if [ ! "$islothere" -o ! "isrothere" ];then

# Either the route or the lo:0 device

# not found.

echo "LVS-DR real server Stopped."

else

echo "LVS-DR real server Running."

fi

;;

*)

# Invalid entry.

echo "$0: Usage: $0 {start|status|stop}"

exit 1

;;

esac

[root@rip2 ~]# chmod +x realserver.sh

[root@rip2 ~]# ll realserver.sh

-rwxr-xr-x 1 root root 1481 Jun 16 01:50 realserver.sh

[root@rip2 ~]# ./realserver.sh start

[root@rip2 ~]# ./realserver.sh status

LVS-DR real server Running.

相關文章
相關標籤/搜索