文章目录
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-01 | 192.168.249.131 | 3306、33061 |
sys-cluster-02 | 192.168.249.132 | 3306、33061 |
sys-cluster-03 | 192.168.249.133 | 3306、33061 |
- 虚拟IP:192.168.249.130
3.安装部署MGR
在3 台服务器上启动MGR
- MGR单主模式安装参考:《CentOS7部署MySQL80集群》
- 目录:
- 安装目录:
/usr/local/mysql
- 数据库文件目录,例如表结构和数据:
/var/lib/mysql
- 日志文件目录:
/var/log/mysql
- 配置文件目录:
/etc/mysql
- 用户名:
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节点配置
- 如下配置需要根据实际环境进行配置:
-
router_id:服务器标识
-
interface:网卡(终端执行命令:ifconfig )
-
real_server:本机IP和端口
-
priority:master的优先级
-
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 | 重新加载服务的配置文件 |