ipvsadm 是安装在调度器上,要设置规则。
安装前准备:
1台 clinet
1台 director server
2 台 real server
CIP : 201.1.1.200
vip : 201.1.1.100
DIP : 192.168.4.100
RIP : 192.168.4.11/12
real server 的GW : 192.168.4.100
[root@ds1 ~]# yum -y install ipvsadm
添加虚拟服务器:
[root@ds1 ~]# ipvsadm -A -t 201.1.1.100:80 -s rr
[root@ds1 ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 201.1.1.100:80 rr
添加真实服务器
[root@ds1 ~]# ipvsadm -a -t 201.1.1.100:80 -r 192.168.4.11 -m
[root@ds1 ~]# ipvsadm -a -t 201.1.1.100:80 -r 192.168.4.12 -m
查看端口转发:
[root@ds1 ~]# cat /proc/sys/net/ipv4/ip_forward
1
打开ip转发:(永久配置)
[root@ds1 ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
:wq
[root@ds1 ~]# sysctl -p
net.ipv4.ip_forward = 1
查看LVS状态:
[root@ds1 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 201.1.1.100:80 rr
-> 192.168.4.11:80 Masq 1 0 0
-> 192.168.4.12:80 Masq 1 0 0
2.配置后端服务器(real server):
1.在后端服务器,安装httpd,启动httpd服务
[root@rs1 ~]# yum -y install httpd
[root@rs2 ~]# yum -y install httpd
[root@rs1 ~]# echo rs1 > /var/www/html/index.html
[root@rs1 ~]# systemctl restart httpd
[root@rs2 ~]# echo rs2 > /var/www/html/index.html
[root@rs2 ~]# systemctl restart httpd
配置网关:
[root@rs1 ~]# nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.4.11/24 ipv4.gateway 192.168.4.100 connection.autoconnect yes
[root@rs1 ~]# nmcli connection up eth0
[root@rs1 ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.4.100 0.0.0.0 UG 100 0 0 eth0
192.168.4.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@rs2 ~]# nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.4.12/24 ipv4.gateway 192.168.4.100 connection.autoconnect yes
[root@rs2 ~]# nmcli connection up eth0
route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.4.100 0.0.0.0 UG 100 0 0 eth0
192.168.4.0 0.0.0.0 255.255.255.0 U 100 0 0 eth0
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
2.调度服务器访问:
[root@ds1 ~]# curl http://192.168.4.11
rs1
[root@ds1 ~]# curl http://192.168.4.12
rs2
客户端访问检测
[root@client ~]# curl http://201.1.1.100
rs2
[root@client ~]# curl http://201.1.1.100
rs1
[root@client ~]# curl http://201.1.1.100
rs2
抓包:
[root@rs1 ~]# tcpdump -nn port 80
[root@client ~]# curl http://201.1.1.100
rs1
[root@client ~]# curl http://201.1.1.100
rs2
[root@ds1 ~]# tcpdump -i eth2 -nn port 80 //指定网卡抓包
换一种调度算法:
[root@ds1 ~]# ipvsadm -C
[root@ds1 ~]# ipvsadm -ln
[root@ds1 ~]# ipvsadm -A -t 201.1.1.100:80 -s wrr
[root@ds1 ~]# ipvsadm -a -t 201.1.1.100:80 -r 192.168.4.11 -m -w 2
[root@ds1 ~]# ipvsadm -a -t 201.1.1.100:80 -r 192.168.4.12 -m -w 1
++++++++++++++++++++++++++++++++++++++++++++++++++
-r 是真实服务器的地址 -m 指定mat 模式 -w 是权重
[root@ds1 ~]# ipvsadm -ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 201.1.1.100:80 wrr
-> 192.168.4.11:80 Masq 2 0 0
-> 192.168.4.12:80 Masq 1 0 0
客户端访问:
[root@client ~]# curl http://201.1.1.100
rs1
[root@client ~]# curl http://201.1.1.100
rs1
[root@client ~]# curl http://201.1.1.100
rs2
[root@client ~]# curl http://201.1.1.100
rs1
[root@client ~]# curl http://201.1.1.100
rs1
++++++++++++++++++++++++++++++++++++++++++
192.168.4.12 的权重比较大,
保存 ipvsadm 规则 开机自动运行ipvsadm规则:
[root@ds1 ~]# ipvsadm-save -n > /etc/sysconfig/ipvsadm
[root@ds1 ~]# cat /etc/sysconfig/ipvsadm
-A -t 201.1.1.100:80 -s wrr
-a -t 201.1.1.100:80 -r 192.168.4.11:80 -m -w 2
-a -t 201.1.1.100:80 -r 192.168.4.12:80 -m -w 1
2)启动服务
[root@ds1 ~]# service ipvsadm start
Redirecting to /bin/systemctl start ipvsadm.service
3)关停服务:
[root@ds1 ~]# service ipvsadm stop
Redirecting to /bin/systemctl stop ipvsadm.service
[root@ds1 ~]# chkconfig ipvsadm on
注意:正在将请求转发到“systemctl enable ipvsadm.service”。
Created symlink from /etc/systemd/system/multi-user.target.wants/ipvsadm.service to /usr/lib/systemd/system/ipvsadm.service.