Deploying MySQL on Kubernetes with NFS Persistent Storage
This tutorial walks through pulling the MySQL image, creating NFS‑backed PersistentVolume and PersistentVolumeClaim, deploying MySQL on Kubernetes, verifying the pod, inserting sample data, and confirming the data is stored on the NFS server.
This guide demonstrates how to deploy a MySQL 8.0.22 instance on a Kubernetes cluster using an NFS server for persistent storage.
First, pull the MySQL image on each node:
[root@node2 ~]# docker pull mysql:8.0.22 [root@node1 ~]# docker pull mysql:8.0.22
Create a PersistentVolume definition (nfs-pv.yaml) that points to the shared NFS directory and apply it:
[root@master ~]# kubectl apply -f nfs-pv.yaml persistentvolume/mysql created
Next, create a PersistentVolumeClaim (nfs-pvc.yaml) and apply it:
[root@master ~]# kubectl apply -f nfs-pvc.yaml persistentvolumeclaim/mysql created
Prepare the MySQL deployment manifest (mysql.yaml) that uses the PVC for storage:
apiVersion: apps/v1 metadata: name: mysql spec: selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - image: mysql:8.0.22 name: mysql env: - name: MYSQL_ROOT_PASSWORD value: 1234 volumeMounts: - name: mysql-storage mountPath: /var/lib/mysql volumes: - name: mysql-storage persistentVolumeClaim: claimName: mysql-pvc
Apply the deployment:
[root@master ~]# kubectl apply -f mysql.yaml
Check that the MySQL pod is running:
[root@master ~]# kubectl get pod NAME READY STATUS RESTARTS AGE mysql-6b8564f498-wd4m4 1/1 Running 0 44s nginx-offi 1/1 Running 0 2d23h
Enter the MySQL pod and create a test table, insert a row, and query it:
[root@master ~]# kubectl exec -it mysql-6b8564f498-wd4m4 -- mysql -uroot -p1234 mysql> create table student( stu_id int auto_increment, name char(29) not null, age int not null, register_date date, primary key (stu_id) ); Query OK, 0 rows affected (0.14 sec) mysql> insert into student(name,age) values("sfqd",20); Query OK, 1 row affected (0.01 sec) mysql> select * from student; +--------+------+-----+---------------+ | stu_id | name | age | register_date | +--------+------+-----+---------------+ | 1 | sfqd | 20 | NULL | +--------+------+-----+---------------+ 1 row in set (0.00 sec)
Finally, verify that the data files are present on the NFS server:
[root@bogon data]# ll
Practical DevOps Architecture
Hands‑on DevOps operations using Docker, K8s, Jenkins, and Ansible—empowering ops professionals to grow together through sharing, discussion, knowledge consolidation, and continuous improvement.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.