一、简介

Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层、第4层和第7层交换机的功能。主要提供loadbalancing(负载均衡)和 high-availability(高可用)功能,负载均衡实现需要依赖Linux的虚拟服务内核模块(ipvs),而高可用是通过VRRP协议实现多台机器之间的故障转移服务。
Keepalived体系结构
上图是Keepalived的功能体系结构,大致分两层:用户空间(user space)和内核空间(kernel space)。

内核空间:主要包括IPVS(IP虚拟服务器,用于实现网络服务的负载均衡)和NETLINK(提供高级路由及其他相关的网络功能)两个部份。
用户空间:
WatchDog:负载监控checkers和VRRP进程的状况
VRRP Stack:负载负载均衡器之间的失败切换FailOver,如果只用一个负载均稀器,则VRRP不是必须的。
Checkers:负责真实服务器的健康检查healthchecking,是keepalived最主要的功能。换言之,可以没有VRRP Stack,但健康检查healthchecking是一定要有的。
IPVS wrapper:用户发送设定的规则到内核ipvs代码
Netlink Reflector:用来设定vrrp的vip地址等。
Keepalived的所有功能是配置keepalived.conf文件来实现的。

二、安装与配置

从官网下载keepalived的最新版本,本文以当前最新版本1.2.24为例。

2.1 安装

shell> cd /usr/local/src
shell> wget http://www.keepalived.org/software/keepalived-1.2.24.tar.gz
shell> tar -zxvf keepalived-1.2.24.tar.gz
shell> cd keepalived-1.2.24
shell> ./configure --prefix=/usr/local/keepalived
shell> make && make install

2.2 配置

安装完成后,进入安装目录的etc目录下,将keepalived相应的配置文件拷贝到系统相应的目录当中。keepalived启动时会从/etc/keepalived目录下查找keepalived.conf配置文件,如果没有找到则使用默认的配置。/etc/keepalived目录安装时默认是没有安装的,需要手动创建。配置文件目录结构如下所示:

shell> tree -l /usr/local/keepalived/etc
-- keepalived
|   |-- keepalived.conf
|   `-- samples
|       |-- keepalived.conf.status_code
|       |-- keepalived.conf.track_interface
|       |-- keepalived.conf.vrrp
|       |-- 。。。
|-- rc.d
|   `-- init.d
|       `-- keepalived
`-- sysconfig
    `-- keepalived

分别对应系统目录(忽略samples目录):

/etc/keepalived/keepalived.conf
/etc/rc.d/init.d/keepalived
/etc/sysconfig/keepalived

将配置文件拷贝到系统对应的目录下:

shell> mkdir /etc/keepalived
shell> cp /usr/local/keepalived/etc/keepalived.conf /etc/keepalived/keepalived.conf
shell> cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/rc.d/init.d/keepalived
shell> cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived

设置keepalived服务开机启动:

shell> chkconfig keepalived on
shell> service keepalived start   #启动服务
shell> service keepalived stop    #停止服务
shell> service keepalived restart #重启服务

另外需要注意的一点是,keepalived启动时不会检查配置文件的语法是否正确,所以我们在编写配置文件时要特别小心,别写错了,否则会出现一些意想不到的现象。

使用service keepalived start命令启动服务时,默认会将/etc/sysconfig/keepalived文件中KEEPALIVED_OPTIONS参数作为keepalived服务启动时的参数,并从/etc/keepalived/目录下加载keepalived.conf配置文件,或用-f参数指定配置文件的位置。

# Options for keepalived. See `keepalived --help' output and keepalived(8) and
# keepalived.conf(5) man pages for a list of all options. Here are the most
# common ones :
#
# --vrrp               -P    Only run with VRRP subsystem.
# --check              -C    Only run with Health-checker subsystem.
# --dont-release-vrrp  -V    Dont remove VRRP VIPs & VROUTEs on daemon stop.
# --dont-release-ipvs  -I    Dont remove IPVS topology on daemon stop.
# --dump-conf          -d    Dump the configuration data.
# --log-detail         -D    Detailed log messages.日志默认输出在/var/log/message文件中
# --log-facility       -S    0-7 Set local syslog facility (default=LOG_DAEMON)
#
KEEPALIVED_OPTIONS="-D"

运行keepalived –help可以查看启动时的可选参数,这些可选参数都可以配置在/etc/sysconfig/keepalived文件中的KEEPALIVED_OPTIONS选项中,作为服务启动时的参数。

keepalived正常运行后,会启动3个进程,其中一个是父进程,负责监控其子进程。一个是vrrp子进程,另外一个是checkers子进程。

shell> ps -ef | grep keepalived
root       831     1  0 11:22 ?        00:00:00 keepalived -D
root       840   831  0 11:22 ?        00:00:00 keepalived -D
root       841   831  0 11:22 ?        00:00:00 keepalived -D

到此keepalived就安装完成了。

三、Keeplive配置文件详解

第一部分:全局定义块 

1、email通知。作用:有故障,发邮件报警。 
2、Lvs负载均衡器标识(lvs_id)。在一个网络内,它应该是唯一的。 
3、花括号“{}”。用来分隔定义块,因此必须成对出现。如果写漏了,keepalived运行时,不会得到预期的结果。由于定义块内存在嵌套关系,因此很容易遗漏结尾处的花括号,这点要特别注意。

global_defs {            
   notification_email {  #指定keepalived在发生切换时需要发送email到的对象,一行一个
    sysadmin@fire.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc #指定发件人
   smtp_server localhost #指定smtp服务器地址
   smtp_connect_timeout 30 #指定smtp连接超时时间
   router_id LVS_DEVEL #运行keepalived机器的一个标识
}

第二部分:vrrp_sync_group作用

确定失败切换(FailOver)包含的路由实例个数。即在有2个负载均衡器的场景,一旦某个负载均衡器失效,需要自动切换到另外一个负载均衡器的实例是哪些? 实例组group{}至少包含一个vrrp实例

vrrp_sync_group VG_1{ #监控多个网段的实例
group {
    VI_1 #实例名
    VI_2
    ......
}
notify_master /path/xx.sh #指定当切换到master时,执行的脚本
netify_backup /path/xx.sh #指定当切换到backup时,执行的脚本
notify_fault "path/xx.sh VG_1" #故障时执行的脚本
notify /path/xx.sh 
smtp_alert #使用global_defs中提供的邮件地址和smtp服务器发送邮件通知
}

第三部分:vrrp_instance,实例名出自实例组group所包含的那些名字。

vrrp_instance VI_1 {
    state BACKUP #指定那个为master,那个为backup,如果设置了nopreempt这个值不起作用,主备考priority决

定
    interface eth0 #设置实例绑定的网卡
    dont_track_primary #忽略vrrp的interface错误(默认不设置)
    track_interface{ #设置额外的监控,里面那个网卡出现问题都会切换
    eth0
    eth1
    }
    mcast_src_ip #发送多播包的地址,如果不设置默认使用绑定网卡的primary ip
    garp_master_delay #在切换到master状态后,延迟进行gratuitous ARP请求
    virtual_router_id 50 #VPID标记
    priority 99 #优先级,高优先级竞选为master
    advert_int 1 #检查间隔,默认1秒
    nopreempt #设置为不抢占 注:这个配置只能设置在backup主机上,而且这个主机优先级要比另外一台高
    preempt_delay #抢占延时,默认5分钟
    debug #debug级别
    authentication { #设置认证
        auth_type PASS #认证方式
        auth_pass 111111 #认证密码
    }
    virtual_ipaddress { #设置vip
        192.168.202.200
    }
}

第四部分:虚拟服务器virtual_server定义块

虚拟服务器定义是keepalived框架最重要的项目了,是keepalived.conf必不可少的部分。 该部分是用来管理LVS的,是实现keepalive和LVS相结合的模块。ipvsadm命令可以实现的管理在这里都可以通过参数配置实现,注意:real_server是被包含在viyual_server模块中的,是子模块。

virtual_server 192.168.202.200 23 {        //VIP地址,要和vrrp_instance模块中的virtual_ipaddress地址一致
    delay_loop 6 #健康检查时间间隔 
    lb_algo rr #lvs调度算法rr|wrr|lc|wlc|lblc|sh|dh 
    lb_kind DR #负载均衡转发规则NAT|DR|RUN 
    persistence_timeout 5 #会话保持时间 
    protocol TCP #使用的协议 
    persistence_granularity <NETMASK> #lvs会话保持粒度 
    virtualhost <string> #检查的web服务器的虚拟主机(host:头) 
    sorry_server<IPADDR> <port> #备用机,所有realserver失效后启用


real_server 192.168.200.5 23 {             //RS的真实IP地址
            weight 1 #默认为1,0为失效
            inhibit_on_failure #在服务器健康检查失效时,将其设为0,而不是直接从ipvs中删除 
            notify_up <string> | <quoted-string> #在检测到server up后执行脚本
            notify_down <string> | <quoted-string> #在检测到server down后执行脚本
            
TCP_CHECK {                    //常用
            connect_timeout 3 #连接超时时间
            nb_get_retry 3 #重连次数
            delay_before_retry 3 #重连间隔时间
            connect_port 23  健康检查的端口的端口
            bindto <ip>   
          }

HTTP_GET | SSL_GET{          //不常用
    url{ #检查url,可以指定多个
         path /
         digest <string> #检查后的摘要信息
         status_code 200 #检查的返回状态码
        }
    connect_port <port> 
    bindto <IPADD>
    connect_timeout 5
    nb_get_retry 3
    delay_before_retry 2
}

SMTP_CHECK{                 //不常用
    host{
    connect_ip <IP ADDRESS>
    connect_port <port> #默认检查25端口
    bindto <IP ADDRESS>
         }
    connect_timeout 5
    retry 3
    delay_before_retry 2
    helo_name <string> | <quoted-string> #smtp helo请求命令参数,可选
}
 
MISC_CHECK{                 //不常用
    misc_path <string> | <quoted-string> #外部脚本路径
    misc_timeout #脚本执行超时时间
    misc_dynamic #如设置该项,则退出状态码会用来动态调整服务器的权重,返回0 正常,不修改;返回1,

检查失败,权重改为0;返回2-255,正常,权重设置为:返回状态码-2
}
}

 生产环境配置文件实例:

[root@LB2 ~]# cat /etc/keepalived/keepalived.conf 
! Configuration File for keepalived

global_defs {
        notification_email {
                49000448@qq.com
        }
        notification_email_from Alexandre.Cassen@firewall.loc
                smtp_server 10.0.0.1
                smtp_connect_timeout 30
                router_id LVS_2
}

vrrp_instance VI_1 {
        state BACKUP
                interface eth0
                virtual_router_id 55
                priority 100
                advert_int 1
                authentication {
                        auth_type PASS
                                auth_pass 1111
                }
        virtual_ipaddress {
                192.168.220.110/24
        }


virtual_server 192.168.220.110 80 {
        delay_loop 6
        lb_algo wrr
        lb_kind DR
        nat_mask 255.255.255.0
        persistence_timeout 300
        protocol TCP
}

real_server 192.168.220.129 80 {
        weight 1
        TCP_CHECK {
                        connect_timeout 8
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 80
                }
}


real_server 192.168.220.138 80 {
        weight 1
        TCP_CHECK {
                        connect_timeout 8
                        nb_get_retry 3
                        delay_before_retry 3
                        connect_port 80
                }
}
}
[root@LB2 ~]# 

为keepalive指定日志文件

默认keepalive的日志文件是
/var/log/messages
[root@LB2 ~]# tail -5 /var/log/messages
Dec  7 22:40:10 LB2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Dec  7 22:40:10 LB2 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(10,11)]
Dec  7 22:40:10 LB2 Keepalived_healthcheckers: Opening file '/etc/keepalived/keepalived.conf'.
Dec  7 22:40:10 LB2 Keepalived_healthcheckers: Configuration is using : 8425 Bytes
Dec  7 22:40:10 LB2 Keepalived_healthcheckers: Using LinkWatch kernel netlink reflector...
[root@LB2 ~]# 

修改配置

[root@LB2 ~]# vim /etc/sysconfig/keepalived
  ......
  #KEEPALIVED_OPTIONS="-D"          //注释掉该行
  KEEPALIVED_OPTIONS="-D -d -S 0"  //添加改行
  ......
[root@LB2 ~]# vim /etc/rsyslog.conf 
  ......
  local0.*       /var/log/keepalived.log           //添加改行
[root@LB2 ~]# /etc/init.d/rsyslog restart            //重启日志服务
Shutting down system logger: [ OK ]           
Starting system logger: [ OK ]
[root@LB2 ~]# /etc/init.d/keepalived restart         //重启keepalive服务
Stopping keepalived: [ OK ]
Starting keepalived: [ OK ]
[root@LB2 ~]# tail -5 /var/log/keepalived.log 
Dec 8 01:02:06 LB2 Keepalived_vrrp: Virtual IP = 1
Dec 8 01:02:06 LB2 Keepalived_vrrp: 192.168.220.110/24 brd 192.168.220.110 dev eth0 scope global
Dec 8 01:02:06 LB2 Keepalived_vrrp: Using LinkWatch kernel netlink reflector...
Dec 8 01:02:06 LB2 Keepalived_vrrp: VRRP_Instance(VI_1) Entering BACKUP STATE
Dec 8 01:02:06 LB2 Keepalived_vrrp: VRRP sockpool: [ifindex(2), proto(112), fd(10,11)]
[root@LB2 ~]#