chinaab 发表于 2019-1-4 10:32:50

LVS集群之持久连接

   LVS持久连接定义:
     某一个周期(时间段)之内,来自于同一个用户的请求,都转向同一个real-server。无论你选择LVS的何种转发策略,来自同一客户端的所有连接均返回到同一台real server,就需要LVS的持久连接了。经常用于SSL,建立一个SSL连接,需要交换SSL密钥,当启用持久性连接时,只需要做一次验证即可。
持久连接模板:
      当使用LVS持久连接时,分发器使用一个连接跟踪(持久连接模板)使所有来自同一客户端的连接被标记为相同的real server;而当客户端向集群服务器请求连接时,分发器(director)会查看持久连接模板,是否real server已经被标记为这种类型的连接,若没有则director重新为每次连接创建一个正常连接的跟踪记录表(持久连接模板)。

持久连接超时:
我们可以在director使用ipvsadm为持久连接模板规定一个具体的时间段,如果记数器归零,而且是激活状态(客户端一直在和real server通信),不管你设置的持久连接时长是多少,记录器将会重置一个默认值2分钟,并开始计数,下降为零后,再开始再一下重置。
持久连接类型:
PCC(persistent client connections):持久客户端连接,对同一客户端发起的所有连接都有效,也叫做零端口的持久连接
     PPC(persistent port connections):持久端口连接,只对一个连接有效,强制所有从客户端的连接为一个特定的目标端口号
     Persistent Netfilter Marked Packet persistence:基于防火墙标记的持久性连接,这种防火墙标记仅在数据包在分发器上时有影响,数据包一旦离开director就不再被标记。
  

  本文以LVS的DR模型做讲解,关于DR模型搭建请参考LVS集群之DR模型
  使用持久连接要求:
      首先在每台realserver启用ssl功能安装mod_ssl模块
      需要在DIP上安装CA证书服务,然后为RIP颁发证书,然后在DIP上对防火墙打标,最后定义集群服务
  实验环境:
     DIP 172.18.20.126    VIP172.18.20.128主机名: node1.com
     RIP1 172.18.20.125 主机名: node2.comRIP2 172.18.20.124主机名: node3.com
  DIP配置成CA服务器:
# cd /etc/pki/CA          切换到CA证书目录
# less /etc/pki/tls/openssl.cnf   阅读模板文件,里面详细讲解配置参数及格式   
# (umask 077;openssl genrsa 2048 > private/cakey.pem)   生成私钥
Generating RSA private key,2048 bit long modulus
....................+++
..................................................................................+++
e is 65537 (0x10001)
# openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 365
          生成请求新证书类型为x509,指定key文件,输出为证书信息文件,指定证书有效天数
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) :CN                  填写国家
State or Province Name (full name) []:GD                填写省份
Locality Name (eg, city) :SZ               填写市县
Organization Name (eg, company) :Test      填写机构名称
Organizational Unit Name (eg, section) []:Tech             填写部门
Common Name (eg, your name or your server's hostname) []:node.com填写CA主机名称         
Email Address []:admin@node.com       填写邮箱
# touch index.txt       创建数据索引文件
# echo 01 > serial      创建序列号  RIP1配置:
# mkdir /etc/httpd/conf/ssl            在RIP1上创建ssl目录
# cd /etc/httpd/conf/ssl            切换到ssl目录
# (umask 077;openssl genrsa 2048 > httpd.key)生成私钥
Generating RSA private key, 2048 bit long modulus
..........................+++
......................................................+++
e is 65537 (0x10001)
# openssl req -new -key httpd.key -out httpd.csr
         生成证书请求,指定key文件,输出请求文件格式为csr                  
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) :CN               填写国家
State or Province Name (full name) []:GD             填写省份
Locality Name (eg, city) :SZ            填写市县
Organization Name (eg, company) :Test    填写机构名称,需与CA一致
Organizational Unit Name (eg, section) []:Tech          填写部门
Common Name (eg, your name or your server's hostname) []:node2.com
      填写主机名或IP,必须与使用证书主机一致.
      如使用IP则SSL只能使用IP,如使用主机名则SSL只能使用主机名,使用其它均不能使用SSL
Email Address []:admin@node2.com               填写邮箱
Please enter the following 'extra' attributes   
to be sent with your certificate request
A challenge password []:linux                   填写证书密码   
An optional company name []:node2               填写公司名称
CA为RIP签署证书:
#scp 172.18.20.125:/etc/httpd/conf/ssl/httpd.csr /tmp 复制RIP1证书请求至本地
# openssl ca -in /tmp/httpd.csr -out httpd.crt -days 365
                  指定请求文件,签署证书,格式为crt,指定签署证书有效天数
# scp httpd.crt 172.18.20.125:/etc/httpd/conf/ssl/复制证书至RIP1
# scp -rp 172.18.20.125:/etc/httpd/conf/ssl/ /tmp/复制RIP1SSL目录至本地
# scp -rp /tmp/ssl/ 172.18.20.124:/etc/httpd/conf/复制SSL目录至RIP2
# vim /etc/httpd/conf.d/ssl.conf       修改RIP1SSL配置
   SSLCertificateFile /etc/httpd/conf/ssl/httpd.crt    指定证书路径
   SSLCertificateKeyFile /etc/httpd/conf/ssl/httpd.key   指定密钥路径
# httpd -t                使用httpd测试配置文件是否有错
# service httpd restart          重启httpd服务
# netstat -tuanlp | grep -E "80|443"   查看80、443端口是否启用
# vim /etc/httpd/conf.d/ssl.conf   修改RIP2SSL配置
   SSLCertificateFile /etc/httpd/conf/ssl/httpd.crt   指定证书路径
   SSLCertificateKeyFile /etc/httpd/conf/ssl/httpd.key 指定密钥路径
# httpd -t                  使用httpd测试配置文件是否有错
# service httpd restart            重启httpd服务
# netstat -tuanlp | grep -E "80|443"   查看80、443端口是否启用
DIP配置集群服务:
# iptables -t mangle -A PREROUTING -d 172.18.20.128 -p tcp --dport 80 -j MARK --set-mark 10
# iptables -t mangle -A PREROUTING -d 172.18.20.128 -p tcp --dport 443 -j MARK --set-mark 10
                   使用iptables将80、443端口进行标记
# ipvsadm -A -f 10 -s wlc -p   创建集群,-f指定标记,-s指定集群算法,-p使用标记
# ipvsadm -a -f 10 -r 172.18.20.124 -g -w 1 为集群添加RIP,指定DR模型,指定权重
# ipvsadm -a -f 10 -r 172.18.20.125 -g -w 2
# ipvsadm -L -n               查看集群情况
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port         Forward Weight ActiveConn InActConn
FWM 10 wlc persistent 360    标记为10,算法为wlc,标记时长默认360秒
-> 172.18.20.124:0            Route   1      0   0         
-> 172.18.20.125:0            Route   2      1    217
# ipvsadm -L -n --rate --stats查看集群情况,具体显示速率、统计信息
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port            Conns   InPktsOutPktsInBytes OutBytes
-> RemoteAddress:Port            结果显示所有80、443服务均定向至同一RIP
FWM 10                      218    1470   0   208963    0
-> 172.18.20.124:0               0      0   0   0    0
-> 172.18.20.125:0             218    1470   0   208963    0http://s3.运维网.com/wyfs02/M01/83/1A/wKioL1drQt7Rh649AACXBXB8q_c798.jpg-wh_500x0-wm_3-wmp_4-s_3148814144.jpg

http://s3.运维网.com/wyfs02/M01/83/1B/wKiom1drQt_RcRe9AACcEi8UkRY685.jpg-wh_500x0-wm_3-wmp_4-s_241649680.jpg
  

  至此LVS集群持久连接完成,如有错误请大家指教!



页: [1]
查看完整版本: LVS集群之持久连接