How to Create a Kubernetes Deployment & Perform Rolling Update

In this blog, we will look at how we can create a Deployment using imperative and declarative ways.

We will also look at Deployment rolling updates and rollback strategies.

Why do we need a Deployment?

We can create multiple Pod Objects on Kubernetes to deploy our application, but what if a Pod fails, we will have to create them manually.

But we have a requirement that whatever happens, the desired number of Pods has to always run, and also want to perform deployment strategies, then we will use the Deployment Object.

The deployment object manages the pods using replicasets.

Let’s look at it practically.

Method 1: Imperative way to create a Deployment Object

kubectl create deployment test-deployment --image nginx:1.16 --replicas 3

To list the Deployment objects, use the following command.

kubectl get deployment
list deployments

The Deployment Controller will always watch these Pods to manage the lifecycle of these Pods.

kubectl get pods
list pods

Method 2: Declarative way to create a Deployment object

Create a Deployment YAML manifest

cat << EOF > deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: test-deployment
  name: test-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: test-deployment
  template:
    metadata:
      labels:
        app: test-deployment
    spec:
      containers:
      - image: nginx:1.16
        name: nginx
EOF

This way we will get more flexible to configure the Deployment object.

kubectl apply -f deployment.yaml

spec.replicas is where we mention the desired number of Pods to run, and when we deploy a Deployment object, a ReplicaSet Object will also be deployed.

kubectl get replicaset
list deployment

spec.selector.matchLabels is indicating that any Pod with these labels will be part of this Deployment.

For example, if you already have two Pods with the labels app: test-deployment, if you deploy the same manifest then only one Pod will be created.

Deployment Rolling Update

You can ask why we have to use the Deployment Object instead of ReplicaSet Object because In ReplicaSet we cannot perform Deployment strategies such as Rolling update or Recreate.

With the help of Deployment Object, we can push the new version of the application without downtime.

First, we describe and see the current version of the container.

kubectl describe deployment test-deployment
image 98

Here, you can see that the default strategy is RollingUpdate, this means that if you are updating the nginx version 1.16 to 1.17, it will not terminate all the old Pods suddenly instead remove and add new pods simultaneously.

kubectl set image deployment test-deployment nginx=nginx:1.17
list pods

In this method, users will not get interrupted during the updation of the application.

describe deployment

How to Rollback the Deployment

We can go back to the previous update if necessary and that also will not affect the user experience.

kubectl rollout undo deployment test-deployment --to-revision 1

Let’s describe and see the changes on the Deployment.

kubectl describe deployment test-deployment
describe deployment

Conclusion

Creating deployments and performing rolling updates and rollbacks is an important topic in CKA certification.

We have looked at those scenarios practically.

If you are preparing for CKA certification, you can make use of the Linux Foundation coupons to save up to 50% on certification registration.

0 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like