文章目录

  • Keepalived 概念
  • VRRP协议
  • Keepalived安装
  • Keepalived核心配置文件keepalived.conf
  • 把Keepalived注册为系统服务
  • 实现双机主备
  • Keepalived配置Nginx自动重启
  • 实现主机热备



Keepalived 概念
  • 解决单点故障
  • 组件免费
  • 可以实现高可用HA(Hight Availability)机制
  • 基于VRRP协议
VRRP协议
  • 虚拟路由冗余协议
  • 解决内网单点故障的路由协议
  • 构建多个路由器 MASTER BACKUP。多个路由器可以理解成每个Nginx节点
  • 虚拟IP - VIP
Keepalived安装

跟Nginx安装基本相同

#解压以后 进入keepalived
./configure --prefix=usr/local/keepalived --sysconf=/etc
make && make install

如果出现 Please install libnl/libnl-3 dev libraries…

yum -y install libnl libnl-devel
Keepalived核心配置文件keepalived.conf
global_defs  {

	#  路由id:当前安装keepalived的节点主机标识符,保证全局唯一
	router_id  keepalived-01
	}
vrrp_instance  VI_1  {

	# 表示状态是MASTER主机还是备用机BACKUP 
	state  MASTER
	# 该实例绑定的网卡  ip adr查看本服务器网卡
	interface  eth0
	# 保证主备节点一致即可
	virtual_router_id  51
	#  权重,master权重一般高于backup,如果有多个,那就是选举,谁的权重高,谁就当选 
	priority  100
	# 主备之间同步检查时间间隔,单位秒
	advert_int  1
	# 认证权限密码,防止非法节点进入
	authentication  {
		auth_type  PASS 
		auth_pass  123456
		}
	#  虚拟出来的ip,可以有多个(vip) 
	virtual_ipaddress  {
		192.168.200.16
		}
}
把Keepalived注册为系统服务

这样就不用 kill -9 进程号 来停止Keepalived

cd /home/software/keepalived-2.0.18/keepalived/etc/

cp init.d/keepalived /etc/init.d/
cp sysconfig/keepalived /etc/sysconfig/
# 配置完成之后,重新加载
systemctl daemon-reload
# 启动keepalived
systemctl start keepalived.service
# 停止keepalived
systemctl stop keepalived.service
# 重启keepalived
systemctl restart keepalived.service
实现双机主备

配置备机配置

global_defs  {

	#  路由id:当前安装keepalived的节点主机标识符,保证全局唯一
	router_id  keepalived-02
	}
vrrp_instance  VI_1  {

	# 表示状态是MASTER主机还是备用机BACKUP 
	state  BACKUP 
	# 该实例绑定的网卡  ip adr查看本服务器网卡
	interface  eth0
	# 保证主备节点一致即可
	virtual_router_id  51
	#  权重,master权重一般高于backup,如果有多个,那就是选举,谁的权重高,谁就当选 
	priority  20
	# 主备之间同步检查时间间隔,单位秒
	advert_int  1
	# 认证权限密码,防止非法节点进入
	authentication  {
		auth_type  PASS 
		auth_pass  123456
		}
	#  虚拟出来的ip,可以有多个(vip) 
	virtual_ipaddress  {
		192.168.200.16
		}
}
Keepalived配置Nginx自动重启
vim /etc/keepalived/check_nginx_alive_or_not.sh

1.编写脚本 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

2.配置keepalived监听nginx脚本
  新增vrrp_script  track_script两个脚本

global_defs  {

	}
	
vrrp_script check_nginx_alive {
	script "/etc/keepalived/check_nginx_alive_or_not.sh" interval 2 # 每隔两秒运行上一行脚本
	weight 10 # 如果脚本运行失败, 则升级权重+10
}
vrrp_instance  VI_1  {
   
	# 认证权限密码,防止非法节点进入
	authentication  {
		auth_type  PASS 
		auth_pass  123456
		}
	#在 vrrp_instance 中新增监控的脚本	
	track_script {
		check_nginx_alive	# 追踪 nginx 脚本
	}
	#  虚拟出来的ip,可以有多个(vip) 
	virtual_ipaddress  {
		192.168.200.16
		}
}
实现主机热备

互相配置第二个 vrrp_instance
以一个虚拟ip分组归为同一个路由

vrrp_instance  VI_1  {

	state  BACKUP 
	interface  eth0
	virtual_router_id  51
	priority  20
	advert_int  1
	authentication  {
		auth_type  PASS 
		auth_pass  123456
		}
	virtual_ipaddress  {
		192.168.200.16
		}
}
#add 
vrrp_instance  VI_2  {

	state  MASTER
	interface  eth0
	virtual_router_id  52
	priority  100
	advert_int  1
	authentication  {
		auth_type  PASS 
		auth_pass  123456
		}
	virtual_ipaddress  {
		192.168.200.17
		}
}