说明:
一、目 的:本文实现的Linux集群是基于Keepalived-1.2.9软件实现,
二、难 点:实现虚拟IP(VIP)自动切换的同时,还要同时实现虚拟MAC地址(VMAC)的切换,即无论集群中的Host如何切换,对外访问的IP和MAC始终不变
三、适用环境:当对外路由或交换机绑定主机MAC时适用
四、说 明:若无须对外绑定MAC地址,则大可简化如下配置
1. 环境及软件:
1) 系统:Debian6 amd64
2) 内核:linux 3.2.0-0.bpo.4-amd64
3) 软件:keepalived-1.2.9
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源文件/etc/apt/sources.list,添加一个向后兼容的源(源地址如下)
deb http://ftp.cn.debian.org/debian-backports squeeze-backports main
b) 更新Debian源文件关联并升级系统内核至3.2.0
# aptitude update
# aptitude -t squeeze-backports install linux 3.2.0-0.bpo.4-amd64
如果编译NVDIA的官方驱动的话,还要装:
# aptitude -t squeeze-backports install linux-headers-3.2.0.bpo.4-amd64
2) 安装keepalived-1.2.9
a) 首先要安装keepalived所依赖的组件
# aptitude install make gcc libpopt-dev libnl-dev libcurl4-openssl-dev
b) 将keepalived-1.2.9.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.9.tar.gz
# cd keepalived-1.2.9
# ./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/
d) 修改keepalived启动脚本
# 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
[ $RETVAL -eq 0 ] && touch /var/lock/$prog
}
stop() {
echo -n $"Stopping $prog: "
killproc keepalived
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/$prog
}
……
condrestart)
if [ -f /var/lock/$prog ]; then
stop
start
fi
;;
……
e) 网卡配置参数
修改文件/etc/sysctl.conf,添加如下内容:
net.ipv4.conf.all.arp_ignore = 1
net.ipv4.conf.all.arp_announce = 1
net.ipv4.conf.all.arp_filter = 0
net.ipv4.conf.eth0.arp_filter = 1
f) 在/etc/keepalived下创建keepalived配置文件keepalived.conf
# mkdir /etc/keepalived
# nano /etc/keepalived/keepalived.conf
global_defs {
notification_email {
[email protected]
}
notification_email_from [email protected]
smtp_server 192.168.1.5
smtp_connect_timeout 30
router_id LVS_DEVEL_11 #与backup配置不相同的任意合法字符串
}
vrrp_instance VI_1 {
state MASTER #backup机器配置为BACKUP
interface eth1 #绑定的网卡名称
virtual_router_id 250 #必须与backup机器配置相同
use_vmac #名字可为任意合法字符串(默认字符串 vrrp.${virtual_router_id} 此处默认使用vrrp.250)
priority 200 #优先级,要比backup机器高至少50
advert_int 1
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
}
e)创建脚本
脚本/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
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安装测试已成功!
Keepalived 的详细介绍:请点这里
Keepalived 的下载地址:请点这里
推荐阅读:
CentOS 6.3下Haproxy+Keepalived+Apache配置笔记 http://www.linuxidc.com/Linux/2013-06/85598.htm
Haproxy + KeepAlived 实现WEB群集 on CentOS 6 http://www.linuxidc.com/Linux/2012-03/55672.htm
Keepalived+Haproxy配置高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/56748.htm
Haproxy+Keepalived构建高可用负载均衡 http://www.linuxidc.com/Linux/2012-03/55880.ht
m