33# NOTE(dciabrin) we might use downward API to populate those in the future
44PODNAME=$HOSTNAME
55SERVICE=${PODNAME/ -galera-[0-9]*/ }
6+ MYSQL_SOCKET=/var/lib/mysql/mysql.sock
67
78# API server config
89APISERVER=https://kubernetes.default.svc
2425 exec & > >( cat >> /proc/1/fd/1) 2>&1
2526fi
2627
28+ # if the mysql socket is not available, mysql is either not started or
29+ # not reachable, orchestration stops here.
30+ if [ ! -e $MYSQL_SOCKET ]; then
31+ exit 0
32+ fi
33+
2734# On update, k8s performs a rolling restart, but on resource deletion,
2835# all pods are deleted concurrently due to the fact that we require
2936# PodManagementPolicy: appsv1.ParallelPodManagement for bootstrapping
@@ -44,16 +51,22 @@ if curl -s --cacert ${CACERT} --header "Content-Type:application/json" --header
4451 done
4552fi
4653
47- log " Initiating orchestrated shutdown of the local galera node"
48-
49- log " Failover service to another available galera node"
50- bash $( dirname $0 ) /mysql_wsrep_notify.sh --status failover
54+ # We now to need disconnect the clients so that when the server will
55+ # initiate its shutdown, they won't receive unexpected WSREP statuses
56+ # when running SQL queries.
57+ # Note: It is safe to do it now, as k8s already removed this pod from
58+ # the service endpoint, so client won't reconnect to it.
5159
5260log " Close all active connections to this local galera node"
5361# filter out system and localhost connections, only consider clients with a port in the host field
5462# from that point, clients will automatically reconnect to another node
5563CLIENTS=$( mysql -uroot -p${DB_ROOT_PASSWORD} -nN -e " select id from information_schema.processlist where host like '%:%';" )
56- echo -n " $CLIENTS " | tr ' \n' ' ,' | xargs mysqladmin -uroot -p${DB_ROOT_PASSWORD} kill
64+ echo -n " $CLIENTS " | tr ' \n' ' ,' | xargs -r mysqladmin -uroot -p${DB_ROOT_PASSWORD} kill
65+
66+ # At this point no clients are connected anymore.
67+ # We can finish this pre-stop hook and let k8s send the SIGTERM to the
68+ # mysql server to make it disconnect from the galera cluster and shut down.
69+ # Note: shutting down mysql here would cause the pod to finish too early,
70+ # and this pre-stop hook would shows up as 'Failed' in k8s events.
5771
58- log " Shutdown local server"
59- mysqladmin -uroot -p" ${DB_ROOT_PASSWORD} " shutdown
72+ exit 0
0 commit comments