在RHEL4系统中配置LVS负载均衡群集
2007-07-13 kenthy#qingdaonews.com
################################################################
系统环境:RHEL4 [ 2.6.9-5.EL ]
软件环境:
linux-2.6.22.1.tar.bz2 []
ipvsadm-1.24.tar.gz []
网络结构:
Linux Virtual Server:
eth0: 173.17.17.1/24
eth1: 192.168.1.254/24
Real Servers:
RServer1: eth0: 192.168.1.1/24 Gateway: 192.168.1.254
RServer2: eth0: 192.168.1.2/24 Gateway: 192.168.1.254
RServer3: eth0: 192.168.1.3/24 Gateway: 192.168.1.254
RServer4: eth0: 192.168.1.4/24 Gateway: 192.168.1.254
Internet
|
-------+---------
| 173.17.17.1 |
| LVS Server |
| 192.168.1.254 |
-------+---------
|
+-----------------+---------+-------+-----------------+
| | | |
-------+------- -------+------- -------+------- -------+-------
| Rserver1 | | Rserver2 | | Rserver3 | | Rserver4 |
| 192.168.1.1 | | 192.168.1.2 | | 192.168.1.3 | | 192.168.1.4 |
--------------- --------------- --------------- ---------------
目标功能:
当外部访问173.17.17.1:80时,基于nat的方式将负载分流至内部的4台真实web服务器上。
################################################################
1、配置Linux Virtual Server
1> 重新编译内核,增加对IP vs的支持,使用新内核启动系统
[ 注:如果有编译好的ip_vs_rr模块,也可以直接使用模块文件,跳过后续内核编译步骤。
如: insmod /lib/modules/2.6.9-5.EL/kernel/net/ipv4/ipvs/ip_vs_rr.ko ]
# tar jxvf linux-2.6.22.1.tar.bz2 -C /usr/src/
# ln -s /usr/src/linux-2.6.22.1 /usr/src/linux
# cd /usr/src/linux
# cp /boot/config-2.6.9-5.EL ./.config
# make menuconfig //增加对IP:Virtual Server Configuration的支持,编进内核
# make bzImage && make modules && make modules_install
# make install
# reboot
2> ipvsadm 安装:
# tar zxvf ipvsadm-1.24.tar.gz -C /usr/src/
# cd /usr/src/ipvsadm-1.24
# make all
# make install
# ipvsadm --version
3> 创建LVS配置脚本
# vi /opt/lvs-up.sh
#!/bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/eth0/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/eth1/send_redirects
LVSADM='/sbin/ipvsadm'
$LVSADM -C
$LVSADM -A -t 173.17.17.1:80 -s rr
$LVSADM -a -t 173.17.17.1:80 -r 192.168.1.1:80 -m -w 1 //-m表示使用nat方式,如果是-g则表示dr方式
$LVSADM -a -t 173.17.17.1:80 -r 192.168.1.2:80 -m -w 1 //-w 1表示服务器的权重
$LVSADM -a -t 173.17.17.1:80 -r 192.168.1.3:80 -m -w 1 //-w 0将该rserver设置为静止状态,以便于维护
$LVSADM -a -t 173.17.17.1:80 -r 192.168.1.4:80 -m -w 1
# chmod a+x /opt/lvs-up.sh
# echo '/opt/lvs-up.sh' >> /etc/rc.d/rc.local
# /opt/lvs-up.sh
2、配置Real Servers
配置并启动Web服务[过程略],网关均指向192.168.1.254。[网站内容同步可以简单的采用NFS共享或者使用rsync]
3、验证LVS的实现
使用外部客户端浏览器多次访问,并检查4台真实web服务器的访问日志
或在173.17.17.1服务器上执行不带参数的ipvsadm命令,应能看到如下内容:
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP redhat.com:http rr
-> 192.168.1.1:http Masq 1 0 0
-> 192.168.1.2:http Masq 1 0 0
-> 192.168.1.3:http Masq 1 0 0
-> 192.168.1.4:http Masq 1 0 0
################################################################
附: 基于直接路由(DR)的方式实现负载均衡
系统及软件环境:
同上。LVS服务器和Real Servers均需要配置内核对IPVS的支持
网络结构:
Linux Virtual Server:
eth0: 173.17.17.1/24
Real Servers:
RServer1: eth0: 173.17.17.5/24
RServer2: eth0: 173.17.17.6/24
RServer3: eth0: 173.17.17.7/24
RServer4: eth0: 173.17.17.8/24
Internet
|
-------+---------
| 173.17.17.1 |
| LVS Server |
-------+---------
|
+-----------------+---------+-------+-----------------+
| | | |
-------+------- -------+------- -------+------- -------+-------
| Rserver1 | | Rserver2 | | Rserver3 | | Rserver4 |
| 173.17.17.5 | | 173.17.17.6 | | 173.17.17.7 | | 173.17.17.8 |
--------------- --------------- --------------- ---------------
********************************************
1、配置Linux Virtual Server
1> 重新编译内核,增加对IP vs的支持,使用新内核启动系统[参考前例]
2> ipvsadm 安装[参考前例]
3> 创建LVS配置脚本
# vi /opt/lvs-up.sh
#!/bin/bash
echo 1 > /proc/sys/net/ipv4/ip_forward
echo 0 > /proc/sys/net/ipv4/conf/all/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/default/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/eth0/send_redirects
echo 0 > /proc/sys/net/ipv4/conf/eth1/send_redirects
LVSADM='/sbin/ipvsadm'
$LVSADM -C
$LVSADM -A -t 173.17.17.1:80 -s rr
$LVSADM -a -t 173.17.17.1:80 -r 173.17.17.5:80 -g -w 1
$LVSADM -a -t 173.17.17.1:80 -r 173.17.17.6:80 -g -w 1
$LVSADM -a -t 173.17.17.1:80 -r 173.17.17.7:80 -g -w 1
$LVSADM -a -t 173.17.17.1:80 -r 173.17.17.8:80 -g -w 1
# chmod a+x /opt/lvs-up.sh
# echo '/opt/lvs-up.sh' >> /etc/rc.d/rc.local
# /opt/lvs-up.sh
2、配置Real Servers
1> 重新编译内核,增加对IP vs的支持,使用新内核启动系统[参考前例]
2> 创建RS配置脚本
# vi /opt/rs-up.sh
#!/bin/bash
ifconfig lo:0 173.17.17.1 netmask 255.255.255.255 //添加虚拟地址
route add -host 173.17.17.1 dev lo:0 //添加路由设置
echo 0 > /proc/sys/net/ipv4/ip_forward
echo 1 > /proc/sys/net/ipv4/conf/lo/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/lo/arp_announce
echo 1 > /proc/sys/net/ipv4/conf/all/arp_ignore
echo 2 > /proc/sys/net/ipv4/conf/all/arp_announce
# chmod a+x /opt/rs-up.sh
# /opt/rs-up.sh
3> 配置并启动Web服务[过程略]
3、验证LVS的实现[参考前例]