Linux学习-LVS持久连接

Posted 丢爸

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux学习-LVS持久连接相关的知识,希望对你有一定的参考价值。

LVS持久连接
无论使用什么算法,LVS持久连接都能实现在一段时间内,将来自同一个客户端请求派发至此前选定的RS。
持久连接模板(内存缓冲区):记录了每一个客户端连接,分配给它的RS的映射关系

ipvsadm -A|E … -p timeout
timeout:持久连接时长,默认300秒,单位:秒
在基于SSL:需要用到持久连接
PPC:将来自于同一个客户端对同一个集群服务的请求,始终定向至此前选定的RS 持久端口连接
PCC:将来自于同一个客户端对所有端口的请求,始终定向至此前选定的RS 持久客户端连接
将所有端口统统定义为集群服务,一律向RS转发

PNMPP:持久防火墙标记连接
#PPC
[root@mail ~]# ipvsadm -E -t 192.168.0.100:80 -s wlc -p 50
[root@mail ~]# 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.0.100:80 wlc persistent 50
  -> 192.168.0.109:80             Route   1      0          0         
  -> 192.168.0.110:80             Route   5      0          0  
#PCC
[root@mail scripts]# ipvsadm -A -t 192.168.0.100:0 -s rr -p 60
[root@mail scripts]# ipvsadm -a -t 192.168.0.100:0 -r 192.168.0.109 -g 
[root@mail scripts]# ipvsadm -a -t 192.168.0.100:0 -r 192.168.0.110 -g 
[root@mail scripts]# ipvsadm -L -n -c
IPVS connection entries
pro expire state       source             virtual            destination
TCP 09:13  ESTABLISHED 192.168.0.105:60642 192.168.0.100:80   192.168.0.110:80
TCP 00:12  NONE        192.168.0.105:0    192.168.0.100:80   192.168.0.110:80

#使用SSH连接,也一样连接至RS2
[C:\\~]$ ssh 192.168.0.100


Connecting to 192.168.0.100:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Sun Jul 18 05:28:21 2021 from 192.168.0.105
[root@lotus ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:D3:6A:50  
          inet addr:192.168.0.110  Bcast:192.255.255.255  Mask:255.0.0.0
          inet6 addr: fe80::20c:29ff:fed3:6a50/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:983 errors:0 dropped:0 overruns:0 frame:0
          TX packets:528 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:116737 (114.0 KiB)  TX bytes:50032 (48.8 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:700 (700.0 b)  TX bytes:700 (700.0 b)

lo:0      Link encap:Local Loopback  
          inet addr:192.168.0.100  Mask:255.255.255.255
          UP LOOPBACK RUNNING  MTU:65536  Metric:1

在这里插入图片描述

#PNMPP
[root@mail scripts]# iptables -t mangle -A PREROUTING -d 192.168.0.100 -i eth0 -p tcp --dport 80 -j MARK --set-mark 8
[root@mail scripts]# iptables -t mangle -A PREROUTING -d 192.168.0.100 -i eth0 -p tcp --dport 22 -j MARK --set-mark 8
[root@mail scripts]# ipvsadm -A -f 8 -s rr
[root@mail scripts]# ipvsadm -a -f 8 -r 192.168.0.109 -g -w 2
[root@mail scripts]# ipvsadm -a -f 8 -r 192.168.0.110 -g -w 5
[root@mail scripts]# 
[root@mail scripts]# 
[root@mail scripts]# 
[root@mail scripts]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
FWM  8 rr
  -> 192.168.0.109:0              Route   2      0          0         
  -> 192.168.0.110:0              Route   5      1          0 


#通过SSH连接,因使用了rr算法,故两次连接的结果会不同
#----第一次SSH连接
[C:\\~]$ ssh 192.168.0.100
Connecting to 192.168.0.100:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Sun Jul 18 06:16:52 2021 from 192.168.0.105
[root@lotus ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:38:28:64  
          inet addr:192.168.0.109  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe38:2864/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2294 errors:0 dropped:0 overruns:0 frame:0
          TX packets:820 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:213669 (208.6 KiB)  TX bytes:81045 (79.1 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:700 (700.0 b)  TX bytes:700 (700.0 b)

lo:0      Link encap:Local Loopback  
          inet addr:192.168.0.100  Mask:255.255.255.255
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
#-------------第2次SSH连接
[C:\\~]$ ssh 192.168.0.100


Connecting to 192.168.0.100:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Sun Jul 18 06:15:59 2021 from 192.168.0.105
[root@lotus ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:D3:6A:50  
          inet addr:192.168.0.110  Bcast:192.255.255.255  Mask:255.0.0.0
          inet6 addr: fe80::20c:29ff:fed3:6a50/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2452 errors:0 dropped:0 overruns:0 frame:0
          TX packets:1050 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:253444 (247.5 KiB)  TX bytes:98710 (96.3 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:700 (700.0 b)  TX bytes:700 (700.0 b)

lo:0      Link encap:Local Loopback  
          inet addr:192.168.0.100  Mask:255.255.255.255
          UP LOOPBACK RUNNING  MTU:65536  Metric:1

#因telnet-server没有加入到集群中,通过telnet连接,直接连接至director服务器
[C:\\~]$ telnet 192.168.0.100
Connecting to 192.168.0.100:23...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.
CentOS release 6.10 (Final)
Kernel 2.6.32-754.el6.x86_64 on an x86_64
mail.tye.com login: hadoop
Password: 
[hadoop@mail ~]$ /sbin/ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:78:18:40  
          inet addr:192.168.0.103  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe78:1840/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:8789 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10521 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:724313 (707.3 KiB)  TX bytes:7490643 (7.1 MiB)

eth0:0    Link encap:Ethernet  HWaddr 00:0C:29:78:18:40  
          inet addr:192.168.0.100  Bcast:192.168.0.100  Mask:255.255.255.255
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
#修改持久连接,使用持久连接,同一台客户端访问为连接至同一台服务器
[root@mail scripts]# ipvsadm -E -f 8 -s rr -p 60
[C:\\~]$ ssh 192.168.0.100


Connecting to 192.168.0.100:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Sun Jul 18 06:22:53 2021 from 192.168.0.105
[root@lotus ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:38:28:64  
          inet addr:192.168.0.109  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe38:2864/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2488 errors:0 dropped:0 overruns:0 frame:0
          TX packets:941 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:236073 (230.5 KiB)  TX bytes:96401 (94.1 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:700 (700.0 b)  TX bytes:700 (700.0 b)

lo:0      Link encap:Local Loopback  
          inet addr:192.168.0.100  Mask:255.255.255.255
          UP LOOPBACK RUNNING  MTU:65536  Metric:1

[root@lotus ~]# exit
logout
Connection closing...Socket close.

Connection closed by foreign host.

Disconnected from remote host(192.168.0.100:22) at 22:26:26.

Type `help' to learn how to use Xshell prompt.
[C:\\~]$ ssh 192.168.0.100


Connecting to 192.168.0.100:22...
Connection established.
To escape to local shell, press 'Ctrl+Alt+]'.

WARNING! The remote SSH server rejected X11 forwarding request.
Last login: Sun Jul 18 06:26:18 2021 from 192.168.0.105
[root@lotus ~]# ifconfig
eth0      Link encap:Ethernet  HWaddr 00:0C:29:38:28:64  
          inet addr:192.168.0.109  Bcast:192.168.0.255  Mask:255.255.255.0
          inet6 addr: fe80::20c:29ff:fe38:2864/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:2544 errors:0 dropped:0 overruns:0 frame:0
          TX packets:982 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:242915 (237.2 KiB)  TX bytes:103642 (101.2 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:65536  Metric:1
          RX packets:10 errors:0 dropped:0 overruns:0 frame:0
          TX packets:10 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:700 (700.0 b)  TX bytes:700 (700.0 b)

lo:0      Link encap:Local Loopback  
          inet addr:192.168.0.100  Mask:255.255.255.255
          UP LOOPBACK RUNNING  MTU:65536  Metric:1

以上是关于Linux学习-LVS持久连接的主要内容,如果未能解决你的问题,请参考以下文章

Linux集群系列之六——LVS持久连接

Linux集群-LVS持久连接与高可用

LVS 持久连接

LVS 持久连接

lvs持久连接

LVS负载均衡之持久性连接介绍(会话篇)