IPVS/LVS集群测试NAT/DR模式
准备WEB环境
安装web软件
yum install httpd
systemctl start httpd
[root@server1 ~]# echo "Welcom to server1.exapmle.com! " > /var/www/html/index.html
[root@server2 ~]# echo "Welcom to server2.exapmle.com! " > /var/www/html/index.html
测试web访问
默认配置时,端口监听在IPV6上
[root@server1 etc]# grep Listen /etc/httpd/conf/httpd.conf
Listen 80
[root@server2 ~]# ss -tuan4 |grep 80
[root@server2 ~]# ss -tuan |grep 80
tcp LISTEN 0 128 :::80 :::*
修改为IPV4
Listen 0.0.0.0:80
[root@server1 etc]# ss -tuan4|grep 80
tcp LISTEN 0 128 *:80 *:*
[root@server1 etc]# ss -tuan6|grep 80
[root@server1 etc]#
两种模式下都可以在本机正常访问 links http://127.0.0.1/
但在其他机器无法访问 http://s1/
修改防火墙,增加http服务
[root@server1 etc]# firewall-cmd --permanent --add-service=http
success
[root@server1 etc]# firewall-cmd --reload
success
[root@server1 etc]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: ssh dhcpv6-client http
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
通过firefox访问http://192.168.122.213/及http://s1/正常
[root@server2 ~]# ss -tuan4|grep 80
[root@server2 ~]# ss -tuan6|grep 80
tcp LISTEN 0 128 :::80 :::*
[root@server2 ~]# grep Listen /etc/httpd/conf/httpd.conf |tail -1
Listen 80
按上述方法放开防火墙后,在其他机器可以正常访问。
** 因此,Listen 80这样的配置是可以支持IPV4的。 **
LVS软件
把LVS(安装ipvsadm)所在机器成为负载均衡器Director。
安装LVS软件及命令说明
[root@server1 ~]# yum install ipvsadm
[root@server1 ~]# rpm -q ipvsadm
ipvsadm-1.27-7.el7.x86_64
ipvsadm --help可以查看说明
[root@server1 etc]# ipvsadm --help
ipvsadm v1.27 2008/5/15 (compiled with popt and IPVS v1.2.1)
Usage:
ipvsadm -A|E -t|u|f service-address [-s scheduler] [-p [timeout]] [-M netmask] [--pe persistence_engine] [-b sched-flags]
ipvsadm -a|e -t|u|f service-address -r server-address [options]
Commands:
Either long or short options are allowed.
--add-service -A add virtual service with options
--add-server -a add real server with options
--clear -C clear the whole table
--restore -R restore rules from stdin
--save -S save rules to stdout
Options:
--tcp-service -t service-address service-address is host[:port]
--real-server -r server-address server-address is host (and port)
--weight -w weight capacity of real server
调度算法
--scheduler -s scheduler one of rr|wrr|lc|wlc|lblc|lblcr|dh|sh|sed|nq,
the default scheduler is wlc.
三种模式
--gatewaying -g gatewaying (direct routing) (default)
--ipip -i ipip encapsulation (tunneling)
--masquerading -m masquerading (NAT)
NAT模式配置及验证
LVS作为Director,修改目的地址的IP地址
组网说明:
物理机:xycto
virbr0网卡地址为192.168.122.1/24
wlp7s0网卡地址为192.168.1.126/24
新增一个浮动IP地址作为web服务对外 192.168.1.10/24
虚拟机2台作为真实服务器RS,网卡eth0地址如下
192.168.122.213 server1.example.com s1
192.168.122.143 server2.example.com s2
在物理机上配置LVS
查看当前配置
ipvsadm -Ln
清空所有配置
ipvsadm -C
增加浮动IP地址(规划为192.168.1.10)
ipvsadm -A -t 192.168.1.10:80
增加2台真实服务器,采用nat模式,未指定权重相同
ipvsadm -a -t 192.168.1.10:80 -r 192.168.122.213:80 -m
ipvsadm -a -t 192.168.1.10:80 -r 192.168.122.143:80 -m
检查配置
[root@xycto ~]# 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.1.126:80 wlc
-> 192.168.122.143:80 Masq 100 0 0
-> 192.168.122.213:80 Masq 100 0 0
此时用ip addr并不能看到浮动地址,手工添加浮动地址到对外的wlp7s0网卡
[root@xycto ~]# ip address add 192.168.1.10/24 dev wlp7s0
此时在2个RS上可以ping通192.168.1.10
停止防火墙
[root@xycto ~]# systemctl stop firewalld
验证负载均衡正常
[xy@xycto ~]$ curl http://192.168.1.10
Welcom to server2.exapmle.com!
[xy@xycto ~]$ curl http://192.168.1.10
Welcom to server1.exapmle.com!
[xy@xycto ~]$ curl http://192.168.1.10
Welcom to server2.exapmle.com!
[xy@xycto ~]$ curl http://192.168.1.10
Welcom to server1.exapmle.com!
[xy@xycto ~]$ curl http://192.168.1.10
Welcom to server2.exapmle.com!
[xy@xycto ~]$
如果用ie或者firefox测试时,由于缓存原因,可能访问内容不会更新,每次测试需要清空缓存。
防火墙问题,xycto上无法访问vip地址
[root@xycto ~]# curl http://192.168.1.10
抓包[root@xycto ~]# tcpdump -i any -s0 port 80 -vv -w /tmp/a.cap
发现发送SYN后,收到[SYN, ACK]未能发送ACK
1 0 192.168.1.126 192.168.122.213 TCP 76 40356 > http [SYN] Seq=0 Win=43690 Len=0 MSS=65495 SACK_PERM=1 TSval=24387358 TSecr=0 WS=128
3 0 192.168.122.143 192.168.1.126 TCP 76 http > 50826 [SYN, ACK] Seq=0 Ack=1 Win=28960 Len=0 MSS=1460 SACK_PERM=1 TSval=4028679 TSecr=23551161 WS=128
尝试1:在public区域增加接口virbr0
[root@xycto ~]# firewall-config
添加后reload生效。默认是Runtime。
[root@xycto ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: virbr0 wlp7s0
sources:
services: dhcpv6-client ftp http ssh
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
重启firewalld后,配置恢复,策略接口中不包括virbr0
[root@xycto ~]# systemctl start firewalld
[root@xycto ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: wlp7s0
此时是开放了端口转发的
[root@xycto ~]# sysctl -a|grep ip_forward
net.ipv4.ip_forward = 1
防火墙基础欠缺,暂未解决。
DR直接路由模式配置及验证
LVS作为Director,修改目的地址的MAC
组网说明:
物理机:xycto
virbr0网卡地址为192.168.122.1/24
虚拟机2台作为真实服务器RS,网卡eth0地址如下
192.168.122.213 server1.example.com s1
192.168.122.143 server2.example.com s2
在物理机上配置LVS
查看当前配置
ipvsadm -Ln
清空所有配置
ipvsadm -C
增加浮动IP地址(规划为192.168.122.20)
ipvsadm -A -t 192.168.122.20:80
增加2台真实服务器,采用nat模式,未指定权重相同
ipvsadm -a -t 192.168.122.20:80 -r 192.168.122.213:80 -g
ipvsadm -a -t 192.168.122.20:80 -r 192.168.122.143:80 -g
检查配置
[root@xycto ~]# 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.122.20:80 wlc
-> 192.168.122.143:80 Route 1 0 0
-> 192.168.122.213:80 Route 1 0 0
此时用ip addr并不能看到浮动地址,手工添加浮动地址到同RS网段的virbr0网卡
[root@xycto ~]# ip address add 192.168.122.20/32 dev virbr0
为了避免出现IP地址冲突,需要关闭真实服务器的ARP广播,指定网卡为eth0
sysctl net.ipv4.conf.eth0.arp_ignore=1
sysctl net.ipv4.conf.eth0.arp_announce=2
在2台真实服务器的lo网卡配置相同的地址
[root@server1 ~]# ip address add 192.168.122.20/32 dev lo
验证需要注意:
curl http://192.168.122.20
1、在LVS所在服务器验证,会一直没有反应。
估计是自己发给自己搞晕了,我也晕了。
[root@xycto ~]# lsof -i:80 -nP
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
curl 27404 xy 3u IPv4 1091220 0t0 TCP 192.168.122.20:48976->192.168.122.20:80 (SYN_SENT)
2、在真实服务器验证,总是返回本机内容。
3、只在第四台机器上验证。
不能监控故障机器:
[root@server2 ~]# systemctl stop httpd
[root@server2 ~]# curl http://192.168.122.20
curl: (7) Failed connect to 192.168.122.20:80; Connection refused
本机总是出现上述错误。
第四台机器随机出现上面的错误。
查阅资料,LVS只能分发请求,不能对服务器访问情况进行检测。后续安装PaceMaker,Keepalived进行测试,而编译安装HeartBeat3.0.6失败。
PS:《CentOS7系统管理与运维实践》书上对NAT样例没有采用多网卡测试,而且认为LVS会自动监控服务器状态。
参考资料:
Linux系统(五)负载均衡LVS集群之DR模式
《CentOS7系统管理与运维实践》ISBN 978-7-302-42395-9 清华大学出版社