yum -y install gcc make openssl openssl-devel wget kernel-devel
mkdir -p /usr/local/src/hasoft
cd /usr/local/src/hasoft
wget http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
tar -zxvf keepalived-1.2.2.tar.gz
cd keepalived-1.2.2
./configure --prefix=/usr/local/keepalived --with-kernel-dir=/usr/src/kernels/2.6.18-238.19.1.el5-i686/
复制代码
预编译后出现:
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.confglobal_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.confglobal_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 reflectorSep 8 18:26:02 centosa Keepalived_vrrp: Registering Kernel netlink command channelSep 8 18:26:02 centosa Keepalived_vrrp: Registering gratutious ARP shared channelSep 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 BytesSep 8 18:26:02 centosa Keepalived_vrrp: Using LinkWatch kernel netlink reflector...Sep 8 18:26:02 centosa Keepalived: Starting VRRP child process, pid=5606Sep 8 18:26:07 centosa Keepalived_vrrp: VRRP_Instance(lnmp) Transition to MASTER STATESep 8 18:26:12 centosa Keepalived_vrrp: VRRP_Instance(lnmp) Entering MASTER STATESep 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=5837Sep 8 18:30:02 centosb Keepalived_vrrp: Registering Kernel netlink reflectorSep 8 18:30:02 centosb Keepalived_vrrp: Registering Kernel netlink command channelSep 8 18:30:02 centosb Keepalived_vrrp: Registering gratutious ARP shared channelSep 8 18:30:02 centosb Keepalived: Starting VRRP child process, pid=5839Sep 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 reflectorSep 8 18:30:02 centosb Keepalived_healthcheckers: Registering Kernel netlink command channelSep 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 BytesSep 8 18:30:02 centosb Keepalived_vrrp: Using LinkWatch kernel netlink reflector...Sep 8 18:30:02 centosb Keepalived_healthcheckers: Configuration is using : 6271 BytesSep 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 STATESep 8 18:33:00 centosb Keepalived_vrrp: VRRP_Instance(lnmp) Entering MASTER STATESep 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 advertSep 8 18:33:31 centosb Keepalived_vrrp: VRRP_Instance(lnmp) Entering BACKUP STATESep 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/shellvi 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
复制代码
可以测试了
本文就写完了,这个可以用于生产环境,本人环境就这样配置的,但不同业务有不同环境,一定要按需调整,不要盲目盲从!