千家信息网

在Linux二进制安装keepalived

发表于:2026-11-03 作者:千家信息网编辑
千家信息网最后更新 2026年11月03日,keepalived的部署keepalived在很多高可用的集群都会用到,一般前端放置的会是nginx、ipvs、haproxy比如我们在使用rabbitmq的时候做了这么一个集群,一般做了集群的话,
千家信息网最后更新 2026年11月03日在Linux二进制安装keepalived

keepalived的部署
keepalived在很多高可用的集群都会用到,一般前端放置的会是nginx、ipvs、haproxy

比如我们在使用rabbitmq的时候做了这么一个集群,一般做了集群的话,那肯定就需要这么一个高可用的负载均衡器来实现流量的分发,如果使用的是haproxy,比如一台rebbitmq的节点突然宕机或者网卡失效,那么虽然RabbitMQ集群没有任何故障,但是对于外界的客户端来说所有的连接都会被断开,结果将是灾难性的。确保负载均衡服务的可靠性同样显得十分的重要。这里就引入Keepalived工具,它能够通过自身健康检查、资源接管功能做高可用(双机热备),实现故障转移。

Keepalived采用VRRP(Virtual Router Redundancy Protocol,虚拟路由冗余协议),以软件的形式实现服务器热备功能。通常情况下是将两台Linux服务器组成一个热备组(Master和Backup),同一时间热备组内只有一台主服务器Master提供服务,同时Master会虚拟出一个公用的虚拟IP地址,简称VIP。这个VIP只存在在Master上并对外提供服务。如果Keepalived检测到Master宕机或者服务故障,备份服务器Backup会自动接管VIP称为Master,Keepalived并将原Master从热备组中移除。当原Master恢复后,会自动加入到热备组,默认再抢占称为Master,起到故障转移的功能。

Keepalived工作在OSI模型中的第3层、第4层和第7层。
工作在第3层是指Keepalived会定期向热备组中的服务器发送一个ICMP数据包来判断某台服务器是否故障,如果故障则将这台服务器从热备组移除。
工作在第4层是指Keepalived以TCP端口的状态判断服务器是否故障,比如检测RabbitMQ的5672端口,如果故障则将这台服务器从热备组中移除。
工作在第7层是指Keepalived根据用户设定的策略(通常是一个自定义的检测脚本)判断服务器上的程序是否正常运行,如果故障将这台服务器从热备组移除。

首先需要去Keepalived的官网下载Keepalived的安装文件,目前最新的版本为:keepalived-2.0.20.tar.gz,下载地址为http://www.keepalived.org/download.html

可以找到最新版

[root@VM_0_8_centos ~]# mkdir keepalived[root@VM_0_8_centos ~]# cd keepalived/[root@VM_0_8_centos keepalived]# wget https://www.keepalived.org/software/keepalived-2.0.20.tar.gz--2020-02-27 10:46:37--  https://www.keepalived.org/software/keepalived-2.0.20.tar.gzResolving www.keepalived.org (www.keepalived.org)... 37.59.63.157, 2001:41d0:8:7a9d::1Connecting to www.keepalived.org (www.keepalived.org)|37.59.63.157|:443... connected.HTTP request sent, awaiting response... 200 OKLength: 1036063 (1012K) [application/x-gzip]Saving to: 'keepalived-2.0.20.tar.gz'100%[==================================================================================================================================>] 1,036,063   13.7KB/s   in 1m 43s 2020-02-27 10:48:37 (9.80 KB/s) - 'keepalived-2.0.20.tar.gz' saved [1036063/1036063]

解压

[root@VM_0_8_centos keepalived]# lskeepalived-2.0.20.tar.gz[root@VM_0_8_centos keepalived]# tar xf keepalived-2.0.20.tar.gz [root@VM_0_8_centos keepalived]# cd keepalived-2.0.20/[root@VM_0_8_centos keepalived-2.0.20]# lsaclocal.m4  AUTHOR       build_setup  compile    configure.ac  COPYING  doc      INSTALL     keepalived          lib          Makefile.in  README.md  TODOar-lib      bin_install  ChangeLog    configure  CONTRIBUTORS  depcomp  genhash  install-sh  keepalived.spec.in  Makefile.am  missing      snap[root@VM_0_8_centos keepalived-2.0.20]# ./configure --prefix=/opt/keepalived --with-init=SYSV checking for a BSD-compatible install... /usr/bin/install -cchecking whether build environment is sane... yeschecking for a thread-safe mkdir -p... /usr/bin/mkdir -pchecking for gawk... gawkchecking whether make sets $(MAKE)... yeschecking whether make supports nested variables... yeschecking whether make supports nested variables... (cached) yeschecking for pkg-config... /usr/bin/pkg-configchecking pkg-config is at least version 0.9.0... yeschecking for gcc... gccchecking whether the C compiler works... yeschecking for C compiler default output file name... a.out

出现报错:缺少openssl-devel

configure: error:   !!! OpenSSL is not properly installed on your system. !!!  !!! Can not include OpenSSL headers files.            !!![root@VM_0_8_centos keepalived-2.0.20]# yum -y install openssl-develLoaded plugins: fastestmirror, langpacksDetermining fastest mirrorsResolving Dependencies--> Running transaction check---> Package openssl-devel.x86_64 1:1.0.2k-19.el7 will be installed

安装后重新再执行
[root@VM_0_8_centos keepalived-2.0.20]# ./configure --prefix=/opt/keepalived --with-init=SYSV

再次报错,说这个版本不支持ipv6,需要安装依赖包
*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.

[root@VM_0_8_centos keepalived-2.0.20]# yum -y install libnl libnl-develLoaded plugins: fastestmirror, langpacksLoading mirror speeds from cached hostfilePackage libnl-1.1.4-3.el7.x86_64 already installed and latest version

安装完依赖包再次执行

[root@VM_0_8_centos keepalived-2.0.20]# ./configure --prefix=/opt/keepalived --with-init=SYSV 

编译

[root@VM_0_8_centos keepalived-2.0.20]# make && make install

将启动文件放到/etc/init.d下,可以使用service来启动keepalived

[root@VM_0_8_centos keepalived-2.0.20]# cp /opt/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/[root@VM_0_8_centos keepalived-2.0.20]# cp /opt/keepalived/etc/sysconfig/keepalived /etc/sysconfig[root@VM_0_8_centos keepalived-2.0.20]# cp /opt/keepalived/sbin/keepalived /usr/sbin[root@VM_0_8_centos keepalived-2.0.20]# chmod +x /etc/init.d/keepalived [root@VM_0_8_centos keepalived-2.0.20]# chkconfig --add keepalived[root@VM_0_8_centos keepalived-2.0.20]# chkconfig keepalived on[root@VM_0_8_centos keepalived-2.0.20]# cp /opt/keepalived/etc/keepalived/keepalived.conf /etc/keepalived.conf

启动失败,查看报错,这里提示 Unable to find configuration file /etc/keepalived/keepalived.conf,没有发现我们的配置文件的地址,这里是因为我们在安装的时候指定了自己的安装路径

[root@VM_0_8_centos keepalived-2.0.20]# service keepalived startStarting keepalived (via systemctl):  Job for keepalived.service failed because the control process exited with error code. See "systemctl status keepalived.service" and "journalctl -xe" for details.                                                           [FAILED][root@VM_0_8_centos keepalived-2.0.20]# journalctl -xe

找出放置我们配置文件的地方,修改文件地址

[root@VM_0_8_centos ~]# find / -name keepalived.conf/opt/keepalived/etc/keepalived/keepalived.conf/etc/keepalived.conf/root/keepalived/keepalived-2.0.20/keepalived/etc/keepalived/keepalived.conf

在最后一行添加我们配置文件的地址

[root@VM_0_8_centos ~]# vim /etc/sysconfig/keepalived [root@VM_0_8_centos ~]# tail -1 /etc/sysconfig/keepalived KEEPALIVED_OPTIONS="-f /etc/keepalived.conf -D -S 0"

使用systemctl启动成功

[root@VM_0_8_centos ~]# systemctl restart keepalived[root@VM_0_8_centos ~]# systemctl status keepalived● keepalived.service - SYSV: Start and stop Keepalived   Loaded: loaded (/etc/rc.d/init.d/keepalived; bad; vendor preset: disabled)   Active: active (running) since Thu 2020-02-27 11:02:22 CST; 10s ago     Docs: man:systemd-sysv-generator(8)  Process: 30129 ExecStart=/etc/rc.d/init.d/keepalived start (code=exited, status=0/SUCCESS) Main PID: 30136 (keepalived)   CGroup: /system.slice/keepalived.service           ├─30136 keepalived -f /etc/keepalived.conf -D -S 0           ├─30138 keepalived -f /etc/keepalived.conf -D -S 0           └─30139 keepalived -f /etc/keepalived.conf -D -S 0Feb 27 11:02:31 VM_0_8_centos Keepalived_vrrp[30139]: Sending gratuitous ARP on eth0 for 192.168.200.18Feb 27 11:02:31 VM_0_8_centos Keepalived_vrrp[30139]: Sending gratuitous ARP on eth0 for 192.168.200.16Feb 27 11:02:31 VM_0_8_centos Keepalived_vrrp[30139]: Sending gratuitous ARP on eth0 for 192.168.200.17Feb 27 11:02:31 VM_0_8_centos Keepalived_vrrp[30139]: Sending gratuitous ARP on eth0 for 192.168.200.18Feb 27 11:02:31 VM_0_8_centos Keepalived_healthcheckers[30138]: HTTP_CHECK on service [192.168.200.3]:tcp:1358 failed after 3 retry.Feb 27 11:02:31 VM_0_8_centos Keepalived_healthcheckers[30138]: Removing service [192.168.200.3]:tcp:1358 to VS [10.10.10.2]:tcp:1358Feb 27 11:02:31 VM_0_8_centos Keepalived_healthcheckers[30138]: Lost quorum 1-0=1 > 0 for VS [10.10.10.2]:tcp:1358Feb 27 11:02:31 VM_0_8_centos Keepalived_healthcheckers[30138]: Adding sorry server [192.168.200.200]:tcp:1358 to VS [10.10.10.2]:tcp:1358Feb 27 11:02:31 VM_0_8_centos Keepalived_healthcheckers[30138]: Removing alive servers from the pool for VS [10.10.10.2]:tcp:1358Feb 27 11:02:31 VM_0_8_centos Keepalived_healthcheckers[30138]: Remote SMTP server [192.168.200.1]:25 connected.

使用service进行启动停止

[root@VM_0_8_centos ~]# service keepalived stopStopping keepalived (via systemctl):                       [  OK  ][root@VM_0_8_centos ~]# service keepalived startStarting keepalived (via systemctl):                       [  OK  ][root@VM_0_8_centos ~]# service keepalived status● keepalived.service - SYSV: Start and stop Keepalived   Loaded: loaded (/etc/rc.d/init.d/keepalived; bad; vendor preset: disabled)   Active: active (running) since Thu 2020-02-27 11:03:27 CST; 10s ago     Docs: man:systemd-sysv-generator(8)  Process: 30394 ExecStop=/etc/rc.d/init.d/keepalived stop (code=exited, status=0/SUCCESS)  Process: 30469 ExecStart=/etc/rc.d/init.d/keepalived start (code=exited, status=0/SUCCESS) Main PID: 30476 (keepalived)   CGroup: /system.slice/keepalived.service           ├─30476 keepalived -f /etc/keepalived.conf -D -S 0           ├─30478 keepalived -f /etc/keepalived.conf -D -S 0           └─30479 keepalived -f /etc/keepalived.conf -D -S 0Feb 27 11:03:36 VM_0_8_centos Keepalived_vrrp[30479]: Sending gratuitous ARP on eth0 for 192.168.200.18Feb 27 11:03:36 VM_0_8_centos Keepalived_vrrp[30479]: Sending gratuitous ARP on eth0 for 192.168.200.16Feb 27 11:03:36 VM_0_8_centos Keepalived_vrrp[30479]: Sending gratuitous ARP on eth0 for 192.168.200.17Feb 27 11:03:36 VM_0_8_centos Keepalived_vrrp[30479]: Sending gratuitous ARP on eth0 for 192.168.200.18Feb 27 11:03:37 VM_0_8_centos Keepalived_healthcheckers[30478]: HTTP_CHECK on service [192.168.200.3]:tcp:1358 failed after 3 retry.Feb 27 11:03:37 VM_0_8_centos Keepalived_healthcheckers[30478]: Removing service [192.168.200.3]:tcp:1358 to VS [10.10.10.2]:tcp:1358Feb 27 11:03:37 VM_0_8_centos Keepalived_healthcheckers[30478]: Lost quorum 1-0=1 > 0 for VS [10.10.10.2]:tcp:1358Feb 27 11:03:37 VM_0_8_centos Keepalived_healthcheckers[30478]: Adding sorry server [192.168.200.200]:tcp:1358 to VS [10.10.10.2]:tcp:1358Feb 27 11:03:37 VM_0_8_centos Keepalived_healthcheckers[30478]: Removing alive servers from the pool for VS [10.10.10.2]:tcp:1358Feb 27 11:03:37 VM_0_8_centos Keepalived_healthcheckers[30478]: Remote SMTP server [192.168.200.1]:25 connected.
服务 服务器 故障 文件 地址 集群 工作 功能 检测 配置 均衡 再次 时候 版本 端口 重要 一行 冗余 前端 只有 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网科技活动banner 一五八零互联网科技 丰台科技园 互联网公司 温州喔噻互联网科技有限公司 恒银科技与互联网金融 昭通云南众福互联网科技有限公司 南平创想时代互联网科技有限公司 崇明区互联网教育科技诚信经营 互联网5g黑科技有哪些 黑科技 体验场景 互联网大会 福州德业互联网科技 互联网科技手机排行榜 湖北辰鑫科技互联网 互联网金融运营模式中的金融科技 互联网科技收购价值 米泉互联网科技专业怎么样 互联网科技博主张洲 互联网科技创新创业项目 互联网高科技绘画 互联网科技自媒体灵感 互联网科技资迅 互联网科技社 科技与互联网发展 常德市电业局互联网科技部 互联网金融科技名词解释 科技互联网海报 新氧科技和美国互联网有关系吗 艺电能源互联网科技有限公司招聘 伍佰万互联网科技是干嘛的 恒生科技互联网指数十大成分股
0