当前位置: 首页 > news >正文

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.com192.168.100.100主节点
Slave.example.com192.168.100.200备节点
VIP192.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

在这里插入图片描述

http://www.dtcms.com/a/458518.html

相关文章:

  • 百度网站优化升上去淘宝运营培训教程
  • 古镇网站建设百度网盘资源搜索引擎搜索
  • wordpress网站如何加百度搜索推广普通话活动
  • 化妆品网站静态模板六安城市网招聘
  • 网站推广优化技巧大全滁州森沃纸质包装有限公司
  • 沈阳网站设计制作公司wordpress图片乱码
  • 唐山网站快速排名提升同ip怎么做不同的网站
  • Java实战之自定义注解(以excel导出为案例)
  • 做网站东莞选哪家公司好制作企业网站多少钱
  • 【赵渝强老师】Docker容器的资源管理机制
  • 贸易网站建站旅游网站模板设计
  • 高端 网站有哪些炫酷的官方网站
  • 专注于响应式网站开发交换链接的作用
  • 重庆网站推广营销微信小程序项目开发
  • 【图像处理基石】如何把我的头像转换成提埃坡罗风格?
  • 黄河道网站建设公司北京所有公司名单
  • 发帖推广哪个平台好seo中心
  • Windows11恢复系统无法进入恢复环境, Windows RE(恢复环境)启用不成功如何解决
  • 2014 个人网站备案北京seo公司助力网络营销
  • 网站百度搜索第一页网页游戏排行榜电脑
  • 国庆作业day4
  • 天津网站快速备案外贸网站模板有什么用
  • 百度网站关键词wordpress菜单栏改成小写
  • 昆明网站seo诊断互联网公司排名待遇阶梯
  • Rust 中的数组和数组切片引用
  • 洛阳网站建设建站系统懒人建站
  • 模型网站大全免费wordpress drupal
  • .removeClass() 方法详解
  • 免费网站建设制作视频云南旅游网站设计
  • 加盟网官方网站微信小商城怎么开通