Deploy MySQL on Kubernetes
I’m a hands-on software engineer with over 18 years of experience solving real-world problems with code. I’ve spent most of that time building web applications, backend systems, and automation tools — often using Python, Django, REST, and a healthy mix of SQL and shell scripts on Linux.
Along the way, I’ve also picked up frontend work with Angular and React, and built infrastructure using Docker, Kubernetes, AWS, and Terraform. I wouldn’t call myself a DevOps engineer, but I do believe in owning the full stack — from writing the API to making sure it runs smoothly in production.
I’ve worked in all kinds of teams — large, small, remote, distributed, fast-paced, slow-paced. For the last few years, I’ve been freelancing, which has been both freeing and demanding in the best possible way. It’s pushed me to keep learning, stay sharp, and step outside my comfort zone. I love the mix of flexibility and challenge it brings.
Here is part two of a series of lab tasks from KodeKloud for Kubernetes level 4. Master blog listing all parts can be seen here. Make sure to follow setup instructions for minikube on your PC.
Problem with Inline Solutions
A new MySQL server needs to be deployed on Kubernetes cluster. The Nautilus DevOps team was working on to gather the requirements. Recently they were able to finalize the requirements and shared them with the team members to start working on it. Below you can find the details:
- Create a PersistentVolume
mysql-pv, its capacity should be250Mi, set other parameters as per your preference.
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv # Make sure matches with requirement
labels:
type: local
spec:
storageClassName: standard
capacity:
storage: 250Mi # important
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
persistentVolumeReclaimPolicy: Retain
- Create a PersistentVolumeClaim to request this PersistentVolume storage. Name it as mysql-pv-claim and request a 250Mi of storage. Set other parameters as per your preference.
apiVersion: v1
kind: PersistentVolumeClaim # Important, the type of object
metadata:
name: mysql-pv-claim # Again, must match with spec
labels:
app: mysql-app
spec:
storageClassName: standard
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 250Mi # Spec!
Create a deployment named
mysql-deployment, use any mysql image as per your preference. Mount the PersistentVolume at mount path/var/lib/mysql.Create a NodePort type service named mysql and set nodePort to 30007.
Create a secret named
mysql-root-passhaving a key pair value, where key ispasswordand its value isYUIidhb667, create another secret namedmysql-user-passhaving some key pair values, where frist key isusernameand its value iskodekloud_cap, second key ispasswordand value isksH85UJjhb, create one more secret namedmysql-db-url, key name isdatabaseand value iskodekloud_db2Define some Environment variables within the container:
a) name:
MYSQL_ROOT_PASSWORD, should pick value fromsecretKeyRefname:mysql-root-passand key:passwordb) name:
MYSQL_DATABASE, should pick value fromsecretKeyRefname:mysql-db-urland key:databasec) name:
MYSQL_USER, should pick value fromsecretKeyRefname:mysql-user-passkey:usernamed) name:
MYSQL_PASSWORD, should pick value fromsecretKeyRefname:mysql-user-passand key:password
kubectl create deployment mysql-deployment \
--dry-run=client --image=mysql:5.6 \
-o yaml > deployment.yaml
Edit deployment.yaml to include all requirements in sections listed above. Final YAML specification can be found here.
Make sure to create secrets as listed in #5 above before applying deployment.
$ kubectl create secret generic mysql-root-pass \
--from-literal=password=YUIidhb667
$ kubectl create secret generic mysql-user-pass \
--from-literal=username=kodekloud_cap \
--from-literal=password=ksH85UJjhb
$ kubectl create secret generic mysql-db-url \
--from-literal=database=kodekloud_db2
That's it. Now you just need to apply the deployment and verify.
$ kubectl apply -f deployment.yaml
$ kubectl get deploy
$ kubectl get pods
Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster.

