编译环境:centos7.9
依赖安装:
yum install curl gcc openssl-devel libnl3-devel net-snmp-devel |
---|
离线安装:
yum localinstall *.rpm -y |
---|
下载解压:
也可以使用:curl -o keepalived-2.2.7.tar.gz https://keepalived.org/software/keepalived-2.2.7.tar.gz 下载到本地
curl —progress https://keepalived.org/software/keepalived-2.2.7.tar.gz | tar xz cd keepalived-2.2.7 ./configure —prefix=/usr/local/keepalived-2.2.7 —with-init=systemd make make install |
---|
配置:
cd /usr/local/keepalived-2.2.7/etc/keepalived/ cp keepalived.conf.sample keepalived.conf cd /usr/local/keepalived-2.2.7/etc/sysconfig/ sed -i ‘s@KEEPALIVED_OPTIONS=.*@KEEPALIVED_OPTIONS=”-f /usr/local/keepalived-2.2.7/etc/keepalived/keepalived.conf -D -S 0”@’ keepalived |
---|
查看日志:tail -f /var/log/messages
修改配置文件:keepalived.conf
查看:/etc/sysconfig/network-scripts下的本地网卡名称或使用ifconfig
修改:将etho改为上一步查询到的网卡名称
state:MASTER(主节点)、BACKUP(备份节点)
priority: 优先级数字越大节点将依次进行选举
设置软连接:ln -s /etc/rc.d/init.d/keepalived.init /etc/rc.d/rc3.d/S99keepalived
设置开机启动:systemctl enable keepalived
启动服务:systemctl start keepalived
卸载:make uninstall
清除编译:make clean
清除编译及configure:make distclean
建立备忘录:
cd /usr/local/keepalived-2.2.7 cat >>README < 2、修改日志文件存放位置/var/log/keepalived/keepalived.log echo ‘local0.* /var/log/keepalived/keepalived.log’ >>/etc/rsyslog.conf eof |
---|
rpm-build
安装构建工具
yum install rpm-build
安装构建依赖
yum install autoconf automake kmod-devel pcre2-devel glib2-devel
配置:
./configure --with-init=systemd --enable-snmp-checker --enable-snmp --enable-snmp-vrrp --enable-bfd --enable-snmp-rfc --enable-snmp-rfcv2 --enable-snmp-rfcv3 --enable-dbus --enable-sha1 --enable-regex --enable-regex-timers --enable-dependency-tracking --enable-json --enable-stacktrace --enable-dump-threads --enable-select-debug --enable-regex-debug --enable-tsm-debug --enable-strict-config-checks
修改:keepalived.spec文件,从2.2.4起,打包文件中keepalived.conf变更为keepalived.conf.sample
不知道什么原因,我的报libkmod缺少,我已经安装了kmod-devel,无法打包,索性注释掉
注意需要先:make rpm之后再改keepalived.spec文件
打包完成。
卸载Keepalived:
rpm -qa|grep keepalived
rpm -e keepalived-2.2.7-1.el7.x86_64
查找遗留文件:
find / -iname keepalived
删除遗留文件:
rm -rf /etc/keepalived
rm -rf 其他文件
错误处理:Can’t open PID file /var/run/keepalived.pid
可能有进程引用文件,关闭关联进程:
systemctl stop keepalived
pkill keepalived
查询是否漂移:
ip addr|grep 192.168.2.16
最基本配置:
! Configuration File for keepalived
global_defs {
notification_email {
acassen@firewall.loc
failover@firewall.loc
sysadmin@firewall.loc
}
notification_email_from Alexandre.Cassen@firewall.loc
smtp_server 192.168.200.1
smtp_connect_timeout 30
router_id LVS_DEVEL_108
vrrp_skip_check_adv_addr
vrrp_strict
vrrp_garp_interval 0
vrrp_gna_interval 0
}
vrrp_instance VI_1 {
state BACKUP
interface ens192
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.2.16
}
}
主备模式:state都配置为BACKUP,方式主节点恢复后ip漂移。
global_defs->router_id:网络中要保持唯一性,一般选用主机名。
vrrp_instance->virtual_router_id:主备节点要保持一致。
检测脚本:
#!/bin/bash
pidof nginx
if [ $? -ne 0 ];then
systemctl start nginx
sleep 2
pidof nginx
if [ $? -ne 0 ]; then
systemctl stop keepalived
fi
fi
防火墙配置:
firewall-cmd --direct --permanent --add-rule ipv4 filter INPUT 0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --direct --permanent --add-rule ipv4 filter OUTPUT 0 --destination 224.0.0.18 --protocol vrrp -j ACCEPT
firewall-cmd --reload
查看配置的规则
firewall-cmd —direct —get-rules ipv4 filter INPUT
firewall-cmd —direct —get-rules ipv4 filter OUTPUT