FWM防火墙标记为什么使用防火墙标记?
将来自于同一个客户端的所有请求都定义到同一个realserver上;如使用防火墙标记将443和80定义为同一个集群服务,这样就可以实现统一调度到后端的real server。
比如:在电商站点中,如果往购物车中添加商品之后,需要付款;这个时候就需要访问direction,发起的连接就不是http,而是https,direction会认为这是一个新的连接,很可能会被分配到其他RS服务器,这个时候购物车中就没有了商品。为了解决这种问题,就需要把用户的http请求和https请求,都调度到同一台服务器。
防火墙标记在什么时候打?
当用户请求报文进行防火墙(PREROUTING),防火墙就将特定报文(如:443和80),打上标记,假设标记为“10”;LVS设置的时候,就可以根据标记进行设置,只要请求带有标记10,就会将请求调度到同一个RS服务器
定义方法:
(1) 打标:在direction上的mangle表的PREROUTING链上实现
语法: iptables -t mangle -A PREROUTING-d $vip -p $protocol --dport $port -j MARK --set-mark [1-99] (2) 基于FWM定义集群服务
# ipvsadm -A -f FWM -s SCHEDULER # ipvsadm -a -f FWM -rserver-address -g|-i|-m -w # 实例配置:说明:我是使用LVS-DR配置的集群服务环境,已经配置ok。
对用户请求打标记:将访问集群的80端口的报文,定义标记为10.
1
2
3
4
5
| [iyunv@LVS ~]# iptables -t mangle -A PREROUTING -d 172.16.4.1 -p tcp --dport 80 -j MARK --set-mark 10
[iyunv@LVS ~]# iptables -t mangle -L -n
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
MARK tcp -- 0.0.0.0/0 172.16.4.1 tcp dpt:80 MARK set 0xa
|
定义集群服务,这个时候VIP地址和端口,就可以使用防火墙标记代替了。
1
2
3
4
| [iyunv@LVS ~]# ipvsadm –C #清除之前DR设置的集群服务
[iyunv@LVS ~]# ipvsadm -A -f 10 -s rr
[iyunv@LVS ~]# ipvsadm -a -f 10 -r 172.16.4.101 -g-w 1
[iyunv@LVS ~]# ipvsadm -a -f 10 -r 172.16.4.102 -g-w 1
|
这个时候访问集群服务和之前DR设置的效果是一模一样的。
443和80一起打标,在下面和PFWM一起设置的。
持久连接持久连接的作用:无论使用哪一种调度方法,持久连接功能都能保证在指定时间范围之内,将来在于同一个IP的请求始终被定向至同一个RS;
和sh调度比较的优点:
sh:会一直将一个用户请求定义到一个后端server,不会考虑后端服务器状态。 持久连接:第一次会根据设置的调度算法(rr,wlc……)调度到后端,在指定的会话存活时间使用持久连接,会话存活时间到期之后,还是根据调度算法进行调度。
persistencetemplate:持久连接模板:将多个集群服务归并到一起统一调度
PPC:每端口持久;持久连接生效范围仅为单个集群服务;如果有多个集群服务,每服务被单独持久调度;
PCC:每客户端持久;持久连接生效范围为所有服务;定义集群服务时,其TCP或UDP协议的目标端口要使用0;
PFWM:每FWM持久:持久连接生效范围为定义为同一个FWM下的所有服务;
lvspersistence语法:
ipvsadm-A -t|-u|-f service-address -s SCHEDULER -p [#]
无-p选项:不启用持久连接
-p #:指定持久时长,省略时长,默认为360秒
定义一个PPC持久连接的服务调度器配置清除之前定义的规则
1
2
| [iyunv@LVS ~]# iptables -t mangle -F
[iyunv@LVS ~]# ipvsadm -C
|
设置持久连接规则
1
2
3
4
5
6
7
8
9
10
| [iyunv@LVS ~]# ipvsadm -A -t 172.16.4.1 -s rr -p
[iyunv@LVS ~]# ipvsadm -a -t 172.16.4.1 -r172.16.4.101 -g
[iyunv@LVS ~]# ipvsadm -a -t 172.16.4.1 -r172.16.4.102 -g
[iyunv@LVS ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
->RemoteAddress:Port ForwardWeight ActiveConn InActConn
TCP 172.16.4.1:0 rr persistent 360 #表示启用了持久连接功能
->172.16.4.101:0 Route 1 0 0
->172.16.4.102:0 Route 1 0 0
|
设置完成之后,客户端访问集群服务将会被始终调度到同一台RS服务器
PCC持久连接在RS服务器上面安装telnet-server
1
2
3
4
5
6
7
8
9
10
| [iyunv@WEB-01 ~]# yum -y install telnet-server
[iyunv@WEB-01 ~]# chkconfig telnet on
[iyunv@WEB-01 ~]# service xinetd start
[iyunv@WEB-01 ~]# useradd pcc
[iyunv@WEB-01 ~]# echo "pcc" | passwd--stdin pcc
[iyunv@WEB-02 ~]# yum -y install telnet-server
[iyunv@WEB-02 ~]# chkconfig telnet on
[iyunv@WEB-02 ~]# service xinetd start
[iyunv@WEB-02 ~]# useradd pcc
[iyunv@WEB-02 ~]# echo "pcc" | passwd--stdin pcc
|
Direction定义集群
1
2
3
| [iyunv@LVS ~]# ipvsadm -A -t 172.16.4.1:0 -s rr -p
[iyunv@LVS ~]# ipvsadm -a -t 172.16.4.1:0 -r172.16.4.101 -g
[iyunv@LVS ~]# ipvsadm -a -t 172.16.4.1:0 -r172.16.4.102 -g
|
访问集群看到的是WEB-02服务器,那么使用telnet连接一定还是web-02。
telnet连接集群
1
2
3
4
5
6
7
8
9
10
11
| Xshell:\> telnet 172.16.4.1
Connecting to 172.16.4.1:23...
Connection established.
Escape character is '^@]'.
CentOS release 6.6 (Final)
Kernel 2.6.32-504.el6.x86_64 on an x86_64
login: pcc
Password:
[pcc@localhost ~]$ ifconfig
eth0 Link encap:Ethernet HWaddr00:0C:29:91:F6:60
inet addr:172.16.4.102 Bcast:172.16.255.255 Mask:255.255.0.0
|
PFWM持久连接实现将同一个用户的80端口访问请求和443端口访问请求定义到同一台web服务器。
创建CA私有CA,及http证书申请由于主机数量有限,在lvs上面创建私有CA
1
2
3
4
5
6
7
8
9
10
11
12
| [iyunv@LVS ~]# cd /etc/pki/CA/
[iyunv@LVS CA]# (umask 077;openssl genrsa -outprivate/cakey.pem 2048)
[iyunv@LVS CA]# openssl req -new -x509 -keyprivate/cakey.pem -out cacert.pem -days 3650
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HA
Locality Name (eg, city) [Default City]:ZZ
Organization Name (eg, company) [Default CompanyLtd]:MageEdu
Organizational Unit Name (eg, section) []:Ops
Common Name (eg, your name or your server'shostname) []:ca.test.com
Email Address []:ca@admin.com
[iyunv@LVS CA]# touch serial index.txt
[iyunv@LVS CA]# echo 01 > serial
|
WEB-01申请证书
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| [iyunv@WEB-01 ~]# cd /etc/httpd/conf
[iyunv@WEB-01 conf]# mkdir ssl
[iyunv@WEB-01 conf]# cd ssl/
[iyunv@WEB-01 ssl]# (umask 077; openssl genrsa -out httpd.key1024)
[iyunv@WEB-01 ssl]# openssl req -new -key httpd.key-out httpd.csr
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:HA
Locality Name (eg, city) [Default City]:ZZ
Organization Name (eg, company) [Default CompanyLtd]:MageEdu
Organizational Unit Name (eg, section) []:Ops
Common Name (eg, your name or your server'shostname) []:www.lvs.com
Email Address []:web@admin.com
A challenge password []:
An optional company name []:
|
复制证书到CA
CA签署证书
1
2
3
| [iyunv@LVS ~]# openssl ca -in httpd.csr -outhttpd.crt -days 3650
Sign the certificate? [y/n]:y
1 out of 1 certificate requests certified, commit?[y/n]y
|
将申请好的证书重新传给WEB-01
配置httpds说明:WEB-01和WEB-02使用的证书是同一个;不需要在两个节点重复申请,只需要在一个节点创建完成,将证书复制到第二个节点即可。
安装mod_ssl模块(如果不安装此模块,则没有ssl.conf的配置文件)
1
| [iyunv@WEB-01 ~]# yum -y install mod_ssl
|
设置https
1
2
3
| DocumentRoot "/var/www/html"
SSLCertificateFile /etc/httpd/conf/ssl/httpd.crt
SSLCertificateKeyFile /etc/httpd/conf/ssl/httpd.key
|
启动服务查看是否监听443端口
1
2
3
| [iyunv@WEB-01 ~]# service httpd restart
[iyunv@WEB-01 ~]# netstat -lnt | grep 443
tcp 0 0 :::443 :::* LISTEN
|
复制ssl目录到WEB-02
1
| [iyunv@WEB-01 ~]# scp -rp /etc/httpd/conf/ssl/root@172.16.4.102:/etc/httpd/conf/
|
这个时候还不能直接复制,ssl配置文件,因为WEB-02还没有安装mod_ssl
web-02安装mod_ssl
1
| [iyunv@WEB-02 ~]# yum -y install mod_ssl
|
复制ssl配置文件
1
| [iyunv@WEB-01 ~]# scp /etc/httpd/conf.d/ssl.confroot@172.16.4.102:/etc/httpd/conf.d/ssl.conf
|
Web-02重启httpd服务,已经监听443端口了
1
2
3
| [iyunv@WEB-02 ~]# service httpd restart
[iyunv@WEB-02 ~]# netstat -lnt | grep 443
tcp 0 0 :::443 :::* LISTEN
|
lvs设置FWM持久连接LVS设置,先不需要设置持久连接,测试https是否可以正常调度
1
2
3
4
5
| [iyunv@LVS ~]# iptables -t mangle -A PREROUTING -d172.16.4.1 -p tcp --dport 80 -j MARK --set-mark 10
[iyunv@LVS ~]# iptables -t mangle -A PREROUTING -d172.16.4.1 -p tcp --dport 443 -j MARK --set-mark 10
[iyunv@LVS ~]# ipvsadm -A -f 10 -s rr
[iyunv@LVS ~]# ipvsadm -a -f 10 -r 172.16.4.101 -g
[iyunv@LVS ~]# ipvsadm -a -f 10 -r 172.16.4.102 -g
|
验证:确保访问http和https都是负载均衡。
修改为持久连接,保证无论冲http协议,切换到https协议,还是https协议切换到http协议都是同一台主机。
1
| [iyunv@LVS ~]# ipvsadm -E -f 10 -s rr -p
|
使用http访问是WEB-02响应
切换到https访问还是WEB-02响应
说明:这里的访问https报证书错误是我没有安装CA的证书。
|