CentOS配置Keepalived 做双机热备切换
系统环境:
************************************************************
两台服务器都装了 CentOS-5.2-x86_64 系统
Virtual IP : 192.168.30.20
Squid1+Real Server 1 : 网卡地址 (eth0):192.168.30.12
Squid2+Real Server 2 : 网卡地址 (eth0):192.168.30.13
************************************************************
软件列表 :
keepalived
http://www.keepalived.org/software/keepalived-1.1.17.tar.gz
openssl-devel
yum -y install openssl-devel
***************************************************************
配置:
配置基于高可用 keepalived, 确定 LVS 使用 DR 模式
1. 安装配置 keepalived
1.1 安装依赖软件 如果系统为基本文本安装,需要安装一下软件
# yum -y install ipvsadm
# yum -y install kernel kernel-devel
# reboot 重启系统切换内核
# yum -y install openssl-devel ;安装 keepalived 依赖软件
#ln -s /usr/src/kernels/`uname -r`-`uname -m`/ /usr/src/linux
;建立内核链接,编译 keepalived 支持 lvs 时需要
注意建立链接的内核名和当前运行的内核一致,否则导致安装失败
#tar zxvf keepalived-1.1.17.tar.gz
#cd keepalived-1.1.17
#./configure --prefix=/usr --sysconf=/etc
Keepalived configuration
------------------------
Keepalived version : 1.1.17
Compiler : gcc
Compiler flags : -g -O2
Extra Lib : -lpopt -lssl -lcrypto
Use IPVS Framework : Yes ;注意编译时一定要支持 lvs
IPVS sync daemon support : Yes
Use VRRP Framework : Yes
Use LinkWatch : No
Use Debug flags : No
#make
#make install
1.2 编辑 keepalived 配置文件
#Vi /etc/keepalived/keepalived.conf
global_defs {
notification_email {
test@163.com
}
notification_email_from root@localhost
smtp_server 127.0.0.1
smtp_connect_timeout 30
router_id LVS_DEVEL
}
vrrp_instance VI_1 {
state MASTER # 备份服务器上将 MASTER 改为 BACKUP
interface eth0 # HA 监测网络接口
virtual_router_id 51 # 主、备机的 virtual_router_id 必须相同
priority 100 # 主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 2 # VRRP Multicast 广播周期秒数
authentication {
auth_type PASS #VRRP 认证方式
auth_pass 1111 #VRRP 口令字
}
virtual_ipaddress {
192.168.30.20 # VRRP HA 虚拟地址如果有多个VIP,继续换行填写
}
}
virtual_server 192.168.30.20 80 {
delay_loop 2 # 每隔 6 秒查询realserver 状态
lb_algo rr #lvs 算法
lb_kind DR # Direct Route
persistence_timeout 50 # 同一IP 的连接60 秒内被分配到同一台realserver
protocol TCP # 用 TCP 协议检查realserver 状态
real_server 192.168.30.12 80 {
weight 3 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.30.13 80 {
weight 3 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
1.3 BACKUP 服务器同上配置,先安装 lvs 再按装 keepalived, 仍后配置 /etc/keepalived/keepalived.conf ,只需将红色标示的部分改一下即可 .
global_defs {
notification_email {
test@163.com
}
notification_email_from root@localhost
smtp_server 127.0.0.1
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 99 # 主、备机取不同的优先级,主机值较大,备份机值较小
advert_int 2 # VRRP Multicast 广播周期秒数
authentication {
auth_type PASS #VRRP 认证方式
auth_pass 1111 #VRRP 口令字
}
virtual_ipaddress {
192.168.30.20 # VRRP HA 虚拟地址
}
}
virtual_server 192.168.30.20 80 {
delay_loop 2 # 每隔 6 秒查询realserver 状态
lb_algo rr #lvs 算法
lb_kind DR # Direct Route
persistence_timeout 50 # 同一IP 的连接60 秒内被分配到同一台realserver
protocol TCP # 用 TCP 协议检查realserver 状态
real_server 192.168.30.12 80 {
weight 3 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
real_server 192.168.30.13 80 {
weight 3 #(权重)
TCP_CHECK {
connect_timeout 10 #(10秒无响应超时)
nb_get_retry 3
delay_before_retry 3
connect_port 80
}
}
}
2. 配置lvs客户端脚本
- #vi /usr/local/sbin/realserver.sh
- #!/bin/bash
- # description: Config realserver lo and apply noarp
- #Written by :NetSeek http://www.linuxtone.org
- SNS_VIP=192.168.30.20
- . /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
- 转自:http://www.codesky.net/article/201111/173572.html