下载地址:http://www.keepalived.org/download.html

cd 
wget http://www.keepalived.org/software/keepalived-2.0.12.tar.gz
tar zxvf keepalived-2.0.12.tar.gz -C /usr/src
cd /usr/src/keepalived-2.0.12
./configure --prefix=/opt/keepalived && make -j 8 && make install
mkdir -p /opt/keepalived/conf
\cp /opt/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
chmod +x /etc/init.d/keepalived
sed -ri "15s#(.)*#.  /opt/keepalived/etc/sysconfig/keepalived#g" /etc/init.d/keepalived
sed -ri 's#^(KEEPALIVED_OPTIONS)(.)*#\1="-D -f /opt/keepalived/conf/keepalived.conf"#g' /opt/keepalived/etc/sysconfig/keepalived
yes|cp /opt/keepalived/sbin/keepalived /usr/bin

一个简易的配置文件:

! Configuration File for keepalived
global_defs {
   router_id node1  #node1,node2

#监控脚本
vrrp_script chk_http_port {
   script "/opt/keepalived/conf/test.sh" 
   interval 3       
   weight 2
}

vrrp_instance VI_1 {
    state MASTER  #MASTER,BACKUP,BACKUP
    interface eth0  #复用的网卡名称,keepalived生成的虚拟ip会附加在这个网卡上
    virtual_router_id 1  #路由标示,取值范围1-255,从节点需要保持一致,且若同一个网段有多套keepalived此值不能重复
    priority 100   #优先级,取值范围0-100,从节点改小
    advert_int 1  #通知间隔
    authentication {
        auth_type PASS  
        auth_pass 123456  #验证密码
    }
 track_script {     #执行监控脚本
        chk_http_port    
    }
    virtual_ipaddress {
        192.168.2.2  #虚拟ip,可以设置多个
    }
}}

简单的监控脚本:

#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
if [ $A -eq 0 ];then
  service nginx restart 
  sleep 3
  if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
    killall keepalived
  fi
fi

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code