From e882f496298d38a9de1072bdb531e40c7cf8f07f Mon Sep 17 00:00:00 2001 From: Shanshan Date: Thu, 7 Mar 2024 17:43:25 +0800 Subject: [PATCH] chore: update mogdb switchover with scripts (#356) --- addons/mogdb-cluster/templates/rbac.yaml | 23 +++++++--- addons/mogdb-cluster/values.schema.json | 8 ++-- addons/mogdb-cluster/values.yaml | 12 +++--- addons/mogdb/templates/swithover.yaml | 53 +++++++++++++++++++----- 4 files changed, 69 insertions(+), 27 deletions(-) diff --git a/addons/mogdb-cluster/templates/rbac.yaml b/addons/mogdb-cluster/templates/rbac.yaml index a4602eb4e..082fc24f6 100644 --- a/addons/mogdb-cluster/templates/rbac.yaml +++ b/addons/mogdb-cluster/templates/rbac.yaml @@ -1,15 +1,28 @@ +{{- include "kblib.rbac" . }} --- apiVersion: rbac.authorization.k8s.io/v1 -kind: ClusterRole +kind: Role metadata: - name: kubeblocks-switchover-pod-role + name: {{ printf "%s-switchover-role" (include "kblib.clusterName" .) }} labels: - app.kubernetes.io/instance: kubeblocks - app.kubernetes.io/name: kubeblocks + {{- include "kblib.clusterLabels" . | nindent 4 }} app.kubernetes.io/required-by: pod rules: - apiGroups: [""] resources: ["pods/exec"] verbs: ["create"] --- -{{- include "kblib.rbac" . }} \ No newline at end of file +apiVersion: rbac.authorization.k8s.io/v1 +kind: RoleBinding +metadata: + name: {{ printf "%s-switchover" (include "kblib.clusterName" .) }} + labels: + {{- include "kblib.clusterLabels" . | nindent 4 }} +roleRef: + apiGroup: rbac.authorization.k8s.io + kind: Role + name: {{ printf "%s-switchover-role" (include "kblib.clusterName" .) }} +subjects: + - kind: ServiceAccount + name: {{ printf "kb-%s" (include "kblib.clusterName" .) }} + namespace: {{ .Release.Namespace }} \ No newline at end of file diff --git a/addons/mogdb-cluster/values.schema.json b/addons/mogdb-cluster/values.schema.json index e4c5c672a..92230cc5b 100644 --- a/addons/mogdb-cluster/values.schema.json +++ b/addons/mogdb-cluster/values.schema.json @@ -27,8 +27,8 @@ "number", "string" ], - "default": 0.5, - "minimum": 0.5, + "default": 1, + "minimum": 1, "maximum": 64, "multipleOf": 0.5 }, @@ -39,8 +39,8 @@ "number", "string" ], - "default": 0.5, - "minimum": 0.5, + "default": 1, + "minimum": 1, "maximum": 1000 }, "storage": { diff --git a/addons/mogdb-cluster/values.yaml b/addons/mogdb-cluster/values.yaml index 14059a4e9..d43b6d8ed 100644 --- a/addons/mogdb-cluster/values.yaml +++ b/addons/mogdb-cluster/values.yaml @@ -8,19 +8,19 @@ version: mogdb-5.0.5 ## @param mode postgresql cluster topology mode, standalone, replication ## -mode: standalone +mode: replication ## @param replicas specify cluster replicas ## -replicas: 1 +replicas: 2 ## @param cpu ## -cpu: 0.5 +cpu: 1 ## @param memory, the unit is Gi ## -memory: 0.5 +memory: 1 ## @param requests.cpu if not set, use cpu ## @param requests.memory, if not set, use memory @@ -31,6 +31,4 @@ requests: ## @param storage size, the unit is Gi ## -storage: 20 - -customRBAC: true \ No newline at end of file +storage: 20 \ No newline at end of file diff --git a/addons/mogdb/templates/swithover.yaml b/addons/mogdb/templates/swithover.yaml index 3431f9f5c..f913e8915 100644 --- a/addons/mogdb/templates/swithover.yaml +++ b/addons/mogdb/templates/swithover.yaml @@ -18,7 +18,7 @@ spec: type: string candidate: description: | - candidate instance name(pod Name). if candidate is not empty, will promote it to primary. + candidate instance name(pod Name). if candidate is not empty, will promote it to primary. otherwise promote a randomly selected pod to primary. type: string type: object @@ -31,13 +31,44 @@ spec: image: docker.io/apecloud/kubeblocks-tools:latest imagePullPolicy: IfNotPresent command: - - sh - - /scripts/switchover.sh - volumeMounts: - - name: scripts - mountPath: /scripts - volumes: - - name: scripts - configMap: - name: mogdb-scripts - defaultMode: 0777 \ No newline at end of file + - /bin/sh + - -c + - | + set -x + # do switchover + echo "INFO: doing switchover.." + echo "INFO: candidate: ${candidate}" + kubectl exec -it ${candidate} -c mogdb -- gosu omm gs_ctl switchover + + # check if switchover successfully. + echo "INFO: start to check if switchover successfully, timeout is 60s" + executedUnix=$(date +%s) + while true; do + sleep 5 + if [ ! -z ${candidate} ]; then + # if candidate specified, only check it + role=$(kubectl get pod ${candidate} -ojson | jq -r '.metadata.labels["kubeblocks.io/role"]') + if [ "$role" == "Primary" ] || [ "$role" == "primary" ] || [ "$role" == "leader" ] || [ "$role" == "master" ]; then + echo "INFO: switchover successfully, ${candidate} is ${role}" + exit 0 + fi + else + # check if the candidate instance has been promote to primary + pods=$(kubectl get pod -l apps.kubeblocks.io/component-name=${KB_COMP_NAME},app.kubernetes.io/instance=${KB_CLUSTER_NAME} | awk 'NR > 1 {print $1}') + for podName in ${pods}; do + if [ "${podName}" != "${primary}" ];then + role=$(kubectl get pod ${podName} -ojson | jq -r '.metadata.labels["kubeblocks.io/role"]') + if [ "$role" == "Primary" ] || [ "$role" == "primary" ] || [ "$role" == "leader" ] || [ "$role" == "master" ]; then + echo "INFO: switchover successfully, ${podName} is ${role}" + exit 0 + fi + fi + done + fi + currentUnix=$(date +%s) + diff_time=$((${currentUnix}-${executedUnix})) + if [ ${diff_time} -ge 60 ]; then + echo "ERROR: switchover failed." + exit 1 + fi + done \ No newline at end of file