Launching your Kubernetes Cluster with Deployment
Day 32 of 90daysofdevops
Deployment in K8s
A Deployment is a high-level resource that provides declarative updates for managing a set of identical replicas of a pod. A pod is the smallest deployable unit in Kubernetes that represents a single instance of a containerized application.
Deployments use a rolling update strategy by default, which ensures that new replicas are gradually introduced and old replicas are gracefully terminated. This strategy helps to minimize downtime and ensure that the application remains available during the update process.
Use Case
The following are typical use cases for Deployments:
Create a Deployment to rollout a ReplicaSet. The ReplicaSet creates Pods in the background. Check the status of the rollout to see if it succeeds or not.
Declare the new state of the Pods by updating the PodTemplateSpec of the Deployment. A new ReplicaSet is created and the Deployment manages moving the Pods from the old ReplicaSet to the new one at a controlled rate. Each new ReplicaSet updates the revision of the Deployment.
Rollback to an earlier Deployment revision if the current state of the Deployment is not stable. Each rollback updates the revision of the Deployment.
Pause the rollout of a Deployment to apply multiple fixes to its PodTemplateSpec and then resume it to start a new rollout.
Use the status of the Deployment as an indicator that a rollout has stuck.
Clean up older ReplicaSets that you don't need anymore.
Example of NGINX Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
Components of Deployment
The key components of deployment in Kubernetes are:
Labels and Selectors: Labels are key-value pairs attached to Kubernetes objects, including pods and deployments. Selectors are used to specify criteria based on labels to identify and group related objects. Deployments use labels and selectors to manage and control the pods associated with them.
Scaling: Deployments allow you to scale your application horizontally by adjusting the number of replicas. You can manually scale up or down based on the desired load or configure autoscaling based on metrics such as CPU utilization.
Replicas: The number of replicas determines how many instances of your application should be running at any given time. It allows you to scale your application horizontally by increasing or decreasing the number of replicas.
Pod Template: A deployment specifies a pod template that defines the desired state of the pods running your application. It includes details such as the container image, environment variables, resource limits, and volumes.
Rollback: Kubernetes deployments support rollback functionality. If an update doesn't work as expected, you can roll back to the previous version of your application by specifying the revision or using the built-in rollback command.
Health Checks: Kubernetes provides liveness and readiness probes to check the health of your application. Liveness probes determine if the application is running properly, and readiness probes indicate if the application is ready to accept traffic.
Task 1: Create one Deployment file to deploy a sample todo-app on K8s using the "Auto-healing" and "Auto-Scaling" feature
Clone the repository into the master.
git clone https://github.com/Abhisek773/django-todo-cicd.git
Create an image from the Dockerfile
cat Dockerfile docker build . -t abhisek6/django-todo:latest
Check the docker image and push it into DockerHub.
docker images docker login docker push abhisek6/django-todo:latest
Now we can verify in the DockerHub that image is pushed into the repo.
Then create a Manifest file named deployment.yml where all the configuration related to the image will store. Add a deployment.yml file.
vim deployment.yml
apiVersion: apps/v1 kind: Deployment metadata: name: my-django-app-deployment labels: app: django-app spec: replicas: 2 selector: matchLabels: app: django-app template: metadata: labels: app: django-app spec: containers: - name: django-app-container image: abhisek6/django-todo:latest ports: - containerPort: 8000
Apply the deployment to your k8s (minikube) cluster by following the command
kubectl apply -f deployment.yml
We can verify the pods are running,
kubectl get pods
On the worker node, to test docker container working or not.
# Get the list of running docker container docker ps # Get into the docker container sudo docker exec -it <docker-container> bash curl -L http://127.0.0.1:8000
Now we can delete a pod by using the following command,
kubectl delete pod <pod-name>
But after deleting a pod still we get 2 pods as Auto Healing is working, so even though 1 pod is removed or deleted a new pod is replaced with the older one.
kubectl get pods
To delete a complete deployment, so that deployment is brought down we can use the following command,
kubectl delete -f deployment.yaml
Thank You,
I want to express my deepest gratitude to each and every one of you who has taken the time to read, engage, and support my journey.
Feel free to reach out to me if any corrections or add-ons are required on blogs. Your feedback is always welcome & appreciated.
~ Abhisek Moharana ๐