本文共 7821 字,大约阅读时间需要 26 分钟。
RS1:192.168.27.144 RS1_VIP:10.0.0.100 RS2:192.168.27.145 RS2_VIP:10.0.0.100 VS-DIP:192.168.27.105 VS-VIP:10.0.0.100 此处的router也是Linux服务器: router_eth0 192.168.27.200 router_eth0:1 10.0.0.200 router_eth1 172.20.0.200
vim /etc/sysconfig/network-scripts/ifcfg-eth1 DEVICE=eth1 IPADDR=172.20.0.101 GATEWAY=172.20.0.200
vim /etc/sysconfig/network-scripts/ifcfg-eth0 IPADDR=192.168.27.200 NETMASK=255.255.255.0 vim /etc/sysconfig/network-scripts/ifcfg-eth1 DEVICE=eth1 IPADDR=172.20.0.200 cd /etc/sysconfig/network-scripts/ cp ifcfg-eth0 ifcfg-eth0:1 IPADDR=10.0.0.200 #复制后更改如下两行; PREFIX=8
IPADDR=192.168.27.105 NETMASK=255.255.255.0 GATEWAY=192.168.27.200
编写名称为lvs_dr_vs的脚本,内容如下
#!/bin/bash vip='10.0.0.100' iface='ens37:1' mask='255.0.0.0' port='80' rs1='192.168.27.144' rs2='192.168.27.145' scheduler='wrr' type='-g' rpm -q ipvsadm &> /dev/null || yum -y install ipvsadm &> /dev/null case $1 in start) ifconfig $iface $vip netmask $mask #broadcast $vip up iptables -F ipvsadm -A -t ${vip}:${port} -s $scheduler ipvsadm -a -t ${vip}:${port} -r ${rs1} $type -w 1 ipvsadm -a -t ${vip}:${port} -r ${rs2} $type -w 3 echo "The VS Server is Ready!" ;; stop) ipvsadm -C ifconfig $iface down echo "The VS Server is Canceled!" ;; *) echo "Usage: $(basename $0) start|stop" exit 1 ;; esac bash lvs_dr_vs.sh start #执行脚本
vim /etc/sysconfig/network-scripts/ifcfg-eth0 IPADDR=192.168.27.144 NETMASK=255.255.255.0 GATEWAY=192.168.27.200
编写名称为lvs_dr_vs.sh的脚本,内容如下
#!/bin/bash vip=10.0.0.100 mask='255.0.0.0' dev=lo:1 case $1 in start) echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce ifconfig $dev $vip netmask $mask #broadcast $vip up #route add -host $vip dev $dev echo "The RS Server is Ready!" ;; stop) ifconfig $dev down echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce echo "The RS Server is Canceled!" ;; *) echo "Usage: $(basename $0) start|stop" exit 1 ;; esac bash lvs_dr_vs.sh start #执行脚本
vim /etc/sysconfig/network-scripts/ifcfg-eth0 IPADDR=192.168.27.145 NETMASK=255.255.255.0 GATEWAY=192.168.27.200
编写名称为lvs_dr_vs的脚本,内容如下
#!/bin/bash vip=10.0.0.100 mask='255.0.0.0' dev=lo:1 case $1 in start) echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore #忽略arp广播,不给予回应 echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce #尽量避免将接口信息向非直接连接网络进行通告 echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce ifconfig $dev $vip netmask $mask #broadcast $vip up #route add -host $vip dev $dev echo "The RS Server is Ready!" ;; stop) ifconfig $dev down echo 0 > /proc/sys/net/ipv4/conf/all/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/lo/arp_ignore echo 0 > /proc/sys/net/ipv4/conf/all/arp_announce echo 0 > /proc/sys/net/ipv4/conf/lo/arp_announce echo "The RS Server is Canceled!" ;; *) echo "Usage: $(basename $0) start|stop" exit 1 ;; esac bash lvs_dr_vs.sh start #执行脚本
6 在客户端访问验证即可;(默认支持长连接模式)
yum install mod_ssl #此处使用自定义证书,安装这个包就附带证书
systemctl restart httpd #重启后会发现多了一个443的端口
ipvsadm -A -t 10.0.0.100:443 -s rr ipvsadm -a -t 10.0.0.100:443 -r 192.168.27.144:443 -g #-g代表dr模式 ipvsadm -a -t 10.0.0.100:443 -r 192.168.27.145:443 -g
curl -k https://10.0.0.100
IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 10.0.0.100:80 wrr -> 192.168.27.144:80 Route 1 0 0 -> 192.168.27.145:80 Route 3 0 0 TCP 10.0.0.100:443 rr -> 192.168.27.144:443 Route 1 0 0 -> 192.168.27.145:443 Route 1 0 0
解答:将80端口和443端口贴上同一个标签,使用以下命令 1 在VS服务器上操作 iptables -t mangle -A PREROUTING -d 10.0.0.100 -p tcp -m multiport --dports 80,443 -j MARK --set-mark 10 #-t:指定防火墙表,打标签要指定mangle表 #-A:指定防火墙链,VS服务器要转发请求 #-d:目标IP地址,此处为VIP地址 #-p:指定协议类型 #-m multiport:表示启用多端口扩展 #MARK --set-mark:使用标签并且更改标签值为10 2 ipvsadm -A -f 10 -s rr #-A:设置集群环境 #-f:使用标签模式 #10:标签名称 #-s:指定算法类型 ipvsadm -a -f 10 -r 192.168.27.144 -g ipvsadm -a -f 10 -r 192.168.27.145 -g 3 ipvsadm -ln #查看策略会显示以下内容 IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn FWM 10 rr -> 192.168.27.144:0 Route 1 0 0 -> 192.168.27.145:0 Route 1 0 0
解答:实现持久连接;(持久连接:在一定时间内,同一个客户端发来的请求始终为一台服务器响应,与调度算法无关,默认360秒) 1 在VS服务器上操作,接着上面的问题继续操作; ipvsadm -E -f 10 -s rr -p [timeout] #-p:开启持久连接,后面也可以手动指定时间[timeout] ipvsadm -ln #再次查看,就会多出"persistent 360"的字样 IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn FWM 10 rr persistent 360 -> 192.168.27.144:0 Route 1 0 0 -> 192.168.27.145:0 Route 1 0 0 2 每客户端持久:端口写0,无论什么端口都往同一个地点调度 1 ipvsadm -A -t 10.0.0.100:0 -s rr -p ipvsadm -a -t 10.0.0.100:0 -r 192.168.27.144 -g ipvsadm -a -t 10.0.0.100:0 -r 192.168.27.145 -g
转载于:https://blog.51cto.com/kaikai0720/2097555