Kubernetes deployment is the most powerfull way to to create and maitain containerized applications. It helps you manage the entire lifecycle of an application - from initial deployment to subsequent updates and disliked but occasionally necessary rollbacks. In this post I will show you how to use deployments rolling updates and rollbacks to keep maximum availibilty of your application.
- Setting up a Deployment
- Understanding Rolling Updates
- Performing Rolling Update
- Executing Rollbacks
- Conclusion
Setting up a Deployment
At first let’s create simple application deployment on Kubernetes. In this example, we’ll use the popular nginx image.
Example: Deployment YAML
Create a manifest file called nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 4
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.27.1
ports:
- containerPort: 80
Explanation of Key Fields:
replicas
: number of pod replicasselector
: defines how the Deployment finds which Pods to managetemplate
: Describes the Pod configurationimage
: specific version of nginx image
Run the following commands to deploy application:
kubectl apply -f nginx.yaml
Verify the deployment:
kubectl get deployments
This command should display the deployment and show that 4 replicas of the Nginx pods are running.
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 4/4 4 4 20s
Understanding Rolling Updates
Rolling update allows you to update the application version (image) without downtime by gradually updates deployments replicas with a new version of the application. Kubernetes achieves this by incrementally updating a few pods at the same time instead of bringing down the entire application (this way is also available using Recreate Strategy). By default Kubernetes will replace pods gradually, creating a smooth transition from the old version to the new. By default, it ensures that at least 75% of the desired number of Pods are up (25% max unavailable). This behavior may be customized by edit .spec.strategy.rollingUpdate section in your deployment manifest - link.
Performing Rolling Update
There are two ways to perform a rolling update: by edit manifest file (declarative way) or using kubectl set command (imperative way). I will show you both ways. It’s up to you to choose the best way.
Declarative way: Modify image version (.spec.template.spec.containers[0].image) in the nginx.yaml file:
containers:
- name: nginx
image: nginx:1.27.2
Apply the changes:
kubectl apply -f nginx.yaml
Imperative way: Run kubectl set command and specify your resource, container and new image version
kubectl set image deployment/nginx nginx=nginx:1.27.2
Rollout status regardless of the chosen way may been checked by:
kubectl rollout status deployment/nginx
The output should be similar to this:
Waiting for rollout to finish: 3 out of 4 new replicas have been updated...
or
deployment "nginx" successfully rolled out
After the rollout succeeds you can see number of replicas and images using:
kubectl get deployments nginx -o=wide
You should see 4 ready replicas with new image version like:
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx 4/4 4 4 3m14s nginx nginx:1.27.2 app=nginx
Executing Rollbacks
We don’t live in a perfect world so occasionally rollbacks are inevitable. Kubernetes Deployment allows you to rollback application to a previous stable version of the deployment in easy way.
Example: To rollback to the previous version of the deployment you should use kubectl rollout command:
kubectl rollout undo deployment/nginx
The above command make rollback to previous version, but if you need to rollback application to specific revision at first you should view the revision history:
kubectl rollout history deployment/nginx
Then, rollback to a specific revision by specifying the revision number:
kubectl rollout undo deployment/nginx --to-revision=1
Conclusion
Kubernetes deployments simplify managing containerized applications by providing robust mechanisms for versioning, rolling updates, and rollbacks. Using these tools effectively minimizes downtime and keeps your applications running smoothly. Experiment with different configurations to master Kubernetes application lifecycle management.