欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

Keepalived-1.3.0编译安装和启动方式详解

程序员文章站 2023-10-27 14:18:10
一、编译安装Keepalived Keepalived是一个免费开源的,用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层、第4层和第7层交换机的功能。主要提供loadbalancing(负载均衡)和high-availability(高可用)功能,负载均衡实现需要依赖L ......

一、编译安装Keepalived         

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

 Keepalived-1.3.0编译安装和启动方式详解

上图是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地址等。

二、官网下载源码包(2018-5-26 最新版keepalived-2.0.0.tar.gz)

http://www.keepalived.org/download.html

cd /usr/local/src
wget http://www.keepalived.org/software/keepalived-1.2.0.tar.gz tar xf keepalived-2.0.0.tar.gz cd keepalived-2.0.0 ./configure --prefix=/application/keepalived-2.0.0

1.系统出现警告信息 “

checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... configure: error: newly created file is older than distributed files!
Check your system clock

”此报错是由于系统时间与硬件时间不一致导致的。

**************************************************************

linux系统的时间分为系统时间 和 硬件时间。 
硬件时间 就是指RTC中的时间,hwclock命令就是对这个时间操作。 
系统时间 是系统开机时读取硬件时间作为系统的时间,此后系统时钟独立运作,在系统关机时再将系统时间写回RTC。

date命令只操作系统时间,hwclock操作硬件时钟,在date 修改时间后 最好用hwclock 同步一下,以免系统非正常关机造成时间不同步。

[root@Web01 src]# date 
Sun Mar 19 09:35:08 CST 2017
[root@Web01 src]# hwclock 
Tue 03 Jul 2018 10:19:17 PM CST  -0.458468 seconds

同步时间,写入CMOS:

[root@Web01 src]# ntpdate ntp1.aliyun.com
 3 Jul 14:28:12 ntpdate[79878]: step time server 120.25.115.20 offset 40711428.938760 sec
[root@Web01 src]# date 
Tue Jul  3 14:28:19 CST 2018
[root@Web01 src]# hwclock 
Tue 03 Jul 2018 10:28:23 PM CST  -0.860282 seconds
[root@Web01 src]# hwclock 
Tue 03 Jul 2018 10:28:36 PM CST  -0.641474 seconds

问题解决!!!!  

2. 系统出现警告信息

!!! OpenSSL is not properly installed on your system. !!!
!!! Can not include OpenSSL headers files. !!!

解决方法:

[root@Web01 keepalived-2.0.0]# yum install -y openssl openssl-devel

3. 系统出现警告信息

“*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.”,具体日志如下:

Keepalived configuration
------------------------
Keepalived version       : 2.0.0
Compiler                 : gcc
Preprocessor flags       : 
Compiler flags           : -Wall -Wunused -Wstrict-prototypes -Wextra -Winit-self -g -O2 -D_GNU_SOURCE -fPIE -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fexceptions --param=ssp-buffer-size=4
Linker flags             :  -pie
Extra Lib                :  -lcrypto  -lssl 
Use IPVS Framework       : Yes
IPVS use libnl           : No
IPVS syncd attributes    : No
IPVS 64 bit stats        : No
fwmark socket support    : Yes
Use VRRP Framework       : Yes
Use VRRP VMAC            : Yes
Use VRRP authentication  : Yes
With ip rules/routes     : Yes
Use BFD Framework        : No
SNMP vrrp support        : No
SNMP checker support     : No
SNMP RFCv2 support       : No
SNMP RFCv3 support       : No
DBUS support             : No
SHA1 support             : No
Use Json output          : No
libnl version            : None
Use IPv4 devconf         : No
Use libiptc              : No
Use libipset             : No
init type                : upstart
Build genhash            : Yes
Build documentation      : No

*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.

解决方法:执行yum命令yum -y install libnl libnl-devel解决上述警告问题

[root@Web01 keepalived-2.0.0]# yum -y install libnl libnl-devel 

4. 系统出现错误信息

“configure: error: libnfnetlink headers missing”,具体日志如下:

checking for nl_socket_modify_cb in -lnl... yes
checking for linux/rtnetlink.h... yes
checking libnfnetlink/libnfnetlink.h usability... no
checking libnfnetlink/libnfnetlink.h presence... no
checking for libnfnetlink/libnfnetlink.h... no
configure: error: libnfnetlink headers missing

安装完成以后,重新执行configure命令

[root@Web01 keepalived-2.0.0]# ./configure --prefix=/application/keepalived-2.0.0

三、编译Keepalived过程中上述报错等问题解决后,没有其他报错,执行编译并安装

[root@Web01 keepalived-2.0.0]# make && make install
[root@Web01 keepalived-2.0.0]# echo $?
0

创建软连接(方便以后软件版本升级)
[root@Web01 keepalived-2.0.0]# ln -s /application/keepalived-2.0.0/ /application/keepalived

四、将Keepalived注册为系统服务

[root@Web01 keepalived-2.0.0]# mkdir /etc/keepalived/
[root@Web01 keepalived-2.0.0]# cp /usr/local/src/keepalived-2.0.0/keepalived/etc/init.d/keepalived /etc/init.d/
[root@Web01 keepalived-2.0.0]# cp /usr/local/src/keepalived-2.0.0/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
[root@Web01 keepalived-2.0.0]# cp /usr/local/src/keepalived-2.0.0/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

这样就可以执行service keepalived [start | stop | reload | restart ]命令。(/etc/init.d/ [start | stop | reload | restart ])

 启动报“Starting keepalived: /bin/bash: keepalived: command not found”错误,具体日志如下:

[root@yoodb /]# service keepalived start
正在启动 keepalived:/bin/bash: keepalived: command not found
                                                           [失败]

解决方法:

cp /usr/local/src/keepalived-2.0.0/bin/keepalived /usr/sbin
重新执行如下输出后,表示启动成功
[root@m01 keepalived-2.0.0]# /etc/init.d/keepalived start
Starting keepalived: 
[root@m01 keepalived-2.0.0]# /etc/init.d/keepalived stop
Stopping keepalived:                                       [  OK  ]
[root@m01 keepalived-2.0.0]# /etc/init.d/keepalived start
Starting keepalived:                                       [  OK  ]
[root@m01 keepalived-2.0.0]# /etc/init.d/keepalived restart
Stopping keepalived:                                       [  OK  ]
Starting keepalived:                                       [  OK  ]
[root@m01 keepalived-2.0.0]# /etc/init.d/keepalived reload
Reloading keepalived: 
[ OK ] 加入开机启动 [root@m01 keepalived-2.0.0]# chkconfig keepalived on [root@m01 keepalived-2.0.0]# chkconfig --list keepalived keepalived 0:off 1:off 2:on 3:on 4:on 5:on 6:off

五、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子进程。

[root@m01 keepalived-2.0.0]# /application/keepalived/sbin/keepalived -f /application/keepalived/etc/keepalived/keepalived.conf   #检查配置参数
[root@m01 keepalived-2.0.0]# ps -ef |grep keep root 22642 1 0 16:55 ? 00:00:00 keepalived -D root 22644 22642 0 16:55 ? 00:00:00 keepalived -D root 22645 22642 0 16:55 ? 00:00:00 keepalived -D

到此keepalived就安装完成了。

六、keepalived.conf配置文件说明

       keepalived服务安装完成之后,后面的主要工作就是在keepalived.conf文件中配置HA和负载均衡。

完整的keepalived配置文件,主要包含三块:全局定义块、VRRP实例定义块和虚拟服务器定义块。全局定义块是必须的,如果keepalived只用来做ha,虚拟服务器是可选的。

#全局定义块
global_defs {
    # 邮件通知配置
    notification_email {
        email1
        email2
    }
    notification_email_from email
    smtp_server host
    smtp_connect_timeout num

    lvs_id string
    router_id string    ## 标识本节点的字条串,通常为hostname
}

#VRRP 实例定义块
vrrp_sync_group string { 
    group {
        string
        string
    }
}

vrrp_instance string {
    state MASTER|BACKUP
    virtual_router_id num
    interface string
    mcast_src_ip @IP 
    priority num
    advert_int num
    nopreempt
    smtp_alert
    lvs_sync_daemon_interface string 
    authentication {
        auth_type PASS|AH
        auth_pass string
    }

    virtual_ipaddress {  # Block limited to 20 IP addresses @IP
        @IP
        @IP
    }
}

#虚拟服务器定义块
virtual_server (@IP PORT)|(fwmark num) { 
    delay_loop num
    lb_algo rr|wrr|lc|wlc|sh|dh|lblc 
    lb_kind NAT|DR|TUN
    persistence_timeout num 
    protocol TCP|UDP
    real_server @IP PORT { 
        weight num
        notify_down /path/script.sh
        TCP_CHECK { 
            connect_port num 
            connect_timeout num
        }
    }

    real_server @IP PORT {
        weight num
        MISC_CHECK {
            misc_path /path_to_script/script.sh(or misc_path “/path_to_script/script.sh <arg_list>”)
        }
    }

    real_server @IP PORT {
        weight num
        HTTP_GET|SSL_GET {
            url { 
                # You can add multiple url block path alphanum
                digest alphanum
            }
            connect_port num
            connect_timeout num 
            nb_get_retry num 
            delay_before_retry num
        }
    }
}

  

全局定义块

1、email通知(notification_email、smtp_server、smtp_connect_timeout):用于服务有故障时发送邮件报警,可选项,不建议用。需要系统开启sendmail服务,建议用第三独立监控服务,如用Zabbix全面监控代替。 
2、lvs_id:lvs负载均衡器标识,在一个网络内,它的值应该是唯一的。 
3、router_id:用户标识本节点的名称,通常为hostname 
4、花括号{}:用来分隔定义块,必须成对出现。如果写漏了,keepalived运行时不会得到预期的结果。由于定义块存在嵌套关系,因此很容易遗漏结尾处的花括号,这点需要特别注意。

VRRP实例定义块

  1. vrrp_sync_group:同步vrrp级,用于确定失败切换(FailOver)包含的路由实例个数。即在有2个负载均衡器的场景,一旦某个负载均衡器失效,需要自动切换到另外一个负载均衡器的实例是哪
  2. group:至少要包含一个vrrp实例,vrrp实例名称必须和vrrp_instance定义的一致
  3. vrrp_instance:vrrp实例名 
    1> state:实例状态,只有MASTER 和 BACKUP两种状态,并且需要全部大写。抢占模式下,其中MASTER为工作状态,BACKUP为备用状态。当MASTER所在的服务器失效时,BACKUP所在的服务会自动把它的状态由BACKUP切换到MASTER状态。当失效的MASTER所在的服务恢复时,BACKUP从MASTER恢复到BACKUP状态。 
    2> interface:对外提供服务的网卡接口,即VIP绑定的网卡接口。如:eth0,eth1。当前主流的服务器都有2个或2个以上的接口(分别对应外网和内网),在选择网卡接口时,一定要核实清楚。 
    3> mcast_src_ip:本机IP地址 
    4> virtual_router_id:虚拟路由的ID号,每个节点设置必须一样,可选择IP最后一段使用,相同的 VRID 为一个组,他将决定多播的 MAC 地址。 
    5> priority:节点优先级,取值范围0~254,MASTER要比BACKUP高 
    6> advert_int:MASTER与BACKUP节点间同步检查的时间间隔,单位为秒 
    7> lvs_sync_daemon_inteface:负载均衡器之间的监控接口,类似于 HA HeartBeat 的心跳线。但它的机制优于 Heartbeat,因为它没有“裂脑”这个问题,它是以优先级这个机制来规避这个麻烦的。在 DR 模式中,lvs_sync_daemon_inteface与服务接口interface使用同一个网络接口 
    8> authentication:验证类型和验证密码。类型主要有 PASS、AH 两种,通常使用PASS类型,据说AH使用时有问题。验证密码为明文,同一vrrp 实例MASTER与BACKUP使用相同的密码才能正常通信。 
    9> smtp_alert:有故障时是否激活邮件通知 
    10> nopreempt:禁止抢占服务。默认情况,当MASTER服务挂掉之后,BACKUP自动升级为MASTER并接替它的任务,当MASTER服务恢复后,升级为MASTER的BACKUP服务又自动降为BACKUP,把工作权交给原MASTER。当配置了nopreempt,MASTER从挂掉到恢复,不再将服务抢占过来。 
    11> virtual_ipaddress:虚拟IP地址池,可以有多个IP,每个IP占一行,不需要指定子网掩码。注意:这个IP必须与我们的设定的vip保持一致。

虚拟服务器virtual_server定义块

  1. virtual_server:定义一个虚拟服务器,这个ip是virtual_ipaddress中定义的其中一个,后面一个空格,然后加上虚拟服务的端口号。 
    1> delay_loop:健康检查时间间隔,单位:秒 
    2> lb_algo:负载均衡调度算法,互联网应用常用方式为wlc或rr 
    3> lb_kind:负载均衡转发规则。包括DR、NAT、TUN 3种,一般使用路由(DR)转发规则。 
    4> persistence_timeout:http服务会话保持时间,单位:秒 
    5> protocol:转发协议,分为TCP和UDP两种
  2. real_server:真实服务器IP和端口,可以定义多个 
    1> weight:负载权重,值越大,转发的优先级越高 
    2> notify_down:服务停止后执行的脚本 
    3> TCP_CHECK:服务有效性检测 
    * connect_port:服务连接端口 
    * connect_timeout:服务连接超时时长,单位:秒 
    * nb_get_retry:服务连接失败重试次数 
    * delay_before_retry:重试连接间隔,单位:秒