Introduction

In this new article we discuss about Deployments.

What is a Deployment?

An entity called “ReplicaSet” ensures that a specified number of pod replicas are running at any given time. However, a Deployment is a higher-level concept that manages ReplicaSets and provides declarative updates to Pods along with a lot of other useful features. Therefore, Kubernetes recommends using Deployments instead of directly using ReplicaSets, unless you require custom update orchestration or don’t require updates at all.

Moreover Deployments are very useful when you want to upgrade not all your applications (PODS) at the same time as you want to be able to roll-out easily an upgrade.

How to create a Deployment

Using a configuration YAML file

As usual, we create the following YAML File with the following mandatory parts :

apiVersion: 
kind: 
metadata:


spec:

Deployments YAML Files are very similar to ReplicaSets YAML Files.

In our example, we will create a Deployment with 3 nginx PODS

So let’s begin with these 4 sections :

  • The api version in our case is “v1”. (You can see that’s different from our previous lecture with PODs)
  • The kind will be Deployment, for obvious reasons
  • Metadata will contain the name and labels.
  • Finally, the “spec” is the most important part of our YAML file. It will be composed by the number of replicas AND by a POD template. It will be exactly the same as you would have used to create a single POD.

Here is our final YAML file called mydeployment.yaml :

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        ports:
        - containerPort: 80

We can now deploy our Deployment running the following command :

$ kubectl create -f mydeployment.yaml
deployment.apps/nginx-deployment created

And that’s all !

You can get more information on your Deployment running the following command :

kubectl describe deployments nginx-deployment

How to update a Deployment ?

You can easily update a Deployment using the following command :

$ kubectl edit deployments.apps nginx-deployment
#make your changes, i.e nginx version and save

deployment.apps/nginx-deployment edited

Or by editing your YAML File and apply it with :

kubectl apply -f mydeployment.yaml

When you first create a deployment, it triggers a rollout. When you upgrade a deployment (for example, the image version as seen previously) a new revision is created. This helps us to rollback on a previous version if necessary.

You can see the status of your rollouts by running the following command :

$ kubectl edit deployments.apps nginx-deployment
deployment.apps/nginx-deployment edited

$ kubectl rollout status deployment nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment" successfully rolled out

We have successfully upgraded our Deployment with the RollingUpdate Strategy.

There are basically 2 different upgrade strategies :

  • Recreate : All existing Pods are killed before new ones are create
  • RollingUpdate : The Deployment updates Pods in a rolling update fashion (so not all at the same time)

How to roll back a Deployment ?

Sometimes, you may want to rollback a Deployment; for example, when the Deployment is not stable, such as crash looping. By default, all of the Deployment’s rollout history is kept in the system so that you can rollback anytime you want.

First you need to get the rollout history with the following command :

$ kubectl rollout history deployment nginx-deployment
deployment.apps/nginx-deployment
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

Then you simply have to run this command to go back to the first version :

kubectl rollout history deployments nginx-deployment --revision=1

Sources

kubernetes.io

Leave a Comment

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