DEV Community

Mesrar
Mesrar

Posted on

Mastering Deployments in Kubernetes

Deployments in Kubernetes play a pivotal role in managing scalable and reliable applications. In this guide, we'll delve into essential commands and operations related to Deployments, empowering you to efficiently roll out and manage application updates.

Key Operations

Create a Deployment

Create a simple deployment using the kubectl create deploy command:

kubectl create deploy nginx --image=nginx --replicas=2

Enter fullscreen mode Exit fullscreen mode

View Deployments
Get a list of deployments in the current namespace:


kubectl get deploy
Enter fullscreen mode Exit fullscreen mode

For deployments in a specific namespace:

kubectl get deploy -n <namespace-name>

Enter fullscreen mode Exit fullscreen mode

Retrieve detailed information about a specific deployment:

kubectl get deploy <deployment-name>
kubectl get deploy <deployment-name> -o yaml | more
kubectl get deploy -o wide
Enter fullscreen mode Exit fullscreen mode

Apply Deployment from YAML
Apply a deployment defined in a YAML file:

kubectl apply -f deploy.yaml
Enter fullscreen mode Exit fullscreen mode

To record the change cause in revision history:


kubectl apply -f deploy.yaml --record
Enter fullscreen mode Exit fullscreen mode

Deployment Rollout Status
Check the rollout status of a deployment:


kubectl rollout status deploy <deployment-name>
Enter fullscreen mode Exit fullscreen mode

Deployment Rollout History
View the revision history of a deployment:

kubectl rollout history deploy <deployment-name>
Enter fullscreen mode Exit fullscreen mode

For detailed history of a specific revision:


kubectl rollout history deploy nginx --revision=2
Enter fullscreen mode Exit fullscreen mode

Rollback Deployment
Rollback a deployment to a previous revision:

kubectl rollout undo deploy <deployment-name>
Enter fullscreen mode Exit fullscreen mode

Or, rollback to a specific revision:

kubectl rollout undo deploy <deployment-name> --to-revision=1

Enter fullscreen mode Exit fullscreen mode

Pause and Resume Deployment
Pause a deployment to apply multiple fixes, then resume to start a new rollout:

kubectl rollout pause deploy <deployment-name>
kubectl rollout resume deploy <deployment-name>
Enter fullscreen mode Exit fullscreen mode

Delete Deployment
Delete a deployment:


kubectl delete deploy <deployment-name>
Enter fullscreen mode Exit fullscreen mode

Scaling a Deployment
Scale up or down a deployment:

kubectl scale deploy <deployment-name> --replicas=3
Enter fullscreen mode Exit fullscreen mode

Create a Deployment from YAML
Create a deployment using YAML definition:

kubectl create deploy <deployment-name> --image=redis -n <namespace-name>
Enter fullscreen mode Exit fullscreen mode

Edit a Running Deployment
Make changes to a running deployment:

kubectl edit deploy <deployment-name> -n <namespace-name>
Enter fullscreen mode Exit fullscreen mode

Important Properties
Deployment strategies and properties to remember:

strategy:
  type: RollingUpdate
  rollingUpdate:
    maxUnavailable: 0
    maxSurge: 1
Enter fullscreen mode Exit fullscreen mode

Type: RollingUpdate: Specifies the update strategy as RollingUpdate, which means that the deployment process will replace old pods with new ones in a rolling fashion, ensuring the availability of the application during the update.

RollingUpdate Configuration:

maxUnavailable: 0: This setting indicates the maximum number or percentage of pods that can be unavailable during the update. A value of 0 means that no pods are allowed to be unavailable at any point during the rolling update. This ensures high availability as the new pods are gradually introduced, and the old ones are terminated.

maxSurge: 1: This setting indicates the maximum number or percentage of new pods that can be created above the desired number of replicas. In this case, a value of 1 means that only one additional pod is allowed to be created during the update process. This controls the rate at which new pods are introduced, preventing any sudden spikes in resource usage
Keys follow camel case naming convention, and values follow initials in uppercase convention.

Additional Insights
Deployments automatically create a ReplicaSet, which, in turn, creates pods.
When creating or updating images in a deployment, a new rollout triggers, creating a new deployment revision.
Identify problematic deployments by checking the READY status. The number of containers running may be less than desired.
Mastering these deployment operations ensures smooth application updates and scalability in your Kubernetes environment.

Happy Deploying!

Top comments (0)