Day32-Launching your Kubernetes Cluster with Deployment/90Days of DevOps Challenge
What is Deployment in K8s?
In Kubernetes (K8s), deployment is an object that provides declarative updates and management for your application containers. It is one of the core resources in Kubernetes and is used to define the desired state of your application, including the number of replicas (instances) of your application that should be running.
Deployments use a rolling update strategy by default, which means that when you update the deployment with a new version of your application, Kubernetes gradually replaces the old instances with the new ones, ensuring that there is no downtime during the update process. This strategy enables you to perform updates without impacting the availability of your application.
Features of Deployment:
Deployments in Kubernetes offer several key features that help manage the deployment and lifecycle of application containers.
Declarative Updates: Deployments allow you to define the desired state of your application and its configuration in a declarative manner. You specify the desired number of replicas, container images, environment variables, ports, and other parameters. Kubernetes ensures that the desired state is maintained, automatically handling updates and changes.
Rolling Updates and Rollbacks: Deployments enable rolling updates, which means that when you update the deployment with a new version of your application, Kubernetes gradually replaces the old instances with the new ones. This strategy ensures there is no downtime during the update process. If an issue arises, you can easily roll back to a previous version of the deployment.
Scaling: Deployments allow you to scale the number of replicas (instances) of your application up or down manually or automatically. You can specify the desired number of replicas, and Kubernetes will adjust the running instances accordingly. Autoscaling based on CPU utilization or custom metrics is also possible.
Pod Template Spec: Deployments use a pod template specification to define the configuration of the pods running your application. This includes container images, volumes, resource requirements, environment variables, and more. The pod template spec allows for flexibility and customization of your application's runtime environment.
Service Discovery and Load Balancing: Deployments are typically associated with a Kubernetes Service object, which provides a stable network endpoint to access the application. The service acts as a load balancer, distributing traffic across the replicas of the deployment.
Health Checks: Deployments allow you to define health checks for your application containers. Kubernetes can periodically probe the containers and take action based on the health check results, such as restarting or terminating unhealthy instances.
Pause and Resume: Deployments provide the ability to pause and resume the updating process. This feature can be useful when you want to temporarily halt the deployment process or perform manual interventions before proceeding with the update.
Configuration Management: Deployments support managing application configuration through Config
Maps and Secrets: You can inject configuration data into your application containers using these mechanisms without modifying the container images.
These features make deployments a powerful tool for managing the lifecycle of your applications in Kubernetes, providing flexibility, scalability, and robustness.
Kubernetes Deployments Command:
a) Create a Deployment:
kubectl create deployment <deployment-name> --image=<container-image>
b) Get Deployments:
kubectl get deployments
c) Describe a Deployment:
kubectl describe deployment <deployment-name>
d) Update a Deployment (Rolling Update):
kubectl set image deployment/<deployment-name> <container-name>=<new-container-image>
e) Rollback a Deployment:
kubectl rollout undo deployment/<deployment-name>
f) Scale a Deployment:
kubectl scale deployment <deployment-name> --replicas=<new-replica-count>
g) Pause/Resume a Deployment:
kubectl rollout pause deployment/<deployment-name>
kubectl rollout resume deployment/<deployment-name>
h) Rolling Restart (Force Restart):
kubectl rollout restart deployment/<deployment-name>
i) Delete a Deployment:
kubectl delete deployment <deployment-name>
j) Exposing a Deployment via a Service:
kubectl expose deployment <deployment-name> --type=NodePort --port=<port>
Tasks:
Create one Deployment file to deploy a sample
todo-app on K8s using the "Auto-healing" and "Auto-Scaling" feature.
With this Deployment file, Kubernetes will create two replicas of the todo-app and ensure they are continuously running. If any pod fails or terminates, Kubernetes will automatically recreate a new pod to maintain the desired number of replicas (auto-healing). Additionally, you can modify the replicas field to scale the application up or down based on the demand.
apiVersion: apps/v1
kind: Deployment
metadata:
name: todo-app #The Deployment is named "todo-app-deployment".
labels:
app: todo
spec:
replicas: 2 # want 2 replicas of the todo-app.
selector:
matchLabels:
app: todo
template:
metadata:
labels:
app: todo
spec:
containers:
- name: todo
image: chandanahdam/todoapp #image will pull from docker hub
ports:
- containerPort: 3000
create a deployment.yaml file . Apply and verify that deployment is created successfully.
kubectl apply -f Deployment.yaml #you will get conformation message kubectl get deployments
Testing Auto-Healing:
To validate the functionality of the auto-healing feature, remove one of the pods.
Subsequently, Kubernetes will automictically generate a new pod to substitute the removed instance.
kubectl delete pod <pod_name>
To set the CPU utilization target for auto-scaling in a Kubernetes Deployment, you can use the
kubectl autoscale
command.kubectl autoscale deployment todo-app-deployment --cpu-percent=50 --min=2 --max=5 kubectl get hpa
This command will enable auto-scaling for your Deployment based on CPU utilization, with the specified parameters.
✨✨Successfully created your first deployment on Kubernetes Cluster.
Thank you for 📖reading my blog, 👍Like it and share it 🔄 with your friends.
Happy learning !!!