Running MySQL in a Kubernetes environment means that as soon as the container is started the Service will start using it. With Group Replication enabled this means it will start using the instance while its still ‘recovering’ . So I made this little bash script which you can use as a Readiness probe. It checks two things with one small sql query. First if its online, and second if there are 2 or more members (hopefully preventing split brain scenarios).
group_ready_check.sh: |
#Pass “password” as parameter
ip=$(ip route get 1 | awk ‘{print $NF;exit}’)
state=”$(mysql -N -B -p@1 -e “SELECT * FROM performance_schema.replication_group_members WHERE MEMBER_STATE=’ONLINE'” 2>/dev/null)”
if ([ “$(grep -c “$ip” <<< “$state”)” -ge 1 ]) && ([ “$(grep -c ‘^’ <<< “$state”)” -ge 2 ]); then
exit 0
else
exit 1
fi