环境准备
五台主机
|----------------------|----------------------------------------|---------------------|
| 主机名称 | ip地址 | 回环地址 |
| client.timinglee.org | eth0:172.25.254.200 | 无 |
| router.timinglee.org | eth0:172.25.254.100 eth1:192.168.0.100 | 无 |
| lvs.timinglee.org | eth1:192.168.0.50 | lo:192.168.0.200/32 |
| web1.timinglee.org | eth0:192.168.0.10 | lo:192.168.0.200/32 |
| web2.timinglee.org | eth0:192.168.0.20 | lo:192.168.0.200/32 |
bash
作用:RS安装mod_ssl模块,让RS支持https
[root@web1 ~]# yum install mod_ssl -y
[root@web1 ~]# systemctl restart httpd
[root@web2 ~]# yum install mod_ssl -y
[root@web2 ~]# systemctl restart httpd
1.防火墙标签解决轮询错误
1.1轮询规则中可能会遇到的错误
- 以http和https为例,当我们在RS中同时开放80和443端口,默认控制是分开轮询的,这样就出 现了一个轮询错乱的问题
问题呈现
bash
lvs中设置调度,因为我们要调度80和443两个端口所以我们需要设定两组策略
] # ipvsadm -C
[root@lvs ~]# ipvsadm -A -t 192.168.0.100:80 -s rr
[root@lvs ~]# ipvsadm -A -t 192.168.0.100:443 -s rr
[root@lvs ~]# ipvsadm -a -t 192.168.0.100:80 -r 192.168.0.101:80 -g
[root@lvs ~]# ipvsadm -a -t 192.168.0.100:80 -r 192.168.0.102:80 -g
[root@lvs ~]# ipvsadm -a -t 192.168.0.200:443 -r 192.168.0.10 -g
[root@lvs ~]# ipvsadm -a -t 192.168.0.200:443 -r 192.168.0.20 -g
[root@lvs ~]# 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.200:80 rr
-> 192.168.0.10:80 Route 1 0 0
-> 192.168.0.20:80 Route 2 0 0
TCP 192.168.0.200:443 rr
-> 192.168.0.10:443 Route 1 0 0
-> 192.168.0.20:443 Route 1 0 0
测试出现问题
bash
[root@client ~]# curl http://192.168.0.200;curl -k https://192.168.0.200
web2 - 192.168.0.20
web2 - 192.168.0.20
# 同时访问192.168.0.20-->不合理
1.2.防火墙标记解决轮询调度问题
bash
[root@lvs ~]# ipvsadm -C
FWM:FireWall Mark
MARK target 可用于给特定的报文打标记,--set-mark value
其中:value 可为0xffff格式,表示十六进制数字借助于防火墙标记来分类报文,而后基于标记定义集群服
务:可将多个不同的应用使用同一个集群服务进行调度
[root@lvs ~]# iptables -t mangle -Ln
iptables v1.8.8 (nf_tables): chain `n' in table `mangle' is incompatible, use 'nft' tool.
[root@lvs ~]# iptables -t mangle -nL
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
# 在vs调度器中设定端口标签,人为80和443是一个整体
[root@lvs ~]# iptables -t mangle -A PREROUTING -d 192.168.0.200 -p tcp -m multiport --dports 80,443 -j MARK --set-mark 66
[root@lvs ~]# iptables -t mangle -nL
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
MARK tcp -- 0.0.0.0/0 192.168.0.200 multiport dports 80,443 MARK set 0x42
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
# 设定调度规则
[root@lvs ~]# ipvsadm -A -f 66 -s rr
[root@lvs ~]# ipvsadm -a -f 66 -r 192.168.0.10 -g
[root@lvs ~]# ipvsadm -a -f 66 -r 192.168.0.20 -g
[root@lvs ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
FWM 66 rr
-> 192.168.0.10:0 Route 1 0 0
-> 192.168.0.20:0 Route 1 0 0
测试结构
bash
[root@client ~]# curl -k https://192.168.0.200;curl 192.168.0.200
web1 - 192.168.0.10
web2 - 192.168.0.20
[root@client ~]# curl -k https://192.168.0.200;curl 192.168.0.200
web1 - 192.168.0.10
web2 - 192.168.0.20
[root@client ~]# curl -k https://192.168.0.200;curl 192.168.0.200
web1 - 192.168.0.10
web2 - 192.168.0.20