Debian安装keepalived

it2026-04-12  4

环境

操作系统:Debian权限: rootkeepalived版本:keepalived-2.0.20

下载安装包

wget https://www.keepalived.org/software/keepalived-2.0.20.tar.gz

或者在https://www.keepalived.org/download.html 寻找其他的版本

#解压缩 tar -zxvf keepalived-2.0.20.tar.gz #移动 mv keepalived-2.0.20 /usr/local/keepalived # 进入目录 cd /usr/local/keepalived

编译安装

./configure

报错 configure: error: no acceptable C compiler found in $PATH

没有安装C编译器的缘故 #安装C编译器 apt-get update && apt-get install build-essential

提示Media change: please insert the disc labeled 按Ctrl+C 结束,然后输入命令

nano /etc/apt/sources.list 注释掉 deb cdrom:[Debian GNU/Linux 9.5.0 _Stretch_ - Official amd64 xfce-CD Binary-1 20180714-10:25]/ stretch main这行

保存退出并再次安装C编译器。

apt-get update && apt-get install build-essential #然后再次执行 ./configure

又有提示 !!! OpenSSL is not properly installed on your system. !!! !!! Can not include OpenSSL headers files. !!!

没有安装openssl #安装openssl apt-get install openssl #执行完成之后再执行 apt-get install libssl-dev #再次执行 ./configure

出现以下输出就表示执行成功

Linker flags : -pie -Wl,-z,relro -Wl,-z,now Extra Lib : -lm -lcrypto -lssl Use IPVS Framework : Yes IPVS use libnl : No IPVS syncd attributes : Yes IPVS 64 bit stats : Yes HTTP_GET regex support : No fwmark socket support : Yes Use VRRP Framework : Yes Use VRRP VMAC : Yes Use VRRP authentication : Yes With ip rules/routes : Yes With track_process : Yes With linkbeat : Yes Use BFD Framework : No SNMP vrrp support : No SNMP checker support : No SNMP RFCv2 support : No SNMP RFCv3 support : No DBUS support : No SHA1 support : No Use JSON output : No libnl version : None Use IPv4 devconf : Yes Use iptables : Yes Use libiptc : No Use libipset : No Use nftables : No init type : systemd Strict config checks : No Build genhash : Yes Build documentation : No 安装 make && make install

至此安装已完成

配置

接下来创建配置文件,keepalived默认在/etc/keepalived/下读取配置文件,所以我们要创建配置文件

mkdir /etc/keepalived touch /etc/keepalived/keepalived.conf nano /etc/keepalived/keepalived.conf keepalived MASTER的配置文件如下 将下列配置复制进去 ! Configuration File for keepalived global_defs { notification_email { #admin@demo.com # 此处填写你的邮箱 } #notification_email_from admin@demo.com #smtp_server 127.0.0.1 #smtp_connect_timeout 30 router_id LVS_DEVEL # 这里有些说两台机器要一致,有些说不能一样,我是保持一样,也成功了,后面可以详细了解,有了解的也可以说一下 } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" # 此处是keepalived检查服务时要执行的脚本 interval 2 #这里是间隔执行脚本的时间,间隔两秒执行一次 weight -20 # 权重,健康检查脚本执行后如果返回非0则会在priority上减去20 } vrrp_instance VI_1 { state MASTER # 设置主备MASTER为主,BACKUP为备 interface eth0 #VIP要挂的网卡 virtual_router_id 51 #虚拟路由标识,主备服务器上这里必须保持一致 priority 100 #定义优先级,数字越大优先级越高,主服务器上的值应该为最大,当改值小于备服务器时,备服务器将抢占VIP advert_int 1 #主备服务器之间检查的时间间隔,1秒;主备必须一致 authentication { #设置验证类型和密码,主备必须一致 auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.16.16.240 #设置虚拟IP地址,可以设置多个IP地址每行一个 } track_script { chk_nginx #检查脚本,就是我们上边定义的vrrp_script } #notify_master "/etc/keepalived/master.sh" #当前节点成为master时,执行脚本 #notify_backup "" #当前节点成为backup时,执行脚本 #notify_fault "" #当前节点出现故障时,执行脚本 }

到这里keepalived的基本配置就完成了,接下来编写检查脚本check_nginx.sh

touch /etc/keepalived/check_nginx.sh nano /etc/keepalived/check_nginx.sh

检查脚本如下:

#!/bin/bash # 该脚本为检查NGINX服务的脚本,也可以根据自己的需求修改 A=`ps -C nginx --no-header | wc -l` if [ $A -eq 0 ];then /usr/local/nginx/sbin/nginx sleep 2 if [ `ps -C nginx --no-header | wc -l` -eq 0 ];then killall keepalived fi fi keepalived BACKUP 配置文件如下: ! Configuration File for keepalived global_defs { notification_email { #admin@demo.com # 此处填写你的邮箱 } #notification_email_from admin@demo.com #smtp_server 127.0.0.1 #smtp_connect_timeout 30 router_id LVS_DEVEL # 这里有些说两台机器要一致,有些说不能一样,我是保持一样,也成功了,后面可以详细了解,有了解的也可以 $ } vrrp_script chk_nginx { script "/etc/keepalived/check_nginx.sh" # 此处是keepalived检查服务时要执行的脚本 interval 2 #这里是间隔执行脚本的时间,间隔两秒执行一次 weight -20 # 权重,健康检查脚本执行后如果返回非0则会在priority上减去20 } vrrp_instance VI_1 { state BACKUP # 设置主备MASTER为主,BACKUP为备 interface eth0 #VIP要挂的网卡 virtual_router_id 51 #虚拟路由标识,主备服务器上这里必须保持一致 priority 90 #定义优先级,数字越大优先级越高,主服务器上的值应该为最大,当改值小于备服务器时,备服$ advert_int 1 #主备服务器之间检查的时间间隔,1秒;主备必须一致 authentication { #设置验证类型和密码,主备必须一致 auth_type PASS auth_pass 1111 } virtual_ipaddress { 172.16.16.240 #设置虚拟IP地址,可以设置多个IP地址每行一个 } track_script { chk_nginx #检查脚本,就是我们上边定义的vrrp_script } #notify_master "/etc/keepalived/master.sh" #当前节点成为master时,执行脚本 #notify_backup "" #当前节点成为backup时,执行脚本 #notify_fault "" #当前节点出现故障时,执行脚本 }

此时启动keepalived 由于我们是用源码安装的keepalived,所以用service keepalived start命令是不行的 使用whereis keepalived命令找到*/sbin/keepalived文件,并cd进去使用./keepalived方法执行

最新回复(0)