菜鸟学Linux 第093篇笔记 keepalived




内容总览

keepalived介绍

配置keepalived的ipvs功能

keepalived的几个问题解决方法




keepalived

它可以实现ipvs的功能,并且还可以实现前端的ipvs-server高可用,以及后台realserver

的健康状态检查,而不像ipvs需要用户自己来写脚本完成后台的realserver健康状态检测


HA的底层功能

ipvs --> ha 为ipvs提供高可用功能ha

ipvs --> 可以使VIP在节点间流转

vrrp 虚拟路由冗余协议

能将多个物理接口虚拟成一个物理接口,用来解决局域网网关的冗余






配置keepalived 使其实现ipvs的功能,可以检测后台realserver的状态


准备工作

两台realserver节点 192.168.11.105 192.168.11.106

/proc/sys/net/ipv4/conf/all/arp_announce=2

/proc/sys/net/ipv4/conf/all/arp_ignore=1

/proc/sys/net/ipv4/conf/lo/arp_announce=2

/proc/sys/net/ipv4/conf/lo/arp_ignore=1

netstat -tunlp 开启80端口 (httpd)


两台keepalived + ipvsadm节点 192.168.11.101 192.168.11.102

虚拟的IP为192.168.11.100



1. 两台keepalived + ipvsadm节点安装

# yum -y install keepalived ipvsadm(安装ipvsadm软件是为了查看规则)


2. 配置节点的keepalived /etc/keepalived/keepalived.conf

# vim /etc/keepalived/keepalived.conf

修改如下

global_defs {

  notification_email {

   root@localhost (此项里是添加email地址,当有信息是将通知到此mail里)

  }

  notification_email_from keepalive@localhost (寄件人)

  smtp_server 127.0.0.1  

  smtp_connect_timeout 30

  router_id LVS_DEVEL

}


vrrp_instance VI_1 {

   state MASTER

   interface eth0

   virtual_router_id 51

   priority 101

   advert_int 1

   authentication {

       auth_type PASS

       auth_pass keepaliveps

   }

   virtual_ipaddress {

192.168.11.100/24 dev eth0 label eth0:0  (所虚拟的地址)

   }

}


virtual_server 192.168.11.100 80 {

   delay_loop 6

   lb_algo rr

   lb_kind DR

   nat_mask 255.255.255.0

   #persistence_timeout 50

   protocol TCP


   real_server 192.168.11.105 80 {

       weight 1

       HTTP_GET { (验证http的方式)

           url {

             path /

     status_code 200

           }

           connect_timeout 2

           nb_get_retry 3

           delay_before_retry 1

       }

   }

   real_server 192.168.11.106 80 {

       weight 1

       HTTP_GET {

           url {

             path /

     status_code 200

           }

           connect_timeout 2

           nb_get_retry 3

           delay_before_retry 1

       }

   }

}

备节点里只需要将此配置文件复制过去然后修改state MASTER 为备份节点即可BACKUP



keepalived的几个问题解决

1. 所有realserver都down,如何处理?

在两台前端ipvs服务器安装httpd 并开启此服务 记得要添加html文档

在配置文件/etc/keepalived/keepalived.conf

virtual_server 192.168.11.100 80 括号里添加sorry_server 127.0.0.1 80


2. 自写监测脚本,完成维护模式切换ipvs-server主备切换

# vim /etc/keepalived/keepalived.conf

在vrrp_instance 添加一段

   track_script {

chk_schedown

   }

添加一段在末尾

vrrp_script chk_schedown {

   script "[[ -f /etc/keepalived/down ]] && exit 1 || exit 0"

   interval 1

   weight -2

   fall 2

   rise 1

}

当要测试使其ipvs服务器切换只需要在/etc/keepalived/目录创建一个down文件,查看其是否

会切换


3. 如何在vrrp事务发生时,主备切换时,如何发送警告邮件给指定的管理员?

编写脚本vim /etc/keepalived/notify.sh

#!/bin/bash

#


contact='root@localhost'


Usage() {

 echo "Usage: `basename $0` {master|backup|fault} VIP"

}


Notify() {

 subject="`hostname`'s state changed to $1"

 mailbody="`date "+%F %T"`: `hostname`'s state changed to $1, $VIP floating."

 echo $mailbody | mail -s "$subject" $contact

}


VIP=$2


case $1 in

 master)

   Notify master

   ;;

 backup)

   Notify backup

   ;;

 fault)

   Notify fault

   ;;

 *)

   Usage

   exit 1

   ;;

esac

此脚本可接受参数,当接受到指定的参数时,则会将相应的信息发送给root用户的mailbox里


编辑/etc/keepalived/keepalived.conf

在vrrp_instance 括号里添加

   notify_master "/etc/keepalived/notify.sh master 192.168.11.100"

   notify_backup "/etc/keepalived/notify.sh backup 192.168.11.100"

   notify_fault "/etc/keepalived/notify.sh fault 192.168.11.100"


至此,当主节点和备节点切换时,,则会以邮件形式通知给root