Kubernetes Deployment strategies – Managing Advanced Kubernetes Resources

Updating an existing Deployment requires you to specify a new container image. That is why we version container images in the first place so that you can roll out, and roll back, application changes as required. As we run everything in containers—and containers, by definition, are ephemeral—this enables a host of different deployment strategies that we can implement. There are several deployment strategies, and some of these are set out as follows:

  • Recreate: This isthe simplest of all. Delete the old pod and deploy a new one.
  • Rolling update: Slowly roll out the pods of the new version while still running the old version, and slowly remove the old pods as the new pods get ready.
  • Blue/green: This is aderived deployment strategy where we keep both versions running simultaneously and switch the traffic to the newer version when we want.
  • Canary: This appliesto Blue/Green Deployments where we switch a percentage of traffic to the newer version of the application before fully rolling out the release.
  • A/B testing: A/B testing is more of a technique to apply to Blue/Green Deployments. This is when you want to roll out the newer version to a subset of willing users and study the usage patterns before completely rolling out the newer version. You do not get A/B testing out of the box with Kubernetes but instead should rely on service mesh technologies that plug in well with Kubernetes, such as Istio, Linkerd, and Traefik.

Kubernetes provides two deployment strategies out of the box—Recreate and RollingUpdate.

Recreate

The Recreate strategy is the most straightforward deployment strategy. When you update the Deployment resource with the Recreate strategy, Kubernetes immediately spins down the old ReplicaSet resource and creates a new one with the required number of replicas along the lines of the following diagram:

Figure 6.2 – Recreate strategy

Kubernetes does not delete the old ReplicaSet resource but instead sets replicas to 0. That is required to roll back to the old version quickly. This approach results in downtime and is something you want to use only in case of a constraint. Thus, this strategy isn’t the default deployment strategy in Kubernetes.

Tip

You can use the Recreate strategy if your application does not support multiple replicas, if it does not support more than a certain number of replicas (such as applications that need to maintain a quorum), or if it does not support multiple versions at once.

Let’s update nginx-deployment with the Recreate strategy. Let’s look at the nginx-recreate.yaml file:

spec:

replicas: 3

strategy:

type: Recreate

The YAML file now contains a strategy section with a Recreate type. Now, let’s apply the nginx-recreate.yaml file and watch the ReplicaSet resources using the following command:

$ kubectl apply -f nginx-recreate.yaml && kubectl get replicaset -w

deployment.apps/nginx configured  
NAMEDESIREDCURRENTREADYAGE
nginx-6799fc88d80000s
nginx-6889dfccd50337m42s
nginx-6889dfccd50007m42s
nginx-6799fc88d83001s
nginx-6799fc88d83302s
nginx-6799fc88d83336s

The Deployment resource creates a new ReplicaSet resource—nginx-6799fc88d8—with 0 desired replicas. It then sets 0 desired replicas to the old ReplicaSet resource and waits for the old ReplicaSet resource to be completely evicted. It then starts automatically rolling out the new ReplicaSet resource to the desired images.

Related Posts

Static provisioning – Managing Advanced Kubernetes Resources-2

As the Service resource is created, we can create a StatefulSet resource that uses the created PersistentVolume and Service resources. The StatefulSet resource manifest, nginx-manual-statefulset.yaml, looks like…

Static provisioning – Managing Advanced Kubernetes Resources-1

Static provisioning is the traditional method of provisioning volumes. It requires someone (typically an administrator) to manually provision a disk and create a PersistentVolume resource using the…

StatefulSet resources – Managing Advanced Kubernetes Resources

StatefulSet resources help manage stateful applications. They are similar to Deployment resources, but unlike a Deployment resource, they also keep track of state and require Volume and…

Managing stateful applications – Managing Advanced Kubernetes Resources

Imagine you’re a librarian in a magical library. You have a bunch of enchanted books that store valuable knowledge. Each book has a unique story and is…

Horizontal Pod autoscaling – Managing Advanced Kubernetes Resources-2

Now, let’s autoscale this deployment. The Deployment resource needs at least 1 pod replica and can have a maximum of 5 pod replicas while maintaining an average…

Horizontal Pod autoscaling – Managing Advanced Kubernetes Resources-1

Imagine you’re the manager of a snack bar at a park. On a sunny day, lots of people come to enjoy the park, and they all want…

Leave a Reply

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