Keepalived 高可用集群配置
Keepalived 高可用集群配置
一、Keepalived 简介
1、什么是 Keepalived?
Keepalived 软件最初专为 LVS 负载均衡软件设计,用于管理和监控 LVS 集群系统中各个服务节点的状态,后来加入了实现高可用的 VRRP 功能。因此,Keepalived 除了能够管理 LVS 软件外,还可以作为其他服务(如 Nginx、Haproxy、MySQL 等)的高可用解决方案。
2、Keepalived 的重要功能
- 管理 LVS 负载均衡软件
- 实现 LVS 集群节点的健康检查
- 作为系统网络服务的高可用性解决方案
3、Keepalived 高可用故障转移原理
Keepalived 高可用服务之间的故障切换转移是通过 VRRP(虚拟路由器冗余协议)实现的。主节点会不断向备节点发送心跳消息,当主节点故障时,备节点无法检测到心跳,就会接管主节点的 IP 资源及服务。
官网地址:http://www.keepalived.org/
二、Keepalived 工作原理
1、VRRP 协议
- VRRP:虚拟路由冗余协议,解决静态路由单点故障问题
- 通信方式:IP 多播(默认多播地址 224.0.0.18)
- 工作机制:主节点发包,备节点接包,备节点接收不到数据包时启动接管程序
- 安全性:支持加密协议,但官方推荐使用明文配置认证
2、Keepalived 工作流程
- 通过 VRRP 竞选机制确定主备角色
- 主节点优先级高于备节点
- 主节点定期发送 VRRP 组播包
- 备节点监听不到组播包时接管资源
- 接管速度可小于 1 秒
三、Keepalived 部署
环境配置
主机名 | IP 地址 | 角色 |
---|---|---|
Master.example.com | 192.168.100.100 | 主节点 |
Slave.example.com | 192.168.100.200 | 备节点 |
VIP | 192.168.100.150 | 虚拟IP |
- 关闭防火墙和 seLinux
- 配置 yum 源
- 安装常用命令 vim wget gcc gcc-c++
1、安装 keepalived
在 master 和 slave 中
[root@master.example.com ~]# yum -y install epel-release
2、Nginx 服务部署
在 master 中
[root@master.example.com ~]# yum -y install nginx
[root@master.example.com ~]# systemctl restart nginx.service
[root@master.example.com ~]# systemctl enable nginx.service
[root@master.example.com ~]# echo "master node" > /usr/share/nginx/html/index.html
在 slave 中
[root@slave.example.com ~]# yum -y install nginx
[root@slave.example.com ~]# systemctl restart nginx.service
[root@slave.example.com ~]# systemctl enable nginx.service
[root@slave.example.com ~]# echo "slave node" > /usr/share/nginx/html/index.html
3、配置主 keepalived
在 master 中
[root@master.example.com ~]# cd /etc/keepalived/
[root@master.example.com /etc/keepalived]# cp keepalived.conf keepalived.conf.bak
[root@master.example.com /etc/keepalived]# vim keepalived.conf
[root@master.example.com /etc/keepalived]# systemctl restart keepalived.service
[root@master.example.com /etc/keepalived]# systemctl enable keepalived.service
在 slave 中
[root@slave.example.com ~]# cd /etc/keepalived/
[root@slave.example.com /etc/keepalived]# cp keepalived.conf keepalived.conf.bak
[root@slave.example.com /etc/keepalived]# vim keepalived.conf
[root@slave.example.com /etc/keepalived]# systemctl restart keepalived.service
[root@slave.example.com /etc/keepalived]# systemctl enable keepalived.service
简单模板
! Configuration File for keepalivedglobal_defs {router_id test01 # 自定义名字,master 与 slave 不一样
}vrrp_instance VI_1 {state MASTER # 类型主(MASTER)备(BACKUP)interface ens33 # 使用网卡名称virtual_router_id 51 # id 主备一致priority 100 # 优先级,数字越大,优先级越高,主要大于备advert_int 1authentication {auth_type PASSauth_pass 1111}virtual_ipaddress {192.168.100.150}
}virtual_server 192.168.100.150 80 { # vip 地址delay_loop 6lb_algo rrlb_kind DRpersistence_timeout 50protocol TCPreal_server 192.168.100.10 80 { # 真实服务器地址weight 1TCP_CHECK {connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3}}real_server 192.168.100.40 80 { # 真实服务器地址weight 1TCP_CHECK {connect_port 80connect_timeout 3nb_get_retry 3delay_before_retry 3}}
}
4、修改内核参数,开启侦听 vip 功能
此步可做可不做,该功能可用于仅监听VIP的时候
在 master 和 slave 中
vim /etc/sysctl.confnet.ipv4.ip_nonlocal_bind = 1sysctl -pnet.ipv4.ip_nonlocal_bind = 1
5、让 keepalived 监控 nginx 负载均衡
在 master 上编写脚本
[root@master.example.com ~]# mkdir /scripts
[root@master.example.com ~]# cd /scripts/
[root@master.example.com /scripts]# vim check.sh
[root@master.example.com /scripts]# chmod +x check.sh
[root@master.example.com /scripts]# vim notify.sh
[root@master.example.com /scripts]# chmod +x notify.sh
check.sh 内容
#!/bin/bash
nginx_status=`ps -ef | grep -v "grep" | grep "nginx" | wc -l`
if [ $nginx_status -lt 1 ];thensystemctl stop keepalived
fi
notify.sh 内容
#!/bin/bash
VIP=$2
sendmail () {subject="${VIP}'s server keepalived state is translate"content="`date +'%F %T'`: `hostname`'s state change to master"echo $content | mail -s "$subject" 2065414714@qq.com
}
case "$1" inmaster)nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)if [ $nginx_status -lt 1 ];thensystemctl start nginxfisendmail;;backup)nginx_status=$(ps -ef|grep -Ev "grep|$0"|grep '\bnginx\b'|wc -l)if [ $nginx_status -gt 0 ];thensystemctl stop nginxfi;;*)echo "Usage:$0 master|backup VIP";;
esac
在 slave 上编写脚本
[root@slave.example.com ~]# mkdir /scripts
[root@slave.example.com ~]# cd /scripts/
[root@slave.example.com /scripts]# scp root@192.168.100.10:/scripts/check.sh .
[root@slave.example.com /scripts]# scp root@192.168.100.10:/scripts/notify.sh .
[root@slave.example.com /scripts]# chmod +x check.sh
[root@slave.example.com /scripts]# chmod +x notify.sh
配置 master 的 keepalived
[root@master.example.com ~]# vim /etc/keepalived/keepalived.conf
[root@master.example.com ~]# systemctl restart keepalived.service
配置 slave 的 keepalived
[root@slave.example.com ~]# vim /etc/keepalived/keepalived.conf
[root@slave.example.com ~]# systemctl restart keepalived.service
6、模拟测试
启用 keepalived,开启 nginx 服务
systemctl restart keepalived.service
systemctl enable keepalived.service
systemctl restart nginx.service
systemctl enable nginx.service
关闭 master 的 nginx服务
[root@master.example.com ~]# systemctl stop nginx.service