Files
easyai-ai-gateway/scripts/cluster/promote-cnpg-primary.sh
T
wangbo 7c142f5960 fix(deploy): 补齐集群备份兼容与切换前门禁
阿里云 OSS 的 S3 兼容层要求 virtual-hosted addressing,且 boto3 需要使用 Signature V2。本提交为 Barman 的归档、备份、保留与恢复统一挂载 AWS 配置,并将实际 WAL 归档和近期全备设为切换硬门禁。

同时补充 CNPG/etcdutl 固定版本安装、显式主库切换、三节点逐台停机、OSS 快照下载校验与远端临时 Secret 清理。已通过 ShellCheck、Kustomize、服务端 dry-run、迁移测试、发布脚本测试、敏感信息扫描及生产 precutover 验收。
2026-07-28 06:46:09 +08:00

46 lines
1.6 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
# shellcheck source=scripts/cluster/common.sh
source "$script_dir/common.sh"
target_primary=${1:-}
[[ $target_primary =~ ^easyai-postgres-[12]$ ]] || {
echo 'usage: promote-cnpg-primary.sh easyai-postgres-{1|2}' >&2
exit 64
}
load_cluster_env
cluster_ssh "$CLUSTER_NINGBO_HOST" bash -s -- "$target_primary" <<'REMOTE'
set -euo pipefail
target_primary=$1
kubectl='k3s kubectl'
command -v kubectl-cnpg >/dev/null
[[ $($kubectl get pod "$target_primary" -n easyai \
-o jsonpath='{.metadata.labels.cnpg\.io/cluster}') == easyai-postgres ]]
current_primary=$($kubectl get cluster easyai-postgres -n easyai \
-o jsonpath='{.status.currentPrimary}')
if [[ $current_primary != "$target_primary" ]]; then
$kubectl cnpg promote easyai-postgres "$target_primary" -n easyai
fi
for _ in $(seq 1 120); do
current_primary=$($kubectl get cluster easyai-postgres -n easyai \
-o jsonpath='{.status.currentPrimary}')
ready_instances=$($kubectl get cluster easyai-postgres -n easyai \
-o jsonpath='{.status.readyInstances}')
if [[ $current_primary == "$target_primary" && $ready_instances == 2 ]]; then
synchronous_replicas=$($kubectl exec -n easyai "$current_primary" -c postgres -- \
psql -X -U postgres -d postgres -At -c \
"SELECT count(*) FROM pg_stat_replication WHERE sync_state IN ('sync','quorum');")
if [[ $synchronous_replicas == 1 ]]; then
echo "cnpg_primary_promotion=PASS primary=$current_primary synchronous_replicas=1"
exit 0
fi
fi
sleep 3
done
echo "CNPG did not converge on target primary: $target_primary" >&2
exit 1
REMOTE