π Introduction:
In this blog, we will explore deployment in Kubernetes and how it works.
What is Deployment in Kubernetes?
In Kubernetes, a Deployment is an object that provides declarative updates to manage application deployments. It defines the desired state of the application and handles the creation, scaling, and updating of the application's replicas. Deployments are a key component of managing containerized applications in a Kubernetes cluster.
A Deployment ensures that the desired number of replicas of an application is running and manages the lifecycle of those replicas. It allows for rolling updates, rollback to previous versions, and scaling of the application replicas based on the defined configuration.
Deployments are often used to manage stateless applications, where each instance of the application is independent of others. They are particularly useful in scenarios where high availability, fault tolerance, and easy scaling are required.
Benefits of Using Deployments in Kubernetes
Using Deployments in Kubernetes offers several benefits:
Ease of Management: Deployments provide a declarative way to define and manage the state of applications. You can specify the desired state of your application, and Kubernetes takes care of creating, updating, and scaling the replicas to match that state.
Rolling Updates and Rollbacks: Deployments support rolling updates, allowing you to update your application gradually without downtime. If an update introduces issues, you can easily roll back to a previous version of the application.
Scalability: Deployments enable easy scaling of your application by simply adjusting the number of replicas. This allows you to handle increased traffic or load by adding more instances of the application.
High Availability: Deployments ensure that the desired number of replicas are always running. If a replica fails, Kubernetes automatically replaces it to maintain the desired state and ensure high availability.
Self-Healing: Kubernetes continuously monitors the health of application replicas managed by a Deployment. If a replica becomes unhealthy or fails, Kubernetes automatically replaces it with a new one, ensuring the overall health of the application.
πΌTask: 1
Create one Deployment file to deploy a sample todo-app on K8s using the "Auto-healing" and "Auto-Scaling" feature
Here are the detailed steps to create a Deployment file to deploy a sample todo-app on Kubernetes with Auto-healing and Auto-scaling features:
- Clone the django-todo-cicd repository from Github.
git clone https://github.com/sutish/django-notes-app.git
- Build the Docker image
docker build -t django-todo-cicd .
- Run the Docker container
docker run -d -p 8000:8000 django-todo-cicd:latest
- Verify that the application is running
docker ps
Create a new directory called k8s in the same directory where the django-todo-cicd repository is located.
Push the Docker image to your Dockerhub account.
docker tag django-todo-cicd:latest sutish/django-todo-cicd:latest
docker login
docker push sutish/django-todo-cicd:latest
- Kill the container that we created previously
docker kill 417159cf5930
- Create a new pod.yaml file. Copy the sample pod.yaml configuration from the official Kubernetes documentation, then make the changes as required.
apiVersion: v1
kind: Pod
metadata:
name: todo-pod
spec:
containers:
- name: todo-pod
image: sutish/django-todo-cicd:latest
ports:
- containerPort: 8000
- Apply the pod.yaml file
kubectl apply -f pod.yaml
- Verify the pod is running
kubectl get pods
- Create a deployment.yaml file. Copy the sample deployment.yaml configuration from the official Kubernetes documentation, then make the changes as required.
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-deployment
labels:
app: todo-app
spec:
replicas: 2
selector:
matchLabels:
app: todo-app
template:
metadata:
labels:
app: todo-app
spec:
containers:
- name: todo-app
image: sutish/django-todo-cicd:latest
ports:
- containerPort: 8000
- Apply the deployment.yaml file
kubectl apply -f deployment.yaml
This will create the deployment and start the specified number of replicas.
- Check the status of the deployment and its replicas
kubectl get deployments
kubectl get pods
- To demonstrate the auto-healing feature, kill one of the running pods
kubectl delete pod todo-deployment-6d86879976-5859r
Kubernetes will automatically create a new pod to replace the deleted one.
- To demonstrate the auto-scaling feature, increase the number of replicas in the deployment
kubectl scale deployment todo-deployment --replicas=4
π Conclusion :
In this blog, we create one deployment file to deploy a sample todo-app on K8s & demonstrate the Auto-healing and Auto-Scaling feature. In the next blog, we will cover some advanced topics.
Contact me on Linkedin π€
Check out my GitHub for more resources π