tringuyen | eb9eb59 | 2020-04-01 17:51:06 +0000 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | MYSQL_USER=$(kubectl exec -n $NAMESPACE_ENV \ |
| 3 | {{ include "common.fullname" . }}-0 -- printenv MYSQL_USER) |
| 4 | |
| 5 | MYSQL_PASSWORD=$(kubectl exec -n $NAMESPACE_ENV \ |
| 6 | {{ include "common.fullname" . }}-0 -- printenv MYSQL_PASSWORD) |
| 7 | |
| 8 | MYSQL_ROOT_PASSWORD=$(kubectl exec -n $NAMESPACE_ENV \ |
| 9 | {{ include "common.fullname" . }}-0 -- printenv MYSQL_ROOT_PASSWORD) |
| 10 | |
| 11 | kubectl create secret generic \ |
| 12 | '{{ include "common.fullname" . }}'-temp-upgrade-root \ |
| 13 | --from-literal=password=$MYSQL_ROOT_PASSWORD |
| 14 | |
| 15 | kubectl create secret generic \ |
| 16 | '{{ include "common.fullname" . }}'-temp-upgrade-usercred \ |
| 17 | --from-literal=login=$MYSQL_USER --from-literal=password=$MYSQL_PASSWORD |
| 18 | |
| 19 | kubectl create -f /upgrade/create-deployment.yml |
| 20 | |
| 21 | TEMP_POD=$(kubectl get pod -n $NAMESPACE_ENV --selector \ |
| 22 | app='{{ include "common.fullname" . }}' -o \ |
| 23 | jsonpath='{.items[?(@.metadata.ownerReferences[].kind=="ReplicaSet")].metadata.name}') |
| 24 | |
| 25 | CLUSTER_NO=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- \ |
| 26 | mysql --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \ |
| 27 | -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster_size';" | \ |
| 28 | awk '{print $2}') |
| 29 | |
| 30 | CLUSTER_STATE=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- \ |
| 31 | mysql --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \ |
| 32 | -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_local_state_comment';" \ |
| 33 | | awk '{print $2}') |
| 34 | |
| 35 | STS_REPLICA=$(kubectl get statefulsets -n $NAMESPACE_ENV \ |
| 36 | {{ include "common.fullname" . }} -o jsonpath='{.status.replicas}') |
| 37 | |
| 38 | DEPLOYMENT_REPLICA=$(kubectl get deployment -n $NAMESPACE_ENV \ |
| 39 | {{ include "common.fullname" . }}-upgrade-deployment -o \ |
| 40 | jsonpath='{.status.replicas}') |
| 41 | |
| 42 | while [[ ! $CLUSTER_NO == $((STS_REPLICA+DEPLOYMENT_REPLICA)) ]] \ |
| 43 | || [[ ! $CLUSTER_STATE == "Synced" ]] |
| 44 | do |
| 45 | echo "$CLUSTER_NO and $CLUSTER_STATE" |
| 46 | CLUSTER_NO=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- mysql \ |
| 47 | --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \ |
| 48 | -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_cluster_size';" \ |
| 49 | | awk '{print $2}') |
| 50 | CLUSTER_STATE=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- mysql \ |
| 51 | --skip-column-names -h{{ $.Values.service.name }} -u$MYSQL_USER \ |
| 52 | -p$MYSQL_PASSWORD -e "SHOW GLOBAL STATUS LIKE 'wsrep_local_state_comment';" \ |
| 53 | | awk '{print $2}') |
| 54 | sleep 2 |
| 55 | if [[ $CLUSTER_NO == $((STS_REPLICA+DEPLOYMENT_REPLICA)) ]] \ |
| 56 | && [[ $CLUSTER_STATE == "Synced" ]] |
| 57 | then |
| 58 | echo "The cluster has $CLUSTER_NO members and $CLUSTER_STATE state." |
| 59 | break |
| 60 | fi |
| 61 | done |
| 62 | |
| 63 | MYSQL_STATUS=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- mysqladmin \ |
| 64 | -uroot -p$MYSQL_ROOT_PASSWORD ping) |
| 65 | |
| 66 | while [[ ! $MYSQL_STATUS == "mysqld is alive" ]] |
| 67 | do |
| 68 | echo "Mariadb deployment is not ready yet." |
| 69 | sleep 2 |
| 70 | MYSQL_STATUS=$(kubectl exec -n $NAMESPACE_ENV $TEMP_POD -- mysqladmin \ |
| 71 | -uroot -p$MYSQL_ROOT_PASSWORD ping) |
| 72 | if [[ $MYSQL_STATUS == "mysqld is alive" ]] |
| 73 | then |
| 74 | echo "Mariadb deployment is ready." |
| 75 | break |
| 76 | fi |
| 77 | done |
| 78 | |
| 79 | kubectl scale statefulsets {{ include "common.fullname" . }} --replicas=0 |
| 80 | MY_REPLICA_NUMBER=$(kubectl get statefulsets -n $NAMESPACE_ENV \ |
| 81 | {{ include "common.fullname" . }} -o jsonpath='{.status.replicas}') |
| 82 | echo "The the cluster has $MY_REPLICA_NUMBER replicas." |
| 83 | |
| 84 | while [[ ! $MY_REPLICA_NUMBER == "0" ]] |
| 85 | do |
| 86 | echo "The cluster is not scaled to 0 yet. Please wait ..." |
| 87 | MY_REPLICA_NUMBER=$(kubectl get statefulsets -n $NAMESPACE_ENV \ |
| 88 | {{ include "common.fullname" . }} -o jsonpath='{.status.replicas}') |
| 89 | echo "The current status of the cluster is $MY_REPLICA_NUMBER" |
| 90 | sleep 2 |
| 91 | if [[ $MY_REPLICA_NUMBER == "0" ]] |
| 92 | then |
| 93 | break |
| 94 | fi |
| 95 | done |
| 96 | |
| 97 | for (( index=0; index<$STS_REPLICA; index+=1 )) |
| 98 | do |
| 99 | kubectl delete pvc \ |
| 100 | "{{ include "common.fullname" . }}-data-{{ include "common.fullname" . }}-$index" |
| 101 | done |