设为首页 收藏本站
查看: 691|回复: 0

[经验分享] LVS集群之持久连接

[复制链接]

尚未签到

发表于 2019-1-4 10:32:50 | 显示全部楼层 |阅读模式
   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    VIP  172.18.20.128  主机名: node1.com
       RIP1 172.18.20.125 主机名: node2.com  RIP2 172.18.20.124  主机名: node3.com
  DIP配置成CA服务器:
[root@node1 ~]# cd /etc/pki/CA          切换到CA证书目录
[root@node1 CA]# less /etc/pki/tls/openssl.cnf   阅读模板文件,里面详细讲解配置参数及格式   
[root@node1 CA]# (umask 077;openssl genrsa 2048 > private/cakey.pem)   生成私钥
Generating RSA private key,2048 bit long modulus
....................+++
..................................................................................+++
e is 65537 (0x10001)
[root@node1 CA]# 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) [XX]:CN                  填写国家
State or Province Name (full name) []:GD                填写省份
Locality Name (eg, city) [Default City]:SZ               填写市县
Organization Name (eg, company) [Default Company Ltd]: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       填写邮箱
[root@node1 CA]# touch index.txt       创建数据索引文件
[root@node1 CA]# echo 01 > serial      创建序列号    RIP1配置:
[root@node2 ~]# mkdir /etc/httpd/conf/ssl            在RIP1上创建ssl目录
[root@node2 ~]# cd /etc/httpd/conf/ssl              切换到ssl目录
[root@node2 ssl]# (umask 077;openssl genrsa 2048 > httpd.key)  生成私钥
Generating RSA private key, 2048 bit long modulus
..........................+++
......................................................+++
e is 65537 (0x10001)
[root@node2 ssl]# 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) [XX]:CN               填写国家
State or Province Name (full name) []:GD             填写省份
Locality Name (eg, city) [Default City]:SZ            填写市县
Organization Name (eg, company) [Default Company Ltd]: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签署证书:
[root@node1CA]#scp 172.18.20.125:/etc/httpd/conf/ssl/httpd.csr /tmp 复制RIP1证书请求至本地
[root@node1 CA]# openssl ca -in /tmp/httpd.csr -out httpd.crt -days 365
                    指定请求文件,签署证书,格式为crt,指定签署证书有效天数
[root@node1 CA]# scp httpd.crt 172.18.20.125:/etc/httpd/conf/ssl/  复制证书至RIP1
[root@node1 CA]# scp -rp 172.18.20.125:/etc/httpd/conf/ssl/ /tmp/  复制RIP1SSL目录至本地
[root@node1 CA]# scp -rp /tmp/ssl/ 172.18.20.124:/etc/httpd/conf/  复制SSL目录至RIP2
[root@node2 ssl]# vim /etc/httpd/conf.d/ssl.conf       修改RIP1SSL配置
   SSLCertificateFile /etc/httpd/conf/ssl/httpd.crt    指定证书路径
   SSLCertificateKeyFile /etc/httpd/conf/ssl/httpd.key   指定密钥路径
[root@node2 ssl]# httpd -t                使用httpd测试配置文件是否有错
[root@node2 ssl]# service httpd restart          重启httpd服务
[root@node2 ssl]# netstat -tuanlp | grep -E "80|443"   查看80、443端口是否启用
[root@node3 ssl]# vim /etc/httpd/conf.d/ssl.conf     修改RIP2SSL配置
   SSLCertificateFile /etc/httpd/conf/ssl/httpd.crt   指定证书路径
   SSLCertificateKeyFile /etc/httpd/conf/ssl/httpd.key 指定密钥路径
[root@node3 ssl]# httpd -t                  使用httpd测试配置文件是否有错
[root@node3 ssl]# service httpd restart            重启httpd服务
[root@node3 ssl]# netstat -tuanlp | grep -E "80|443"   查看80、443端口是否启用
  DIP配置集群服务:
[root@node1 CA]# iptables -t mangle -A PREROUTING -d 172.18.20.128 -p tcp --dport 80 -j MARK --set-mark 10
[root@node1 CA]# iptables -t mangle -A PREROUTING -d 172.18.20.128 -p tcp --dport 443 -j MARK --set-mark 10
                   使用iptables将80、443端口进行标记
[root@node1 CA]# ipvsadm -A -f 10 -s wlc -p   创建集群,-f指定标记,-s指定集群算法,-p使用标记
[root@node1 CA]# ipvsadm -a -f 10 -r 172.18.20.124 -g -w 1 为集群添加RIP,指定DR模型,指定权重
[root@node1 CA]# ipvsadm -a -f 10 -r 172.18.20.125 -g -w 2  
[root@node1 CA]# 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
[root@node1 CA]# ipvsadm -L -n --rate --stats  查看集群情况,具体显示速率、统计信息
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port            Conns   InPkts  OutPkts  InBytes 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    0



  

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




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-659239-1-1.html 上篇帖子: LVS概念介绍 下篇帖子: LVS 之 algorithm and NAT model
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表