IT技术互动交流平台

轻量级keepalive实现高可用和热备系列一之WEB服务的简单高可用

作者:菜鸟小败  发布日期:2013-05-16 14:15:44

一、keepalive软件概述:

那么什么是keepalived呢?keepalived见名知意,那就是保持存活,那么在网络术语中就是保持在线,这就是所谓的高可用或热备,用来防止单点故障的发生。那说到keepalived时不得不说的一个协议就是VRRP协议,可以说这个协议就是keepalived实现的基础,那么首先我们来看看VRRP协议;

vrrp协议概述:

随着Internet的不断发展,人们对于网路可靠性的要求越来越高,特别是针对于依赖于网络办公的终端用户,能够实时与网路其他部分保持联系是非常重要的。由于公网IP资源的稀少,我们不得不依靠其他的网路技术来实现上网功能;下面这幅图就是一个典型的网路拓扑:
 


主机A、B、C通过网关联系互联网,以实现上网通信的目的,但是此时我们不得不考虑一个问题,那就是当我们只拥有一个网关设备时,这个网关就是在整个的网络中的一个关键点;我们不得不去考虑当网关宕机的时候该如何去处理;此时另外一种技术应运而生:那就是VRRP。

VRRP(Virtual Router Redundancy Protocol,虚拟路由器冗余协议)将可用承担网关功能的一组路由器加入到备份组中形成有一台虚拟路由器,由VRRP的选举机制决定哪台路由器承担转发任务,局域网内的主机只需要将虚拟路由器配置为网关即可实现正常的上网功能,并且在物理网关中的某台或某几台设备宕机之后也不影响客户端的使用。拓扑如下:

我们使用多台的路由设备,将其组成一台虚拟路由器,此时客户端只需要知道虚拟路由的地址配置网关即可,不仅实现了网络的高可用,同时也减少了网络管理的繁琐工作;

VRRP备份组简介:

  VRRP将局域网内的一组路由器划分在一起,称为一个备份组。备份组由一个master路由器和多个backup路由器组成。Master由选举产生,优先级高的成为master,优先级的取值范围在0到255之间,可配置的范围在1到254之间。

  在同一个物理设备上可以配置多个组,组与组之间依靠组ID进行区分;同组之间的backup路由器有两种工作模式:

  非抢占方式:依靠各路由器的优先级进行自主选择,选择完成后如非路由故障,否则即使其他路由器的优先级被提升也不会成为master。

  抢占方式:备份路由器如果工作在抢占方式下,那么一旦路由的优先级发生变动,那么master角色就会根据优先级重新选举产生;

  那么VRRP是如何知道优先级变化呢?

  VRRP自身会维护两种定时器:VRRP通告报文间隔时间定时器和VRRP抢占延时时间定时器;VRRP会在固定的时间内向外发送VRRP通告报文,当backup路由器在固定时间内没有收到报文,并且间隔三次时间之后依然没有收到报文,那么backup路由器就会认定自己是master路由器,并向外通告进行重新选举;

  由于报文需要在网络中进行流转发送,VRRP提供了三种认证方式:

  无认证:不进行任何的VRRP报文的合法性认证,不提供安全性保障;

  简单字符认证:发送VRRP报文的路由器将认证字填入到报文中,而收到报文的路由器会将报文中的认证字和本地配置的认证字进行对比,从而实现认证安全保障;www.it165.net

  MD5认证:发送报文的路由器利用认证字和MD5算法对VRRP报文进行加密,加密后的报文保存在Authentication Header中,收到报文的路由器利用认证字解密报文,检查该报文的合法性。

基于这些技术,keepalive实现了高可用。

三、keepalive安装配置

1、安装准备:

keepalived、lm_sensors、httpd软件包

两台主机:

node1:172.16.12.8

node2:172.16.12.9

虚拟IP:172.16.12.254

2、配置:

master配置:


# cd /etc/keepalived/ 
# vim keepalived.conf 
! Configuration File for keepalived 
global_defs { 
   notification_email { 
         wangej@126.com
   } 
   notification_email_from kanotify@magedu.com 
   smtp_connect_timeout 3
   smtp_server 127.0.0.1
   router_id LVS_DEVEL 
} 
vrrp_script chk_httpd { 
    script "killall -0 httpd"
    interval 2
        # check every 2 seconds 
    weight -2
        # if failed, decrease 2 of the priority 
    fall 2
        # require 2 failures for failures 
    rise 1
        # require 1 sucesses for ok 
} 
vrrp_script chk_schedown { 
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
   interval 2
   weight -2
} 
vrrp_instance VI_1 { 
    interface eth0 
        # interface for inside_network, bound by vrrp 
    state MASTER 
        # Initial state, MASTER|BACKUP 
        # As soon as the other machine(s) come up, 
        # an election will be held and the machine 
        # with the highest "priority" will become MASTER. 
        # So the entry here doesn't matter a whole lot. 
    priority 101
        # for electing MASTER, highest priority wins. 
        # to be MASTER, make 50 more than other machines. 
    virtual_router_id 51
        # arbitary unique number 0..255 
        # used to differentiate multiple instances of vrrpd 
        # running on the same NIC (and hence same socket). 
    garp_master_delay 1
    authentication { 
        auth_type PASS 
        auth_pass password 
    } 
    track_interface { 
       eth0 
    } 
        # optional, monitor these as well. 
        # go to FAULT state if any of these go down. 
    virtual_ipaddress { 
        172.16.12.254/16 dev eth0 label eth0:0
    } 
    track_script { 
        chk_httpd 
        chk_schedown 
    } 
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

backup配置:

 

# cd /etc/keepalived/ 
# vim keepalived.conf 
! Configuration File for keepalived 
global_defs { 
   notification_email { 
         wangej@126.com
   } 
   notification_email_from kanotify@magedu.com 
   smtp_connect_timeout 3
   smtp_server 127.0.0.1
   router_id LVS_DEVEL 
} 
vrrp_script chk_httpd { 
    script "killall -0 httpd"
    interval 2
        # check every 2 seconds 
    weight -2
        # if failed, decrease 2 of the priority 
    fall 2
        # require 2 failures for failures 
    rise 1
        # require 1 sucesses for ok 
} 
vrrp_script chk_schedown { 
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
   interval 2
   weight -2
} 
vrrp_instance VI_1 { 
    interface eth0 
        # interface for inside_network, bound by vrrp 
    state BACKUP 
        # Initial state, MASTER|BACKUP 
        # As soon as the other machine(s) come up, 
        # an election will be held and the machine 
        # with the highest "priority" will become MASTER. 
        # So the entry here doesn't matter a whole lot. 
    priority 100
        # for electing MASTER, highest priority wins. 
        # to be MASTER, make 50 more than other machines. 
    virtual_router_id 51
        # arbitary unique number 0..255 
        # used to differentiate multiple instances of vrrpd 
        # running on the same NIC (and hence same socket). 
    garp_master_delay 1
    authentication { 
        auth_type PASS 
        auth_pass password 
    } 
    track_interface { 
       eth0 
    } 
        # optional, monitor these as well. 
        # go to FAULT state if any of these go down. 
    virtual_ipaddress { 
        172.16.12.254/16 dev eth0 label eth0:0
    } 
    track_script { 
        chk_httpd 
        chk_schedown 
    } 
    notify_master "/etc/keepalived/notify.sh master"
    notify_backup "/etc/keepalived/notify.sh backup"
    notify_fault "/etc/keepalived/notify.sh fault"
}

内容简述:

 

vrrp_instance VI_1 {                                #定义虚拟路由 
    state MASTER                                    #定义初始状态下的主从路由 
    interface eth0                                  #定义连接端口 
    virtual_router_id 51                            #定义虚拟路由的ID 
    priority 100                                    #定义优先级 
    advert_int 1                                    #定义通告间隔时间 
    authentication { 
        auth_type PASS                              #定义认证类型 
        auth_pass 1111                              #定义认证密码 
    }       
    virtual_ipaddress {                             #定义虚拟地址 
        192.168.200.16
    } 
}

四、keepalive功能验证

1、安装httpd


# yum -y install httpd 
# echo '<h1>node2.wangej.com</h1>' > /var/www/html/index.html 
# cat /var/www/html/index.html 
# service httpd start

2、启动keepalive服务

在两台机器上启动keepalive服务


# service keepalived start

关闭主节点,重新测试页面:
 

 

至此,一个简易的为WEB服务提供高可用服务的集群就搭建完成了;

PS:关闭主节点是在脚本中完成的,脚本内容如下:


#!/bin/bash 
# Author: MageEdu <linuxedu@foxmail.com> 
# description: An example of notify script 
# 
ifalias=${2:-eth0:0} 
interface=$(echo $ifalias | awk -F: '{print $1}') 
vip=$(ip addr show $interface | grep $ifalias | awk '{print $2}') 
#contact='linuxedu@foxmail.com' 
contact='root@localhost'
workspace=$(dirname $0) 
notify() { 
    subject="$ip change to $1"
    body="$ip change to $1 $(date '+%F %H:%M:%S')"
    echo $body | mail -s "$1 transition" $contact 
} 
case "$1" in
    master) 
        notify master 
        exit 0 
    ;; 
    backup) 
        notify backup 
        /etc/rc.d/init.d/httpd restart 
        exit 0 
    ;; 
    fault) 
        notify fault 
        exit 0 
    ;; 
    *) 
        echo 'Usage: $(basename $0) {master|backup|fault}'
        exit 1 
    ;; 
esac

在/etc/keepalived目录中创建一个down文件即可实现关闭主节点的目的。

我们最后再看一下master和backup的IP地址:


Tag标签: keepalive   WEB服务  
  • 专题推荐

About IT165 - 广告服务 - 隐私声明 - 版权申明 - 免责条款 - 网站地图 - 网友投稿 - 联系方式
本站内容来自于互联网,仅供用于网络技术学习,学习中请遵循相关法律法规