Introduction to Kubernetes Deployment Strategies – Part 2

Reading Time: 3 minutes

Kubernetes provides many deployment strategies help in maintaining the desired state of the application. In the previous blog we have covered the overview of 4 deployment strategies in K8s:

  • Rolling Update
  • Recreate
  • Canary
  • Blue/Green

You may go through the Introduction to Kubernetes Deployment Strategies (Introduction to Kubernetes Deployment Strategies – Knoldus Blogs). So, In this Blog, we will discuss the recreate strategy in detail.

Recreate

Recreate is the simplest deployment strategy in K8s. A recreate deployment is basically recreating the deployment with the new changes.
As we can see in the diagram in this deployment strategy, we scale down the replicas of the previous version and then deploy the replicas of the new version.

Recreate deployment strategy

The biggest drawback of this strategy of deployment is that there is a small window of downtime between the scaling down of previous version pods and deploying of the new version pods.

The below is a template for deployment using recreate strategy.

  1. We create deployment with recreate strategy by saving the template to a file recreate.yaml.
apiVersion: apps/v1
kind: Deployment
metadata:
    name: nginx-deployment-recreate
    namespace: nginx-app
    labels:
        app: nginx-recreate
    annotations:
        description: "nginx deployment with recrete strategy"
spec:
    selector:
        matchLabels:
            app: nginx-recreate
    replicas: 4
    strategy:
        type: Recreate
    template:
        metadata:
            name: nginx
            labels:
                app: nginx-recreate
        spec:
            containers:
                - name: nginx
                  image: nginx:1.16-perl
                  ports:
                      - containerPort: 8080
                  resources:
                      requests:
                          memory: "64Mi"
                          cpu: "250m"
                      limits:
                          memory: "128Mi"
                          cpu: "500m"
  1. Then we create the deployment using kubectl command. This will be v1 of the app.
kubectl apply -f recreate.yaml

The Deployment resource is a wrapper for Replicaset. It uses Replicaset to maintain the desired state.

By default, every deployment resource uses rolling updates as deployments. For using recreate strategy, you can change .spec.strategy.type to recreate.

Check the pods and Replicaset

kubectl get pods --namespace nginx-app --show-labels
kubectl get replicasets

Check container image on first(v1) Replicaset (Replicaset name will be different)

kubectl describe replicaset nginx-deployment-recreate-6d47fc67d4
  1. For upgrading it to v2, we update the image of nginx from 1.16-perl to 1.18.0-perl using kubectl command.
kubectl set image deployment nginx-deployment-recreate nginx=nginx:1.18.0-perl

Check the pods and Replicaset

kubectl get pods --namespace nginx-app --show-labels
kubectl get replicasets

Check container image on new(v2) Replicaset (Replicaset name will be different from the v1 Replicaset)

kubectl describe replicaset nginx-deployment-recreate-7c26ef35g7

Steps

Behind the scenes, Kubernetes takes the following actions:
When using a Recreate deployment strategy in Kubernetes, the following actions occur.

  1. The Deployment resource created a ReplicaSet that uses nginx:1.16-perl as container image.
  2. The ReplicaSet created 4 pods using nginx:1.16-perl container image.
  3. The image in the deployment was changed to nginx:1.18.0-perl.
  4. The Kubernetes noticed this change and created a new ReplicaSet that uses nginx:1.18.0-perl.
  5. Kubernetes set the v1ReplicaSet using nginx:1.16-perl replica count to 0.
  6. The v1 ReplicaSet deleted all of its pods.
  7. Kubernetes changed the replica count on the v2 ReplicaSet to 4.
  8. The v2 ReplicaSet created 4 new pods all using nginx:1.18.0-perl container image.
  9. Update complete!

When to use

As we know the main drawback of using Recreate is having downtime between the scaling down of the first Replicaset and scaling up of the second Replicaset.
We can use this strategy:

  1. When having a small downtime is not an issue.
  2. When you can’t have two different version of application at same time.

Example code can be found on the following gist:
Template for deployment using the recreate strategy

References

Written by 

Shivam Gupta is a Software Consultant at Knoldus Software LLP. He has done MCA from Bharati Vidyapeeth Institute of Computer Application and Management, Paschim Vihar. He has a keen interest toward learning new technologies. He has a decent knowledge of Java Language. His practice area is DevOps. When he is not working, you will find him watching anime or with a book.

1 thought on “Introduction to Kubernetes Deployment Strategies – Part 23 min read

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading