Keepalived之安装、双机主备、双机热备、Nginx自动重启配置
一、Keepalived安装
下载
Keepalived官网: https://www.keepalived.org/download.html
[root@administrator ~]# wget https://www.keepalived.org/software/keepalived-2.2.0.tar.gz
--2021-09-17 11:20:11-- https://www.keepalived.org/software/keepalived-2.2.0.tar.gz
正在解析主机 www.keepalived.org (www.keepalived.org)... 91.121.30.175, 2001:41d0:1:71af::1
正在连接 www.keepalived.org (www.keepalived.org)|91.121.30.175|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:1149437 (1.1M) [application/octet-stream]
正在保存至: “keepalived-2.2.0.tar.gz”
keepalived-2.2.0.tar.gz 100%[=================================================================================================================================================>] 1.10M 846KB/s 用时 1.3s
2021-09-17 11:20:15 (846 KB/s) - 已保存 “keepalived-2.2.0.tar.gz” [1149437/1149437])
[root@administrator ~]# ll
安装
解压keepalived
[root@administrator ~]# tar -zxvf keepalived-2.2.0.tar.gz
进入解压出来的目录,执行配置检查命令
[root@administrator ~]# cd keepalived-2.2.0/
[root@administrator keepalived-2.2.0]# ./configure --prefix=/usr/local/keepalived --sysconf=/etc
prefix:指定keepalived安装位置
sysconf:Keepalived核心配置文件所在位置,固定位置,改成其他位置Keepalived启动不了
配置过程中可能出现警告信息:
*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.
安装libnl/libnl-3依赖,从新configure即可
[root@administrator keepalived-2.2.0]# yum -y install libnl libnl-devel
*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.
[root@administrator keepalived-2.2.0]# yum -y install libnl libnl-devel
Repository epel is listed more than once in the configuration
上次元数据过期检查:2:29:10 前,执行于 2021年09月17日 星期五 09时03分49秒。
未找到匹配的参数: libnl
未找到匹配的参数: libnl-devel
错误:没有任何匹配: libnl libnl-devel
[root@administrator keepalived-2.2.0]#
执行编译与安装命令
make && make install
查看keepalived相关位置: whereis keepalived
[root@administrator keepalived-2.2.0]# whereis keepalived
keepalived: /etc/keepalived /usr/local/keepalived
[root@administrator keepalived-2.2.0]#
二、Keepalived双机主备配置
配置主Keepalived
vim keepalived.conf,核心配置如下:
global_defs {
# 路由id:当前安装keepalived的节点主机标识符,保证全局唯一
router_id keep_1
}
vrrp_instance VI_1 {
# 表示状态是MASTER主机还是备用机BACKUP
state MASTER
# 该实例绑定的网卡,通过ip addr查看网卡名称
interface eth0
# 保证主备节点一致即可
virtual_router_id 1
# 权重,master权重一般高于backup,如果有多个,那就是选举,谁的权重高,谁就当选
priority 100
# 主备之间同步检查时间间隔,单位秒
advert_int 2
# 认证权限密码,防止非法节点进入
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟出来的ip,可以有多个(vip)
virtual_ipaddress {
192.168.1.1
}
}
配置备Keepalived
global_defs {
# 路由id:当前安装keepalived的节点主机标识符,保证全局唯一
router_id keep_2
}
vrrp_instance VI_1 {
# 表示状态是MASTER主机还是备用机BACKUP
state BACKUP
# 该实例绑定的网卡,通过ip addr查看网卡名称
interface eth0
# 保证主备节点一致即可
virtual_router_id 1
# 权重,master权重一般高于backup,如果有多个,那就是选举,谁的权重高,谁就当选
priority 80
# 主备之间同步检查时间间隔,单位秒,默认1s
advert_int 2
# 认证权限密码,防止非法节点进入
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟出来的ip,可以有多个(vip)
virtual_ipaddress {
192.168.1.1
}
}
注册成服务
进入keepalived解压目录
[root@administrator etc]# pwd
/root/keepalived-2.2.0/keepalived/etc
[root@administrator etc]# ls
init init.d keepalived Makefile Makefile.am Makefile.in openrc sysconfig
[root@administrator etc]#
拷贝Keepalived相关配置到系统配置目录
[root@administrator etc]# cp init.d/keepalived /etc/init.d/
[root@administrator etc]# cp sysconfig/keepalived /etc/sysconfig/
刷新加载新配置
[root@administrator etc]# cp sysconfig/keepalived /etc/sysconfig/
启动keepalived服务
systemctl start keepalived.service
查看相关进程
ps -ef | grep keepalived
测试
1.启动主机Keepalived
[root@administrator keepalived]# cd /usr/local/keepalived/sbin
[root@administrator sbin]./keepalived
结果:
执行ip addr可查看到网卡下多了一个配置的虚拟IP: 192.168.1.1,即主机IP与此虚拟IP绑定了
2.停止主机Keepalived
systemctl stop keepalived.service
结果:
查看从机,执行ip addr可查看到网卡下多了一个配置的虚拟IP:192.168.1.1,即从机IP与虚拟IP绑定了,此时访问则是访问从机
3.启动主机Keepalived
systemctl start keepalived.service
结果:
查看主机,执行ip addr可查看到网卡下多了一个配置的虚拟IP:192.168.1.1,即主机IP与虚拟IP绑定了,再查看从机网卡,发现虚拟IP已经与从机IP解绑了
三、Nginx自动重启配置
添加Nginx重启检测脚本
vim /etc/keepalived/check_nginx.sh
#!/bin/bash
A=`ps -C nginx --no-header |wc -l`
# 判断nginx是否宕机,如果宕机了,尝试重启
if [ $A -eq 0 ];then
/usr/local/nginx/sbin/nginx
# 等待一小会再次检查nginx,如果没有启动成功,则停止keepalived,使其启动备用机
sleep 3
if [ `ps -C nginx --no-header |wc -l` -eq 0 ];then
killall keepalived
fi
fi
添加运行权限
chmod +x /etc/keepalived/check_nginx.sh
配置keepalived监听nginx脚本
vrrp_script check_nginx_alive {
script "/etc/keepalived/check_nginx.sh"
interval 2 # 每隔两秒运行上一行脚本
weight 10 # 如果脚本运行成功,则升级权重+10
#weight -10 # 如果脚本运行失败,则降低权重-10
}
在vrrp_instance中新增监控脚本
track_script {
check_nginx_alive # 追踪 nginx 脚本
}
重启Keepalived使得配置文件生效
systemctl restart keepalived
完整配置
global_defs {
# 路由id:当前安装keepalived的节点主机标识符,保证全局唯一
router_id keep_1
}
vrrp_script check_nginx_alive {
script "/etc/keepalived/check_nginx.sh"
interval 2 # 每隔两秒运行上一行脚本
weight 10 # 如果脚本运行成功,则升级权重+10
#weight -10 # 如果脚本运行失败,则降低权重-10
}
vrrp_instance VI_1 {
# 表示状态是MASTER主机还是备用机BACKUP
state MASTER
# 该实例绑定的网卡,通过ip addr查看网卡名称
interface eth0
# 保证主备节点一致即可
virtual_router_id 1
# 权重,master权重一般高于backup,如果有多个,那就是选举,谁的权重高,谁就当选
priority 100
# 主备之间同步检查时间间隔,单位秒
advert_int 2
# 认证权限密码,防止非法节点进入
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
check_nginx_alive # 追踪 nginx 脚本
}
# 虚拟出来的ip,可以有多个(vip)
virtual_ipaddress {
192.168.1.1
}
}
四、Keepalived双机热备配置
使用Keepalived双机主备时,会严重存在资源浪费的情况,即备机资源的浪费
配置主Keepalived
vim keepalived.conf,核心配置如下:
global_defs {
# 路由id:当前安装keepalived的节点主机标识符,保证全局唯一
router_id keep_1
}
vrrp_instance VI_1 {
# 表示状态是MASTER主机还是备用机BACKUP
state MASTER
# 该实例绑定的网卡,通过ip addr查看网卡名称
interface eth0
# 保证主备节点一致即可
virtual_router_id 1
# 权重,master权重一般高于backup,如果有多个,那就是选举,谁的权重高,谁就当选
priority 100
# 主备之间同步检查时间间隔,单位秒
advert_int 2
# 认证权限密码,防止非法节点进入
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟出来的ip,可以有多个(vip)
virtual_ipaddress {
192.168.1.1
}
}
vrrp_instance VI_2 {
# 表示状态是MASTER主机还是备用机BACKUP
state BACKUP
# 该实例绑定的网卡,通过ip addr查看网卡名称
interface eth0
# 保证主备节点一致即可
virtual_router_id 2
# 权重,master权重一般高于backup,如果有多个,那就是选举,谁的权重高,谁就当选
priority 80
# 主备之间同步检查时间间隔,单位秒
advert_int 2
# 认证权限密码,防止非法节点进入
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟出来的ip,可以有多个(vip)
virtual_ipaddress {
192.168.1.2
}
}
配置备Keepalived
global_defs {
# 路由id:当前安装keepalived的节点主机标识符,保证全局唯一
router_id keep_2
}
vrrp_instance VI_1 {
# 表示状态是MASTER主机还是备用机BACKUP
state BACKUP
# 该实例绑定的网卡,通过ip addr查看网卡名称
interface eth0
# 保证主备节点一致即可
virtual_router_id 1
# 权重,master权重一般高于backup,如果有多个,那就是选举,谁的权重高,谁就当选
priority 80
# 主备之间同步检查时间间隔,单位秒,默认1s
advert_int 2
# 认证权限密码,防止非法节点进入
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟出来的ip,可以有多个(vip)
virtual_ipaddress {
192.168.1.1
}
}
vrrp_instance VI_2 {
# 表示状态是MASTER主机还是备用机BACKUP
state MASTER
# 该实例绑定的网卡,通过ip addr查看网卡名称
interface eth0
# 保证主备节点一致即可
virtual_router_id 2
# 权重,master权重一般高于backup,如果有多个,那就是选举,谁的权重高,谁就当选
priority 100
# 主备之间同步检查时间间隔,单位秒
advert_int 2
# 认证权限密码,防止非法节点进入
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟出来的ip,可以有多个(vip)
virtual_ipaddress {
192.168.1.2
}
}
测试
分别启动主从Keepalived后
查看主Keepalived,执行ip addr查看网卡信息发现主机ip与虚拟ip:192.168.1.1绑定在一起了
查看从Keepalived,执行ip addr查看网卡信息发现从机ip与虚拟ip:192.168.1.2绑定在一起了
停止主Keepalived后
查看从Keepalived,执行ip addr查看网卡信息发现从机ip与虚拟ip:192.168.1.1和192.168.1.2绑定在一起了
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/137033.html