[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.0
[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.0
由於需要在此主機上配置負載叢集服務,需要安裝ipvsadm:
yum -y install ipvsadm
後端RS配置:
RS1:192.168.23.4,eth0
RS2:192.168.23.5,eth1
具體配置:
到這裡,所有關於ip網路的配置已經完成了,確保RS後端伺服器的web服務已經可以正常訪問了:
[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