Test Scaling
More slaves can be added to the MySQL Cluster to increase read capacity. This can be done by following command.
kubectl -n mysql scale statefulset mysql --replicas=5
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 5 servers are running.
Verify if the newly deployed slave (mysql-3) 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-3.mysql -e "SELECT * FROM test.messages"
It will show the same data that master has.
Scale down replicas to 3 by following command.
kubectl -n mysql scale statefulset mysql --replicas=3
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.
Check scale in is completed by following command.
kubectl -n mysql get pods -l app=mysql
Check data-mysql-3, data-mysql-4 PVCs still exist by following command.
kubectl -n mysql get pvc -l app=mysql
Challenge
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-3” to “Retain”. Please see Kubernetes documentation for help
Expand here to see the solution
Change the reclaim policy:
Find the PersistentVolume attached to the PersistentVolumeClaim data-mysql-3
export pv=$(kubectl -n mysql get pvc data-mysql-3 -o json | jq --raw-output '.spec.volumeName')
echo data-mysql-3 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
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-93799c6d-3fd4-11ea-94be-0aff3e98c5a0 10Gi RWO Retain Bound mysql/data-mysql-3 mysql-gp2 19m
pvc-a4a40181-3fd4-11ea-94be-0aff3e98c5a0 10Gi RWO Delete Bound mysql/data-mysql-4 mysql-gp2 19m
pvc-c3d09831-3fca-11ea-94be-0aff3e98c5a0 10Gi RWO Delete Bound mysql/data-mysql-0 mysql-gp2 89m
pvc-e17bef75-3fca-11ea-94be-0aff3e98c5a0 10Gi RWO Delete Bound mysql/data-mysql-1 mysql-gp2 88m
pvc-f22aed7c-3fca-11ea-94be-0aff3e98c5a0 10Gi RWO Delete Bound mysql/data-mysql-2 mysql-gp2 88m
Now, if you delete the PersistentVolumeClaim data-mysql-3, 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-3 and data-mysql-4 with following commands.
kubectl -n mysql delete pvc data-mysql-3
kubectl -n mysql delete pvc data-mysql-4