作者: WalterWj 原文来源:https://tidb.net/blog/c9889701
操作背景
如图,当前机房 1 为 DRAutoSync 的主机房。
非计划内切换下,会模拟没有任何操作的情况下,机房 1 直接掉电。然后将机房 2 剩余的节点升级为完整集群,提供服务。
注意由于 primary 集群挂掉,tidb 多副本丢失,机房 2 的节点在恢复前是不提供服务的。
前置条件
机房二有 tiup 备机,且有完整的镜像源和 ctl 工具
推荐服务器安装 jq 命令
操作流程
关闭机房 1 所有节点
指定 -N 关闭
tiup cluster stop <cluster-name> -N 172.16.201.18:20160 -y
复制代码
关闭节点后,手动登录数据库会发现无法登录
备份 tiup 的 meta.yaml 文件,且删除主机房信息
进入集群元数据存储目录,tidb-wj 改为集群名
cd ~/.tiup/storage/cluster/clusters/tidb-wj/
复制代码
备份 meta 文件
cp meta.yaml meta.yaml.bak
复制代码
手动删除机房 1 的相关节点信息
脚本化
这个步骤可以使用脚本
#!/bin/bashfunction parse_args() { local tiup_base_dir="" local cluster_name="" local del_node_ip_list="" # 解析参数 for arg in "$@" do case $arg in tiup_base_dir=*) tiup_base_dir="${arg#*=}" shift ;; cluster_name=*) cluster_name="${arg#*=}" shift ;; del_node_ip_list=*) del_node_ip_list="${arg#*=}" ;; *) echo "Unknown argument: $arg" exit 1 ;; esac done
# 检查必要参数是否存在 if [ -z "$tiup_base_dir" ]; then echo "Missing required argument: tiup_base_dir" fi
if [ -z "$cluster_name" ]; then echo "Missing required argument: cluster_name" fi if [ -z "$del_node_ip_list" ]; then echo "Missing required argument: del_node_ip_list" fi
# 返回参数 echo "$tiup_base_dir" "$cluster_name" "$del_node_ip_list"}
function createnewmatefile() { local tiup_base_dir="$1" local cluster_name="$2" local del_node_ip_list="$3" rm -f /tmp/DR-Auto-Sync_Failover-tmp1.log rm -f /tmp/DR-Auto-Sync_Failover-tmp2.log if [ $context_status -eq "0" ];then echo "上下文状态异常!" exit 1 fi echo " 创建集群新的mate文件"
cd $tiup_base_dir/storage/cluster/clusters/$cluster_name/ cp meta.yaml meta.yaml.bak_`date +%Y%m%d%H%M%S` if [ "$?" -eq "0" ];then echo -e "mate.yaml 备份成功!" else echo -e "mate。yaml 备份失败!" context_status=0 exit 1 fi cp meta.yaml meta_tmp.yaml sed -i '/^ \{2\}[a-z-].*$/icutline' meta_tmp.yaml if [ "$?" -eq "0" ];then echo -e "mate.yaml标记成功!" else echo -e "mate.yaml标记失败!" context_status=0 exit 1 fi echo $del_node_ip_list > /tmp/del_node_list.txt sed -i 's/,/ /g' /tmp/del_node_list.txt del_node_ip_list=`cat /tmp/del_node_list.txt` for i in $del_node_ip_list do echo $i sed -n "/ - host: ${i}/,/cutline/=" meta_tmp.yaml >> /tmp/DR-Auto-Sync_Failover-tmp1.log done echo -n "sed -i " >> /tmp/DR-Auto-Sync_Failover-tmp2.log for j in `cat /tmp/DR-Auto-Sync_Failover-tmp1.log` do echo -n "-e \"${j}d\" " >> /tmp/DR-Auto-Sync_Failover-tmp2.log done echo -n "meta_tmp.yaml" >> /tmp/DR-Auto-Sync_Failover-tmp2.log sh /tmp/DR-Auto-Sync_Failover-tmp2.log grep -v cutline meta_tmp.yaml > meta.yaml
}context_status=1# 解析参数args=$(parse_args "$@")
# 调用元数据备份函数createnewmatefile $args
复制代码
调用脚本需要传入:
这个脚本会匹配 nod ip list,将需要删除的行记录,然后进行过滤写入 meta.yaml 文件中。
这步之后,在机房 2 查看集群信息,就只会查看到机房 2 的节点信息了。
使用命令:
sh test.sh tiup_base_dir=~/.tiup cluster_name=tidb-wj del_node_ip_list=172.16.201.18
复制代码
可以查看 meta.yaml 文件中相关 IP 新全部被删除成功
PD 恢复
恢复前关闭集群 & 刷新加脚本和配置
tiup cluster stop <cluster-name> -ytiup cluster reload <cluster-name> --skip-restart --force -y
复制代码
登录 PD 节点,修改脚本内容
sed -i '/pd-server/ s#pd-server#pd-server --force-new-cluster#' run_pd.sh
# 启动 pd 组件tiup cluster start <cluster-name> -R pd -y
复制代码
使用 pd-recover 命令恢复 pd 组件
./bin/pd-recover --from-old-member --endpoints=http://127.0.0.1:2379 # 指定对应的 PD 地址
复制代码
出现 recovery is successful 的提示信息即可。
过 10s 后,重启下 pd 节点
10s 无特殊意义,只是最好等一会操作
tiup cluster reload <cluster-name> -N ip:2379 -y
复制代码
调度规则恢复
中控机操作即可,这里规则可以按照实际需要进行调整
cat > /tmp/dr-auto-sync_rules_dr.json <<EOF[ { "group_id": "pd", "group_index": 0, "group_override": false, "rules": [ { "group_id": "pd", "id": "voters", "start_key": "", "end_key": "", "role": "voter", "count": 3, "location_labels": ["dc","logic","rack", "host"] } ] }]EOF
复制代码
应用规则
tiup ctl:${cluster_version} pd -u http://${pd_conn} config placement-rules rule-bundle save --in="/tmp/dr-auto-sync_rules_dr.json"
复制代码
关闭 DRAutoSync
tiup ctl:${cluster_version} pd -u http://${pd_conn} config set replication-mode majority
复制代码
以上成功后,PD 基本就 OK 了
TIKV 恢复
启动 TIKV 节点
tiup cluster start <cluster-name> -R tikv
复制代码
获取异常 tikv 的 store id
tiup ctl:${cluster_version} pd -u http://${pd_conn} store
复制代码
在线删除异常 tikv 节点
tiup ctl:${cluster_version} pd -u http://${pd_conn} unsafe remove-failed-stores <store_id1,store_id2,...>
# 如果 store id 找不到,可以使用 auto detecttiup ctl:${cluster_version} pd -u http://${pd_conn} unsafe remove-failed-stores --auto-detect
复制代码
查看进度
tiup ctl:${cluster_version} pd -u http://${pd_conn} unsafe remove-failed-stores show
复制代码
当 remove 完成后
启动整个集群
tiup cluster start <cluster-name> -y
复制代码
留存信息清理
tiup cluster prune <cluster-name>
复制代码
以上操作完成后,集群可以正常登录使用。
如果需要做高可用扩容等,按需操作即可。
评论