|
linux集群 负载均衡实验笔记
一、网络拓扑图:
二、虚拟机配置
在一台物理电脑上建立三台虚拟机,本机为Windows操作系统,配192.168.1.0网段的地址,三台虚拟机的操作系统都装的CentOS5.4。其中一台做负载均衡器,这台虚拟机需要配两张网卡。另外两台只要一张网卡,同时装上httpd。
a) 负载均衡器的配置
第一张网卡配置:
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0
BOOTPROTO=static
BROADCAST=192.168.1.255
HWADDR=00:0C:29:57:28:8e
IPADDR=192.168.1.12
NETMASK=255.255.255.0
NETWORK=192.168.1.0
GATEWAY=192.168.1.1
ONBOOT=yes
第二张网卡配置:
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth1
BOOTPROTO=static
BROADCAST=192.168.11.255
HWADDR=00:0C:29:57:28:89
IPADDR=192.168.11.1
NETMASK=255.255.255.0
NETWORK=192.168.11.0
ONBOOT=yes
b) 实施服务器配置
第一台web网卡配置:
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0
BOOTPROTO=static
BROADCAST=192.168.1.255
HWADDR=00:0C:29:57:28:82
IPADDR=192.168.11.2
NETMASK=255.255.255.0
NETWORK=192.168.11.0
GATEWAY=192.168.11.1
ONBOOT=yes
第二台web网卡配置:
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0
BOOTPROTO=static
BROADCAST=192.168.1.255
HWADDR=00:0C:29:57:28:84
IPADDR=192.168.11.3
NETMASK=255.255.255.0
NETWORK=192.168.11.0
GATEWAY=192.168.11.1
ONBOOT=yes
这里需要说明下:每个mac不同。根据实际情况填写。如果虚拟机的网卡启动有问题参考:http://hi.baidu.com/wxlccsu/blog/item/f58b4acf2c09b5400eb34561.html
三、实施配置
1、在负载均衡器上
#yum –y install ipvsadm (如果有问题检查网络或yum源)
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
addons | 951 B 00:00
base | 2.1 kB 00:00
extras | 2.1 kB 00:00
mod-pagespeed | 951 B 00:00
update | 1.9 kB 00:00
Setting up Install Process
Resolving Dependencies
There are unfinished transactions remaining. You might consider running yum-complete-transaction first to finish them.
The program yum-complete-transaction is found in the yum-utils package.
--> Running transaction check
---> Package ipvsadm.i386 0:1.24-10 set to be updated
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
ipvsadm i386 1.24-10 base 31 k
Transaction Summary
================================================================================
Install 1 Package(s)
Upgrade 0 Package(s)
Total download size: 31 k
Downloading Packages:
ipvsadm-1.24-10.i386.rpm | 31 kB 00:01
Running rpm_check_debug
Running Transaction Test
Finished Transaction Test
Transaction Test Succeeded
Running Transaction
Installing : ipvsadm 1/1
Installed:
ipvsadm.i386 0:1.24-10
Complete!
# sysctl -w net.ipv4.ip_forward=1打开路由功能
#ipvsadm -C 清除原来的集群策略
#ipvsadm -A -t 192.168.1.12:80 -s rr //-A 增加一个集群,-t tcp协议,-s 算法
#ipvsadm -L n // 查看集群成员
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.10:80 rr
#ipvsadm -a -t 192.168.1.12:80
//-a 增加一个realserver -t tcp协议的ip -m 地址伪装命中次数
# ipvsadm -a -t 192.168.1.12:80 -r 192.168.11.3:80 -m -w 1
#ipvsadm -L n // 再次查看集群成员
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 192.168.1.10:80 rr
-> 192.168.11.3:80 Masq 1 0 0
-> 192.168.11.2:80 Masq 1 0 0
或者直接配置文件
#vi/etc/sysconfig/ipvsadm
添加如下内容
-A -t 192.168.1.12:80 -s rr
-a -t 192.168.1.12:80 -r 192.168.11.2:80 -m -w 1
-a -t 192.168.1.12:80 -r 192.168.11.3:80 -m -w 1
第一条为VIP的地址以及负载均衡的算法
-s参数为选择负载均衡算法
*轮叫调度(Round-RobinScheduling)RR
*加权轮叫调度(WeightedRound-RobinScheduling)WRR
*最小连接调度(Least-ConnectionScheduling)LCS
*加权最小连接调度(WeightedLeast-ConnectionScheduling)WLC
*基于局部性的最少链接(Locality-BasedLeastConnectionsScheduling)LLCS
*带复制的基于局部性最少链接(Locality-BasedLeastConnectionswithReplicationScheduling)LLCRS
*目标地址散列调度(DestinationHashingScheduling)DHS
*源地址散列调度(SourceHashingScheduling)SHS
后面为增加各个节点
-w参数为每个节点的权值
启动ipvsadm
#/etc/init.d/ipvsadmrestart
2、在两台web服务器上同时启动httpd服务
#/etc/init.d/httpd start
在第一台
# cd /var/www/html/
#echo “this is realserv 1” > index.html
在第二台
# cd /var/www/html/
#echo “this is realserv 2” > index.html
四、测试
在Windows客户机的浏览器中输入192.168.1.12 可以看到浏览器先显示” this is realserv 1”,再刷新后显示“this is realserv 2”
注:如果ipvsadm 是源码的话,那就要考虑到系统内核的版本。要找到与内核版本匹配的源码安装包。下载地址:http://www.linuxvirtualserver.org/software/ipvs.html