一、Keepalived安装

1、去Keepalived官网下载安装包

官网地址:Keepalived for Linux

有互联网可以使用wget直接下载

先到/usr/local/src目录

cd /usr/local/src

然后下载

wget https://www.keepalived.org/software/keepalived-2.2.7.tar.gz --no-check-certificate

然后解压

tar -zxvf keepalived-2.2.7.tar.gz

进入解压目录

cd keepalived-2.2.7

安装libnl/libnl-3依赖

yum -y install libnl libnl-devel

使用configure命令配置安装目录与核心配置文件所在位置

prefix keepalived的安装位置
sysconf keepalived核心配置文件所在的位置,固定位置,改成其他位置keepalived启动不了,/var/log/message中会报错

./configure --prefix=/usr/local/keepalived --sysconf=/etc

安装keepalived

make && make install

二、Keepalived配置  /etc/keepalived/keepalived.conf

主节点配置

global_defs {
## 路由id: 当前安装的keepalived节点主机的标识符,全局唯一
router_id srv248
}

## 检测nginx是否运行
vrrp_script chk_nginx {
## 检测nginx状态的脚本路径
script "/etc/keepalived/nginx_check.sh"
#检测时间间隔
interval 2
## weight正负值都可,这里为-20
## weight值为负数时 
## 脚本检测失败 priority(100) + weight(-20) <  BACKUP(priority 90) 则主、备切换
## 脚本检测成功 priority(100) >  BACKUP(priority 90) 则不发生切换
## 如果条件成立(没有运行),权重-20
weight -20
}

## 定义虚拟路由, VI_1 为虚拟路由的标示符,自己定义名称
vrrp_instance VI_1 {
## 主节点为 MASTER, 对应的备份节点为 BACKUP
state MASTER
## 绑定虚拟 IP 的网络接口,与本机 IP 地址所在的网络接口相同, 目前主机为ens32(命令ip addr查看)
interface ens32
## 虚拟路由的 ID 号, 两个节点设置必须一样(VI_1节点这里主备都是51)
virtual_router_id 51
## 节点优先级, 值范围 0-254, MASTER 要比 BACKUP 高
priority 100
## 主备之间同步检查时间间隔,单位秒
advert_int 1
## 认证权限密码,防止非法节点进入(主备需一致)
authentication {
auth_type PASS
auth_pass 1111
}
## 调用脚本
track_script {
## 上面的脚本名
chk_nginx
}
## 虚拟IP, 两个节点设置必须一样
virtual_ipaddress {
192.168.1.128
}

}

## 这里为双机热备实现方案:两个vip互相飘移,互为主备,以下节点将此台主机配置未备用机
## 虚拟IP为192.168.1.129

vrrp_instance VI_2 {
state BACKUP
interface ens32
virtual_router_id 52
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.1.129
}

}

从节点配置

global_defs {
router_id srv249
}

vrrp_script chk_nginx {
script "/etc/keepalived/nginx_check.sh"
interval 2
weight -20
}

vrrp_instance VI_1 {
state BACKUP
interface ens32
virtual_router_id 51
priority 90
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.1.128
}

}

vrrp_instance VI_2 {
state MASTER
interface ens32
virtual_router_id 52
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass 1111
}
track_script {
chk_nginx
}
virtual_ipaddress {
192.168.1.129
}

}

三、nginx+keepalived检测脚本

脚本位置与配置路径相同 (根据自己配置放置)/etc/keepalived/nginx_check.sh

#!/bin/bash
counter=$(ps -C nginx |grep -v PID |wc -l)
if [ $counter == 0 ];then
/usr/local/webserver/nginx/sbin/nginx
sleep 5
counter=$(ps -C nginx |grep -v PID |wc -l)
if [ $counter == 0 ];then
systemctl stop keepalived
fi
fi

注意:

/usr/local/webserver/nginx/sbin/nginx  //启动nginx 根据本机nginx的安装目录填写

sleep 5 //由于启动服务需要时间,sh脚本休眠5秒再执行

脚本是windows下写的,然后传到linux下运行的,文件格式需要给dos文件格式转成unix
dos转unix

安装: yum install dos2unix

命令: dos2unix filename

给脚本添加可执行权限: chmod -R 777 filename.sh

Tips:

当前配置为

keepalived配置SSL keepalived安装配置_IP

如需变为(去掉 vrrp_instance VI_2 节点即可)

 

keepalived配置SSL keepalived安装配置_keepalived配置SSL_02