Deployment

In this page, you can find an explanation of what is the Deployment, how to create and use it.

Table of contents

  1. Create new Deployment
  2. Updating a Deployment
  3. Rolling Back a Deployment
  4. Scaling a Deployment
  5. Cleanup

Create new Deployment

Deployment controller provides declarative updates for Pods and ReplicaSets.

1) The following is an example of a Deployment. It creates a ReplicaSet to bring up three nginx Pods

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

Save this file with name like deployment.yaml and run command:

kubectl create -f deployment.yaml

2) Run kubectl get deployments to check if the Deployment was created. If the Deployment is still being created, the output is similar to the following:

NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3         3         3            3           9s
  • NAME lists the names of the Deployments in the cluster.

  • DESIRED displays the desired number of replicas of the application, which you define when you create the Deployment. This is the desired state.

  • CURRENT displays how many replicas are currently running.

  • UP-TO-DATE displays the number of replicas that have been updated to achieve the desired state.

  • AVALIBLE displays how many replicas of the application are available to your users.

  • AGE displays the amount of time that the application has been running.


Updating a Deployment

1) Let’s update the nginx Pods to use the nginx:1.9.1 image instead of the nginx:1.7.9 image.

kubectl --record deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.9.1

The output is similar to this:

deployment.apps/nginx-deployment image updated

2) To see the rollout status, run:

kubectl rollout status deployment.v1.apps/nginx-deployment

The output is similar to this:

Waiting for rollout to finish: 2 out of 3 new replicas have been updated...

or

deployment "nginx-deployment" successfully rolled out

Get more details on your updated Deployment:

kubectl get deployments

The output is similar to this:

NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3         3         3            3           50s

Run kubectl get rs to see that the Deployment updated the Pods by creating a new ReplicaSet and scaling it up to 3 replicas, as well as scaling down the old ReplicaSet to 0 replicas.

The output is similar to this:

NAME                          DESIRED   CURRENT   READY     AGE
nginx-deployment-67594d6bf6   0         0         0         50s
nginx-deployment-6fdbb596db   3         3         3         9s

Running kubectl get pods should now show only the new Pods

The output is similar to this:

NAME                                READY     STATUS    RESTARTS   AGE
nginx-deployment-6fdbb596db-5s62f   1/1       Running   0          50s
nginx-deployment-6fdbb596db-p568z   1/1       Running   0          50s
nginx-deployment-6fdbb596db-xt4dl   1/1       Running   0          50s

Rolling Back a Deployment

First, check the revisions of this Deployment:

kubectl rollout history deployment.v1.apps/nginx-deployment

The output is similar to this:

deployments "nginx-deployment"
REVISION  CHANGE-CAUSE
3         kubectl deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.7.9 --record=true
4         kubectl deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.9.1 --record=true
5         kubectl set image deployment.v1.apps/nginx-deployment nginx=nginx:1.91 --record=true

Rollback to a specific revision by specifying it with --to-revision:

kubectl rollout undo deployment.v1.apps/nginx-deployment --to-revision=3

Or rollback to the previous revision:

kubectl rollout undo deployment.v1.apps/nginx-deployment

The output is similar to this:

deployment.apps/nginx-deployment

Check if the rollback was successful:

kubectl get deployment nginx-deployment

The output is similar to this:

NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3         3         3            3           1m

Get the description of the Deployment:

kubectl describe deployment nginx-deployment

The output is similar to this:

Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Wed, 18 Sep 2019 07:22:44 +0000
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision=6
                        kubernetes.io/change-cause=kubectl deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.7.9 --record=true
Selector:               app=nginx
Replicas:               3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx:1.7.9
    Port:         80/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-67594d6bf6 (3/3 replicas created)
Events:
  Type    Reason              Age   From                   Message
  ----    ------              ----  ----                   -------
  Normal  DeploymentRollback  5m    deployment-controller  Rolled back deployment "nginx-deployment" to revision 3
  Normal  ScalingReplicaSet   5m    deployment-controller  Scaled down replica set nginx-deployment-58c7645486 to 0
  Normal  ScalingReplicaSet   5m    deployment-controller  Scaled up replica set nginx-deployment-67594d6bf6 to 1
  Normal  ScalingReplicaSet   5m    deployment-controller  Scaled down replica set nginx-deployment-6fdbb596db to 2
  Normal  ScalingReplicaSet   5m    deployment-controller  Scaled up replica set nginx-deployment-67594d6bf6 to 2
  Normal  ScalingReplicaSet   5m    deployment-controller  Scaled down replica set nginx-deployment-6fdbb596db to 1
  Normal  ScalingReplicaSet   5m    deployment-controller  Scaled up replica set nginx-deployment-67594d6bf6 to 3
  Normal  ScalingReplicaSet   5m    deployment-controller  Scaled down replica set nginx-deployment-6fdbb596db to 0

Scaling a Deployment

You can scale a Deployment:

kubectl scale deployment.v1.apps/nginx-deployment --replicas=10

The output is similar to this:

deployment.apps/nginx-deployment scaled
kubectl get deploy

The output is similar to this:

NAME               DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   10        10        10           10          1m

Cleanup

kubectl delete -f deployment.yaml

The output is similar to this:

deployment.apps "nginx-deployment" deleted