keepalived识别MGR主节点

keepalived识别MGR主节点

一、前言

  • Linux 发行版:CentOS-7-x86_64-DVD-1804.iso
  • MySQL 版本:mysql80-community-release-el7-9 (version:8.0.34)

keepalived 官网:https://www.keepalived.org/

Group Replication:https://dev.mysql.com/doc/refman/8.0/en/group-replication.html

Single-Primary Mode:https://dev.mysql.com/doc/refman/8.0/en/group-replication-single-primary-mode.html

Multi-Primary Mode:https://dev.mysql.com/doc/refman/8.0/en/group-replication-multi-primary-mode.html

MySQL Yum Repository:https://dev.mysql.com/downloads/repo/yum/

[ContOS] MySQL安装部署:https://blog.csdn.net/u011424614/article/details/94555816

[Windows] MySQL安装部署:https://blog.csdn.net/u011424614/article/details/102466819

CentOS7安装部署MySQL80:https://blog.csdn.net/u011424614/article/details/132418916

CentOS7部署MySQL80集群:https://blog.csdn.net/u011424614/article/details/133222091

CentOS基础操作命令:https://blog.csdn.net/u011424614/article/details/94555916

二、正文

1.场景说明

  • MGR单主模式下,通过 keepalived 识别 MGR 主节点,当主节点下线后,自动识别主节点,并切换到主节点
  • 另外说明:MGR多主模式下,不需要识别主节点,确认健康状态,切换到任意一个主节点即可

2.硬件配置

机器名IP端口
sys-cluster-01192.168.249.1313306、33061
sys-cluster-02192.168.249.1323306、33061
sys-cluster-03192.168.249.1333306、33061
  • 虚拟IP:192.168.249.130

3.安装部署MGR

在3 台服务器上启动MGR

  1. 安装目录:/usr/local/mysql
  2. 数据库文件目录,例如表结构和数据:/var/lib/mysql
  3. 日志文件目录:/var/log/mysql
  4. 配置文件目录:/etc/mysql
  5. 用户名:root 密码:Root@#123456

4.部署Keepalived

1)安装Keepalived

  • 3台服务器上,安装 Keepalived
# 创建安装目录
mkdir /opt/keepalived
cd /opt/keepalived

# 下载安装包
wget https://www.keepalived.org/software/keepalived-2.2.8.tar.gz --no-check-certificate
# 解压缩
tar -zxvf keepalived-2.2.8.tar.gz

cd keepalived-2.2.8/
# 安装依赖
yum -y install openssl-devel gcc-c++ libnl libnl-devel libnfnetlink-devel

# 安装 Keepalived
./configure --prefix=/opt/keepalived/ --sysconf=/etc

make && make install

3)检测脚本

  • 3台服务器上,创建检测脚本
# 创建脚本目录
mkdir /opt/keepalived/script

# 编辑检测脚本
vim /opt/keepalived/script/check_mysql_primary.sh
  • 配置检查脚本,根据实际情况,修改 MySQL 用户和密码
#!/bin/bash

# 设置 MySQL 连接参数
MYSQL_USER="root"
MYSQL_PASSWORD="Root@#123456"
MYSQL_DATABASE="performance_schema"

# 获取当前电脑的主机名和 IP 地址
HOSTNAME=$(hostname)
IP=$(hostname -I | awk '{print $1}')

# 查询 MySQL 中的主节点 host
PRIMARY_HOST=$(mysql -u$MYSQL_USER -p$MYSQL_PASSWORD -e "SELECT MEMBER_HOST FROM $MYSQL_DATABASE.replication_group_members WHERE MEMBER_ROLE='PRIMARY';" -s)

# 检查主机名和 IP 是否与主节点匹配
if [ "$HOSTNAME" == "$PRIMARY_HOST" ]; then
  exit 0
elif [ "$IP" == "$PRIMARY_HOST" ]; then
  exit 0
else
  exit 1
fi
  • 设置文件的执行权限
chmod +x /opt/keepalived/script/check_mysql_primary.sh

4)MASTER节点配置

  • 如下配置需要根据实际环境进行配置:
  1. router_id:服务器标识

  2. interface:网卡(终端执行命令:ifconfig

  3. real_server:本机IP和端口

  4. priority:master的优先级

  5. unicast_src_ip 、unicast_peer:节点IP

  • 192.168.249.131 配置文件
cat > /etc/keepalived/keepalived.conf <<EOF
# ==== 全局默认配置
global_defs {
   # keepalived 的服务器标识
   router_id MFG_LVS_01
}
# ==== 检测脚本
vrrp_script check_mysql_primary {
   script "/opt/keepalived/script/check_mysql_primary.sh"
   interval 5
}
# ==== 配置冗余协议
vrrp_instance VI_1 {
   # 主节点
   state MASTER
   # 网卡地址
   interface ens33
   virtual_router_id 51
   # 成为master的优先级
   priority 100
   advert_int 1
   # 通信认证-master和backup的通信授权
   authentication {
   	  auth_type PASS
      auth_pass 9876543
   }
   # 当前节点的 IP 地址
   unicast_src_ip 192.168.249.131
   # 其他节点的 IP 地址
   unicast_peer {
      192.168.249.132
      192.168.249.133
   }
   # 虚拟IP
   virtual_ipaddress {
      192.168.249.130
   }
   # 引用检测脚本
   track_script {
      check_mysql_primary
   }
}
# ==== 配置LVS
# 对外的IP和端口,与虚拟IP一致
virtual_server 192.168.249.130 3306 {
   delay_loop 6
   # 负载均衡算法
   lb_algo rr
   # 转发规则
   lb_kind NAT
   persistence_timeout 50
   protocol TCP
   # 服务器IP和端口
   real_server 192.168.249.131 3306 {
      weight 1
      TCP_CHECK {
         connect_timeout 3
         retry 3
         delay_before_retry 3
      }
   }
}
EOF

5)BACKUP节点配置

  • 192.168.249.132 配置文件
cat > /etc/keepalived/keepalived.conf <<EOF
# ==== 全局默认配置
global_defs {
   # keepalived 的服务器标识
   router_id MFG_LVS_02
}
# ==== 检测脚本
vrrp_script check_mysql_primary {
   script "/opt/keepalived/script/check_mysql_primary.sh"
   interval 5
}
# ==== 配置冗余协议
vrrp_instance VI_1 {
   # 备用节点
   state BACKUP
   # 网卡地址
   interface ens33
   virtual_router_id 51
   # 成为master的优先级
   priority 68
   advert_int 1
   # 通信认证-master和backup的通信授权
   authentication {
   	  auth_type PASS
      auth_pass 9876543
   }
   # 当前节点的 IP 地址
   unicast_src_ip 192.168.249.132
   # 其他节点的 IP 地址
   unicast_peer {
      192.168.249.131
      192.168.249.133
   }
   # 虚拟IP
   virtual_ipaddress {
      192.168.249.130
   }
   # 引用检测脚本
   track_script {
      check_mysql_primary
   }
}
# ==== 配置LVS
# 对外的IP和端口,与虚拟IP一致
virtual_server 192.168.249.130 3306 {
   delay_loop 6
   # 负载均衡算法
   lb_algo rr
   # 转发规则
   lb_kind NAT
   persistence_timeout 50
   protocol TCP
   # 服务器IP和端口
   real_server 192.168.249.132 3306 {
      weight 1
      TCP_CHECK {
         connect_timeout 3
         retry 3
         delay_before_retry 3
      }
   }
}
EOF
  • 192.168.249.133 配置文件
cat > /etc/keepalived/keepalived.conf <<EOF
# ==== 全局默认配置
global_defs {
   # keepalived 的服务器标识
   router_id MFG_LVS_03
}
# ==== 检测脚本
vrrp_script check_mysql_primary {
   script "/opt/keepalived/script/check_mysql_primary.sh"
   interval 5
}
# ==== 配置冗余协议
vrrp_instance VI_1 {
   # 备用节点
   state BACKUP
   # 网卡地址
   interface ens33
   virtual_router_id 51
   # 成为master的优先级
   priority 48
   advert_int 1
   # 通信认证-master和backup的通信授权
   authentication {
   	  auth_type PASS
      auth_pass 9876543
   }
   # 当前节点的 IP 地址
   unicast_src_ip 192.168.249.133
   # 其他节点的 IP 地址
   unicast_peer {
      192.168.249.131
      192.168.249.132
   }
   # 虚拟IP
   virtual_ipaddress {
      192.168.249.130
   }
   # 引用检测脚本
   track_script {
      check_mysql_primary
   }
}
# ==== 配置LVS
# 对外的IP和端口,与虚拟IP一致
virtual_server 192.168.249.130 3306 {
   delay_loop 6
   # 负载均衡算法
   lb_algo rr
   # 转发规则
   lb_kind NAT
   persistence_timeout 50
   protocol TCP
   # 服务器IP和端口
   real_server 192.168.249.133 3306 {
      weight 1
      TCP_CHECK {
         connect_timeout 3
         retry 3
         delay_before_retry 3
      }
   }
}
EOF

6)Keepalived自启动

  • 3台服务器上,配置 Keepalived 自启动
cat > /etc/systemd/system/keepalived.service <<EOF
[Unit]
Description=LVS and VRRP High Availability Monitor
After=network-online.target syslog.target
Wants=network-online.target

[Service]
Type=forking
PIDFile=PIDFile=var/run/keepalived.pid
KillMode=process
EnvironmentFile=-/etc/sysconfig/keepalived
ExecStart=/opt/keepalived/sbin/keepalived $KEEPALIVED_OPTIONS
ExecReload=/bin/kill -HUP $MAINPID

[Install]
WantedBy=multi-user.target
EOF
  • 重新加载服务的配置文件
systemctl daemon-reload
  • 启动服务
systemctl start keepalived.service
  • 查询服务状态
systemctl status keepalived.service
  • master 启动成功提示
Keepalived_vrrp[80017]: (VI_1) Entering MASTER STATE

5常用命令

命令功能
systemctl start keepalived.service启动服务
systemctl enable keepalived.service开机自启动
systemctl status keepalived.service服务状态
systemctl restart keepalived.service重启服务
systemctl stop keepalived.service停止服务
systemctl daemon-reload重新加载服务的配置文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

趴着喝可乐

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值