CentOS 5.4 64-bit
服务器8台,定义
130.130.1.0/24 为公网
10.20.0.0/24 为内网
Hostname:lb1
ech0: 130.130.1.51
eth1: 10.20.0.81
该机角色为主负载均衡器,负责分发所有用户请求给webserver
Hostname:lb2
ech0: 130.130.1.52
eth1: 10.20.0.82
该机角色为备负载均衡器,当主负载均衡器不能提供服务时由该机接管服务;负责IDC 正式环境的上线前的程序最后测试。
负载均衡部署-LB1
——————————-
ipvsadm-1.24.tar.gz
keepalived-1.1.18.tar.gz
mkdir –p /soft
cd /soft/
上传软件
注:下面所有服务器配置,安装的源码包都用此路径,下文不再强调。
此次LB的VIP,设为:130.130.1.50
lsmod |grep ip_vs #检查是否存在ip_vs模块
uname -r #查看内核版本
tar zxvf ipvsadm-1.24.tar.gz
cd ipvsadm-1.24
ln -s /usr/src/kernels/2.6.18-164.el5-x86_64/ /usr/src/linux
make
make install
cd ..
find / -name ipvsadm #查看安装后路径,可选
/sbin/ipvsadm #加载ip_vs模块
lsmod |grep ip_vs
若看到上面信息,则ip_vs模块加载成功
——————————————
————————–
tar zxvf keepalived-1.1.18.tar.gz
cd keepalived-1.1.18
./configure
make
make install
cd ..
cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/ #设置为service方式启动
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/
/etc/init.d/keepalived start #启动keepalived
echo “/etc/init.d/keepalived start”;/etc/rc.local #添加为开机自启动
sed -i 's#net.ipv4.ip_forward = 0#net.ipv4.ip_forward = 1#' /etc/sysctl.conf #开启内核转发功能
sysctl –p #查看是否开启内核转发
————————————
mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak #备份原始配置文件
vi /etc/keepalived/keepalived.conf #分别在LB1,LB2上建立配置文件
#!/bin/bash
Configuration File for keepalived
global_defs {
notification_email {
[email protected] #填写自己的邮箱出现故障接收报警邮件用
}
notification_email_from [email protected]
#smtp_server 192.168.200.1
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_01 #备份服务器改为LVS_02
}
vrrp_instance VI_1 {
state MASTER #备份服务器上MASTER为BACKUP
interface eth0
lvs_sync_daemon_inteface eth1
virtual_router_id 51
priority 100 #备份服务上优先级要低于100,如改为90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
#192.168.200.16
130.130.1.50 #这里填写VIP地址,也可添加多个VIP
}
}
virtual_server 130.130.1.50 80 { #这里填写VIP地址
delay_loop 6
lb_algo wrr
lb_kind DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 130.130.1.53 80 { #这里是LAMP服务器对外的IP
weight 1
TCP_CHECK {
connect_timeout 8
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 130.130.1.54 80 { #这里是LNMP对外的IP
weight 1
TCP_CHECK {
connect_timeout 8
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
注:以上keepalived.conf文件在LB1,LB2上都需建立;红色标记处,是在LB2使用该配置文件需修改的地方
/etc/init.d/keepalived restart
配置完毕!
使用 ip add 命令查看LB1的IP,若出现上图红圈标记的IP,即配置成功!