More followers can be added to the MySQL Cluster to increase read capacity. This can be done by running the following command.
kubectl -n mysql scale statefulset mysql --replicas=3
You can see the message that StatefulSet
“mysql” scaled.
Watch the progress of ordered and graceful scaling.
kubectl -n mysql rollout status statefulset mysql
It may take few minutes to launch all the pods.
Open another terminal to check loop if you closed it.
kubectl -n mysql run mysql-client-loop --image=mysql:5.7 -i -t --rm --restart=Never --\
bash -ic "while sleep 1; do mysql -h mysql-read -e 'SELECT @@server_id,NOW()'; done"
You will see 3 servers are running.
Verify if the newly deployed follower (mysql-2) have the same data set by following command.
kubectl -n mysql run mysql-client --image=mysql:5.7 -i -t --rm --restart=Never --\
mysql -h mysql-2.mysql -e "SELECT * FROM test.messages"
It will show the same data that the leader has.
Scale down replicas to 2 by running the following command.
kubectl -n mysql scale statefulset mysql --replicas=2
You can see StatefulSet “mysql” scaled
Note that scale in doesn’t delete the data or PVCs attached to the pods. You have to delete them manually.
kubectl -n mysql get pods -l app=mysql
Check data-mysql-2
PVCs still exist by following command.
kubectl -n mysql get pvc -l app=mysql
By default, deleting a PersistentVolumeClaim
will delete its associated persistent volume. What if you wanted to keep the volume?
Change the reclaim policy of the PersistentVolume
associated with PersistentVolumeClaim
called “data-mysql-2” to “Retain”. Please see Kubernetes documentation for help
Change the reclaim policy:
Find the PersistentVolume attached to the PersistentVolumeClaim data-mysql-2
export pv=$(kubectl -n mysql get pvc data-mysql-2 -o json | jq --raw-output '.spec.volumeName')
echo data-mysql-2 PersistentVolume name: ${pv}
Now update the ReclaimPolicy
kubectl -n mysql patch pv ${pv} -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'
Verify the ReclaimPolicy with this command.
kubectl get persistentvolume
Now, if you delete the PersistentVolumeClaim data-mysql-2, you can still see the EBS volume in your AWS EC2 console, with its state as “available”.
Let’s change the reclaim policy back to “Delete” to avoid orphaned volumes:
kubectl patch pv ${pv} -p '{"spec":{"persistentVolumeReclaimPolicy":"Delete"}}'
unset pv
Delete data-mysql-2
with following commands.
kubectl -n mysql delete pvc data-mysql-2