08 | Deployment in Kubernetes

What is a deployment?

The process of manually updating containerized applications can be time consuming and tedious. Upgrading a service to the next version requires starting the new version of the pod, stopping the old version of a pod, waiting and verifying that the new version has launched successfully, and sometimes rolling it all back to a previous version in the case of failure.

Performing these steps manually can lead to human errors, and scripting properly can require a significant amount of effort, both of which can turn the release process into a bottleneck. 

A Kubernetes deployment makes this process automated and repeatable. 

A deployment ensures the desired number of pods are running and available at all times. The update process is also wholly recorded, and versioned with options to pause, continue, and roll back to previous versions.

Deployment is higher in hierarchy: Above Pod, and above ReplicaSet

Can you list few features of deployment?

The Kubernetes deployment object lets you:

  • Deploy a replica set or pod
  • Update pods and replica sets
  • Rollback to previous deployment versions
  • Scale a deployment
  • Pause or continue a deployment

How to create deployment?

Step 1: Create deployment definition yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: think-app-deployment
  labels:
    app: think-app
    env: eat
spec:
  template:
    metadata:
      name: think-app-pod
      labels:
        app: think-app
        type: load-balancer
    spec:
      containers:
      - name: thinkscholar-nginx-container
         image:  nginx    
  replicas: 3
  selector:
    matchLabels:
      type: load-balancer

Step2: Run the command:

kubectl create -f deployment-def.yaml

How to find all deployments?

$ kubectl get deployments
NAME                     DESIRED   CURRENT   UP-TO-DATE     AVAILABLE   AGE
think-app-deployment     3         3         3              3           3s 

Note: you can run get replicaset and get pods to check related replicaset and pods which were created along with other other replicaset and pods already present.

Output:

$ kubectl get rs
NAME                            DESIRED   CURRENT   READY     AGE
think-app-deployment-985342     3         3         3         3s 
$ kubectl get pods
 NAME                  READY     STATUS              RESTARTS   AGE
 think-app-pod-53thy   1/1       Running             0          2s
 think-app-pod-k0xz6   1/1       Running             0          2s
 think-app-pod-q3vkg   1/1       Running             0          2s

Is there command to get all deployments, replicasets and pods present?

Yes. Run the command:

kubectl get all

Output:

NAME                            DESIRED   CURRENT   UP-TO-DATE     AVAILABLE   AGE
 deploy/think-app-deployment     3         3         3              3           3s
 

 NAME                               DESIRED   CURRENT   READY     AGE
 rs/think-app-deployment-985342     3         3         3         3s
 

 NAME                     READY     STATUS              RESTARTS   AGE
 po/think-app-pod-53thy   1/1       Running             0          2s
 po/think-app-pod-k0xz6   1/1       Running             0          2s
 po/think-app-pod-q3vkg   1/1       Running             0          2s

How to get detailed description about deployment?

Well to get detailed description of all deployments you can run:

kubectl describe deployment

Output:

Name:           think-app-deployment
 Namespace:      default
 CreationTimestamp:  Tue, 15 Mar 2016 14:48:04 -0700
 Labels:         app=think-app
 Selector:       app=think-app
 Replicas:       3 desired | 1 updated | 4 total | 3 available | 1 unavailable
 StrategyType:       RollingUpdate
 MinReadySeconds:    0
 RollingUpdateStrategy:  25% max unavailable, 25% max surge
 Pod Template:
   Labels:  app=think-app
   Containers:
    nginx:
     Image:        nginx:1.161
     Port:         80/TCP
     Host Port:    0/TCP
     Environment:  <none>
     Mounts:       <none>
   Volumes:        <none>
 Conditions:
   Type           Status  Reason
   ----           ------  ------
   Available      True    MinimumReplicasAvailable
   Progressing    True    ReplicaSetUpdated
 OldReplicaSets:     think-app-deployment-1564180365 (3/3 replicas created)
 NewReplicaSet:      think-app-deployment-3066724191 (1/1 replicas created)
 Events:
   FirstSeen LastSeen    Count   From                    SubObjectPath   Type        Reason              Message
   --------- --------    -----   ----                    -------------   --------    ------              -------
   1m        1m          1       {deployment-controller }                Normal      ScalingReplicaSet   Scaled up replica set think-app-deployment-2035384211 to 3
   22s       22s         1       {deployment-controller }                Normal      ScalingReplicaSet   Scaled up replica set think-app-deployment-1564180365 to 1
   22s       22s         1       {deployment-controller }                Normal      ScalingReplicaSet   Scaled down replica set think-app-deployment-2035384211 to 2
   22s       22s         1       {deployment-controller }                Normal      ScalingReplicaSet   Scaled up replica set think-app-deployment-1564180365 to 2
   21s       21s         1       {deployment-controller }                Normal      ScalingReplicaSet   Scaled down replica set think-app-deployment-2035384211 to 1
   21s       21s         1       {deployment-controller }                Normal      ScalingReplicaSet   Scaled up replica set think-app-deployment-1564180365 to 3
   13s       13s         1       {deployment-controller }                Normal      ScalingReplicaSet   Scaled down replica set think-app-deployment-2035384211 to 0
   13s       13s         1       {deployment-controller }                Normal      ScalingReplicaSet   Scaled up replica set think-app-deployment-3066724191 to 1

To get detailed description about specific deployment, you should run:

kubectl describe deployment <deployment-name>

How to update deployment?

Well update the deployment details in the definition yaml:

  • replicas
  • image version
  • labels

Apply the changes:

kubectl apply -f deployment-def.yaml

Can I update version of image in deployment without applying changes to definition yaml?

Yes.

kubectl set image deployment/thinkscholar-deployment nginx=ngixn:2.0.1

Note: Just be careful, that you didn’t update deployment definition file. Hence you should be careful using this definition file in future

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s