这里咱们仅仅只利用Keepalive作双机热备,也就是保证服务器的高可用性,其余的不用管。可能您会说这样在实际应用中不多会这样用,这您可就错了,Keepalived仅仅作双机热备的状况仍是有的,我就碰到过几回这样的案例,下面就我碰到的几个案例作个小结
一,Keepalived双机热备的应用场景
1,网站流量不高,压力不大,可是对服务器的可靠性要求极其高,例如实时在线OA系统,政府部门网站系统,医院实时报医系统,公安局在线报案系统,股市后台网站系统等等,他们的压力不是很大,可是对可靠性要求是很是高的
2,有钱没地方花的,典型的政府企业,公办学校等等
二,Keepalived双机热备的特性以及优缺点
特性:
1,至少须要两台服务器,其中一台为master始终提供服务,另一台做为backup始终处于空闲状态,只有在主服务器挂掉的时候他就来帮忙了,这是典型的双击热备
2,能根据需求判断服务是否可用,在不可用的时候要即便切换
优缺点:
优势:数据同步很是简单,不像负载均衡对数据一致性要求很是高,实现起来相对复杂维护也颇为不便,双机热备用rsync就能够实现了操做和维护很是简单
缺点:服务器有点浪费,始终有一台处于空闲状态
三,Keepalived双机热备的配置
首先画个双机热备拓扑图吧:
php
这里我只写最终实现的配置,至于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等等html
yum -y install gcc make openssl openssl-devel wget kernel-devel
node
mkdir -p /usr/local/src/hasoft
mysql
cd /usr/local/src/hasoft
linux
wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
nginx
tar -zxvf keepalived-1.2.2.tar.gz
sql
cd keepalived-1.2.2
shell
./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-238.19.1.el5-i686/centos
复制代码bash
预编译后出现:
Keepalived configuration
------------------------
Keepalived version : 1.2.2
Compiler : gcc
Compiler flags : -g -O2 -DETHERTYPE_IPV6=0x86dd
Extra Lib : -lpopt -lssl -lcrypto
Use IPVS Framework : Yes
IPVS sync daemon support : Yes
IPVS use libnl : No
Use VRRP Framework : Yes
Use Debug flags : No
复制代码
make && make install
复制代码
这里注意哦,我上面是指通用的安装方法,若是你没有用到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
global_defs
{
notification_email
{
admin@example.com
admin@ywlm.net
}
notification_email_from admin@example.com
smtp_server 127.0.0.1
stmp_connect_timeout 30
router_id lnmp_node1
}
vrrp_instance lnmp {
state MASTER
interface eth0
virtual_router_id 100
priority 200
advert_int 5
track_interface {
eth0
eth1
}
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.17.200
}
}
复制代码
节点B配置以下:
vi /etc/keepalived/keepalived.conf
global_defs
{
notification_email
{
admin@example.com
admin@ywlm.net
}
notification_email_from admin@example.com
smtp_server 127.0.0.1
stmp_connect_timeout 30
router_id lnmp_node1
}
vrrp_instance lnmp {
state MASTER
interface eth0
virtual_router_id 100
priority 150
advert_int 5
track_interface {
eth0
eth1
}
authentication {
auth_type PASS
auth_pass 123456
}
virtual_ipaddress {
192.168.17.200
}
}
复制代码
四,启动调试
在节点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
#!/bin/bash
while :
do
mysqlcheck=`/usr/local/lnmp/mysql/bin/mysqladmin -uroot ping 2>&1`
mysqlcode=`echo $?`
phpcheck=`ps -C php-fpm --no-header | wc -l`
nginxcheck=`ps -C nginx --no-header | wc -l`
keepalivedcheck=`ps -C keepalived --no-header | wc -l`
if [ $nginxcheck -eq 0 ]|| [ $phpcheck -eq 0 ]||[ $mysqlcode -ne 0 ];then
if [ $keepalivedcheck -ne 0 ];then
killall -TERM keepalived
else
echo "keepalived is stoped"
fi
else
if [ $keepalivedcheck -eq 0 ];then
/etc/init.d/keepalived start
else
echo "keepalived is running"
fi
fi
sleep 5
done
复制代码
注意,用/etc/init.d/keepalived start若是起不来,能够用/usr/local/keepalived/sbin/keepalived二进制文件直接执行启动便可
启动脚本:
chmod +x /root/shell/keepcheck.sh
nohup sh /root/shell/keepcheck.sh &
复制代码
节点B也用这个脚本
写入/etc/rc.local开机自动启动
echo "nohup sh /root/shell/keepcheck.sh &" >> /etc/rc.loal
复制代码
能够测试了本文就写完了,这个能够用于生产环境,本人环境就这样配置的,但不一样业务有不一样环境,必定要按需调整,不要盲目盲从!