Keepalived双机热备
http://bbs.nanjimao.com/thread-855-1-1.html
一,Keepalived双机热备的应用场景
1,网站流量不高,压力不大,但是对服务器的可靠性要求极其高,例如实时在线OA系统,政府部门网站系统,医院实时报医系统,公安局在线报案系统,股市后台网站系统等等,他们的压力不是很大,但是对可靠性要求是非常高的
2,有钱没地方花的,典型的政府企业,公办学校等等
二,Keepalived双机热备的特性以及优缺点
特性:
1,至少需要两台服务器,其中一台为master始终提供服务,另外一台作为backup始终处于空闲状态,只有在主服务器挂掉的时候他就来帮忙了,这是典型的双击热备
2,能根据需求判断服务是否可用,在不可用的时候要即使切换
优缺点:
优点:数据同步非常简单,不像负载均衡对数据一致性要求非常高,实现起来相对复杂维护也颇为不便,双机热备用rsync就可以实现了操作和维护非常简单
缺点:服务器有点浪费,始终有一台处于空闲状态
三,Keepalived双机热备的配置
首先画个双机热备拓扑图吧:
![](http://bbs.nanjimao.com/data/attachment/forum/201109/09/150748peex6wcwcnnegwpt.jpg.thumb.jpg)
这里我只写最终实现的配置,至于Keepalived的理论知识请参考《 Keepalived原理与实战精讲 》
1,本例通过Keepalived来实现两台LNMP(也就是linux+nginx+mysql+php)架构服务器的双机热备
LNMP的配置请参考:《 Lnmp配置精讲第一版 》
2,Keepalived配置双机安装配置
1》Keepalived安装
keepalived官方地址: http://www.keepalived.org/download.html ,大家可以到这里下载最新版本的keepalived
操作系统:centos 5.5 32bit
系统安装:最小化安装,也就是去掉所有组件
环境配置:安装make 和 gcc openssl openssl-devel等等 预编译后出现:
这里注意哦,我上面是指通用的安装方法,如果你没有用到LVS可以把lvs去掉即
./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-238.19.1.el5-i686/ --disable-lvs-syncd --disable-lvs
但这个没有影响,就按照我的来配置吧,不过如果你要是集成了LVS,那么就不可加这两个参数了哦
整理管理文件:
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
建立配置文件目录(注意:keepalived的配置文件默认在/etc/keepalived/目录)
mkdir -p /etc/etc/keepalived/
两台服务器(两个节点)都这样安装即可
2》配置
节点A配置如下:
vi /etc/keepalived/keepalived.conf
节点B配置如下:
vi /etc/keepalived/keepalived.conf
四,启动调试
在节点A上启动
/usr/local/keepalived/sbin/keepalived
启动日志:
Sep 8 18:26:02 centosa Keepalived_vrrp: Registering Kernel netlink reflector
Sep 8 18:26:02 centosa Keepalived_vrrp: Registering Kernel netlink command channel
Sep 8 18:26:02 centosa Keepalived_vrrp: Registering gratutious ARP shared channel
Sep 8 18:26:02 centosa Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
Sep 8 18:26:02 centosa Keepalived_vrrp: Configuration is using : 36076 Bytes
Sep 8 18:26:02 centosa Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Sep 8 18:26:02 centosa Keepalived: Starting VRRP child process, pid=5606
Sep 8 18:26:07 centosa Keepalived_vrrp: VRRP_Instance(lnmp) Transition to MASTER STATE
Sep 8 18:26:12 centosa Keepalived_vrrp: VRRP_Instance(lnmp) Entering MASTER STATE
Sep 8 18:26:12 centosa avahi-daemon[2528]: Registering new address record for 192.168.17.200 on eth0.
在节点B上启动
/usr/local/keepalived/sbin/keepalived
开机自动启动
echo /usr/local/keepalived/sbin/keepalived >> /etc/rc.local
启动日志:
Sep 8 18:30:02 centosb Keepalived: Starting Keepalived v1.2.2 (09/08,2011)
Sep 8 18:30:02 centosb Keepalived: Starting Healthcheck child process, pid=5837
Sep 8 18:30:02 centosb Keepalived_vrrp: Registering Kernel netlink reflector
Sep 8 18:30:02 centosb Keepalived_vrrp: Registering Kernel netlink command channel
Sep 8 18:30:02 centosb Keepalived_vrrp: Registering gratutious ARP shared channel
Sep 8 18:30:02 centosb Keepalived: Starting VRRP child process, pid=5839
Sep 8 18:30:02 centosb kernel: IPVS: Registered protocols (TCP, UDP, AH, ESP)
Sep 8 18:30:02 centosb kernel: IPVS: Connection hash table configured (size=4096, memory=32Kbytes)
Sep 8 18:30:02 centosb kernel: IPVS: ipvs loaded.
Sep 8 18:30:02 centosb Keepalived_healthcheckers: Registering Kernel netlink reflector
Sep 8 18:30:02 centosb Keepalived_healthcheckers: Registering Kernel netlink command channel
Sep 8 18:30:02 centosb Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'.
Sep 8 18:30:02 centosb Keepalived_vrrp: Opening file '/etc/keepalived/keepalived.conf'.
Sep 8 18:30:02 centosb Keepalived_vrrp: Configuration is using : 36252 Bytes
Sep 8 18:30:02 centosb Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Sep 8 18:30:02 centosb Keepalived_healthcheckers: Configuration is using : 6271 Bytes
Sep 8 18:30:02 centosb Keepalived_healthcheckers: Using LinkWatch kernel netlink reflector...
Sep 8 18:30:02 centosb Keepalived_vrrp: VRRP_Instance(lnmp) Entering BACKUP STATE
从日志可以看出,启动都没有问题,并且安装我给的优先级完成了竞选,各自成就了各自的状态
关闭节点A的网卡测试切换是否正常
ifdown eth0
观察节点B的日志:
Sep 8 18:32:55 centosb Keepalived_vrrp: VRRP_Instance(lnmp) Transition to MASTER STATE
Sep 8 18:33:00 centosb Keepalived_vrrp: VRRP_Instance(lnmp) Entering MASTER STATE
Sep 8 18:33:00 centosb avahi-daemon[2531]: Registering new address record for 192.168.17.200 on eth0.
启动节点A的网卡测试切换是否正常
ifup eth0
观察节点B的日志:
Sep 8 18:33:31 centosb Keepalived_vrrp: VRRP_Instance(lnmp) Received higher prio advert
Sep 8 18:33:31 centosb Keepalived_vrrp: VRRP_Instance(lnmp) Entering BACKUP STATE
Sep 8 18:33:31 centosb avahi-daemon[2531]: Withdrawing address record for 192.168.17.200 on eth0.
Received higher prio advert:表示接收到更高优先级的公告(advert公告的意思)
Withdrawing: 撤回的意思,可以看出切换过程一目了然
OK,到这里我们的安装部分完成,下面我们来看看如何监控服务吧,我们这里仅仅是监控了网络故障和keepalived本身进程,在网络或者keepalived进程出现问题的时候会切换,但是我的节点A里面还有很多服务呢,例如nginx,PHP,mysql进程出问题或高负载的时候相应过慢怎么办,怎么切换的呢,这时就要用到脚本了,下面我们来看看keepalived是如何控制脚本来实现对服务器的监控和切换的
写个脚本来实时监控三个服务,若有一个出现问题遍切换mkdir /root/shell/
cd /root/shell
vi keepcheck.sh
注意,用 /etc/init.d/keepalived start如果起不来,可以用/usr/local/keepalived/sbin/keepalived二进制文件直接执行启动即可
启动脚本:
节点B也用这个脚本
写入/etc/rc.local开机自动启动
可以测试了
本文就写完了,这个可以用于生产环境,本人环境就这样配置的,但不同业务有不同环境,一定要按需调整,不要盲目盲从!