Kubernetes Advanced
Deploying MySQL database on Kubernetes with config map/secrets and attaching volumes.
Create a deployment file.
vim deployment.yml
cat deployment.yml
Dry run the deployment file.
kubectl apply -f deployment.yml --dry-run=client
It will run the deployment file without creating a deployment.
View the deployment file after fixing the errors.
cat deployment.yml
Create a namespace.
kubectl create namespace mysql-ns
Applying the deployment file.
kubectl apply -f deployment.yml
View the pods created by deploymwent
kubectl get pods -n mysql-ns
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
Dry-run configMaps file
kubectl apply -f configMMaps.yml --dry-run=client
View configMaps file after fixing the errors.
cat configMaps.yml
Applying configMaps file
kubectl apply -f configMaps.yml
View configMaps
kubectl get configMaps -n mysql-ns
Now edit the deployment file and view it after editing.
vim deployment.yml
cat deployment.yml
Applying the deployment file after editing.
kubectl apply -f deployment -n mysql-ns
Now create a secrets file to store encoded passwords.
vim secrets.yml
Apply and view the status of the secrets.yml file
kubectl apply -f secrets.yml
kubectl get secrets -n mysql-ns
View deployment after change.
vim deployment.yml
Apply deployment after changes.
kubectl apply -f deployment.yml
View status of the pods.
kubectl get pods -n mysql-ns
Create persistent volume file
vim persistentVolume.yml
Apply persistentVolume.yml
kubectl apply -f persistentVolume.yml
Status of the persistent Volume
kubectl get pv -n mysql-ns
Create persistentVolumeClaim file
vim persistentVolumeClaim.yml
Applying the persistenVolumeClaim file.
kubectl apply -f persistentVolumeCLaim.yml
Status of persistenVolumeClaim
kubectl get pvc -n mysql-ns
Edit and view deployment after changes.
cat deployment.yml
Applying the deployment file after changes.
kubectl apply -f deployment.yml
Go to worker node and check the mysql container
docker ps
Enter into the sql container.
docker exec -it e032ff06ddc3 bash
Enter the password encoded in the secrets file
View database created by configMaps file
show databases;
Database my-db has been created.
Check the volume mounted in persistent volume file
ls
All the file from the container is now also stored in the worker node.
Interview Questions:
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.
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.
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.
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.




