[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