0、Keepalived介绍
Keepalived是一个基于VRRP协议来实现的服务高可用方案,可以利用其来避免IP单点故障,类似的工具还有heartbeat、corosync、pacemaker。但是它一般不会单独出现,而是与其它负载均衡技术(如lvs、haproxy、nginx)一起工作来达到集群的高可用。
Keepalived的作用是检测服务器的状态,如果有一台web服务器死机,或工作出现故障,Keepalived将检测到,并将有故障的服务器从系统中剔除,同时使用其他服务器代替该服务器的工作;当服务器工作正常后Keepalived自动将服务器加入到服务器群中,这些工作全部自动完成,不需要人工干涉,需要人工做的只是修复故障的服务器。
健康检查和失败切换是keepalived的两大核心功能。 所谓的健康检查, 就是采用tcp三次握手, icmp请求, http请求, udp echo请求等方式对负载均衡器后面的实际的服务器(通常是承载真实业务的服务器)进行保活;而失败切换主要是应用于配置了主备模式的负载均衡器, 利用VRRP(虚拟路由冗余协议, 可参考RFC文档http://tools.ietf.org/html/rfc5798) 维持主备负载均衡器的心跳, 当主负载均衡器出现问题时, 由备负载均衡器承载对应的业务, 从而在最大限度上减少流量损失, 并提供服务的稳定性。
1、VRRP协议
VRRP全称 Virtual Router Redundancy Protocol,即 虚拟路由冗余协议。可以认为它是实现路由器高可用的容错协议,即将N台提供相同功能的路由器组成一个路由器组(Router Group),这个组里面有一个master和多个backup,但在外界看来就像一台一样,构成虚拟路由器,拥有一个虚拟IP(vip,也就是路由器所在局域网内其他机器的默认路由),占有这个IP的master实际负责ARP相应和转发IP数据包,组中的其它路由器作为备份的角色处于待命状态。master会发组播消息,当backup在超时时间内收不到vrrp包时就认为master宕掉了,这时就需要根据VRRP的优先级来选举一个backup当master,保证路由器的高可用。
在VRRP协议实现里,虚拟路由器使用 00-00-5E-00-01-XX 作为虚拟MAC地址,XX就是唯一的 VRID (Virtual Router IDentifier),这个地址同一时间只有一个物理路由器占用。在虚拟路由器里面的物理路由器组里面通过多播IP地址 224.0.0.18 来定时发送通告消息。每个Router都有一个 1-255 之间的优先级别,级别最高的(highest priority)将成为主控(master)路由器。通过降低master的优先权可以让处于backup状态的路由器抢占(pro-empt)主路由器的状态,两个backup优先级相同的IP地址较大者为master,接管虚拟IP。
2、与heartbeat/corosync等比较
Heartbeat、Corosync、Keepalived这三个集群组件我们到底选哪个好,首先我想说明的是,Heartbeat、Corosync是属于同一类型,Keepalived与Heartbeat、Corosync,根本不是同一类型的。Keepalived使用的vrrp协议方式,虚拟路由冗余协议 (Virtual Router Redundancy Protocol,简称VRRP);Heartbeat或Corosync是基于主机或网络服务的高可用方式;简单的说就是,Keepalived的目的是模拟路由器的高可用,Heartbeat或Corosync的目的是实现Service的高可用。
所以一般Keepalived是实现前端高可用,常用的前端高可用的组合有,就是我们常见的LVS+Keepalived、Nginx+Keepalived、HAproxy+Keepalived。而Heartbeat或Corosync是实现服务的高可用,常见的组合有Heartbeat v3(Corosync)+Pacemaker+NFS+Httpd 实现Web服务器的高可用、Heartbeat v3(Corosync)+Pacemaker+NFS+MySQL 实现MySQL服务器的高可用。总结一下,Keepalived中实现轻量级的高可用,一般用于前端高可用,且不需要共享存储,一般常用于两个节点的高可用。而Heartbeat(或Corosync)一般用于服务的高可用,且需要共享存储,一般用于多节点的高可用。这个问题我们说明白了。
又有博友会问了,那heartbaet与corosync我们又应该选择哪个好啊,我想说我们一般用corosync,因为corosync的运行机制更优于heartbeat,就连从heartbeat分离出来的pacemaker都说在以后的开发当中更倾向于corosync,所以现在corosync+pacemaker是最佳组合。
3、Keepalived + nginx
keepalived可以认为是VRRP协议在Linux上的实现,主要有三个模块,分别是core、check和vrrp。core模块为keepalived的核心,负责主进程的启动、维护以及全局配置文件的加载和解析。check负责健康检查,包括常见的各种检查方式。vrrp模块是来实现VRRP协议的。
一、安装
1、首先安装依赖包
[root@bogon /]# yum install -y libnl*[root@bogon /]# yum install -y libnfnetlink-devel zlib zlib-devel gcc gcc-c++ openssl openssl-devel openssh
- 1
- 2
2、下载并解压Keepalived
[root@bogon src]# pwd/usr/local/src[root@bogon src]# wget http://www.keepalived.org/software/keepalived-1.3.5.tar.gz[root@bogon src]# tar xvf keepalived-1.3.5.tar.gz
- 1
- 2
- 3
- 4
3、编译安装
[root@bogon src]# cd keepalived-1.3.5[root@bogon keepalived-1.3.5]# ./configure --prefix=/usr/local/keepalived[root@bogon keepalived-1.3.5]# make[root@bogon keepalived-1.3.5]# make install
- 1
- 2
- 3
- 4
二、配置
keepalived安装完成后默认不会注册为系统服务,所以需要手工添加系统服务脚本。在/etc/init.d目录下新建keepalived文件,并更改权限其即可。
1、去 keepalived 源码目录下拷贝启动脚本到/etc/init.d/下
[root@bogon keepalived-1.3.5]# pwd/usr/local/src/keepalived-1.3.5[root@bogon keepalived-1.3.5]# cp /usr/local/src/keepalived-1.3.5/keepalived/etc/init.d/keepalived /etc/init.d/keepalived[root@bogon keepalived-1.3.5]# ll /etc/init.d/ | grep keepalived-rwxr-xr-x 1 root root 1308 8月 2 14:15 keepalived
- 1
- 2
- 3
- 4
- 5
2、修改/etc/init.d/keepalived启动脚本
需要修改的数据有以下几项:
- # config: /etc/keepalived/keepalived.conf 文件的位置;
- # Source configuration file (we set KEEPALIVED_OPTIONS there) 文件的位置。. /etc/sysconfig/keepalived
修改为实际安装的文件路径,我的安装路径如下:
- # config: /usr/local/keepalived/etc/keepalived/keepalived.conf
- . /usr/local/keepalived/etc/sysconfig/keepalived
另外添加如下变量,并修改文件中相应的部分:
- keepalived=/usr/local/keepalived/sbin/keepalived
- keepalived_config=/usr/local/keepalived/etc/keepalived/keepalived.conf
- keepalived_pid=/usr/local/keepalived/run/keepalived.pid
[root@bogon keepalived-1.3.5]# vim /etc/init.d/keepalived#!/bin/sh## Startup script for the Keepalived daemon## processname: keepalived# pidfile: /usr/local/keepalived/run/keepalived.pid# config: /usr/local/keepalived/etc/keepalived/keepalived.conf# chkconfig: - 21 79# description: Start and stop Keepalived# Source function library. /etc/rc.d/init.d/functions# Source configuration file (we set KEEPALIVED_OPTIONS there). /usr/local/keepalived/etc/sysconfig/keepalivedkeepalived=/usr/local/keepalived/sbin/keepalivedkeepalived_config=/usr/local/keepalived/etc/keepalived/keepalived.confkeepalived_pid=/usr/local/keepalived/run/keepalived.pidRETVAL=0prog="keepalived"start() { echo -n $"Starting $prog: " daemon $keepalived -f ${keepalived_config} ${KEEPALIVED_OPTIONS} RETVAL=$? echo [ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog}stop() { echo -n $"Stopping $prog: " killproc $keepalived RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog}reload() { echo -n $"Reloading $prog: " killproc $keepalived -1 RETVAL=$? echo}# See how we were called.case "$1" in start) start ;; stop) stop ;; reload) reload ;; restart) stop start ;; condrestart) if [ -f /var/lock/subsys/$prog ]; then stop start fi ;; status) status $keepalived RETVAL=$? ;; *) echo "Usage: $0 {start|stop|reload|restart|condrestart|status}" RETVAL=1esacexit $RETVAL
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
2、修改其权限并开机启动
- 修改权限:chmod 755 /etc/init.d/keepalived
- 加为系统服务:chkconfig –add keepalived
- 开机启动:chkconfig keepalived on
- 查看开机启动的服务:chkconfig –list
3、备注: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 #查看服务状态
附:另一种添加为系统服务的方式
1、复制相应文件到相应目录下
# keepalived启动脚本变量引用文件,默认文件路径是/etc/sysconfig/,也可以不做软链接,直接修改启动脚本中文件路径即可(安装目录下)[root@bogon /]# cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/keepalived # 将keepalived主程序加入到环境变量(安装目录下)[root@bogon /]# cp /usr/local/keepalived/sbin/keepalived /usr/sbin/keepalived# keepalived启动脚本(源码目录下),放到/etc/init.d/目录下就可以使用service命令便捷调用[root@bogon /]# cp /usr/local/src/keepalived-1.3.5/keepalived/etc/init.d/keepalived /etc/init.d/keepalived# 将配置文件放到默认路径下[root@bogon /]# mkdir /etc/keepalived[root@bogon /]# cp /usr/local/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
2、设置开机启动
# 设置开机启动[root@bogon /]# chkconfig keepalived on[root@bogon /]# service keepalived start|stop|restart
- 1
- 2
- 3
- 4
附、安装过程中的问题
1、修改启动脚本 /etc/init.d/keepalived 后,未生效
对于此问题一般的解决方法是执行如下命令,重新加载脚本。
[root@bogon sbin]# systemctl daemon-reload
- 1
以上方法大多时候会生效,但也有例外。此时,可以试试如下方法:
删除路径 /usr/lib/systemd/system/ 下相应的启动脚本
[root@bogon /]# chkconfig --del keepalived[root@bogon /]# cd /usr/lib/systemd/system/[root@bogon system]# ls | grep keepalivedkeepalived.service[root@bogon system]# rm keepalived.servicerm:是否删除普通文件 "keepalived.service"?y
- 1
- 2
- 3
- 4
- 5
- 6
然后重新注册为系统服务:
[root@bogon system]# chkconfig --add keepalived
- 1
重新注册后,修改后的配置文件就会生效。
2、a configured resource limit was exceeded.
执行 systemctl start keepalived.service 后出现如下错误:
[root@bogon sbin]# systemctl start keepalived.serviceJob for keepalived.service failed because a configured resource limit was exceeded. See "systemctl status keepalived.service" and "journalctl -xe" for details.
- 1
- 2
这个问题在网上找了半天也没找可以解决的方法,但是把 问题1 解决之后 问题2 就没再出现
3、安装时出现如下问题
缺少依赖包,安装一下就好:
[root@bogon /]# yum install -y libnfnetlink-devel
- 1
再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow