u 拓扑描述
后端web服务器1:192.168.15.233
后端web服务器2:192.168.15.234
负载服务器master:192.168.15.235
负载服务器backup:192.168.15.236
Lvs负载虚拟服务器:192.168.15.253
u 设定安装环境
[root@localhost keepalived-1.1.20]#yum install kernel-devel kernel-headers openssl-devel
注:保证内核版本一致 若不一致可以采用yum方式更新
[root@hbchen ~]# rpm -qa|grep kernel
kernel-2.6.18-164.el5
kernel-headers-2.6.18-164.el5
kernel-devel-2.6.18-164.el5
u 软件安装配置
ü Ipvsadm安装配置
wget http://www.linuxvirtualserver.org/software/kernel-2.6/ipvsadm-1.24.tar.gz
#tar -zxvf ipvsadm-1.24.tar.gz
#cd ipvsadm-1.24
#建立编译时必须的一个软链接
#ln -s /usr/src/kernels/2.6.9-42.EL-i686/ /usr/src/linux
#编译安装
#make && make install
#确认安装成功
#whereis ipvsadm
注:可以采用yum方式安装
配置直接编写shell文件即可,如下
cat /root/ipvsadm.sh
#!/bin/sh
VIP=192.168.15.253
RIP1=192.168.15.233
RIP2=192.168.15.234
case "$1" in
start)
echo " start LVS "
# set the Virtual IP Address
# /sbin/modprobe ipip
/sbin/ifconfig eth0:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev eth0:0
/sbin/ipvsadm -C
/sbin/ipvsadm -A -t $VIP:80 -s rr
/sbin/ipvsadm -a -t $VIP:80 -r $RIP1:80 -g -w 1
/sbin/ipvsadm -a -t $VIP:80 -r $RIP2:80 -g -w 1
;;
stop)
echo "close LVS Director"
/sbin/ipvsadm -C
/sbin/ifconfig eth0:0 down
# /sbin/modprobe -r ipip
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
ü Keepalived安装配置
wget http://www.keepalived.org/software/keepalived-1.1.15.tar.gz
注:可以到http://www.keepalived.org/中更新最新版本
#yum install kernel-devel kernel-headers openssl-devel
#wget http://www.keepalived.org/software/keepalived-1.1.20.tar.gz
#tar -zxvf keepalived-1.1.15.tar.gz
#cd keepalived-1.1.15
#./configure --with-kernel-dir=/usr/src/kernels/2.6.18-194.32.1.el5-i686
#make && make install
# cp /usr/local/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/
# 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/
#chkconfig –-add keepalived(添加至服务)
注:切记./configure添加kernel指向,如果make出现乱码错误请按如下错误汇总处理
Keepalived配置如下:
主调度器(192.168.15.235)
[root@localhost ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
88fly@163.com
chenhaibo@myhexin.com
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server smtp.163.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.15.253
}
}
virtual_server 192.168.15.253 80 {
delay_loop 10
lb_algo wlc
lb_kind DR
persistence_timeout 50
protocol TCP
# sorry_server 127.0.0.1 80
real_server 192.168.15.233 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.15.234 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
备用调度(192.168.15.236)
[root@localhost ~]# vi /etc/keepalived/keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
88fly@163.com
chenhaibo@myhexin.com
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server smtp.163.com
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state BACKUP
interface eth0
virtual_router_id 51
priority 90
advert_int 5
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.15.253
}
}
virtual_server 192.168.15.253 80 {
delay_loop 10
lb_algo wlc
lb_kind DR
persistence_timeout 50
protocol TCP
# sorry_server 127.0.0.1 80
real_server 192.168.15.233 80 {
weight 3
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.15.234 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
u Real机配置
(192.168.15.233/234,写一shell即可)
[root@localhost ~]# vi /root/real.sh
#!/bin/bash
#description : start realserver
VIP=192.168.15.253
/etc/rc.d/init.d/functions
case "$1" in
start)
echo " start LVS of REALServer"
#/sbin/modprobe ipip
/sbin/ifconfig lo:0 $VIP broadcast $VIP netmask 255.255.255.255 up
/sbin/route add -host $VIP dev lo:0
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
sysctl -p >/dev/null 2>&1
;;
stop)
#echo " stop LVS of REALServer"
echo "close LVS Directorserver"
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/lo/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
#/sbin/ifconfig lo down
#/sbin/modprobe -r ipip
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
u 测试负载切换
ü 验证ipvsadm
启动两台web机器real.sh(./real.sh start)启动调度器上的ipvsadm (/root/ipvsadm start)
测试web服务器是否可以负载切换
ü 验证keepalived高可用性
关闭ipvsadm(/root/ipvsadm stop)启动keepalived 查看日志验证相关信息
1. 关闭web(192.168.15.234)
[root@localhost keepalived]# tail -f /var/log/messages
Apr 11 23:08:21 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Apr 11 23:08:21 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.15.253
Apr 11 23:08:21 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.15.253 added
Apr 11 23:08:21 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.15.253 added
Apr 11 23:08:21 localhost avahi-daemon[2915]: Registering new address record for 192.168.15.253 on eth0.
Apr 11 23:08:27 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.15.253
Apr 11 23:11:40 localhost Keepalived_healthcheckers: TCP connection to [192.168.15.234:80] failed !!!
Apr 11 23:11:40 localhost Keepalived_healthcheckers: Removing service [192.168.15.234:80] from VS [192.168.15.253:80]
Apr 11 23:11:40 localhost Keepalived_healthcheckers: Remote SMTP server [127.0.0.1:25] connected.
Apr 11 23:11:40 localhost Keepalived_healthcheckers: SMTP alert successfully sent.
关闭主调度(192.168.15.235 service keepalived stop)
Apr 12 00:52:33 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.15.253 removed
Apr 12 00:52:33 localhost avahi-daemon[2915]: Withdrawing address record for 192.168.15.253 on eth0.
Apr 12 00:53:57 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Transition to MASTER STATE
Apr 12 00:54:02 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering MASTER STATE
Apr 12 00:54:02 localhost Keepalived_vrrp: VRRP_Instance(VI_1) setting protocol VIPs.
Apr 12 00:54:02 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.15.253
Apr 12 00:54:02 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.15.253 added
Apr 12 00:54:02 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.15.253 added
Apr 12 00:54:02 localhost avahi-daemon[2915]: Registering new address record for 192.168.15.253 on eth0.
Apr 12 00:54:07 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Sending gratuitous ARPs on eth0 for 192.168.15.253
开启主调度(service keepalived start)
Apr 12 00:57:36 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Received higher prio advert
Apr 12 00:57:36 localhost Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Apr 12 00:57:36 localhost Keepalived_vrrp: VRRP_Instance(VI_1) removing protocol VIPs.
Apr 12 00:57:36 localhost Keepalived_vrrp: Netlink reflector reports IP 192.168.15.253 removed
Apr 12 00:57:36 localhost Keepalived_healthcheckers: Netlink reflector reports IP 192.168.15.253 removed
Apr 12 00:57:36 localhost avahi-daemon[2915]: Withdrawing address record for 192.168.15.253 on eth0.