keepalived--部署过程

一.简介

Keepalived使用的vrrp协议方式,虚拟路由冗余协议 (Virtual Router Redundancy Protocol);

Heartbeat或Corosync是基于主机或网络服务的高可用方式;
Keepalived的目的是模拟路由器的高可用,Heartbeat的目的是实现Service的高可用。
Keepalived常用的前端高可用的组合有,LVS+Keepalived、Nginx+Keepalived、HAproxy+Keepalived。
Heartbeat常见的组合有Heartbeat v3(Corosync)+Pacemaker+NFS+Httpd 实现Web服务器的高可用、Heartbeat v3(Corosync)+Pacemaker+NFS+MySQL 实现MySQL服务器的高可用。

keepalived可以认为是VRRP协议在Linux上的实现,主要有三个模块,分别是core、check和vrrp。
core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。
check负责健康检查,包括常见的各种检查方式。
vrrp模块是来实现VRRP协议的。

二.环境

       ip                     负载-反向代理        高可用                             vip

192.168.15.106                nginx                 Keepalived             192.168.15.102

192.168.15.105                nginx                 Keepalived             192.168.15.102

三.keepalived安装

1.下载
wget http://www.keepalived.org/software/keepalived-1.3.2.tar.gz
2.安装
tar xf keepalived-1.3.2.tar.gz
cd keepalived-1.3.2
./configure
make && make install
cp /usr/local/src/keepalived-1.3.2/keepalived/etc/init.d/keepalived /etc/init.d/
cp /usr/local/etc/sysconfig/keepalived /etc/sysconfig/
mkdir /etc/keepalived
cp /usr/local/etc/keepalived/keepalived.conf /etc/keepalived/
cp /usr/local/sbin/keepalived /usr/sbin/

四.配置

1,master

cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /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 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
   vrrp_skip_check_adv_addr
   vrrp_strict
   vrrp_garp_interval 0
   vrrp_gna_interval 0
}
#监控脚本,监控nginx每两秒运行一次
vrrp_script chk_http_port {      
    script "/opt/chk_nginx.sh"  
    interval 2                  
    weight -5                    
    fall 2                    
    rise 1                    
}

vrrp_instance VI_1 {
    state MASTER
    interface eth0    #网卡
    mcast_src_ip 192.168.15.105
    virtual_router_id 51
    priority 100  #权重
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111    #密码master和slave必须一致
    }
    virtual_ipaddress {
        192.168.15.102    #vip
    }
track_script {   
   chk_http_port    
}
}

2.slave 

cp /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak
vim /etc/keepalived/keepalived.conf

! Configuration File for keepalived

global_defs {
notification_email {                
ops@wangshibo.cn                     
tech@wangshibo.cn
}
  
notification_email_from ops@wangshibo.cn  
smtp_server 127.0.0.1                    
smtp_connect_timeout 30                 
router_id slave-node                    
}
  
vrrp_script chk_http_port {         
    script "/opt/chk_nginx.sh"   
    interval 2                      
    weight -5                       
    fall 2                   
    rise 1                  
}
  
vrrp_instance VI_1 {            
    state BACKUP           
    interface eth0            
    mcast_src_ip 192.168.15.106  
    virtual_router_id 51        
    priority 99            #从要小于主   
    advert_int 1               
    authentication {            
        auth_type PASS         
        auth_pass 1111          
    }
    virtual_ipaddress {        
        192.168.15.102
    }
track_script {                     
   chk_http_port                 
}
}

3.开启防火墙

vim /etc/sysconfig/iptables

-A INPUT -d 224.0.0.0/8 -i eth0 -j ACCEPT

/etc/init.d/iptables restart

五.nginx的监控脚本

vim /opt/chk_nginx.sh

#!/bin/bash
counter=$(ps -C nginx --no-heading|wc -l)
if [ "${counter}" = "0" ]; then
    /usr/local/nginx/sbin/nginx
    sleep 2
    counter=$(ps -C nginx --no-heading|wc -l)
    if [ "${counter}" = "0" ]; then
        /etc/init.d/keepalived stop
    fi
fi

  

posted @ 2017-12-12 10:37  王嘉喆  阅读(226)  评论(0编辑  收藏  举报