外观
MySQL Orchestrator 使用
Orchestrator 安装
系统要求
硬件要求:
- CPU:2 核或以上
- 内存:4GB 或以上
- 磁盘:10GB 或以上
软件要求:
- Go 1.16+(编译 Orchestrator 需要)
- MySQL 5.7+ 或 MariaDB 10.3+(用于存储 Orchestrator 元数据)
- 浏览器:支持现代浏览器(Chrome、Firefox、Safari、Edge)
安装方式
1. 从源码编译
bash
# 克隆代码
git clone https://github.com/openark/orchestrator.git
cd orchestrator
# 编译
go build
# 安装
cp orchestrator /usr/local/bin/2. 二进制安装
bash
# 下载二进制文件
wget https://github.com/openark/orchestrator/releases/download/v3.2.6/orchestrator-3.2.6-linux-amd64.tar.gz
# 解压
tar -xzf orchestrator-3.2.6-linux-amd64.tar.gz
cd orchestrator-3.2.6-linux-amd64
# 安装
cp orchestrator /usr/local/bin/3. Docker 安装
bash
# 拉取镜像
docker pull openark/orchestrator:latest
# 运行容器
docker run -d -p 3000:3000 --name orchestrator openark/orchestrator:latestOrchestrator 配置
配置文件
1. 基本配置
json
{
"Debug": false,
"EnableSyslog": false,
"ListenAddress": "0.0.0.0",
"ListenPort": 3000,
"MySQLOrchestratorHost": "orchestrator-meta-db.example.com",
"MySQLOrchestratorPort": 3306,
"MySQLOrchestratorDatabase": "orchestrator",
"MySQLOrchestratorUser": "orchestrator",
"MySQLOrchestratorPassword": "password",
"DefaultInstancePort": 3306,
"DiscoverByShowSlaveHosts": true,
"InstancePollSeconds": 5,
"UnseenInstanceForgetHours": 24,
"SlaveLagQuery": "SELECT COALESCE(MAX(seconds_behind_master), 0) AS slave_lag FROM INFORMATION_SCHEMA.PROCESSLIST WHERE COMMAND = 'ReplicaSQL' OR COMMAND = 'SlaveSQL'",
"SlaveLagTimeCritical": 60,
"SlaveLagTimeWarning": 10,
"FailMasterPromotionIfSQLThreadNotUpToDate": true,
"MaxLagToAdoptMaster": 10,
"RejectReadOnlyMaster": true,
"RejectReadBinlogDisabledMaster": true,
"RecoverMasterClusterFilters": [
{
"Pattern": ".*",
"Policy": "always"
}
],
"FailureDetectionPeriodSeconds": 60,
"RecoveryPeriodBlockSeconds": 3600,
"RecoveryIgnoreHostnameFilters": [],
"RecoveryIgnoreReplicaHostnameFilters": [],
"RecoveryMasterCandidatePriorityFilters": [
{
"Pattern": ".*",
"Priority": 100
}
],
"PostMasterFailoverProcess": "/path/to/post-failover-script.sh",
"PostMasterFailoverSQLProcess": "CALL post_failover(?)"
}2. 关键配置项说明
- MySQLOrchestratorHost/Port/User/Password:Orchestrator 元数据库的连接信息
- DiscoverByShowSlaveHosts:是否通过 SHOW SLAVE HOSTS 命令发现实例
- InstancePollSeconds:检查实例状态的间隔时间(秒)
- SlaveLagQuery:检查从库延迟的 SQL 语句
- SlaveLagTimeCritical/Warning:从库延迟的警告和临界阈值
- RecoverMasterClusterFilters:自动恢复的集群过滤规则
- FailureDetectionPeriodSeconds:检测主库故障的间隔时间
- RecoveryPeriodBlockSeconds:故障恢复后的阻塞时间
- PostMasterFailoverProcess:故障转移后执行的脚本
- PostMasterFailoverSQLProcess:故障转移后执行的 SQL 语句
数据库准备
1. 创建元数据库
sql
CREATE DATABASE orchestrator;
CREATE USER 'orchestrator'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON orchestrator.* TO 'orchestrator'@'%';
FLUSH PRIVILEGES;2. 配置被管理实例的权限
sql
-- 在所有被管理的 MySQL 实例上执行
CREATE USER 'orchestrator'@'%' IDENTIFIED BY 'password';
GRANT SUPER, PROCESS, REPLICATION SLAVE, RELOAD, SELECT ON *.* TO 'orchestrator'@'%';
FLUSH PRIVILEGES;Orchestrator 启动
命令行启动
bash
# 使用配置文件启动
orchestrator --config=/etc/orchestrator.conf.json
# 后台运行
nohup orchestrator --config=/etc/orchestrator.conf.json > /var/log/orchestrator.log 2>&1 &Systemd 启动
1. 创建 systemd 服务文件
ini
# /etc/systemd/system/orchestrator.service
[Unit]
Description=Orchestrator - MySQL replication topology manager
After=network.target
[Service]
Type=simple
User=root
ExecStart=/usr/local/bin/orchestrator --config=/etc/orchestrator.conf.json
Restart=on-failure
RestartSec=5s
[Install]
WantedBy=multi-user.target2. 启动服务
bash
systemctl daemon-reload
systemctl start orchestrator
systemctl enable orchestratorOrchestrator 使用
Web 界面
1. 访问 Web 界面
- 地址:http://orchestrator-host:3000
- 登录:无需登录(默认配置)
2. 主要功能
- 拓扑图:显示 MySQL 复制拓扑
- 实例列表:显示所有实例的状态
- 操作菜单:
- 主从切换(Failover)
- 手动切换主库(SwitchMaster)
- 重新排序列表(Reparent)
- 重置复制(ResetSlave)
- 停止/启动复制(StopSlave/StartSlave)
CLI 工具
1. 基本命令
bash
# 发现实例
orchestrator -c discover -i master.example.com:3306
# 查看拓扑
orchestrator -c topology -i master.example.com:3306
# 手动故障转移
orchestrator -c failover -i master.example.com:3306
# 手动切换主库
orchestrator -c switch-master -i slave.example.com:3306 -d new-master.example.com:3306
# 重新排序列表
orchestrator -c reparent -i slave.example.com:3306 -d new-master.example.com:3306
# 重置复制
orchestrator -c resetslave -i slave.example.com:3306
# 停止复制
orchestrator -c stopslave -i slave.example.com:3306
# 启动复制
orchestrator -c startslave -i slave.example.com:3306
# 查看实例状态
orchestrator -c instance -i master.example.com:33062. 批量操作
bash
# 批量重启复制
orchestrator -c batch -query "SELECT host, port FROM orchestrator.instances WHERE cluster_name = 'mycluster'" -exec "resetslave {hostname} {port}"自动故障转移
工作原理
- 故障检测:Orchestrator 定期检查所有实例的状态
- 主库故障判断:当主库无法连接,且满足故障检测条件时,判断为主库故障
- 选择新主库:根据预设规则选择最合适的从库作为新主库
- 执行故障转移:
- 停止受影响的从库复制
- 将选中的从库提升为主库
- 重新配置其他从库连接到新主库
- 启动复制
- 执行后续操作:执行配置的 post-failover 脚本或 SQL 语句
新主库选择规则
- 从库延迟:延迟最低的从库优先
- 复制位置:复制位置最接近主库的从库优先
- 配置的优先级:根据 RecoveryMasterCandidatePriorityFilters 配置的优先级
- 实例状态:实例必须处于正常状态,没有错误
- 只读状态:默认拒绝只读实例作为新主库
- 二进制日志状态:默认拒绝二进制日志禁用的实例作为新主库
故障转移配置
1. 启用自动故障转移
json
{
"RecoverMasterClusterFilters": [
{
"Pattern": ".*",
"Policy": "always"
}
]
}2. 配置故障转移策略
json
{
"FailureDetectionPeriodSeconds": 60,
"RecoveryPeriodBlockSeconds": 3600,
"RecoveryIgnoreHostnameFilters": [],
"RecoveryIgnoreReplicaHostnameFilters": [],
"RecoveryMasterCandidatePriorityFilters": [
{
"Pattern": "^master.*",
"Priority": 200
},
{
"Pattern": ".*",
"Priority": 100
}
]
}3. 配置故障转移后操作
json
{
"PostMasterFailoverProcess": "/path/to/post-failover.sh",
"PostMasterFailoverSQLProcess": "CALL post_failover(?)"
}4. 示例 post-failover 脚本
bash
#!/bin/bash
# 获取故障转移信息
CLUSTER_NAME=$1
OLD_MASTER=$2
OLD_MASTER_PORT=$3
NEW_MASTER=$4
NEW_MASTER_PORT=$5
FAILURE_DOMAIN=$6
SUCCESS=$7
ERROR_MESSAGE=$8
# 记录日志
echo "$(date) - Failover completed: Cluster: $CLUSTER_NAME, Old Master: $OLD_MASTER:$OLD_MASTER_PORT, New Master: $NEW_MASTER:$NEW_MASTER_PORT, Success: $SUCCESS" >> /var/log/orchestrator-failover.log
# 更新应用配置
if [ "$SUCCESS" = "yes" ]; then
# 更新应用配置文件中的主库地址
sed -i "s/$OLD_MASTER:$OLD_MASTER_PORT/$NEW_MASTER:$NEW_MASTER_PORT/g" /etc/application/config.yml
# 重启应用服务
systemctl restart application.service
# 发送通知
echo "MySQL 主库已切换:$OLD_MASTER:$OLD_MASTER_PORT -> $NEW_MASTER:$NEW_MASTER_PORT" | mail -s "MySQL 故障转移通知" admin@example.com
fi手动操作
手动切换主库
1. Web 界面操作
- 访问 Orchestrator Web 界面
- 找到要切换的从库
- 点击 "Switch Master" 按钮
- 选择新的主库
- 确认切换
2. CLI 操作
bash
# 切换 slave.example.com:3306 的主库到 new-master.example.com:3306
orchestrator -c switch-master -i slave.example.com:3306 -d new-master.example.com:3306重新排序列表
1. Web 界面操作
- 访问 Orchestrator Web 界面
- 找到要重新排序的从库
- 点击 "Reparent" 按钮
- 选择新的主库
- 确认重新排序
2. CLI 操作
bash
# 将 slave.example.com:3306 重新排序到 new-master.example.com:3306 下
orchestrator -c reparent -i slave.example.com:3306 -d new-master.example.com:3306重置复制
1. Web 界面操作
- 访问 Orchestrator Web 界面
- 找到要重置的从库
- 点击 "Reset Slave" 按钮
- 确认重置
2. CLI 操作
bash
# 重置 slave.example.com:3306 的复制
orchestrator -c resetslave -i slave.example.com:3306Orchestrator 高可用
部署多个 Orchestrator 实例
1. 配置共享元数据库
- 所有 Orchestrator 实例连接到同一个元数据库
- 元数据库建议使用主从复制或集群部署
2. 配置负载均衡
- 使用 Nginx 或 HAProxy 作为负载均衡器
- 配置 Orchestrator 实例的健康检查
3. Nginx 配置示例
nginx
upstream orchestrator {
server orchestrator1.example.com:3000;
server orchestrator2.example.com:3000;
server orchestrator3.example.com:3000;
}
server {
listen 80;
server_name orchestrator.example.com;
location / {
proxy_pass http://orchestrator;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
proxy_read_timeout 60s;
}
location /health {
proxy_pass http://orchestrator/api/health;
proxy_http_version 1.1;
proxy_set_header Host $host;
}
}Orchestrator 监控
监控指标
- 实例状态:所有实例的在线状态
- 复制延迟:从库延迟情况
- 复制拓扑变化:拓扑结构的变化
- 故障转移事件:自动故障转移的次数和结果
- Orchestrator 自身状态:Orchestrator 服务的健康状态
监控集成
1. Prometheus 集成
yaml
# prometheus.yml
scrape_configs:
- job_name: 'orchestrator'
static_configs:
- targets: ['orchestrator1.example.com:3000', 'orchestrator2.example.com:3000']
metrics_path: '/api/prometheus/metrics'2. 日志监控
- 监控 Orchestrator 的日志文件
- 关注错误信息和故障转移事件
3. 健康检查
- 使用
/api/health端点进行健康检查 - 配置负载均衡器或监控系统定期检查
最佳实践
部署建议
- 元数据库:使用独立的 MySQL 实例存储 Orchestrator 元数据
- 高可用:部署多个 Orchestrator 实例,配置负载均衡
- 权限配置:为 Orchestrator 用户配置最小必要权限
- 网络隔离:将 Orchestrator 部署在与数据库相同的网络区域
配置建议
- 故障检测:合理设置故障检测间隔,避免误判
- 故障转移策略:根据实际需求配置新主库选择规则
- 后续操作:配置适当的 post-failover 脚本,更新应用配置
- 监控:配置全面的监控,及时发现问题
使用建议
- 定期测试:定期测试自动故障转移功能
- 手动操作:熟悉手动操作流程,以便在自动故障转移失败时手动干预
- 备份:定期备份 Orchestrator 元数据库
- 日志审计:保存 Orchestrator 日志,用于审计和故障分析
常见问题(FAQ)
Q1: Orchestrator 如何自动发现 MySQL 实例?
A1: Orchestrator 通过以下方式发现实例:
- 手动添加实例
- 通过 SHOW SLAVE HOSTS 命令自动发现
- 通过配置的种子实例递归发现
Q2: 如何配置 Orchestrator 自动故障转移?
A2: 配置自动故障转移需要:
- 配置 RecoverMasterClusterFilters 启用自动恢复
- 配置故障检测和恢复策略
- 配置新主库选择规则
- 配置故障转移后操作
Q3: 如何测试 Orchestrator 自动故障转移?
A3: 可以通过以下方式测试:
- 停止主库的 MySQL 服务
- 等待 Orchestrator 检测到故障
- 观察 Orchestrator 执行自动故障转移
- 验证故障转移结果,检查应用是否正常
Q4: 如何处理 Orchestrator 误判主库故障?
A4: 可以通过以下方式处理:
- 手动取消正在进行的故障转移
- 调整故障检测配置,增加检测间隔
- 检查网络连接,确保 Orchestrator 能正常连接所有实例
- 考虑使用多个 Orchestrator 实例进行冗余检测
Q5: Orchestrator 支持 GTID 复制吗?
A5: 是的,Orchestrator 支持 GTID 复制:
- 自动识别 GTID 模式
- 支持基于 GTID 的复制拓扑管理
- 支持基于 GTID 的故障转移
Q6: 如何升级 Orchestrator?
A6: 升级步骤:
- 备份 Orchestrator 元数据库
- 停止现有 Orchestrator 实例
- 安装新版本的 Orchestrator
- 启动新版本的 Orchestrator 实例
- 验证功能正常
Q7: 如何扩展 Orchestrator?
A7: 可以通过以下方式扩展 Orchestrator:
- 部署更多 Orchestrator 实例
- 使用插件机制扩展功能
- 集成其他工具,如 Consul、Etcd 等
- 开发自定义的 post-failover 脚本
