解决单点故障
组件免费
可以实现高可用HA机制
基于VRRP协议
Virtual Router Redundancy Protocol
解决内网单机故障的路由协议
构建有多个路由器MASTER BACKUP
虚拟IP - VIP(Virtual IP Address)
1.下载地址:Keepalived for Linux
2.通过ftp工具上传到linux中,/home/software
3.解压
tar -zxvf keepalived-2.2.4.tar.gz
4.解压后进入到解压出来的目录,看到会有configure,那么就可以做配置了(配置安装和nginx一模一样)
5.使用configure命令配置安装目录与核心配置文件所在位置:
./configure --prefix=/usr/local/keepalived --sysconf=/etc
prefix:keepalived安装的位置
sysconf:keepalived核心配置文件所在位置,固定位置,改成其他位置则keepalived启动不了,/var/log/messages中会报错
5.1 配置过程中可能会出现警告信息,如下所示:
*** WARNING - this build will not support IPVS with IPv6. Please install libnl/libnl-3 dev libraries to support IPv6 with IPVS.
5.2 安装libnl、libnl-3依赖
yum -y install libnl libnl-devel
5.3 重新configure一下,此时OK。
6.安装keepalived
# 两种方式都可以:
# 第一种
make && make install
# 第二种
# 编译
make
# 安装
make install
7.进入到/etc/keepalived,该目录下为keepalived核心配置文件
如果忘记安装配置的目录,则通过如下命令找到:
1.通过命令vim keepalived.conf打开配置文件
global_defs {
# 路由id:当前安装keepalived的节点主机标识符,保证全局唯一
router_id keep_171
}
vrrp_instance VI_1 {
# 表示状态是MASTER主机还是备用机BACKUP
state MASTER
# 该实例绑定的网卡
interface ens0
# 保证主备节点一致即可
virtual_router_id 51
# 权重,master权重一般高于backup,如果有多个,那就是选举,谁的权重高,谁就当选
priority 100
# 主备之间同步检查时间间隔,单位秒
advert_int 2
# 认证权限密码,防止非法节点进入
authentication {
auth_type PASS
auth_pass 1111
}
# 虚拟出来的ip,可以有多个(v_ip)
virtual_ipaddress {
192.168.1.100
}
}
查看网卡名称命令: ifconfig
2.启动Keepalived
在sbin目录中进行启动(同nginx),如下图:
3.查看进程
ps -ef | grep keepalived
4.查看ip
在网卡ens0下,多了一个192.168.1.100,这个就是虚拟v_ip。
1.通过命令 vim keepalived.conf 打开配置文件
global_defs {
router_id keep_172
}
vrrp_instance VI_1 {
# 备用机设置为BACKUP
state BACKUP
interface ens0
virtual_router_id 51
# 权重低于MASTER
priority 80
advert_int 2
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
# 注意:主备两台的v_ip都是一样的,绑定到同一个v_ip
192.168.1.100
}
}
2.启动Keepalived
# 启动keepalived
systemctl start keepalived
# 停止keepalived
systemctl stop keepalived
# 重启keepalived
systemctl restart keepalived
3.查看进程
ps -ef|grep keepalived
1.增加Nginx重启检测脚本
vim /etc/keepalived/check_nginx_alive_or_not.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_alive_or_not.sh
2.配置keepalived监听nginx脚本,keepalived.conf文件中
vrrp_script check_nginx_alive {
script "/etc/keepalived/check_nginx_alive_or_not.sh"
interval 2 # 每隔两秒运行上一行脚本
weight 10 # 如果脚本运行成功,则升级权重+10
# weight -10 # 如果脚本运行失败,则升级权重-10
}
3.在vrrp_instance中新增监控的脚本
track_script {
check_nginx_alive # 追踪 nginx 脚本
}
4.重启Keepalived使得配置文件生效
systemctl restart keepalived
规则:以一个虚拟ip分组归为同一个路由
主节点配置:
global_defs {
router_id keep_171
}
vrrp_instance VI_1 {
state MASTER
interface ens0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
}
vrrp_instance VI_2 {
state BACKUP
interface ens0
virtual_router_id 52
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.101
}
}
备用节点配置:
global_defs {
router_id keep_172
}
vrrp_instance VI_1 {
state BACKUP
interface ens0
virtual_router_id 51
priority 80
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.100
}
}
vrrp_instance VI_2 {
state MASTER
interface ens0
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
virtual_ipaddress {
192.168.1.101
}
}
注意:分别重启两条Keepalived
# 重启Keepalived
systemctl restart keepalived