编译安装keepalived

本文档详细记录了在CentOS7上编译安装keepalived的过程,包括依赖包安装、源码编译、配置选项、启动服务等步骤。在遇到初始启动失败的问题后,创建了配置文件并成功启动服务,生成了3个VIP地址,并通过ping测试验证了VIP的可用性。虽然没有生成iptables规则,但keepalived已成功运行并实现了VIP漂移。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编译安装keepalived

[root@centos7 ~]#yum -y install gcc curl openssl-devel libnl3-devel net-snmp-devel
[root@centos7 ~]#tar xvf keepalived-2.2.2.tar.gz -C /usr/local/src
[root@centos7 ~]#cd /usr/local/src/keepalived-2.2.2/
[root@centos7 keepalived-2.2.2]#./configure --prefix=/apps/keepalived
[root@centos7 keepalived-2.2.2]#make && make install
[root@centos7 keepalived-2.2.2]#ln -s /apps/keepalived/sbin/keepalived /usr/sbin/
[root@centos7 keepalived-2.2.2]#keepalived -v
Keepalived v2.2.2 (03/05,2021)

Copyright(C) 2001-2021 Alexandre Cassen, <acassen@gmail.com>

Built with kernel headers for Linux 3.10.0
Running on Linux 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020
Distro: CentOS Linux 7 (Core)

configure options: --prefix=/apps/keepalived

Config options:  LVS VRRP VRRP_AUTH VRRP_VMAC OLD_CHKSUM_COMPAT INIT=systemd

System options:  VSYSLOG LIBNL3 RTA_ENCAP RTA_EXPIRES RTA_PREF FRA_SUPPRESS_PREFIXLEN FRA_TUN_ID RTAX_CC_ALGO RTAX_QUICKACK RTA_VIA IFA_FLAGS NET_LINUX_IF_H_COLLISION LIBIPTC_LINUX_NET_IF_H_COLLISION LIBIPVS_NETLINK IFLA_LINK_NETNSID GLOB_BRACE GLOB_ALTDIRFUNC INET6_ADDR_GEN_MODE SO_MARK

#默认会自动生成unit文件
[root@centos7 keepalived-2.2.2]#cat /usr/lib/systemd/system/keepalived.service
[Unit]
Description=LVS and VRRP High Availability Monitor
After=network-online.target syslog.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=/run/keepalived.pid
KillMode=process
EnvironmentFile=-/apps/keepalived/etc/sysconfig/keepalived
ExecStart=/apps/keepalived/sbin/keepalived  $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
[root@centos7 keepalived-2.2.2]#cat /apps/keepalived/etc/sysconfig/keepalived
# 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.
# --log-facility       -S    0-7 Set local syslog facility (default=LOG_DAEMON)
#

KEEPALIVED_OPTIONS="-D"

#默认无法启动
[root@centos7 keepalived-2.2.2]#systemctl start keepalived.service
Job for keepalived.service failed because the control process exited with error code. See "systemctl status keepalived.service" and "journalctl -xe" for details.

#创建配置文件
[root@centos7 keepalived-2.2.2]#mkdir /etc/keepalived
[root@centos7 keepalived-2.2.2]#cp /apps/keepalived/etc/keepalived/keepalived.conf /etc/keepalived/

#再次启动成功
[root@centos7 keepalived-2.2.2]#systemctl enable --now keepalived.service
[root@centos7 keepalived-2.2.2]#systemctl status keepalived.service
● keepalived.service - LVS and VRRP High Availability Monitor
   Loaded: loaded (/usr/lib/systemd/system/keepalived.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2022-04-15 10:48:47 CST; 25s ago
  Process: 11669 ExecStart=/apps/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 11670 (keepalived)
   CGroup: /system.slice/keepalived.service
           ├─11670 /apps/keepalived/sbin/keepalived -D
           ├─11671 /apps/keepalived/sbin/keepalived -D
           └─11672 /apps/keepalived/sbin/keepalived -D

Apr 15 10:48:57 centos7 Keepalived_healthcheckers[11671]: HTTP_CHECK on service [192.168.200.2]:tcp:1358 failed ...es.
Apr 15 10:48:57 centos7 Keepalived_healthcheckers[11671]: Removing service [192.168.200.2]:tcp:1358 to VS [10.10...358
Apr 15 10:48:57 centos7 Keepalived_healthcheckers[11671]: Lost quorum 1-0=1 > 0 for VS [10.10.10.2]:tcp:1358
Apr 15 10:48:57 centos7 Keepalived_healthcheckers[11671]: Adding sorry server [192.168.200.200]:tcp:1358 to VS [...358
Apr 15 10:48:57 centos7 Keepalived_healthcheckers[11671]: Removing alive servers from the pool for VS [10.10.10....358
Apr 15 10:48:57 centos7 Keepalived_healthcheckers[11671]: Remote SMTP server [192.168.200.1]:25 connected.
Apr 15 10:48:57 centos7 Keepalived_healthcheckers[11671]: HTTP_CHECK on service [192.168.201.100]:tcp:443 failed...es.
Apr 15 10:48:57 centos7 Keepalived_healthcheckers[11671]: Removing service [192.168.201.100]:tcp:443 to VS [192....443
Apr 15 10:48:57 centos7 Keepalived_healthcheckers[11671]: Lost quorum 1-0=1 > 0 for VS [192.168.200.100]:tcp:443
Apr 15 10:48:57 centos7 Keepalived_healthcheckers[11671]: Remote SMTP server [192.168.200.1]:25 connected.
Hint: Some lines were ellipsized, use -l to show in full.

[root@centos7 keepalived-2.2.2]#ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 00:0c:29:33:b7:af brd ff:ff:ff:ff:ff:ff
    inet 10.0.0.7/24 brd 10.0.0.255 scope global noprefixroute eth0
       valid_lft forever preferred_lft forever
    inet 192.168.200.16/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.200.17/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet 192.168.200.18/32 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::1232:ca51:c6c3:ef6c/64 scope link noprefixroute
       valid_lft forever preferred_lft forever

#生成了3个VIP地址
[root@centos7 keepalived-2.2.2]#hostname -I
10.0.0.7 192.168.200.16 192.168.200.17 192.168.200.18

#测试VIP地址可以访问
[root@centos7 keepalived-2.2.2]#ping -c 1 192.168.200.18
PING 192.168.200.18 (192.168.200.18) 56(84) bytes of data.
64 bytes from 192.168.200.18: icmp_seq=1 ttl=64 time=0.058 ms

--- 192.168.200.18 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.058/0.058/0.058/0.000 ms
[root@centos7 keepalived-2.2.2]#ping -c 1 192.168.200.17
PING 192.168.200.17 (192.168.200.17) 56(84) bytes of data.
64 bytes from 192.168.200.17: icmp_seq=1 ttl=64 time=0.054 ms

--- 192.168.200.17 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.054/0.054/0.054/0.000 ms
[root@centos7 keepalived-2.2.2]#ping -c 1 192.168.200.16
PING 192.168.200.16 (192.168.200.16) 56(84) bytes of data.
64 bytes from 192.168.200.16: icmp_seq=1 ttl=64 time=0.073 ms

--- 192.168.200.16 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.073/0.073/0.073/0.000 ms

#没有生成iptables规则
[root@centos7 keepalived-2.2.2]#iptables -vnL
Chain INPUT (policy ACCEPT 2121 packets, 139K bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination

Chain OUTPUT (policy ACCEPT 4815 packets, 421K bytes)
 pkts bytes target     prot opt in     out     source               destination

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一直在努力学习的菜鸟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值