Keepalived简介:
Keepalived的作用是检测web服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的web服务器从系统中剔除,当web服务器工作正常后Keepalived自动将web服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的web服务器。
实验环境
LVS-DR-Master 192.168.3.11
LVS-DR-BACKUP 192.168.3.12
LVS-DR-VIP 192.168.3.254
WEB2-Realserver 192.168.3.13
WEB1-Realserver 192.168.3.14
关闭防火墙
service iptables stop
setenforce 0
Keepalvied 官网:http://www.keepalived.org
1.master和slave安装依赖包
yum -y install ipvsadm kernel-devel openssl-devel popt-devel
rpm安装
[root@LVS-slave ~]# rpm -ivh /mnt/Cluster/ipvsadm-1.24-10.i386.rpm
[root@LVS-slave ~]# rpm -ivh /mnt/Server/kernel-devel-2.6.18-194.el5.i686.rpm
[root@LVS-slave ~]# rpm -ivh /mnt/Server/openssl-devel-0.9.8e-12.el5_4.6.i386.rpm
[root@LVS-slave ~]# rpm -ivh /mnt/Server/popt-1.10.2.3-18.el5.i386.rpm
一、安装web服务器(yum)
1.安装WEB1-Realserver
[root@web1 ~]# yum -y install httpd
[root@web1 ~]# service httpd restart
[root@web1 ~]# vim /var/www/html/index.html
aaaaaaaaaaaaaaaaaaaa
[root@web1 ~]# service httpd restart
[root@web1 ~]# elinks --dump http://localhost
aaaaaaaaaaaaaaaaaaaa
2.安装WEB2-Realserver
[root@web2 ~]# yum -y install httpd
[root@web2 ~]# service httpd restart
[root@web2 ~]# vim /var/www/html/index.html
bbbbbbbbbbbbbbbbbbbbbbb
[root@web2 ~]# service httpd restart
[root@web2 ~]# elinks --dump http://localhost
bbbbbbbbbbbbbbbbbbbbbbb
二、配置LVS-DR-Master
1.编译安装keepalived
[root@LVS-master ~]# tar xf keepalived-1.2.2.tar.gz
[root@LVS-master ~]# cd keepalived-1.2.2
[root@LVS-master keepalived-1.2.2]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.18-194.el5-i686/
[root@LVS-master keepalived-1.2.2]# make && make install
[RHEL5.5报错解决方案]
RHEL5.5 32位上编译安装keepalived-1.2.2.tar.gz报错
报错信息如下:
/root/keepalived-1.2.2/keepalived/vrrp/vrrp.c:787: undefined reference to `ipvs_syncd_backup'
collect2: ld 返回 1
make[1]: *** [all] 错误 1
make[1]: Leaving directory `/root/keepalived-1.2.2/keepalived'
make: *** [all] 错误 2
解决方法:
vi keepalived/libipvs-2.6/ip_vs.h
#include <sys/types.h>
#include <linux/types.h>
这两行先后顺序错误
#include <sys/types.h>在#include <linux/types.h>的前面。
2.添加系统服务
[root@LVS-master keepalived-1.2.2]# chkconfig --add keepalived
[root@LVS-master keepalived-1.2.2]# chkconfig keepalived on
[root@LVS-master keepalived-1.2.2]# chkconfig --list keepalived
keepalived 0:关闭 1:关闭 2:启用 3:启用 4:启用 5:启用 6:关闭
[root@LVS-master keepalived-1.2.2]# service keepalived restart
3.设置网卡ip地址
[root@LVS-master ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
HWADDR=00:0c:29:2d:8b:e8
NETMASK=255.255.255.0
IPADDR=192.168.3.11
TYPE=Ethernet
[root@LVS-master ~]# service network restart
4.配置keepalived.conf
[root@LVS-master ~]# cd /etc/keepalived/
[root@LVS-master keepalived]# cp keepalived.conf{,.bak}
[root@LVS-master keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_MASTER
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.3.254
}
}
virtual_server 192.168.3.254 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.3.14 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.3.13 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
[root@LVS-master keepalived]# service keepalived restart
[root@LVS-master keepalived]# chkconfig keepalived on
三、配置LVS-DR-BACKUP
1.编译安装keepalived
[root@LVS-slave ~]# tar xf keepalived-1.2.2.tar.gz
[root@LVS-slave ~]# cd keepalived-1.2.2
[root@LVS-slave keepalived-1.2.2]# ./configure --prefix=/ --with-kernel-dir=/usr/src/kernels/2.6.18-194.el5-i686/
[root@LVS-slave keepalived-1.2.2]# make && make install
2.设置网卡ip地址
[root@LVS-slave ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0
BOOTPROTO=none
ONBOOT=yes
HWADDR=00:0c:29:48:02:53
NETMASK=255.255.255.0
IPADDR=192.168.3.12
TYPE=Ethernet
[root@LVS-slave ~]# service network restart
正在关闭接口 eth0: [确定]
关闭环回接口: [确定]
弹出环回接口: [确定]
弹出界面 eth0: [确定]
3.配置keepalived.conf
[root@LVS-slave ~]# cd /etc/keepalived/
[root@LVS-slave keepalived]# cp keepalived.conf{,.bak}
[root@LVS-slave keepalived]# vim keepalived.conf
! Configuration File for keepalived
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_MASTER
}
vrrp_instance VI_2 {
state SLAVE
interface eth0
virtual_router_id 51
priority 50
advert_int 1
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.3.254
}
}
virtual_server 192.168.3.254 80 {
delay_loop 6
lb_algo wrr
lb_kind DR
persistence_timeout 50
protocol TCP
real_server 192.168.3.14 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.3.13 80 {
weight 1
TCP_CHECK {
connect_timeout 10
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
[root@LVS-slave keepalived]# service keepalived restart
[root@LVS-slave keepalived]# chkconfig keepalived on
四、配置Realserver
1.编写脚本
[root@web1 ~]# vim realserver.sh
#!/bin/bash
SNS_VIP=192.168.3.254
. /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_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
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $SNS_VIP >/dev/null 2>&1
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
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
[root@web1 ~]# chmod +x realserver.sh
[root@web1 ~]# ./realserver.sh
RealServer Start OK
在 RealServer2 上的设置
[root@web2 ~]# vim realserver.sh
#!/bin/bash
SNS_VIP=192.168.3.254
. /etc/rc.d/init.d/functions
case "$1" in
start)
ifconfig lo:0 $SNS_VIP netmask 255.255.255.255 broadcast $SNS_VIP
/sbin/route add -host $SNS_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
echo "RealServer Start OK"
;;
stop)
ifconfig lo:0 down
route del $SNS_VIP >/dev/null 2>&1
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
echo "RealServer Stoped"
;;
*)
echo "Usage: $0 {start|stop}"
exit 1
esac
exit 0
[root@web2 ~]# chmod +x realserver.sh
[root@web2 ~]# ./realserver.sh start
RealServer Start OK
五、测试
(1).首先在master上查看一下IP地址(是否有VIP)
[root@LVS-master ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
link/ether 00:0c:29:2d:8b:e8 brd ff:ff:ff:ff:ff:ff
inet 192.168.3.11/24 brd 192.168.3.255 scope global eth0
inet 192.168.3.254/32 scope global eth0
inet6 fe80::20c:29ff:fe2d:8be8/64 scope link
valid_lft forever preferred_lft forever
3: sit0: <NOARP> mtu 1480 qdisc noop
link/sit 0.0.0.0 brd 0.0.0.0
(2)客户端访问: http://192.168.3.254
刷新查看是否一直切换显示页面