![]() |
VOOZH | about |
A Kubernetes Deployment is a high level resource used to manage and scale applications while ensuring they remain in the desired state. With a Deployment, you can:
Think of it as both a blueprint and controller for Pods that simplifies application management in Kubernetes. A Deployment does not manage Pods directly. Instead, it manages ReplicaSets, and ReplicaSets manage Pods.
Kubernetes Deployments are widely used to manage application lifecycles in a cluster. Typical use cases include:
It mainly consists of three components:
We will be using Minikube to use Kubernetes on our local machine. The Deployment configuration file for Nginx will be:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
resources:
limits:
memory: "128Mi"
cpu: "500m"
ports:
- containerPort: 80
Now first open the directory where you have created nginx.yaml file in your terminal and create deployment using the command:
kubectl apply -f nginx.yaml👁 kubectl applyNow check the status of your deployment if it is ready or not by running the command
kubectl get allHence we have successfully deployed created a deployment for Nginx.
To update a Kubernetes deployment we can simply update its config file using 2 methods:
Method 1: Using the kubectl edit command from the terminal
kubectl edit deployment deployment-nameNow you can edit the deployment configuration by pressing "i" for inserting and after editing it you can just press the escape key and then ":wq" to save your changes and exit.
👁 fileMethod 2: Updating Configuration Directly
You can open your config file in an IDE like VS Code and edit the config there and apply the config by using the command
kubectl apply -f deployment_config.yamlLet's say in this case we are updating the container port from 80 to 800.
In kubernetes deployment, you can revert back to the previous version of the application if you find any bugs in the present version. It will help you to reduce the problems which are facing by the end users in the current version or updated version of the application.
The following steps can be followed for rolling back a deployment:
Step 1: First list all the revisions by using the following command and select the version of deployment to which you want to roll back.
kubectl rollout history deployment/nginxStep 2: If you want to roll back to the previous version of the deployment you can do that by using the following command. it will roll back the immediate version of deployments.
kubectl rollout undo deployment/nginx-deployment --to-revision=1 (You can Mention the required version)Step 3: Reverting back to the previous version of the application will help us to reduce the downtime for the customers It's crucial to regularly test and validate your rollback process to ensure its effectiveness in real world scenarios.
Rollout history can be seen by using the following command.
kubectl rollout history <name of the deployment>This command allows you to view the no.of revisions available in the kubernetes cluster and changes made to them. If you want to see the detailed history of a specific version then you use the following command.
kubectl rollout history deployment/web-appdeployment --revision=3Rollout history will help you to roll to previous versions of the application if you find bugs or problems in the currently deployed version of the application.
Scaling the deployment can be done in many ways we can do it by using the following command.
kubectl scale deployment/tomcat-1stdeployment --replicas= 5You can do the scaling of the pods with the help of horizontal pod autoscaling by enabling it in the cluster where you chose the required no.of pods to run continuously if there is traffic or not and how many no.of pods should be run while the incoming traffic is increased.
kubectl autoscale deployment/tomcat-1st deployment --min=5 --max=8 --cpu-percent=75--min= 5 defines how many minimum no.of pods are to be run if there is traffic or not and if there is sudden traffic --max= 8 to how much it can maximize it and it depends upon the --CPU-percent.
You can pause the deployments which you are updating currently and resume the fallout updates for deployment when you feel that the changes are made correctly you can use the following command to pause the rollouts.
kubectl rollout pause deployment/webapp-deploymentTo resume the deployment which is paused you can use the following command.
kubectl rollout resume deployment/webapp-deployment When you pause the rollouts you can update the image by using the following command. Then the current version of the deployment will be replaced with the image which we are going to update.
kubectl set image deployment/webapp-deployment webapp=webapp: 2.1The deployment will pass different stages while it was in deploying. Each stage will say the health of the pods and shows us if any problems are arising.
There are a few deployment statuses as follows which will show the health of the pods there are.
Deployment status will help you to monitor the pods and you can take action immediately if you found any trouble or bugs in it.
If any deployment is in progress it is meant to be that deployment is in the stage of updating or creating a new replicaset. Following are some of the reasons that deployment is in progress.
If you want to troubleshoot the deployment then you can use the following command.
kubectl describe deploymentsThe following conditions must be satisfied to mark deployment as completed.
To check the status of the pods which are running you can use the following command.
kubectl rollout status <deployment/name of deployment>Deployment may fail for several reasons when you try to deploy its newest ReplicaSet it may be in an incomplete position forever. This can cause for different reasons following are the reasons.
To get more information about the deployment and why is was failed you can use the following command. Which gives you a detailed description of the deployment.
kubectl describe deployments A canary deployment is a release strategy where a new version of an application is deployed alongside the stable version and exposed to a small percentage of production traffic first.
Blue/Green Deployment is a release strategy where two identical production environments are maintained to reduce downtime and deployment risk.