Keepalived

Keepalived 是一种高性能的服务器高可用或热备解决方案,Keepalived可以用来防止服务器单点故障(单点故障是指一旦某一点出现故障就会导致整个系统架构的不可用)的发生,通过配合Nginx可以实现web前端服务的高可用。 

Keepalived实现的基础是VRRP协议,Keepalived就是巧用VRRP协议来实现高可用性(HA)

VRRP(Virtual Router Redundancy Protocol)协议是用于实现路由器冗余的协议,VRRP协议将两台或多台路由器设备虚拟成一个设备,对外提供虚拟路由器IP(一个或多个),而在路 由器组内部,如果实际拥有这个对外IP的路由器如果工作正常的话就是MASTER,或者是通过算法选举产生,MASTER实现针对虚拟路由器IP的各种网 络功能,如ARP请求,ICMP,以及数据的转发等;其他设备不拥有该IP,状态是BACKUP,除了接收MASTERVRRP状态通告信息外,不执行 对外的网络功能。当主机失效时,BACKUP将接管原先MASTER的网络功能。 

VRRP协议使用多播数据来传输VRRP数据,VRRP数据使用特殊的虚拟源MAC地址发送数据而不是自身网卡的MAC地址,VRRP运行时只有 MASTER路由器定时发送VRRP通告信息,表示MASTER工作正常以及虚拟路由器IP(),BACKUP只接收VRRP数据,不发送数据,如果一 定时间内没有接收到MASTER的通告信息,各BACKUP将宣告自己成为MASTER,发送通告信息,重新进行MASTER选举状态。 

安装keepalived所依赖的函数库  

安装 openssl 

yum install openssl* 

安装popt 

yum install popt* 

安装ipvsadm 

yum isntall ipvsadm 

安装libnl-dev 

yum install libnl-dev*

VRRP的优势:

 

冗余:可以使用多个路由器设备作为LAN客户端的默认网关,大大降低了默认网关成为单点故障的可能性;

负载共享:允许来自LAN客户端的流量由多个路由器设备所共享;

多VRRP组:在一个路由器物理接口上可配置多达255个VRRP组;

多IP地址:基于接口别名在同一个物理接口上配置多个IP地址,从而支持在同一个物理接口上接入多个子网;

抢占:在master故障时允许优先级更高的backup成为master;

通告协议:使用IANA所指定的组播地址224.0.0.18进行VRRP通告;

VRRP追踪:基于接口状态来改变其VRRP优先级来确定最佳的VRRP路由器成为master;

 配置keepalived为实现haproxy高可用的配置文件示例:

! Configuration File for keepalived  
global_defs {  
   notification_email {  
        Email@Email.com  
   }  
   notification_email_from Username@Email.com 
   smtp_connect_timeout 3  
   smtp_server 127.0.0.1  
   router_id LVS_DEVEL  
}  
vrrp_script 需要在实例之外定义, 但是这只是定义,执行需要下面的track_script
vrrp_script chk_haproxy {  
    script "killall -0 haproxy"  # outside script 
    interval 1  #check every 1 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_mantaince_down {
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"   
   interval 1
   weight 2
}
 
vrrp_instance VI_1 {  
    interface eth0  
    state MASTER  # BACKUP for slave routers
    priority 101  # 100 for BACKUP master’s priority large than BACKUP
    virtual_router_id 51  need be consistent with BACKUP
    garp_master_delay 1 
  
    authentication {  
        auth_type PASS  
        auth_pass password  
    }  
    track_interface {  
       eth0    
    }  
    virtual_ipaddress {  
        172.16.100.1/16 dev eth0 label eth0:0 
    }  
    track_script {  # 在实例中定义
        chk_haproxy  
        chk_mantaince_down
    }  
    notify_master "/etc/keepalived/notify.sh master"  
    notify_backup "/etc/keepalived/notify.sh backup"  
    notify_fault "/etc/keepalived/notify.sh fault"  


下面是一个notify.sh脚本的简单示例:

#!/bin/bash
# Author: MageEdu 
# description: An example of notify script

 
vip=172.16.100.1
contact='root@localhost'
 
Notify() {
    mailsubject="`hostname` to be $1: $vip floating"
    mailbody="`date '+%F %H:%M:%S'`: vrrp transition, `hostname` changed to be $1"
    echo $mailbody | mail -s "$mailsubject" $contact
}
 
case "$1" in
    master)
        notify master
        /etc/rc.d/init.d/haproxy start
        exit 0
    ;;
    backup)
        notify backup
        /etc/rc.d/init.d/haproxy restart
        exit 0
    ;;
    fault)
        notify fault
        exit 0
    ;;
    *)
        echo 'Usage: `basename $0` {master|backup|fault}'
        exit 1
    ;;
Esac
 


配置keepalived为实现haproxy高可用的双主模型配置文件示例:

 

说明:其基本实现思想为创建两个虚拟路由器,并以两个节点互为主从。

 

! Configuration File for keepalived  
  
global_defs {  
   notification_email {  
         linuxedu@foxmail.com
         mageedu@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_haproxy {  
    script "killall -0 haproxy"  
    interval 1  
    weight 2  
}  
 
vrrp_script chk_mantaince_down {
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
   interval 1
   weight 2
}
 
vrrp_instance VI_1 {  
    interface eth0  
    state MASTER  # BACKUP for slave routers
    priority 101  # 100 for BACKUP
    virtual_router_id 51 
    garp_master_delay 1 
  
    authentication {  
        auth_type PASS  
        auth_pass password  
    }  
    track_interface {  
       eth0    
    }  
    virtual_ipaddress {  
        172.16.100.1/16 dev eth0 label eth0:0 
    }  
    track_script {  
        chk_haproxy  
        chk_mantaince_down
    }  
  
 
    notify_master "/etc/keepalived/notify.sh master"  
    notify_backup "/etc/keepalived/notify.sh backup"  
    notify_fault "/etc/keepalived/notify.sh fault"  

 
vrrp_instance VI_2 {  
    interface eth0  
    state BACKUP  # BACKUP for slave routers
    priority 100  # 100 for BACKUP
    virtual_router_id 52
    garp_master_delay 1 
  
    authentication {  
        auth_type PASS  
        auth_pass password  
    }  
    track_interface {  
       eth0    
    }  
    virtual_ipaddress {  
        172.16.100.2/16 dev eth0 label eth0:1
    }  
    track_script {  
        chk_haproxy  
        chk_mantaince_down
    }    
}


LVS + keepalived的实现:

 

! Configuration File for keepalived  
  
global_defs {  
   notification_email {  
         linuxedu@foxmail.com
         mageedu@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_schedown {
   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"
   interval 2
   weight -2
}
 
vrrp_instance VI_1 {  
    interface eth0  
    state MASTER  
    priority 101
    virtual_router_id 51 
    garp_master_delay 1 
 
    authentication {  
        auth_type PASS  
        auth_pass password  
    }  
 
    track_interface {  
       eth0    
    }  
 
    virtual_ipaddress {  
        172.16.100.1/16 dev eth0 label eth0:0
    }  
 
    track_script {  
        chk_schedown
    }    

 
 
virtual_server 172.16.100.1 80 {
    delay_loop 6
    lb_algo rr 
    lb_kind DR
    persistence_timeout 50
    protocol TCP
 
#    sorry_server 192.168.200.200 1358
 
    real_server 172.16.100.11 80 {
        weight 1
        HTTP_GET {
            url { 
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
 
    real_server 172.16.100.12 80 {
        weight 1
        HTTP_GET {
            url { 
              path /
              status_code 200
            }
            connect_timeout 3
            nb_get_retry 3
            delay_before_retry 3
        }
    }
}


如果要使用TCP_CHECK检测各realserver的健康状态,那么,上面关于realserver部分的定义也可以替换为如下内容:

virtual_server 172.16.100.1 80 {
    delay_loop 6
    lb_algo rr 
    lb_kind DR
    persistence_timeout 300
    protocol TCP
 
    sorry_server 127.0.0.1 80
 
    real_server 172.16.100.11 80 {
        weight 1
        TCP_CHECK {
    tcp_port 80
            connect_timeout 3
        }
    }
 
    real_server 172.16.100.12 80 {
        weight 1
        TCP_CHECK {
    connect_port 80
            connect_timeout 3
          }
    }
}


说明:其中的sorry_server是用于定义所有realserver均出现故障时所用的服务器。

 

keepalived通知脚本进阶示例:

 

下面的脚本可以接受选项,其中:

-s, --service SERVICE,...:指定服务脚本名称,当状态切换时可自动启动、重启或关闭此服务;

-a, --address VIP: 指定相关虚拟路由器的VIP地址;

-m, --mode {mm|mb}:指定虚拟路由的模型,mm表示主主,mb表示主备;它们表示相对于同一种服务而方,其VIP的工作类型;

-n, --notify {master|backup|fault}:指定通知的类型,即vrrp角色切换的目标角色;

-h, --help:获取脚本的使用帮助;

 

#!/bin/bash
# Author: MageEdu 
# description: An example of notify script
# Usage: notify.sh -m|--mode {mm|mb} -s|--service SERVICE1,... -a|--address VIP  -n|--notify {master|backup|falut} -h|--help 
 
#contact='linuxedu@foxmail.com'
helpflag=0
serviceflag=0
modeflag=0
addressflag=0
notifyflag=0
 
contact='root@localhost'
 
Usage() {
  echo "Usage: notify.sh [-m|--mode {mm|mb}] [-s|--service SERVICE1,...] <-a|--address VIP>  <-n|--notify {master|backup|falut}>" 
  echo "Usage: notify.sh -h|--help"
}
 
ParseOptions() {
  local I&#61;1;
  if [ $# -gt 0 ]; then
    while [ $I -le $# ]; do
      case $1 in
  -s|--service)
[ $# -lt 2 ] && return 3
     serviceflag&#61;1
 services&#61;(&#96;echo $2|awk -F"," &#39;{for(i&#61;1;i<&#61;NF;i&#43;&#43;) print $i}&#39;&#96;)
shift 2 ;;
  -h|--help)
 helpflag&#61;1
return 0
        shift
;;
  -a|--address)
[ $# -lt 2 ] && return 3
    addressflag&#61;1
vip&#61;$2
shift 2
;;
  -m|--mode)
[ $# -lt 2 ] && return 3
mode&#61;$2
shift 2
;;
  -n|--notify)
[ $# -lt 2 ] && return 3
notifyflag&#61;1
notify&#61;$2
shift 2
;;
  *)
echo "Wrong options..."
Usage
return 7
;;
       esac
    done
    return 0
  fi
}
 
#workspace&#61;$(dirname $0)
 
RestartService() {
  if [ ${#&#64;} -gt 0 ]; then
    for I in $&#64;; do
      if [ -x /etc/rc.d/init.d/$I ]; then
        /etc/rc.d/init.d/$I restart
      else
        echo "$I is not a valid service..."
      fi
    done
  fi
}
 
StopService() {
  if [ ${#&#64;} -gt 0 ]; then
    for I in $&#64;; do
      if [ -x /etc/rc.d/init.d/$I ]; then
        /etc/rc.d/init.d/$I stop
      else
        echo "$I is not a valid service..."
      fi
    done
  fi
}
 
 
Notify() {
    mailsubject&#61;"&#96;hostname&#96; to be $1: $vip floating"
    mailbody&#61;"&#96;date &#39;&#43;%F %H:%M:%S&#39;&#96;, vrrp transition, &#96;hostname&#96; changed to be $1."
    echo $mailbody | mail -s "$mailsubject" $contact
}
 
 
# Main Function
ParseOptions $&#64;
[ $? -ne 0 ] && Usage && exit 5
 
[ $helpflag -eq 1 ] && Usage && exit 0
 
if [ $addressflag -ne 1 -o $notifyflag -ne 1 ]; then
  Usage
  exit 2
fi
 
mode&#61;${mode:-mb}
 
case $notify in
&#39;master&#39;)
  if [ $serviceflag -eq 1 ]; then
      RestartService ${services[*]}
  fi
  Notify master
  ;;
&#39;backup&#39;)
  if [ $serviceflag -eq 1 ]; then
    if [ "$mode" &#61;&#61; &#39;mb&#39; ]; then
      StopService ${services[*]}
    else
      RestartService ${services[*]}
    fi
  fi
  Notify backup
  ;;
&#39;fault&#39;)
  Notify fault
  ;;
*)
  Usage
  exit 4
  ;;
Esac

keepalived.conf配置文件中&#xff0c;其调用方法如下所示&#xff1a;

    notify_master "/etc/keepalived/notify.sh -n master -a 172.16.100.1"  

    notify_backup "/etc/keepalived/notify.sh -n backup -a 172.16.100.1"  

    notify_fault "/etc/keepalived/notify.sh -n fault -a 172.16.100.1"  

使用bash  -n  检查是否有语法错误