Test Scaling
More slaves can be added to the MySQL Cluster to increase read capacity. This can be done by following command.
kubectl scale statefulset mysql --replicas=5
You can see the message that statefulset “mysql” scaled.
Watch the progress of ordered and graceful scaling.
kubectl get pods -l app=mysql -w
It may take few minutes to launch all the pods.
Press Ctrl+C to stop watching.
Open another terminal to check loop if you closed it.
kubectl 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 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 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 get pods -l app=mysql
Check 2 PVCs(data-mysql-3, data-mysql-4) still exist by following command.
kubectl 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 PVC “data-mysql-3” to “Retain”. Please see Kubernetes documentation for help
Expand here to see the solution
Change the reclaim policy:
kubectl patch pv <your-pv-name> -p '{"spec":{"persistentVolumeReclaimPolicy":"Retain"}}'
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 <your-pv-name> -p '{"spec":{"persistentVolumeReclaimPolicy":"Delete"}}'
Delete data-mysql-3, data-mysql-4 by following command.
kubectl delete pvc data-mysql-3
kubectl delete pvc data-mysql-4