This section demonstrates how to provide persistent storage for a MySQL database by:
-
Create PV and PVC.
-
Deploy MySQL.
-
Add data to MySQL.
-
Kubernetes automatically migrates MySQL to other nodes to simulate node downtime.
-
Verify data consistency.
Create the PV and PVC as follows:
mysql-pv.yml
mysql-pvc.yml
Create mysql-pv and mysql-pvc;
MySQL > install MySQL
PVC mysql-pvc Bound PV mysql-pv will be mounted to the mysql data directory var/lib/mysql.
MySQL > install MySQL on k8s-node2
Kubectl run it --rm --image=mysql:5.6 --restart=Never mysql-client -- mysql -h mysql-ppassword
Update database:
① Switch to the mysql database.
② Create database table my_id.
③ Insert a data.
④ Ensure that data has been written.
Shut down K8S-node2 to simulate node outage.
After some time, Kubernetes migrated MySQL to K8S-node1.
Verify data consistency:
MySQL service is restored and data is intact.
summary
In this chapter we discussed how Kubernetes manages storage resources.
EmptyDir and hostPath volumes are convenient, but not durable. Kubernetes supports volumes from multiple external storage systems.
PV and PVC separate the responsibilities of administrators from ordinary users and are more suitable for production environments. We also learned how to implement more efficient dynamic feeding with StorageClass.
Finally, we demonstrated how to use PersistentVolume in MySQL for data persistence.
Books:
1. Kubernetes in 5 Minutes every day item.jd.com/26225745440…
2. Docker Container Technology in 5 minutes a day item.jd.com/16936307278…
3. Play OpenStack in 5 Minutes every day item.jd.com/12086376.ht…