
Hello Readers !!! In this blog we’ll see How To Deploy MySQL Using Persistent Volume . You may found this article quite interesting while performing. Hope you’ll enjoy learning this new topic .
So let’s Get Start !!!
INTRODUCTION
This MySQL
application deployment demonstrates how to provide persistent storage for a MySQL
database.
Prerequisites
- A K8s cluster with AWS EFS CSI driver installed
- A ready to use AWS EFS file system
Steps
To deploy MySQL using Persistent Volume one should follow these steps to do so :
- Create Storage Class
- Create PVC (
PersistentVolumeClaim
) forMySQL
. - Deploy
MySQL
(Deployment and Service). - Add data to
MySQL
. - Simulate a node failure, and K8s automatically migrates
MySQL
to other nodes.
Now let’s see How we can perform the above steps one by one :
STEP-1 Create Storage Class
To create persistent volumes dynamically, you need to define a StorageClass
first. Below the the sample YAML file to create storageclass:
# storage_class.yml
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
name: efs-sc
provisioner: k8s.io/minikube-hostpath
volumeBindingMode: Immediate
parameters:
type: gp2 # This configures SSDs (recommended).
allowVolumeExpansion: true
reclaimPolicy: Retain
I will perform all this under MySQL namespace. So create namespace by running the following command:
kubectl create ns mysql

Create the above StorageClass by running the following command:
kubectl apply -f storageclass.yaml -n mysql
kubectl get storageclass -n mysql

STEP-2 Create PVC for MySQL
Now you should create pvc for mysql . The yaml file looks like :
# mysql-pvc.yml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteMany
storageClassName: efs-sc
resources:
requests:
storage: 5Gi
Create the PVC by :
kubectl create -f mysqlpvc.yaml -n mysql
kubectl get pvc -n mysql

STEP – 3 Deploy MySQL App
Now, let’s deploy the MySQL
application, the YAML definition is as follows:
# mysql-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
securityContext:
runAsUser: 1000
allowPrivilegeEscalation: false
env:
- name: MYSQL_ROOT_PASSWORD
value: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc
Deploy this deployment file by running the command:
kubectl create -f mysql-deployment.yaml -n mysql
kubectl get pods -n mysql
kubectl logs mysql-5d646c5467-9ghgd -n mysql
You we’ll see something like this :


STEP – 4 Deploy MySQL Service
Next step is to deploy the mysql service . The mysql-service.yaml looks like:
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
ports:
- port: 3306
selector:
app: mysql
Deploy this yaml by running the following command:
kubectl create -f mysql-service.yml -n mysql

STEP – 5 Simulate Node Failure
Now MySQL
is deployed to node , and we can accesses the MySQL
service through the client mysql
. Run the following command:
kubectl run -it --rm --image=mysql:5.7 --restart=Never mysql-client -- mysql
-h mysql -ppassword

Let’s update the database and add some data:

let’s shutdown the mysql database as:

STEP – 6 Verfiy Consistency
Let’s shut down the node and yes we have done .
CONCLUSION
Here in this blog we have seen How To Deploy MySQL Using Persistent Volume .Hope you learn something new and interesting . In case of any problem you can ask me .
Thankyou
Keep Learning !!!