[root@wadeson ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
HWADDR=00:0C:29:4D:19:A6
TYPE=Ethernet
UUID=e00a22ce-9c20-4495-896f-7323b984425e
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=172.16.118.249
GATEWAY=172.16.118.254
NETMASK=255.255.255.0html
[root@wadeson ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
UUID=e00a22ce-9c20-4495-896f-7323b984425e
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=none
IPADDR=192.168.23.2
NETMASK=255.255.255.0nginx
因为须要在此主机上配置负载集群服务,须要安装ipvsadm:web
yum -y install ipvsadm算法
后端RS配置:后端
RS1:192.168.23.4,eth0缓存
RS2:192.168.23.5,eth1服务器
具体配置:网络
到这里,全部关于ip网络的配置已经完成了,确保RS后端服务器的web服务已经能够正常访问了:session
[root@wadeson ~]# curl http://192.168.23.4
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# curl http://192.168.23.5
<h1>test 192.168.23.5 html</h1>负载均衡
而后开始在director上建立集群服务:
[root@wadeson ~]# ipvsadm -A -t 172.16.118.249:80 -s rr
ipvsadm -A|E -t|u|f service-address [-s scheduler]
而后添加RS到集群中:
[root@wadeson ~]# ipvsadm -a -t 172.16.118.249:80 -r 192.168.23.4 -m 这里RS默认的端口为80,由于
支持端口映射,因此这里能够修改成8080
ipvsadm -a|e -t|u|f service-address -r server-address
[-g|i|m] [-w weight] [-x upper] [-y lower] -m为指定为NAT模式
查看列表信息:
[root@wadeson ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.118.249:80 rr
-> 192.168.23.4:80 Masq 1 0 0
再次添加一个RS:
[root@wadeson ~]# ipvsadm -a -t 172.16.118.249:80 -r 192.168.23.5 -m
[root@wadeson ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.118.249:80 rr
-> 192.168.23.4:80 Masq 1 0 0
-> 192.168.23.5:80 Masq 1 0 0
集群服务,RS配置已经完成,而后进行访问:
[root@wadeson ~]# curl http://172.16.118.249
<h1>test 192.168.23.5 html</h1>
[root@wadeson ~]# curl http://172.16.118.249
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# curl http://172.16.118.249
<h1>test 192.168.23.5 html</h1>
[root@wadeson ~]# curl http://172.16.118.249
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# curl http://172.16.118.249
<h1>test 192.168.23.5 html</h1>
[root@wadeson ~]# curl http://172.16.118.249
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.118.249:80 rr
-> 192.168.23.4:80 Masq 1 0 5
-> 192.168.23.5:80 Masq 1 0 5
负载均衡调度算法:
修改调度算法,将rr轮询算法改成sh:
[root@wadeson ~]# ipvsadm -E -t 172.16.118.249:80 -s sh
[root@wadeson ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.118.249:80 sh
-> 192.168.23.4:80 Masq 1 0 0
-> 192.168.23.5:80 Masq 1 0 0
而后观察效果:
[root@wadeson ~]# curl http://172.16.118.249:80
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# curl http://172.16.118.249:80
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# curl http://172.16.118.249:80
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# curl http://172.16.118.249:80
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# curl http://172.16.118.249:80
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# curl http://172.16.118.249:80
<h1>test 192.168.23.4 html</h1>
[root@wadeson ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 172.16.118.249:80 sh
-> 192.168.23.4:80 Masq 1 0 6 只有这一台响应
-> 192.168.23.5:80 Masq 1 0 0
将规则保存下来:
ipvsadm -S > /etc/sysconfig/ipvsadm
清空规则:
ipvsadm -C
[root@wadeson ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn