持久鏈接便是不考慮LVS的轉發方法,確保全部來自同一個用戶的鏈接轉發到同一個RealServer上。html
使用的選項:web
ipvsadm -p timeout超時時間間隔(秒鐘)默認300秒 電商網站給用戶多長時間的鏈接持久,根據統計的用戶黏性作判斷。通常3600、7200秒都是可取範圍。算法
持久鏈接類型:shell
1.PCC(persistent client connector,持久用戶鏈接)同一個用戶全部的請求在超時範圍以內都被定位到同一個RealServer上,這個時候在指定端口的時候使用的是0端口,就是全部的請求都轉發出去。服務器
2.PPC(persistent port connector)用戶的全部請求在超時範圍內按照端口定位到不一樣的RS上。併發
3.防火牆標記:把相關聯的端口在防火牆上打上一樣的標記,用戶在訪問兩個相關聯的服務的時候,就會定位到同一個RealServer上。ssh
4.FTP connection:因爲ftp使用的是兩個端口號,因此須要單獨列出來。FTP Connections (FTP鏈接,在被動模式下控制鏈接端口21,數據鏈接大於1024隨機端口,主動模式數據鏈接20端口)tcp
4.一、FTP服務器被動模式下要把數據鏈接端口限定在必定範圍內;10001-15000ide
4.二、將這5000個端口與21端口作成姻親關係,打上同一個防火牆標記網站
4.三、將這種協議開放
實驗一:PCC 持久用戶鏈接
[root@director ~]# ipvsadm –lcn #查看當前持久鏈接模板中信息
IPVS connection entries
pro expire state source virtual destination
[root@director ~]# service ipvsadm start
ipvsadm: Clearing the current IPVS table: [ OK ]
ipvsadm: Applying IPVS configuration: [ OK ]
[root@director ~]# ipvsadm –ln #查看ipvs配置
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 ~]# hwclock -s #--systohc 系統時間同步硬件時間,各節點時間同步
[root@rip1 ~]# hwclock -s
[root@rip2 ~]# hwclock -s
[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
Director配置IP地址
[root@director ~]# ifconfig eth0 192.168.1.3/24 up
[root@director ~]# ifconfig eth0:1 192.168.1.6 broadcast 192.168.1.6 netmask 255.255.255.255 up
[root@director ~]# route add -host 192.168.1.6 dev eth0:1
[root@director ~]# echo 1 > /proc/sys/net/ipv4/ip_forward
Director配置 ipvs
[root@director ~]# ipvsadm -A -t 192.168.1.6:0 -s wrr -p 1000
-t tcp 端口0 -s 調度算法 wrr加權輪調 默認wlc 加權最少鏈接 -p 超時1000s
[root@director ~]# ipvsadm -a -t 192.168.1.6:0 -r 192.168.1.7 -g -w 100
-r realserver -g gateway網關模型省略dr -w 權重
[root@director ~]# ipvsadm -a -t 192.168.1.6:0 -r 192.168.1.8 -g -w 200
[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:0 wrr persistent 1000
-> 192.168.1.8:0 Route 200 0 0
-> 192.168.1.7:0 Route 100 0 0
RealServer1 配置arp廣播規則,再配IP
[root@rip1 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@rip1 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@rip1 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@rip1 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@rip1 ~]# ifconfig eth0 192.168.1.7/24
[root@rip1 ~]# ifconfig lo:0 192.168.1.6 broadcast 192.168.1.6 netmask 255.255.255.255 up
[root@rip1 ~]# route add -host 192.168.1.6 dev lo:0
[root@rip1 ~]# yum -y install httpd
[root@rip1 ~]# echo "web1" > /var/www/html/index.html
[root@rip1 ~]# service httpd start
RealServer2 配置arp廣播規則,再配IP
[root@rip2 ~]# echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
[root@rip2 ~]# echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
[root@rip2 ~]# echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
[root@rip2 ~]# echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
[root@rip2 ~]# ifconfig eth0 192.168.1.8/24
[root@rip2 ~]# ifconfig lo:0 192.168.1.6 broadcast 192.168.1.6 netmask 255.255.255.255 up
[root@rip2 ~]# route add -host 192.168.1.6 dev lo:0
[root@rip2 ~]# yum -y install httpd
[root@rip2 ~]# echo "web2" > /var/www/html/index.html
[root@rip2 ~]# service httpd start
Director檢查ipvs
[root@director ~]# ipvsadm –ln #檢查ipvs
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:0 wrr persistent 1000
-> 192.168.1.8:0 Route 200 0 6
-> 192.168.1.7:0 Route 100 0 0
wrr 哪一個realserver的權重大,就多鏈接哪一個。 持續鏈接1000秒。
打開IE http:// 192.168.1.6, 顯示web2, 多刷新幾回一直持續鏈接在web2
[root@director ~]# ipvsadm –lcn #檢查持續鏈接狀況
IPVS connection entries
pro expire state source virtual destination
TCP 01:55 FIN_WAIT 192.168.1.250:33578 192.168.1.6:80 192.168.1.8:80
TCP 01:55 FIN_WAIT 192.168.1.250:33577 192.168.1.6:80 192.168.1.8:80
TCP 01:54 FIN_WAIT 192.168.1.250:33575 192.168.1.6:80 192.168.1.8:80
TCP 01:55 FIN_WAIT 192.168.1.250:33580 192.168.1.6:80 192.168.1.8:80
TCP 01:55 FIN_WAIT 192.168.1.250:33581 192.168.1.6:80 192.168.1.8:80
TCP 16:20 NONE 192.168.1.100:0 192.168.1.6:0 192.168.1.8:0
TCP 01:54 FIN_WAIT 192.168.1.250:33574 192.168.1.6:80 192.168.1.8:80
TCP 01:56 FIN_WAIT 192.168.1.250:33584 192.168.1.6:80 192.168.1.8:80
TCP 01:55 FIN_WAIT 192.168.1.250:33579 192.168.1.6:80 192.168.1.8:80
TCP 01:55 FIN_WAIT 192.168.1.250:33582 192.168.1.6:80 192.168.1.8:80
TCP 16:36 NONE 192.168.1.250:0 192.168.1.6:0 192.168.1.8:0
TCP 01:55 FIN_WAIT 192.168.1.250:33583 192.168.1.6:80 192.168.1.8:80
TCP 01:54 FIN_WAIT 192.168.1.250:33576 192.168.1.6:80 192.168.1.8:80
TCP 00:40 SYN_RECV 192.168.1.100:53407 192.168.1.6:80 192.168.1.8:80
客戶端station250
[root@station250 ~]# ab -c 10 -n 1000 http://192.168.1.6/index.html
# -c 併發數 –n 總共請求數
Concurrency Level: 10
Time taken for tests: 0.370147 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Total transferred: 264000 bytes
HTML transferred: 5000 bytes
Requests per second: 2701.63 [#/sec] (mean)
Time per request: 3.701 [ms] (mean)
Time per request: 0.370 [ms] (mean, across all concurrent requests)
Transfer rate: 694.32 [Kbytes/sec] received
[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:0 wrr persistent 1000
-> 192.168.1.8:0 Route 200 0 1000 #1000次鏈接
-> 192.168.1.7:0 Route 100 0 0
[root@director ~]# ipvsadm -lcn
IPVS connection entries
pro expire state source virtual destination
TCP 01:36 FIN_WAIT 192.168.1.250:44898 192.168.1.6:80 192.168.1.8:80
TCP 01:36 FIN_WAIT 192.168.1.250:44931 192.168.1.6:80 192.168.1.8:80
TCP 01:36 FIN_WAIT 192.168.1.250:44829 192.168.1.6:80 192.168.1.8:80
TCP 01:36 FIN_WAIT 192.168.1.250:45554 192.168.1.6:80 192.168.1.8:80
TCP 01:36 FIN_WAIT 192.168.1.250:45555 192.168.1.6:80 192.168.1.8:80
TCP 01:36 FIN_WAIT 192.168.1.250:45399 192.168.1.6:80 192.168.1.8:80
TCP 01:36 FIN_WAIT 192.168.1.250:44756 192.168.1.6:80 192.168.1.8:80
TCP 01:36 FIN_WAIT 192.168.1.250:45494 192.168.1.6:80 192.168.1.8:80
TCP 01:36 FIN_WAIT 192.168.1.250:45415 192.168.1.6:80 192.168.1.8:80
TCP 01:36 FIN_WAIT 192.168.1.250:45016 192.168.1.6:80 192.168.1.8:80
TCP 01:36 FIN_WAIT 192.168.1.250:44858 192.168.1.6:80 192.168.1.8:80
TCP 01:36 FIN_WAIT 192.168.1.250:44631 192.168.1.6:80 192.168.1.8:80
驗證PCC功能,在Xshell新建頁鏈接director172.16.100.6:22 成功後指向R2 172.16.100.8
全部指向VIP的鏈接,如論什麼端口,直接轉發後臺realserver R2
[root@rip2 ~]# yum -y install telnet-server
[root@rip2 ~]# chkconfig telnet on
[root@rip2 ~]# service xinetd start
[root@rip2 ~]# netstat -ntlp
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10245/httpd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3979/sshd
tcp 0 0 0.0.0.0:23 0.0.0.0:* LISTEN 12667/xinetd
[root@rip2 ~]# useradd king
[root@rip2 ~]# echo king | passwd --stdin king
[root@rip1 ~]# yum -y install telnet-server
[root@rip1 ~]# chkconfig telnet on
[root@rip1 ~]# service xinetd start
[root@rip1 ~]# netstat -ntlp
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 10410/httpd
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 3976/sshd
tcp 0 0 0.0.0.0:23 0.0.0.0:* LISTEN 12904/xinetd
[root@station250 ~]# telnet 192.168.1.6 #telnet默認不容許root遠程登陸
Connected to 192.168.1.6 (192.168.1.6).
Escape character is '^]'.
CentOS release 5.7 (Final)
Kernel 2.6.18-274.el5 on an i686
login: king
Password: king
[king@rip2 ~]$ exit #鏈接到192.168.1.6都定向到1.8 rip2
logout
實驗2、PPC Persistent port connections (PPC) 持久端口鏈接,同一用戶請求的特定服務定向到同一server.
[root@director ~]# ipvsadm -C
[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 wlc -p 1000 # 默認 加權最少鏈接數
[root@director ~]# ipvsadm -A -t 192.168.1.6:23 -s wlc -p 1000
[root@director ~]# ipvsadm -a -t 192.168.1.6:80 -r 192.168.1.7 -g -w 100
[root@director ~]# ipvsadm -a -t 192.168.1.6:23 -r 192.168.1.7 -g -w 100
[root@director ~]# ipvsadm -a -t 192.168.1.6:80 -r 192.168.1.8 -g -w 200
[root@director ~]# ipvsadm -a -t 192.168.1.6:23 -r 192.168.1.8 -g -w 200
打開IE 172.16.100.6 web1
[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:23 wlc persistent 1000
-> 192.168.1.8:23 Route 200 1 0
-> 192.168.1.7:23 Route 100 0 0
TCP 192.168.1.6:80 wlc persistent 1000
-> 192.168.1.8:80 Route 200 0 0
-> 192.168.1.7:80 Route 100 0 0
[root@director ~]# ipvsadm -lcn
IPVS connection entries
pro expire state source virtual destination
TCP 01:20 NONE 192.168.1.100:0 192.168.1.6:80 192.168.1.8:80
TCP 01:29 FIN_WAIT 192.168.1.250:39524 192.168.1.6:23 192.168.1.8:23
TCP 12:04 NONE 192.168.1.250:0 192.168.1.6:23 192.168.1.8:23
TCP 11:05 NONE 192.168.1.250:0 192.168.1.6:80 192.168.1.7:80
[root@rip1 ~]# useradd king
[root@rip1 ~]# echo king | passwd --stdin king
[root@station250 ~]# telnet 192.168.1.6
Connected to 192.168.1.6 (192.168.1.6).
login: king
Password:king
[king@rip2 ~]$ exit
logout
Connection closed by foreign host.
[root@director ~]# ipvsadm -lcn
IPVS connection entries
pro expire state source virtual destination
TCP 01:21 FIN_WAIT 192.168.1.250:43786 192.168.1.6:23 192.168.1.8:23
TCP 15:32 NONE 192.168.1.250:0 192.168.1.6:23 192.168.1.8:23
TCP 08:58 NONE 192.168.1.250:0 192.168.1.6:80 192.168.1.7:80
斷開重連,只要在超時範圍1000s內,一個用戶請求23會一直轉發到一臺realserver r2
只持續定向一個端口, 同一用戶請求的特定服務定向到同一臺server.
[root@station250 ~]# telnet 192.168.1.6 80
出現空白,說明鏈接成功,繼續輸入下一行
#GET/HTTP/1.1
Trying 192.168.1.6...
Connected to 192.168.1.6 (192.168.1.6).
Escape character is '^]'.
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>501 Method Not Implemented</title>
</head><body>
<h1>Method Not Implemented</h1>
to /index.html not supported.<br />
<hr>
<address>Apache/2.2.3 (CentOS) Server at rip1.example.com Port 80</address>
</body></html>
Connection closed by foreign host.