使用keeepalived进行检测后端服务状态并控制LVS的分发

发布时间:2020-09-10编辑:脚本学堂
使用keeepalived进行检测后端服务状态并控制LVS的分发

首先,下载:
http://www.keepalived.org/software/keepalived-1.2.2.tar.gz
 
然后,编译安装
keeepalived时需要支持LVS时才可以做LVS调度,执行./configure 出现下面情况,将不支持lvs!:
Keepalived configuration
------------------------
Keepalived version       : 1.2.2
Compiler                 : gcc
Compiler flags           : -g -O2 -DETHERTYPE_IPV6=0x86dd
Extra Lib                : -lpopt -lssl -lcrypto
Use IPVS Framework       : No
IPVS sync daemon support : No
Use VRRP Framework       : Yes
Use Debug flags          : No
 
原因:是keepalived需要kernelsource的支持。
解决办法:指定kernel目录(没有安装kernel-devel的同学,请自行安装匹配版本的kernel-devel包):
./configure --with-kernel-dir=/usr/src/kernels/2.6.18-164.el5PAE-i686/ (rhel5.4_32平台)
 
正确情况:
Keepalived configuration
------------------------
Keepalived version       : 1.2.2
Compiler                 : gcc
Compiler flags           : -g -O2 -DETHERTYPE_IPV6=0x86dd
Extra Lib                : -lpopt -lssl -lcrypto
Use IPVS Framework       : Yes
IPVS sync daemon support : Yes
IPVS use libnl           : No
Use VRRP Framework       : Yes
Use Debug flags          : No
然后在make,make install
 
编译过程出现的各种问题和解决方法:
1.
checking openssl/ssl.h usability... no
checking openssl/ssl.h presence... no
checking for openssl/ssl.h... no
configure: error:
  !!! OpenSSL is not properly installed on your system. !!!
  !!! Can not include OpenSSL headers files.            !!!
解决方式:安装openssl-devel
 
2.
In file included from /usr/include/stdlib.h:438,
                 from ../../lib/memory.h:30,
                 from vrrp_arp.c:28:
/usr/include/sys/types.h:62: 错误:与 ‘dev_t’ 类型冲突
/usr/include/linux/types.h:13: 错误:‘dev_t’ 的上一个声明在此
/usr/include/sys/types.h:67: 错误:与 ‘gid_t’ 类型冲突
/usr/include/linux/types.h:27: 错误:‘gid_t’ 的上一个声明在此
/usr/include/sys/types.h:72: 错误:与 ‘mode_t’ 类型冲突
/usr/include/linux/types.h:15: 错误:‘mode_t’ 的上一个声明在此
/usr/include/sys/types.h:77: 错误:与 ‘nlink_t’ 类型冲突
/usr/include/linux/types.h:16: 错误:‘nlink_t’ 的上一个声明在此
/usr/include/sys/types.h:82: 错误:与 ‘uid_t’ 类型冲突
/usr/include/linux/types.h:26: 错误:‘uid_t’ 的上一个声明在此
In file included from /usr/include/sys/types.h:133,
                 from /usr/include/stdlib.h:438,
                 from ../../lib/memory.h:30,
                 from vrrp_arp.c:28:
/usr/include/time.h:105: 错误:与 ‘timer_t’ 类型冲突
/usr/include/linux/types.h:22: 错误:‘timer_t’ 的上一个声明在此
In file included from /usr/include/sys/types.h:220,
                 from /usr/include/stdlib.h:438,
                 from ../../lib/memory.h:30,
                 from vrrp_arp.c:28:
/usr/include/sys/select.h:78: 错误:与 ‘fd_set’ 类型冲突
/usr/include/linux/types.h:12: 错误:‘fd_set’ 的上一个声明在此
In file included from /usr/include/stdlib.h:438,
                 from ../../lib/memory.h:30,
                 from vrrp_arp.c:28:
/usr/include/sys/types.h:235: 错误:与 ‘blkcnt_t’ 类型冲突
/usr/include/linux/types.h:114: 错误:‘blkcnt_t’ 的上一个声明在此
解决方式:安装iptraf
 
3.
In file included from /usr/include/sys/uio.h:24,
                 from /usr/include/sys/socket.h:27,
                 from /usr/include/netinet/in.h:24,
                 from ip_vs.h:12,
                 from ip_vs_nl_policy.c:1:
/usr/include/sys/types.h:62: 错误:与 ‘dev_t’ 类型冲突
/usr/src/kernels/2.6.18-164.el5-i686//include/linux/types.h:22: 错误:‘dev_t’ 的上一个声明在此
/usr/include/sys/types.h:67: 错误:与 ‘gid_t’ 类型冲突
/usr/src/kernels/2.6.18-164.el5-i686//include/linux/types.h:54: 错误:‘gid_t’ 的上一个声明在此
/usr/include/sys/types.h:72: 错误:与 ‘mode_t’ 类型冲突
/usr/src/kernels/2.6.18-164.el5-i686//include/linux/types.h:24: 错误:‘mode_t’ 的上一个声明在此
/usr/include/sys/types.h:77: 错误:与 ‘nlink_t’ 类型冲突
/usr/src/kernels/2.6.18-164.el5-i686//include/linux/types.h:25: 错误:‘nlink_t’ 的上一个声明在此
/usr/include/sys/types.h:82: 错误:与 ‘uid_t’ 类型冲突
/usr/src/kernels/2.6.18-164.el5-i686//include/linux/types.h:53: 错误:‘uid_t’ 的上一个声明在此
In file included from /usr/include/sys/types.h:133,
                 from /usr/include/sys/uio.h:24,
                 from /usr/include/sys/socket.h:27,
                 from /usr/include/netinet/in.h:24,
                 from ip_vs.h:12,
                 from ip_vs_nl_policy.c:1:
/usr/include/time.h:105: 错误:与 ‘timer_t’ 类型冲突
/usr/src/kernels/2.6.18-164.el5-i686//include/linux/types.h:31: 错误:‘timer_t’ 的上一个声明在此
In file included from /usr/include/sys/types.h:220,
                 from /usr/include/sys/uio.h:24,
                 from /usr/include/sys/socket.h:27,
                 from /usr/include/netinet/in.h:24,
                 from ip_vs.h:12,
                 from ip_vs_nl_policy.c:1:
/usr/include/sys/select.h:78: 错误:与 ‘fd_set’ 类型冲突
/usr/src/kernels/2.6.18-164.el5-i686//include/linux/types.h:21: 错误:‘fd_set’ 的上一个声明在此
In file included from /usr/include/sys/uio.h:24,
                 from /usr/include/sys/socket.h:27,
                 from /usr/include/netinet/in.h:24,
                 from ip_vs.h:12,
                 from ip_vs_nl_policy.c:1:
/usr/include/sys/types.h:235: 错误:与 ‘blkcnt_t’ 类型冲突
/usr/src/kernels/2.6.18-164.el5-i686//include/linux/types.h:142: 错误:‘blkcnt_t’ 的上一个声明在此
解决方法:修改./keepalived/libipvs-2.6/ip_vs.h文件,将#include linux/types.h行移到#include sys/types.h行之后
 
服务脚本和配置文件的位置:
1.如果configure时不指定prefix,按默认路径安装,则:
 启动命令位置:/usr/sbin/keepalived
 服务脚本位置:/etc/init.d/keepalived
 配置文件位置:/etc/keepalived/keepalived.conf
 附加选项配置文件:/etc/sysconfig/keepalived
  可执行此命令启动:/usr/sbin/keepalived -D -f /etc/keepalived/keepalived.conf
 开机自启动可如下操作:
 [root@localhost ~]# chkconfig --add keepalived
 [root@localhost ~]# chkconfig --level 2345 keepalived on
 
2.如果configure时指定了prefix,如: --prefix=/opt/keepalived/,则:
 启动命令位置:/opt/keepalived/sbin/keepalived
 服务脚本位置:/opt/keepalived/etc/rc.d/init.d/keepalived
 配置文件位置:/opt/keepalived/etc/keepalived/keepalived.conf
 附加选项配置文件:/opt/keepalived/etc/sysconfig/keepalived
 可执行此命令启动:/opt/keepalived/sbin/keepalived -D -f /opt/keepalived/etc/keepalived/keepalived.conf
 开机自启动可如下操作:
 [root@localhost ~]# cp /opt/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/keepalived (注意该文件中关于配置文件和附加选项配置文件的位置定义)
 [root@localhost ~]# chkconfig --add keepalived
 [root@localhost ~]# chkconfig --level 2345 keepalived on
 
 
配置文件样例:(分发模式:DR,分配策略:lc,保持时间:180s)
 

复制代码 代码如下:
! Configuration File for keepalived
global_defs {
#   notification_email {
#     acassen@firewall.loc
#     failover@firewall.loc
#     sysadmin@firewall.loc
#   }
#   notification_email_from Alexandre.Cassen@firewall.loc
#   smtp_server 192.168.200.1
#   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
vrrp_instance VI_1 {
    state MASTER
    interface eth0
    virtual_router_id 51
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        192.168.1.48
    }
}
virtual_server 192.168.1.48 8080 {
    delay_loop 6
    lb_algo lc
    lb_kind DR
    persistence_timeout 180
    protocol TCP
    real_server 192.168.1.45 8080 {
        weight 1
        TCP_CHECK {
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
                connect_port 8080
        }
    }
    real_server 192.168.1.46 8080 {
        weight 1
        TCP_CHECK {
        connect_timeout 3
        nb_get_retry 3
        delay_before_retry 3
        connect_port 8080
        }
    }
}

日志:
/var/log/messages