Skip to main content

Command Palette

Search for a command to run...

Deploy MySQL on Kubernetes

Published
3 min read
R

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:

  1. Create a PersistentVolume mysql-pv, its capacity should be 250Mi, 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
  1. 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!
  1. Create a deployment named mysql-deployment, use any mysql image as per your preference. Mount the PersistentVolume at mount path /var/lib/mysql.

  2. Create a NodePort type service named mysql and set nodePort to 30007.

  3. Create a secret named mysql-root-pass having a key pair value, where key is password and its value is YUIidhb667, create another secret named mysql-user-pass having some key pair values, where frist key is username and its value is kodekloud_cap, second key is password and value is ksH85UJjhb, create one more secret named mysql-db-url, key name is database and value is kodekloud_db2

  4. Define some Environment variables within the container:

    a) name: MYSQL_ROOT_PASSWORD, should pick value from secretKeyRef name: mysql-root-pass and key: password

    b) name: MYSQL_DATABASE, should pick value from secretKeyRef name: mysql-db-url and key: database

    c) name: MYSQL_USER, should pick value from secretKeyRef name: mysql-user-pass key: username

    d) name: MYSQL_PASSWORD, should pick value from secretKeyRef name: mysql-user-pass and key: password

💡
Create a bare skeleton specification using dry-run as shown below and edit as per requirements in all points except #5 above.
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.

More from this blog

Rajesh

25 posts