一:安装配置 LVS+DR+Keepalived
client 访问的地址 VIP
192.168.133.100
负载服务器master真实IP
192.168.133.128
负载服务器backup真实IP
192.168.133.150
负载服务器虚拟IP
192.168.133.100
后端WEB服务器IP
192.168.133.128
后端WEB服务器IP
192.168.133.150
系统: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
我这里是直接安装的RHEL4.0自带的ipvsadm-1.24-6.i386.rpm
wget http://www.keepalived.org/software/keepalived-1.1.18.tar.gz
2.安装ipvsadm(负载均衡器都要配置master和backup)
从Linux内核版本2.6起,ip_vs code已经被整合进了内核中,因此,只要在编译内核的时候选择了ipvs的功能,您的Linux即能支持LVS。Linux
2.4.23以后的内核版本也整合了ip_vs code,但如 果是更旧的内核版本,您得自己手动将ip_vs
code整合进内核原码中,并重新编译内核方可使用lvs。
我使用的RHEL4.0
这个版本支持ipvsadm,所以无需更改内核配置 rpm -ivh ipvsadm-1.24-6.i386.rpm
然后使用ipvsadm -v
ipvsadm v1.24 2005/12/10 (compiled with popt and
IPVS v1.2.1)
则Ipvsadm安装成功
3.安装keepalived负载均衡器都要配置master和backup)
以下建议完全参照,否则会有一些问题
tar zxvf keepalived-1.1.18tar.gz
cd
keepalived-1.1.18
./configure
--prefix=/usr/local/keepalived
make
make install
cp
/usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
cp
/usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
mkdir
/etc/keepalived
cp /usr/local/keepalived/etc/keepalived/keepalived.conf
/etc/keepalived/
cp /usr/local/keepalived/sbin/keepalived
/usr/sbin/
启动keepalived
service keepalived start|stop|restart
4.配置master和backup
A.开启ip_forward 转发功能:
echo 1
>/proc/sys/net/ipv4/ip_forward
默认是0,关闭ip转发;这里需要开启,所以设置值为1
B.配置keepalived.conf 配置文件 (backup
192.168.133.150)
! Configuration File for keepalived
global_defs {
notification_email
{
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.133.128
smtp_connect_timeout
30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
#配置负载均衡器的 主和备 ,并做相关设置
state
BACKUP
#备份服务器上将MASTER改为BACKUP
interface
eth0
#HA监测网络接口
virtual_router_id 51
#主、备机的virtual_router_id必须相同
priority
100
#主、备机取不同的优先级,主机值较大,备份机值较小
advert_int
1 #VRRP
Multicast广播周期秒数
authentication
{
auth_type
PASS
#VRRP认证方式
auth_pass
1111 #VRRP口令字
}
virtual_ipaddress
{
192.168.133.100
#(如果有多个VIP,继续换行填写.)
}
}
virtual_server 192.168.133.100 80 {
VIP设置
delay_loop
2
#每隔2秒查询realserver状态
lb_algo
rr
#lvs 算法
lb_kind
DR
#Direct Route
nat_mask
255.255.255.0
persistence_timeout
50
#同一IP的连接60秒内被分配到同一台realserver
protocol
TCP
#用TCP协议检查realserver状态
real_server 192.168.133.128 80 {
真实主机1配置
weight
100
#设置后台主机的权重
TCP_CHECK
{
#TCP协议检查
connect_timeout
3
#
nb_get_retry
3
#
delay_before_retry 3
#
}
}
real_server 192.168.133.150 80 {
真实主机2配置
weight
1
#设置后台主机的权重
TCP_CHECK
{
#TCP协议检查
connect_timeout
3
#
nb_get_retry
3
#
delay_before_retry 3
#
}
}
}
配置keepalived.conf 配置文件 (master 192.168.133.128)
! Configuration
File for keepalived
global_defs {
notification_email
{
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.133.128
smtp_connect_timeout
30
router_id LVS_DEVEL
}
vrrp_instance VI_1
{
state MASTER
interface
eth0
virtual_router_id 51
priority
100
advert_int 1
authentication
{
auth_type
PASS
auth_pass
1111
}
virtual_ipaddress
{
192.168.133.100
}
}
virtual_server 192.168.133.100 80 {
delay_loop
2
lb_algo rr
lb_kind
DR
nat_mask 255.255.255.0
persistence_timeout 50
protocol TCP
real_server 192.168.133.128 80
{
weight
100
TCP_CHECK
{
connect_timeout
3
nb_get_retry
3
delay_before_retry 3
}
}
real_server 192.168.133.150 80
{
weight
1
TCP_CHECK
{
connect_timeout
3
nb_get_retry
3
delay_before_retry 3
}
}
}
5.配置 VIP: master 配置一下就可以
vim
/etc/sysconfig/network-scripts/ifcfg-lo:0
DEVICE=lo:0
IPADDR=192.168.133.100
NETMASK=255.255.255.255
ONBOOT=yes
NAME=loopback
重启网卡,启动keepalived
显示如下成功!
service network restart
service
keepalived restart
6.配置ipvsadm(配置master和backup)得先启动keepalived
添加负载均衡器
/sbin/ipvsadm -A -t
192.168.133.100:80 -s rr
增加realserver
/sbin/ipvsadm -a -t
192.168.133.100:80 -r 192.168.133.128:80 -g #192.168.133.128
是真实机的ip
/sbin/ipvsadm -a -t 192.168.133.100:80 -r 192.168.133.150:80
-g #192.168.133.150 是真实机的ip
route add -host 192.168.133.100
dev lo:0
这些命令只能使用一次,第二次会有提示已经输入该命令
7.配置realserver (web1和web2)
配置vip 地址
/sbin/ifconfig lo
192.168.133.100 netmask 255.255.255.255 broadcast 192.168.133.100 up
启用loopback
route add -host 192.168.133.100 dev
lo 增加一条路由
指向端口和IP地址
8.在realserver 安装测试httpd(只为显示结果)
yum install httpd –y
service
httpd start
vim /var/www/html/index.html
this is 192.168.133.128/150
9.在master负载均衡器上查看
[root@master rc.d]# ipvsadm -L -n
IP Virtual Server
version 1.2.0 (size=4096)
Prot LocalAddress:Port Scheduler Flags
->
RemoteAddress:Port
Forward Weight ActiveConn InActConn
TCP 192.168.133.100:80 rr
persistent
50
注意这里的rr persistent 50 其中rr 代表轮询(round robin, rr); persistent
50:表示同一IP的连接60秒内被
分配到同一台realserver
->
192.168.133.150:80
Route 1
0
0
这里realserver只有一台,实际我设置的两台,但是配置权重weight的时候,不一样,128的权重是100;150的权重是
1, 这里选择权重最小的150.
TCP 10.10.10.3:1358 rr persistent
50
TCP 10.10.10.2:1358 rr persistent 50
->
192.168.200.200:1358
Masq 1
0 0
10.访问:192.168.133.100
查看IE显示状态,正常应该显示
默认应该指向
192.168.133.150 主机的/var/www/html/index.php
如果关闭192.168.133.150的httpd服务,会指向192.168.133.128主机的/var/www/html/index.php
出现一下表示安装成功
刷新几次。
当master服务器down的时候,backup自动会接替服务,当master起来的时候,backup会自动断掉。