Linux下Keepalived安装与配置
一、简介
负载平衡是一种在真实服务器集群中分配IP流量的方法,可提供一个或多个高度可用的虚拟服务。在设计负载均衡拓扑时,重要的是要考虑负载均衡器本身的可用性以及它背后的真实服务器。用C编写的类似于layer3, 4 & 7交换机制软件,具备我们平时说的第3层、第4层和第7层交换机的功能。
Keepalived为负载平衡和高可用性提供了框架。负载平衡框架依赖于众所周知且广泛使用的Linux虚拟服务器(IPVS)内核模块,该模块提供第4层负载平衡。Keepalived实现了一组健康检查器,以根据其健康动态地自适应地维护和管理负载平衡的服务器池。虚拟冗余路由协议(VRRP)实现高可用性。VRRP是路由器故障转移的基础砖块。此外,keepalived实现了一组挂钩到VRRP有限状态机,提供低级别和高速协议交互。每个Keepalived框架可以独立使用或一起使用,以提供弹性基础设施。
在这种情况下,负载平衡器也可以被称为导向器或LVS路由器。
简而言之,Keepalived提供了两个主要功能:
- 健康检查LVS系统
- 实施VRRPv2堆栈以处理负载均衡器故障转移
Keepalived使用四个Linux内核组件:
- LVS框架:使用getsockopt和setsockopt调用来获取和设置套接字上的选项。
- Netfilter框架:支持NAT和伪装的IPVS代码。
- Netlink接口:设置和删除网络接口上的VRRP虚拟IP。
- 组播:将VRRP通告发送到保留的VRRP MULTICAST组(224.0.0.18)。
上图是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
2.1 方法一:yum方式
1、yum安装
安装依赖包
[root@localhost ~]# yum install -y curl gcc openssl-devel libnl3-devel net-snmp-devel
[root@localhost ~]# yum install -y keepalived
2、初始化及启动
[root@localhost ~]# systemctl start keepalived //启动keepalived
[root@localhost ~]# systemctl enable keepalived //加入开机启动keepalived
[root@localhost ~]# systemctl restart keepalived //重新启动keepalived
[root@localhost ~]# systemctl status keepalived //查看keepalived状态
2.2方法二、源码包
1、安装keepalived,需要安装依赖包openssl-devel,可是无奈服务器不允许使用网络,无法使用yum安装,所以研究了下如何离线安装,进入了很多坑,所以记录分享下
前提:基于redhat 6.5或者CentOS 6.5
#rpm -e --nodeps keyutils-libs-1.4-4.el6.x86_64
rpm -ivh keyutils-libs-1.4-5.el6.x86_64.rpm
rpm -ivh keyutils-libs-devel-1.4-5.el6.x86_64.rpm
rpm -ivh libsepol-devel-2.0.41-4.el6.x86_64.rpm
#rpm -e --nodeps libselinux-utils-2.0.94-5.3.el6_4.1.x86_64
#rpm -Uvh libselinux-2.0.94-5.8.el6.x86_64.rpm
rpm -ivh libselinux-devel-2.0.94-5.8.el6.x86_64.rpm
#rpm -e --nodeps krb5-libs-1.10.3-10.el6_4.6.x86_64
rpm -ivh krb5-libs-1.10.3-42.el6.x86_64.rpm
#rpm -e --nodeps libcom_err-1.41.12-18.el6.x86_64
rpm -ivh libcom_err-1.41.12-22.el6.x86_64.rpm
rpm -ivh libcom_err-devel-1.41.12-22.el6.x86_64.rpm
rpm -ivh krb5-devel-1.10.3-42.el6.x86_64.rpm
rpm -ivh zlib-devel-1.2.3-29.el6.x86_64.rpm
#rpm -e --nodeps openssl-1.0.1e-15.el6.x86_64
rpm -ivh openssl-1.0.1e-42.el6.x86_64.rpm
rpm -ivh openssl-devel-1.0.1e-42.el6.x86_64.rpm
OK,安装成功
2、官网下载keepalived的最新版本,解压并安装
[root@localhost src]# pwd
/usr/local/src
[root@localhost src]# wget http://www.keepalived.org/software/keepalived-2.0.13.tar.gz
[root@localhost src]# tar xvf keepalived-2.0.13.tar.gz
[root@localhost src]# cd keepalived-2.0.13
[root@localhostr keepalived-2.0.13]# ./configure --prefix=/usr/local/keepalived
a. 系统出现警告信息“*** 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.13 Compiler : gcc Preprocessor flags : Compiler flags : -Wall -Wunused -Wstrict-prototypes -Wextra -g -O2 Linker flags : 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 SNMP vrrp support : No SNMP checker support : No SNMP RFCv2 support : No SNMP RFCv3 support : No DBUS support : No SHA1 support : No Use Debug flags : No Stacktrace support : No Memory alloc check : 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@localhost keepalived-2.0.13]# yum -y install libnl libnl-devel
解决方案二:(离线安装)
离线安装libnl-devel
包,从网上下载libnl-devel-1.1.4-2.el6.x86_64.rpm
包,执行rpm -ivh libnl-devel-1.1.4-2.el6.x86_64.rpm
命令。
[root@localhost other-packages]# wget http://mirror.centos.org/centos/6/os/x86_64/Packages/libnl-devel-1.1.4-2.el6.x86_64.rpm ####执行wget命令是从另外一台可以连接外网的服务器上下载libnl-devel-1.1.4-2.el6.x86_64.rpm包的命令,可以忽略,只是为大家提供个下载libnl-devel地址#### [root@localhost other-packages]# rpm -ivh libnl-devel-1.1.4-2.el6.x86_64.rpm Preparing... ########################################### [100%] package libnl-devel-1.1.4-2.el6.x86_64 is already installed
安装完成以后,重新执行configure命令
[root@localhost keepalived-2.0.13]# ./configure --prefix=/usr/local/keepalived
b. 系统出现错误信息“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
解决方案一:(在线安装)
执行yum命令yum install -y libnfnetlink-devel解决上述错误问题
[root@localhost keepalived-2.0.13]# yum install -y libnfnetlink-devel
解决方案二:(离线安装)
Linux执行wget下载rpm包(离线安装libnfnetlink包时所需rpm包,执行wget下载libnfnetlink rpm包命令,此步可以忽略,只是为大家提供个下载libnfnetlink地址),具体命令如下:
wget ftp://mirror.switch.ch/mirror/centos/6/os/x86_64/Packages/libnfnetlink-1.0.0-1.el6.x86_64.rpm wget ftp://mirror.switch.ch/mirror/centos/6/os/x86_64/Packages/libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm
执行rpm -ivh libnfnetlink-1.0.0-1.el6.x86_64.rpm命令安装libnfnetlink-1.0.0-1.el6.x86_64.rpm包,具体如下:
[root@localhost other-packages]# rpm -ivh libnfnetlink-1.0.0-1.el6.x86_64.rpm Preparing... ########################################### [100%] 1:libnfnetlink ########################################### [100%]
执行rpm -ivh libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm命令安装libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm包,具体如下:
[root@localhost other-packages]# rpm -ivh libnfnetlink-devel-1.0.0-1.el6.x86_64.rpm Preparing... ########################################### [100%] 1:libnfnetlink-devel ########################################### [100%]
安装完成以后,重新执行configure命令
[root@localhost keepalived-2.0.13]# ./configure --prefix=/usr/local/keepalived
c. 编译keepalived不会出现上述警告和其他错误,执行make && make install命令进行安装:
[root@localhost keepalived-2.0.13]# make && make install
至此Keepalived编译并安装结束,在下一篇文章中会详解介绍Nginx搭配Keepalived的详细配置和使用方法,欢迎大家收藏本站素文宅博客。
使用命令 /usr/local/keepalived/sbin -h 参看各启动参数,启动命令:
[root@localhost keepalived-2.0.13]# ./sbin/keepalived -f /usr/local/keepalived/etc/keepalived.conf
制作快捷启动,在源文件的目录./keepalived-2.0.13/keepalived/etc/init.d下有两个个快捷启动文件和生成/usr/local/keepalived目录下一个配置文件需要复制,具体执行如下命令:
[root@localhost keepalived-2.0.13]# cp ./keepalived-2.0.13/keepalived/etc/init.d/keepalived /etc/init.d/ [root@localhost keepalived-2.0.13]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/ [root@localhost keepalived-2.0.13]# cp ./keepalived-2.0.13/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
这样就可以执行service keepalived [start | stop | reload | restart ]
命令。
启动报“Starting keepalived: /bin/bash: keepalived: command not found”错误,具体日志如下:
[root@localhost /]# service keepalived start 正在启动 keepalived:/bin/bash: keepalived: command not found [失败]
解决方案:
[root@localhost /]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/ [root@localhost /]# service keepalived start 正在启动 keepalived: [确定] [root@localhost /]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/
有上述日志信息,表示启动成功。
[root@localhost keepalived-2.0.13]# make && make install
完成后会在以下路径生成:
/usr/local/etc/keepalived/keepalived.conf
/usr/local/etc/sysconfig/keepalived
/usr/local/keepalived/sbin/keepalived
3、初始化及启动
# keepalived启动脚本变量引用文件,默认文件路径是/etc/sysconfig/,也可以不做软链接,直接修改启动脚本中文件路径即可(安装目录下)
[root@localhost /]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived
# 将keepalived主程序加入到环境变量(安装目录下)
[root@localhost /]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived
# keepalived启动脚本(源码目录下),放到/etc/init.d/目录下就可以使用service命令便捷调用
[root@localhost /]# cp /usr/local/src/keepalived-2.0.13/keepalived/etc/init.d/keepalived /etc/init.d/keepalived
# 将配置文件放到默认路径下
[root@localhost /]# mkdir /etc/keepalived
[root@localhost /]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
加为系统服务:chkconfig --add keepalived
开机启动:chkconfig keepalived on
查看开机启动的服务:chkconfig --list
启动、关闭、重启service keepalived start|stop|restart
三、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 } } }
方案二:
keepalived服务控制
systemctl enable keepalived.service #设置开机自动启动
systemctl disable keepalived.service #取消开机自动启动
systemctl start keepalived.service #启动服务
systemctl restart keepalived.service #重启服务
systemctl stop keepalived.service #停止服务
systemctl status keepalived.service #查看服务状态
设置防火墙打开
firewall-cmd --permanent–-add-service=keepalived
firewall-cmd --reload
再次强调:Keepalived的所有功能是配置keepalived.conf文件来实现的。
2.2 配置文件的详细说明
keepalived.conf的配置
如下图所示:
主机配置:
备机配置:
全局定义块
1、email通知(notification_email、smtp_server、smtp_connect_timeout):用于服务有故障时发送邮件报警,可选项,不建议用。需要系统开启sendmail服务,建议用第三独立监控服务,如用nagios全面监控代替。
2、lvs_id:lvs负载均衡器标识,在一个网络内,它的值应该是唯一的。
3、router_id:用户标识本节点的名称,通常为hostname
4、花括号{}:用来分隔定义块,必须成对出现。如果写漏了,keepalived运行时不会得到预期的结果。由于定义块存在嵌套关系,因此很容易遗漏结尾处的花括号,这点需要特别注意。
VRRP实例定义块
- vrrp_sync_group:同步vrrp级,用于确定失败切换(FailOver)包含的路由实例个数。即在有2个负载均衡器的场景,一旦某个负载均衡器失效,需要自动切换到另外一个负载均衡器的实例是哪
- group:至少要包含一个vrrp实例,vrrp实例名称必须和vrrp_instance定义的一致
- 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定义块
- 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两种 - real_server:真实服务器IP和端口,可以定义多个
1> weight:负载权重,值越大,转发的优先级越高
2> notify_down:服务停止后执行的脚本
3> TCP_CHECK:服务有效性检测
* connect_port:服务连接端口
* connect_timeout:服务连接超时时长,单位:秒
* nb_get_retry:服务连接失败重试次数
* delay_before_retry:重试连接间隔,单位:秒