负载服务器 master 真实 IP        192.168.1.106
负载服务器 backup 真实 IP  192.168.1.114
负载服务器虚拟 IP       192.168.1.10
后端 WEB 服务器 IP                 192.168.1.50
后端 WEB 服务器 IP                192.168.1.60
系统: Redhat 5.4 x86 内核: 2.6.18-164.el5
1. 下载所需要的软件: ipvsadm keepalived
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
wget http://www.keepalived.org/software/keepalived-1.1.18.tar.gz
2. 安装 ipvsadm( 负载均衡器都要配置 master backup)
A .建立内核快捷方式:
ln –s /usr/src/kernels/2.6.18-164.e15-i686 /usr/src/linux
B. 解压 ipvsadm ,编译 ipvsadm
tarzxvfipvsadm-1.24.tar.gz
cd ipvsadm-1.24
make ; make install
C. 显示如下:
[root@master ~]# /sbin/ipvsadm -v
ipvsadm v1.24 2005/12/10 (compiled with popt and IPVS v1.2.1)
Ipvsadm 安装成功
3. 安装 keepalived 负载均衡器都要配置 master backup)
tarzxvf keepalived-1.1.18tar.gz
cd keepalived-1.1.18
./configure --prefix=/usr/local/keepalived
make ; make install
cp etc/rc.d/init.d/keepalived/etc/rc.d/init.d/
cp etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp etc/keepalived/keepalived.conf/etc/keepalived/
cp sbin/keepalived /usr/sbin/
启动 keepalived
service keepalived start|stop|restart
4. 配置 master backup
A .开启 ip_forward 转发功能:
sysctl –w net.ipv4.ip_forward = 1
B .配置 keepalived.conf 配置文件
Vim /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server smtp.139.com
#smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP  
interface eth0 #HA 监测网络接口
virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
priority 90 # 主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 1    #VRRP Multicast 广播周期秒数
authentication {
auth_type PASS #VRRP 认证方式
auth_pass 1111 #VRRP 口令字
}
virtual_ipaddress {
192.168.1.10 #( 如果有多个 VIP ,继续换行填写 .)
}
}
virtual_server 192.168.1.10 80 {
delay_loop 2 # 每隔 2 秒查询 realserver 状态
lb_algo rr    #lvs 算法
lb_kind DR   #Direct Route
persistence_timeout 50 # 同一 IP 的连接 60 秒内被分配到同一台 realserver
protocol TCP # TCP 协议检查 realserver 状态
real_server 192.168.1.50 80 {
weight 100
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
real_server 192.168.1.60 80 {
weight 1
TCP_CHECK {
connect_timeout 3
nb_get_retry 3
delay_before_retry 3
}
}
}
BACKUP 服务器同上配置,先安装 lvs 再按装 keepalived, 仍后配置 /etc/keepalived/keepalived.conf ,只需将红色标示的部分改一下即可 .
5. 配置 VIP:
vim /etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.1.10
NETMASK=255.255.255.255
ONBOOT=yes
NAME=loopback
重启网卡,启动 keepalived 显示如下成功!
6. 配置 ipvsadm (配置 master backup
添加负载均衡器
/sbin/ipvsadm -A -t 192.168.1.10:80 -s rr
增加 realserver
/sbin/ipvsadm -a –t 192.168.1.10:80 -r 192.168.1.50:80 -g #192.168.1.50 是真实机的 ip
/sbin/ipvsadm -a –t 192.168.1.10:80 -r 192.168.1.60:80 -g#192.168.1.60 是真实机的 ip
routeadd -host 192.168.1.10 dev lo:0

7. 配置 realserver web1 web2
配置 vip 地址
/sbin/ifconfig lo:0 192.168.1.10 netmask 255.255.255.255 broadcast 192.168.1.10 up
route add -host 192.168.1.10 dev lo:0
8. realserver 安装测试 httpd (只为显示结果)
yum install httpd –y
service httpd start
vim /var/www/html/index.html
this is 192.168.1.50/60
9. master 负载均衡器上查看
[root@master ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:PortForward Weight ActiveConn InActConn
TCP192.168.1.10:80 rr persistent 50
-> 192.168.1.60:80Route100
-> 192.168.1.50:80Route10000
10 .访问: 192.168.1.10 出现一下表示 安装成功
刷新几次。
master 服务器 down 的时候, backup 自动会接替服务,当 master 起来的时候, backup 会自动断掉。