说明:
一、目 的:本文实现的Linux集群是基于Keepalived-1.2.7软件实现,
二、难 点:实现虚拟IP(VIP)自动切换的同时,还要同时实现虚拟MAC地址(VMAC)的切换,即无论集群中的Host如何切换,对外访问的IP和MAC始终不变
三、适用环境:当对外路由或交换机绑定主机MAC时适用
四、说 明:若无须对外绑定MAC地址,则大可简化如下配置
1. 环境及软件:
1) 系统:Debian6 x32
2) 内核:linux 3.2.0-0.bpo.3-686-pae
3) 软件:keepalived-1.2.7
2. 配置:
MASTER设备IP: eth0: 10.10.1.200/24 eth1:192.168.61.253/21
BACKUP设备IP: eth0: 10.10.1.100/24 eth1:192.168.61.252/21
VIP: 192.168.61.248/21
3. 安装步骤:
1)升级当前Debian系统内核linux 2.6.32
a) 修改Debian源文件,添加一个向后兼容的源
deb http://ftp.cn.debian.org/debian-backports squeeze-backports main
# nano /etc/apt/sources.list
b) 更新Debian源文件关联并升级系统内核至3.2.0
# aptitude update
# aptitude -t squeeze-backports install linux-image-3.2.0-0.bpo.3-686-pae
如果编译NVDIA的官方驱动的话,还要装:
# aptitude -t squeeze-backports install linux-headers-3.2.0.bpo.3-686-pae
2) 安装keepalived-1.2.7
a) 首先要安装keepalived所依赖的组件
# aptitude install make gcc libpopt-dev libnl-dev libcurl4-openssl-dev
b) 将keepalived-1.2.7.tar.gz文件拷贝到/usr/local/soft下,解压,编译并安装到
/usr/local/keepalived
# mkdir /usr/local/soft
通过ftp将文件拷贝至/usr/local/soft
# cd /usr/local/soft
# tar zxvf keepalived-1.2.7.tar.gz
# cd keepalived-1.2.7
# ./configure --prefix=/usr/local/keepalived
# make && make install
c) 重置keepalived相关文件,以便开机自动启动
# cp /usr/local/keepalived/sbin/keepalived /usr/sbin
# mkdir /etc/sysconfig
# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
# nano /etc/init.d/keepalived
修改/etc/init.d/keepalived文件,部分片段如下(蓝色--注释,红色--修改):
……
#. /etc/rc.d/init.d/functions
. /lib/lsb/init-functions
# Source configuration file (we set KEEPALIVED_OPTIONS there)
. /etc/sysconfig/keepalived
RETVAL=0
prog="keepalived"
start() {
echo -n $"Starting $prog: "
# keepalived ${KEEPALIVED_OPTIONS}
keepalived -D
RETVAL=$?
echo "1" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "1" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_filter
echo "1" >/proc/sys/net/ipv4/conf/eth1/arp_filter
echo
[ $RETVAL -eq 0 ] && touch/var/lock/$prog
}
stop() {
echo -n $"Stopping $prog: "
killproc keepalived
RETVAL=$?
echo "0" >/proc/sys/net/ipv4/conf/all/arp_ignore
echo "0" >/proc/sys/net/ipv4/conf/all/arp_announce
echo "0" >/proc/sys/net/ipv4/conf/all/arp_filter
echo "0" >/proc/sys/net/ipv4/conf/eth1/arp_filter
echo
[ $RETVAL -eq 0 ] && rm -f/var/lock/$prog
}
……
condrestart)
if [ -f /var/lock/$prog ]; then
stop
start
fi
;;
……
d) 在/etc/keepalived下创建keepalived配置文件keepalived.conf
# mkdir /etc/keepalived
# nano /etc/keepalived/keepalived.conf
global_defs {
notification_email {
}
notification_email_from [email protected]
smtp_server 192.168.60.131
smtp_connect_timeout 30
router_id LVS_DEVEL_11 #与backup配置不相同的任意合法字符串
}
vrrp_instance VI_1 {
state MASTER #backup机器配置为BACKUP
interface eth1 #绑定的网卡名称
virtual_router_id 248 #必须与backup机器配置相同
use_vmac uvmac #名字可为任意合法字符串
priority 200 #优先级,要比backup机器高至少50
advert_int 1
debug
authentication {
auth_type PASS #授权类型,必须与backup相同
auth_pass 1111 #授权密码,必须与backup相同
}
virtual_ipaddress {
192.168.61.249/21 #虚拟ip地址,必须与backup相同
}
notify_master /usr/local/keepalived/script/vmac_tweak.sh
notify_backup /usr/local/keepalived/script/vmac_backup.sh
}
e)创建脚本
i ) 脚本/usr/local/keepalived/script/vmac_tweak.sh
# mkdir /usr/local/keepalived/script
# touch /usr/local/keepalived/script/vmac_tweak.sh
脚本内容如下:
#!/bin/bash
echo "0" > /proc/sys/net/ipv4/conf/uvmac/arp_filter
echo "1" > /proc/sys/net/ipv4/conf/uvmac/accept_local
ii ) 脚本/usr/local/keepalived/script/vmac_backup.sh
# mkdir /usr/local/keepalived/script
# touch /usr/local/keepalived/script/vmac_backup.sh
脚本内容如下:
#!/bin/bash
echo "0" > /proc/sys/net/ipv4/conf/uvmac/arp_filter
echo "0" > /proc/sys/net/ipv4/conf/uvmac/accept_local
3) 启动keepalived
# invoke-rc.d keepalived start
4) 测试keepalived
a) MASTER启动时,查看日志/var/log/messages,显示keepalived进入MASTER状态同理,BACKUP机器将进入BACKUP状态
b) 当MASTER机器down掉时,查看BACKUP机器日志,显示keepalived进入MASTER,当MASTER重新启动时,MASTER机器进入MASTER状态而BACKUP机器keepalived重新进入BACKUP状态,此时表示keepalived安装测试已成功!