Skip to content

Commit

Permalink
chore: update mogdb switchover with scripts (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
shanshanying authored Mar 7, 2024
1 parent 8a02472 commit e882f49
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 27 deletions.
23 changes: 18 additions & 5 deletions addons/mogdb-cluster/templates/rbac.yaml
Original file line number Diff line number Diff line change
@@ -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" . }}
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 }}
8 changes: 4 additions & 4 deletions addons/mogdb-cluster/values.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
"number",
"string"
],
"default": 0.5,
"minimum": 0.5,
"default": 1,
"minimum": 1,
"maximum": 64,
"multipleOf": 0.5
},
Expand All @@ -39,8 +39,8 @@
"number",
"string"
],
"default": 0.5,
"minimum": 0.5,
"default": 1,
"minimum": 1,
"maximum": 1000
},
"storage": {
Expand Down
12 changes: 5 additions & 7 deletions addons/mogdb-cluster/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,6 +31,4 @@ requests:

## @param storage size, the unit is Gi
##
storage: 20

customRBAC: true
storage: 20
53 changes: 42 additions & 11 deletions addons/mogdb/templates/swithover.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
- /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

0 comments on commit e882f49

Please sign in to comment.