231321 发表于 2016-3-21 09:10:08

keepalived入门之keepalive+haproxy双机热备

Keepalive+haproxy双机热备如图所示为整体的拓扑图:

一.部署前说明:(1)系统版本: centos 6.6(64位)(2)角色及ip相关信息:
角色名称网络ip信息
客户端(CIP)10.58.137.203/24
Master_DReth0:172.51.96.105/24 &&eth1:192.168.0.105/24
Backup_DReth0:172.51.96.119/24 &&eth1:192.168.0.119/24
VIP172.51.96.175/24
(3)相关中间件信息keepalive版本信息: keepalived-1.2.15nginx版本信息:haproxy-1.5.15(提供proxy代理)二.部署操作:

haproxy部署分别在Master_DR和backup_DR上安装haproxy,操作如下:1.1 到haproxy官网下载haproxy源码包如下
cd ~
wget http://www.haproxy.org/download/1.5/src/haproxy-1.5.15.tar.gz

1.2 创建haproxy运行用户

groupadd -r haproxy
useradd -r -g haproxy -M -s /sbin/nologin haproxy

1.3 编译安装haproxy:

cd ~
tar zxvf haproxy-1.5.15.tar.gz -C /usr/local/src
cd /usr/local/src/haproxy-1.5.15
make TARGET=linux26 PREFIX=/usr/local/haproxy
make install PREFIX=/usr/local/haproxy

注意:TARGET=Linux26 是通过uname -a 来查看Linux内核版本的

1.4 创建haproxy主配置文件:

mkdir /etc/haproxy/
vim /etc/haproxyhaproxy.cfg

其中master_dr以及backup_dr上的haproxy配置代码内容如下

#---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
      log 127.0.0.1   local3            
      maxconn 204800
      chroot /usr/local/haproxy
      userhaproxy
      group haproxy
      daemon
      nbproc 1
      pidfile /var/run/haproxy.pid
      stats socket /usr/local/haproxy/stats
      description haproxy server
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
      log   global
      mode    http
      maxconn 10000
      optionhttplog
      optionhttpclose
      optiondontlognull
      optionforwardfor      except 127.0.0.0/8
      retries 3
      option redispatch
      option abortonclose
      balance roundrobin
      timeout http-request    10s
      timeout queue         1m
      timeout connect         10s
      timeout client          1m
      timeout server          1m
      timeout http-keep-alive 10s
      timeout check         10s
#---------------------------------------------------------------------
# use listen setting the haproxy status for site
#---------------------------------------------------------------------
listen admin_status   #设置haproxy监控状态
      bind *:3030
      mode http
      log 127.0.0.1 local3 err
      stats refresh 5s
      stats uri /status   #监控状态页面访问url
      stats realm www.skeryp.com
      stats auth admin:admin
      stats hide-version
      stats admin if TRUE
#------------------------------

1.5 创建haproxy系统服务启动脚本:

关于haproxy服务脚本代码请访问:http://www.iyunv.com/thread-190479-1-1.html
1.6 启动haproxy服务:

server haproxy restart


Keepalive部署

(1)分别在Master_DR和backup_DR上安装keepalive,操作如下:

1. 安装Keepalive所需要的相关依赖包:

# yum install openssl-devel popt-devel libnl-devel kernel-devel-y

2. 编译安装keepalive

1.1 keepalived的源码获取

keepalived源码包我们可以到keepalived的官网:http://www.keepalived.org/去下载,相关说明文档亦可在其官网查看,比如keepalived的使用,相关配置说明,这里演示的版本为:1.2.15

cd ~
wget http://www.keepalived.org/software/keepalived-1.2.15.tar.gz

1.2 编译安装keepalived

<--编译安装keepalived-->

ln -s /usr/src/kernels/2.6.32-573.18.1.el6.x86_64/ /usr/src/linux
tar zxvf keepalived-1.2.15.tar.gz -C /usr/local/src
cd /usr/local/src/keepalived-1.2.15/
./configure \
--prefix=/usr/local/keepalived \
--with-kernel-dir=/usr/src/kernels/2.6.32-573.18.1.el6.x86_64
#2.6.32-573.18.1.el6.x86_64 这个需要根据kernel来匹配,一般安装了kernel-devel即可查看
makemake install

<--对keepalived进行相关路径优化调整-->

<---拷贝keepalived相关启动命令--->
cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/

<---将keepalived启动脚本添加到系统服务--->
# cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
# chkconfig --add keepalived
# chkconfig --level 2345 keepalived on

<---创建keepalived相关配置文件--->
# mkdir -p /etc/keepalived
# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived

(3)分别配置Master_DR以及Backup_DR上的keepalive实例,如下所示:

1. master_dr配置代码示例(主调度器)

vim /etc/keepalived/keepalived.conf

内容如下

! Configuration File for keepalived

global_defs {
   notification_email {
       admin@bluemobi.cn
   }
   notification_email_fromlvs_admin@bluemobi.cn
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id DR_MASTER
}

vrrp_script check_haproxy {                              #表示创建一个脚本check_haproxy
   script "/etc/keepalived/scripts/check_haproxy.sh"   #引用的脚本路径
   interval 3                                          #表示检测时间的间隔为3s
}

vrrp_instance http {
    state BACKUP
    interface eth0   
    dont_track_primary
    nopreempt                        #不抢占master

    track_interface {                #表示需要检测的网卡
    eth0
    eth1
    }

    mcast_src_ip 172.51.96.105       #表示设置组播的源地址
    garp_master_delay 6
    virtual_router_id 60
    priority 110
    advert_int 1

    authentication {
    auth_type PASS
    autp_pass 1234
    }

    virtual_ipaddress {
    172.51.96.175/24 brd172.51.96.255 dev eth0 label eth0:1
    }

    virtual_routes {
    172.51.96.175/24 dev eth0
    }

    track_script {
    check_haproxy
    }

    notify_master /etc/keepalived/scripts/state_master.sh
    notify_backup /etc/keepalived/scripts/state_backup.sh
    notify_fault/etc/keepalived/scripts/state_fault.sh
}

2. backup_dr配置示例(备调度器)

vim /etc/keepalived/keepalived.conf

内容如下

! Configuration File for keepalived

global_defs {
   notification_email {
       admin@bluemobi.cn
   }
   notification_email_fromadmin@bluemobi.cn
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id DR_BACKUP
}

vrrp_script check_haproxy {
   script "/etc/keepalived/scripts/check_haproxy.sh"
   interval 3
}

vrrp_instance http {
    state BACKUP
    interface eth0
    dont_track_primary

    track_interface {
    eth0
    eth1
    }

    mcast_src_ip 172.51.96.119
    garp_master_delay 6
    virtual_router_id 60
    priority 105
    advert_int 1

    authentication {
    auth_type PASS
    autp_pass 1234
    }

    virtual_ipaddress {
    172.51.96.175/24 brd172.51.96.255 dev eth0 label eth0:1
    }

    virtual_routes {
    172.51.96.175/24 dev eth0
    }
   
    track_script {
    check_haproxy
    }

    notify_master /etc/keepalived/scripts/state_master.sh
    notify_backup /etc/keepalived/scripts/state_backup.sh
    notify_fault/etc/keepalived/scripts/state_fault.sh
}

3.分别在主调度server和备调度server编写以下脚本,如下:

i 当调度器为切换master server时,记录切换时间日志

vim /etc/keepalived/scripts/state_master.sh

代码如下:

#!/bin/bash
echo -e>> $LOGFILE
host=CN-SH-DR01      #设置当前的主机名
LOGFILE="/var/log/keepalived-state.log"
echo "" >> $LOGFILE
date >> $LOGFILE
echo "The ${host}Starting to become master server...." >> $LOGFILE 2>&1

echo "Please run the “ipvsadm -Ln”check the keepalived state ..." >> $LOGFILE
echo ".........................................................................!">> $LOGFILE
echo >>$LOGFILE

ii 当调度器为切换backup server时,记录切换时间日志

vim /etc/keepalived/scripts/state_backup.sh

代码如下:

#!/bin/bash
echo -e >> $LOGFILE
host=CN-SH-DR01   #设置当前的主机名
LOGFILE="/var/log/keepalived-state.log"
echo "" >> $LOGFILE
date >> $LOGFILE
echo "The ${host}Starting to become Backup server...." >> $LOGFILE 2>&1

echo "Please run the “ipvsadm -Ln”check the state ..." >> $LOGFILE
echo "........................................................................!">> $LOGFILE
echo>> $LOGFILE

iii当调度器出现错误时,记录错误时间日志

vim /etc/keepalived/scripts/state_fault.sh

代码如下:

#!/bin/bash
echo -e >> $LOGFILE
host=CN-SH-DR01      #设置当前的主机名
LOGFILE="/var/log/keepalived-state.log"
echo "" >> $LOGFILE
date >> $LOGFILE
echo "The ${host}is fault error...." >> $LOGFILE 2>&1
echo "Please check the server state ..." >> $LOGFILE
echo "........................................................................!">> $LOGFILE
echo>> $LOGFILE

iiii 服务状态健康监测脚本,比如当haproxy不可用时,及时切换到backup调度器上

vim /etc/keepalived/scripts/check_haproxy.sh

#!/bin/bash
#nginx="/usr/local/haproxy/sbin/haproxy"

PID=`ps -C haproxy --no-heading|wc -l`
if [ "${PID}" = "0" ];
    then
         /etc/init.d/haproxy start
    sleep 3
    LOCK=`ps -C haproxy --no-heading|wc -l`
    if [ "${LOCK}" = "0" ];
    then
      /etc/init.d/keepalived stop
    fi
fi

(4)依次重启master_dr,backup-dr上keepalive服务

# service keepalived restart


三.测试验证:

在两台调度器创建相关测试页,如下:

master-dr主调度服务器的测试页面

# curl 172.51.96.105
this is master server
#

backup-dr主调度服务器的测试页面

# curl 127.0.0.1

this is backup server
#

通过messages查看vip抢夺情况,如下所示:


页: [1]
查看完整版本: keepalived入门之keepalive+haproxy双机热备