首页 > 代码库 > Keepalived安装配置

Keepalived安装配置

1、安装依赖包

yum -y install libnl libnl-devel
yum install -y libnfnetlink libnfnetlink-devel
rpm -ivhlibnfnetlink-1.0.0-1.el6.x86_64.rpm libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm

2、安装Keepalived

cd /app
tar -zxfkeepalived-1.3.5.tar.gz
cd/app/keepalived-1.3.5
./configure
 
make && makeinstall

3、安装后配置开启自启动

cp /usr/local/etc/sysconfig/keepalived/etc/sysconfig/keepalived
cp -r /usr/local/etc/keepalived/ /etc/
cp /app/keepalived-1.3.5/keepalived/etc/init.d/keepalived/etc/init.d/keepalived
chkconfig --add keepalived
chkconfig keepalived on

4、配置Keepalived

/etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
   notification_email {
     acassen@firewall.loc   #发生邮件目标地址
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc   #发生邮件源地址
   smtp_server 192.168.200.1                               #smtp服务器
   smtp_connect_timeout 30
   router_id LVS_DEVEL                                     #机器标识,通常可设为hostname。故障发生时,邮件通知会用到
## --------------------------------------------------   
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}

vrrp_script chk_http_port {
   script "/usr/local/src/check_nginx_pid.sh"          #监控服务脚步
   interval 2                                          #检测时间间隔(执行脚步间隔),每2s检测一次
   weight 2                                            #检测失败(脚本返回非0)则优先级 2,增减优先级
   fall 2                                              #检测连续 2 次失败才算确定是真失败。会用weight减少优先级(1-255之间)
   rise 1                                              #检测 1 次成功就算成功。但不修改优先级
}

vrrp_instance VI_1 {
    state MASTER             #主备的区别点,标识  MASTER  BACKUP
    interface eth0           #对应的网卡
    # mcast_src_ip 172.29.88.224 #发送多播数据包时的源IP地址,默认绑定网卡的IP
    virtual_router_id 51     #主、备机的virtual_router_id必须相同
    priority 100             #优先级,谁做主(值越大,优先级越高)
    advert_int 2             #检查间隔,默认为1秒。这就是VRRP的定时器,MASTER每隔这样一个时间间隔,就会发送一个advertisement报文以通知组内其他路由器自己工作正常
    authentication {         #定义认证方式和密码,主从必须一样
        auth_type PASS
        auth_pass 1111
    }
    track_interface {        #监控的网卡。如果只监控服务,这里可以不填
      eth1
    }
    track_script {           #以脚本为监控chk_http_port是前面填写的
        chk_http_port
    }
    virtual_ipaddress {      #虚拟IP地址,可以设置多个IP地址
        10.252.3.165/24 dev eth0 label eth0:1
        # 192.168.200.16
        # 192.168.200.17
        # 192.168.200.18
    }
}

/usr/local/src/check_nginx_pid.sh

## -----------------------------
# 该脚本检测ngnix的运行状态,并在nginx进程不存在时尝试重新启动ngnix,如果启动失败则停止keepalived,准备让其它机器接管。
## -----------------------------
#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
    /usr/local/bin/nginx
    sleep 2
    counter=$(ps -C nginx --no-heading|wc -l)
    if [ "${counter}" = "0" ]; then
        /etc/init.d/keepalived stop
    fi
fi

5、管理Keepalived

停止:

service keepalived stop

启动:

service keepalived start

重启:

service keepalived restart

重新加载配置文件:

service keepalived reload


本文出自 “潘思亮的博客” 博客,请务必保留此出处http://siliang.blog.51cto.com/5882215/1918488

Keepalived安装配置