Skip to main content

Command Palette

Search for a command to run...

Kubernetes Advanced

Published
•3 min read•View as Markdown

Deploying MySQL database on Kubernetes with config map/secrets and attaching volumes.

Create a deployment file.

vim deployment.yml

cat deployment.yml

image

Dry run the deployment file.

kubectl apply -f deployment.yml --dry-run=client

It will run the deployment file without creating a deployment.

image

View the deployment file after fixing the errors.

cat deployment.yml

image

Create a namespace.

kubectl create namespace mysql-ns

image

Applying the deployment file.

kubectl apply -f deployment.yml

image

View the pods created by deploymwent

kubectl get pods -n mysql-ns

image

Status = CrashLoopBackOff as the container is crashing inside the pod (Mysql requires Database and password)

Create a configMaps file to store the database value.

vim configMaps.yml

cat configMaps.yml

image

Dry-run configMaps file

kubectl apply -f configMMaps.yml --dry-run=client

image

View configMaps file after fixing the errors.

cat configMaps.yml

image

Applying configMaps file

kubectl apply -f configMaps.yml

image

View configMaps

kubectl get configMaps -n mysql-ns

image

Now edit the deployment file and view it after editing.

vim deployment.yml

cat deployment.yml

image

Applying the deployment file after editing.

kubectl apply -f deployment -n mysql-ns

image

Now create a secrets file to store encoded passwords.

vim secrets.yml

image

Apply and view the status of the secrets.yml file

kubectl apply -f secrets.yml

kubectl get secrets -n mysql-ns

image

View deployment after change.

vim deployment.yml

image

Apply deployment after changes.

kubectl apply -f deployment.yml

image

View status of the pods.

kubectl get pods -n mysql-ns

image

Create persistent volume file

vim persistentVolume.yml

image

Apply persistentVolume.yml

kubectl apply -f persistentVolume.yml

image

Status of the persistent Volume

kubectl get pv -n mysql-ns

image

Create persistentVolumeClaim file

vim persistentVolumeClaim.yml

image

Applying the persistenVolumeClaim file.

kubectl apply -f persistentVolumeCLaim.yml

image

Status of persistenVolumeClaim

kubectl get pvc -n mysql-ns

image

Edit and view deployment after changes.

cat deployment.yml

image

Applying the deployment file after changes.

kubectl apply -f deployment.yml

image

Go to worker node and check the mysql container

docker ps

image

Enter into the sql container.

docker exec -it e032ff06ddc3 bash

image

Enter the password encoded in the secrets file

View database created by configMaps file

show databases;

image

Database my-db has been created.

Check the volume mounted in persistent volume file

ls

image

All the file from the container is now also stored in the worker node.

Interview Questions:

  1. Explain the deployment pattern you'd use to deploy MySQL on Kubernetes while ensuring data persistence and high availability.

    Answer: To deploy MySQL on Kubernetes with data persistence and high availability, I'd use a combination of StatefulSets and PersistentVolumeClaims (PVCs). StatefulSets provide unique network identities for each pod, and PVCs ensure data is retained even if pods are restarted or rescheduled.

  2. Describe the role of a Kubernetes StatefulSet when deploying MySQL.

    Answer: A Kubernetes StatefulSet is the ideal choice for deploying stateful applications like MySQL. It maintains stable network identities for pods, manages ordered scaling, ensures ordered pod termination, and allows data persistence across pod rescheduling.

  3. What is the purpose of using a PersistentVolumeClaim (PVC) when deploying MySQL on Kubernetes?

    Answer: A PersistentVolumeClaim defines a request for storage resources, and when used with a StorageClass, dynamically provisions PersistentVolumes for pods. For MySQL, PVCs guarantee data persistence by binding the same volume even after pod rescheduling.

  4. Explain the concept of a StorageClass and how it contributes to deploying MySQL on Kubernetes.

    Answer: A StorageClass is an abstraction that defines the storage provisioner and settings for dynamically creating PersistentVolumes. When deploying MySQL, StorageClass allows automatic provisioning of storage resources for PersistentVolumeClaims, ensuring data persistence.

More from this blog

Untitled Publication

32 posts