Welcome back, to my DevOps blog series!
In my previous blog post, we delved into the fascinating world of Kubernetes with Minikube, exploring its basics and understanding how it can transform your development process. If you missed that post, I highly recommend checking it out to get a solid foundation for what we're about to cover today.
You can check my previous blog here: Launching your First Kubernetes Cluster with Nginx running
In today's blog, we're going to explore an important topic: launching your Kubernetes cluster using Deployment. This concept is crucial in Kubernetes as it allows you to define how an application behaves, how it scales, and how updates are handled, all without causing any interruptions. So, let's get started and learn more!
What is Deployment in Kubernetes?
In Kubernetes, Deployment is a tool that lets you easily control one or more Pods. A Pod, in simpler terms, is like a task or process running on your Kubernetes system.
Deployments are handy because they let you define important aspects of your application's life cycle. You can specify things like what images your app uses, how many identical copies of your task should run (replicas), and how to handle updates seamlessly.
A deployment offers a setup for managing updates for Pods and Replica Sets.
In case the node hosting an instance goes offline or the Pod is removed, the deployment controller steps in and replaces it. This automatic process acts as a self-healing mechanism, ensuring continuity even during machine failures or maintenance activities.
Task 01
Create one Deployment file to deploy a sample todo-app on K8s using the "Auto-healing" and "Auto-Scaling" feature
Follow these steps:
Prepare Your Environment:
Ensure you have Kubernetes installed and configured on your system. You can refer to my previous blogs on Kubernetes with Minikube for guidance.
You can check my previous blog here: Launching your First Kubernetes Cluster with Nginx running
Or follow by using the following code
# First all update system and install Docker sudo apt-get update sudo apt-get install docker.io -y sudo usermod -aG docker $USER && newgrp docker # To install Minikube & Kubectl curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 sudo install minikube-linux-amd64 /usr/local/bin/minikube sudo snap install kubectl --classic minikube start --driver=docker
Clone this repository to your computer: Django-Todo-App
git clone https://github.com/gawandekgaurav/django-todo-cicd.git
Next we docker build by going inside the repository, we use the command
sudo docker build . -t gawandekgaurav/django-todo:latest
Next, we push our image to DockerHub, by using the following command
docker push gawandekgaurav/django-todo:latest
Write a Deployment Configuration File
Create a YAML file (
deployment.yaml
) containing your Deployment configuration. Define the desired state of your application, including the container image, number of replicas, and other necessary details.apiVersion: apps/v1 kind: Deployment metadata: name: todo-app spec: replicas: 3 # number of replicas selector: matchLabels: app: todo-app template: metadata: labels: app: todo-app spec: containers: - name: todo-app image: gawandekgaurav/django-todo ports: - containerPort: 8000
Explanation of deployment.yml:
apiVersion: Indicates the version of the Kubernetes API to be used.
kind: Identifies the resource type as a Deployment.
metadata: Contains essential information about the Deployment, including its name.
spec.replicas: Specifies the desired number of replicas (pods) to be maintained, forming the basis for auto-scaling.
spec.Selector: Defines how the Deployment selects the pods to manage, based on specified labels.
spec.template: Describes the pod template used for creating new pods.
spec.template.metadata.labels: Specifies the labels to be applied to the pods.
spec.template.spec.containers: Specifies the containers within the pod, including details like the image and ports.
Apply the Deployment
Apply the configuration file using the
kubectl apply -f deployment.yaml
command. This will create the Deployment and start the specified number of replicas.kubectl apply -f my-app-deployment.yaml
Verify Your Deployment
Check the status of your Deployment using
kubectl get deployments
and ensure that the desired number of replicas are running.kubectl get deployments
Validate the Functionality
To test the auto-healing feature, we can intentionally remove one of the pods to confirm its functionality. Then run
kubectl get pods
code to test if the pods are back and running.
Congratulations! You’ve successfully launched your Kubernetes cluster with Deployment.
Conclusion
I hope this blog post has been insightful and helpful in your Kubernetes journey. Deployments are a fundamental building block in Kubernetes that empower you to manage your applications effectively.
If you have any questions or need further assistance, feel free to connect with me on LinkedIn. I’d love to hear about your experiences with Kubernetes and Deployment. Your feedback is invaluable, so please drop your comments and thoughts below.
Thank you for reading, and happy learning!