github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/deploy/mongodb/templates/scriptstemplate.yaml (about) 1 apiVersion: v1 2 kind: ConfigMap 3 metadata: 4 name: mongodb-scripts 5 labels: 6 {{- include "mongodb.labels" . | nindent 4 }} 7 data: 8 mongos-setup.sh: |- 9 #!/bin/sh 10 11 PORT=27018 12 CONFIG_SVR_NAME=$KB_CLUSTER_NAME"-configsvr" 13 DOMAIN=$CONFIG_SVR_NAME"-headless."$KB_NAMESPACE".svc.cluster.local" 14 mongos --bind_ip_all --configdb $CONFIG_SVR_NAME/$CONFIG_SVR_NAME"-0."$DOMAIN:$PORT,$CONFIG_SVR_NAME"-1."$DOMAIN:$PORT,$CONFIG_SVR_NAME"-2."$DOMAIN:$PORT 15 replicaset-setup.sh: |- 16 {{- .Files.Get "scripts/replicaset-setup.tpl" | nindent 4 }} 17 switchover-check-role.sh: |- 18 #!/bin/sh 19 check_role() { 20 CLIENT=`which mongosh>/dev/null&&echo mongosh||echo mongo` 21 local role=$($CLIENT --quiet --eval "rs.isMaster().ismaster" "$1" --username "$USERNAME" --password "$PASSWORD") 22 if [ "${role}" = "true" ]; then 23 echo "Primary" 24 else 25 echo "Secondary" 26 fi 27 } 28 switchover-verify.sh: |- 29 #!/bin/sh 30 verify() { 31 count=0 32 while true; do 33 local candidate_role_after_switchover=$(check_role "$CANDIDATE_URI") 34 if [ "${candidate_role_after_switchover}" = "Primary" ]; then 35 echo "switchover successfully." 36 break 37 else 38 count=$((count+1)) 39 if [ ${count} -ge 10 ]; then 40 echo "Failed to switch over to primary after 10 attempts" 41 break 42 else 43 echo "Attempt $count: $candidate_role_after_switchover" 44 sleep 3 45 fi 46 fi 47 done 48 } 49 switchover-with-candidate.sh: |- 50 #!/bin/sh 51 URI="mongodb://$KB_CONSENSUS_LEADER_POD_FQDN:27017" 52 CANDIDATE_URI="mongodb://$KB_SWITCHOVER_CANDIDATE_FQDN:27017" 53 USERNAME=$MONGODB_ROOT_USER 54 PASSWORD=$MONGODB_ROOT_PASSWORD 55 CLIENT=`which mongosh>/dev/null&&echo mongosh||echo mongo` 56 CANDIDATE_HOST="$KB_SWITCHOVER_CANDIDATE_FQDN.$KB_NAMESPACE.svc.cluster.local:27017" 57 . /scripts/switchover-check-role.sh 58 . /scripts/switchover-verify.sh 59 switchover() { 60 echo "Checking current role..." 61 local current_leader_role=$(check_role "$URI") 62 if [ "${current_leader_role}" = "Secondary" ]; then 63 echo "Current instance role is not the primary, can not do switchover" 64 exit 1 65 fi 66 echo "Switchover to new primary: $CANDIDATE_HOST" 67 $CLIENT --quiet --eval "conf=rs.config();conf.members.forEach(member => member.priority = 1);const candidateHost = '$CANDIDATE_HOST';const member = conf.members.find(member => member.host === candidateHost);if (member) {member.priority = 2;};rs.reconfig(conf)" "$URI" --username "$USERNAME" --password "$PASSWORD" 68 echo "Checking candidate instance role after switchover..." 69 verify 70 } 71 72 switchover 73 echo "Switchover complete" 74 switchover-without-candidate.sh: |- 75 #!/bin/sh 76 77 URI="mongodb://$KB_CONSENSUS_LEADER_POD_FQDN:27017" 78 USERNAME=$MONGODB_ROOT_USER 79 PASSWORD=$MONGODB_ROOT_PASSWORD 80 CLIENT=`which mongosh>/dev/null&&echo mongosh||echo mongo` 81 OLD_LEADER_HOST="$KB_CONSENSUS_LEADER_POD_FQDN.$KB_NAMESPACE.svc.cluster.local:27017" 82 . /scripts/switchover-check-role.sh 83 . /scripts/switchover-verify.sh 84 switchover() { 85 echo "Checking current role..." 86 local current_leader_role=$(check_role "$URI") 87 if [ "${current_leader_role}" = "Secondary" ]; then 88 echo "Current instance role is not the primary, can not do switchover" 89 exit 1 90 fi 91 echo "Switchover without candidate, try to select a new primary randomly ..." 92 local CANDIDATE_HOST=$($CLIENT --quiet --eval "conf=rs.config();const candidateHost = '$OLD_LEADER_HOST';const member=conf.members.find(member => member.host !== candidateHost);if (member) {console.log(member.host)}" "$URI" --username "$USERNAME" --password "$PASSWORD") 93 local CANDIDATE_URI="mongodb://$CANDIDATE_HOST" 94 if [ -z "$CANDIDATE_HOST" ]; then 95 echo "Failed to select a new candidate primary, exit" 96 exit 1 97 fi 98 echo "Switchover to new primary: $CANDIDATE_HOST" 99 $CLIENT --quiet --eval "conf=rs.config();conf.members.forEach(member => member.priority = 1);const candidateHost = '$CANDIDATE_HOST';const member = conf.members.find(member => member.host === candidateHost);if (member) {member.priority = 2;};rs.reconfig(conf)" "$URI" --username "$USERNAME" --password "$PASSWORD" 100 echo "Checking candidate instance role after switchover..." 101 verify 102 } 103 104 switchover 105 echo "Switchover without candidate complete"