Monday, 27 July 2020

Kubernetes: Rolling Update example

Rolling update is the default deployment strategy in Kubernetes. In this strategy, instead of deleting all the Pods in one shot, we take down one pod at a time. Once a Pod is deleted, a newer Pod with the Specified image gets created. It is the default deployment strategy in Kubernetes.

 

You can configure a deployment strategy by specifying ‘strategy’ property.

 

  strategy:

    type: RollingUpdate

    rollingUpdate:

      maxSurge: 2

      maxUnavailable: 1

 

maxUnavailble: It specifies the maximum number of Pods that can be upgraded at the same time.

 

maxSurge: It specifies the maximum number of Pods that can go beyond the desired number of Pods specified in the replica to achieve minimal availability

 

Step 1: Create a deployment definition file.

 

rollingUpdate.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: domain-service-deployment
  labels:
    app: domain-service-deployment
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: domain-service
      labels:
        app: domain-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: domain-service-container
        image: jboss/wildfly

  replicas: 5
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 2
      maxUnavailable: 1
  selector:
    matchLabels:
      app: domain-service

Step 2: Create a deployment using the above definition file.

$kubectl create -f rollingUpdate.yml 
deployment.apps/domain-service-deployment created

Query all the deployments.

$kubectl get deployments
NAME                        READY   UP-TO-DATE   AVAILABLE   AGE
domain-service-deployment   5/5     5            5           51s

Step 3: Check the rollout history of the deployment.

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

As you see the output, revision 1 is created.

 

Step 4: let's update the deployment by changing the replicas to 3.

 

Execute the below command to edit the deployment.

kubectl edit deployment domain-service-deployment

 

Update the replicas to 3 (For your information, vi editor commands will work here).
$kubectl edit deployment domain-service-deployment
deployment.apps/domain-service-deployment edited

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

As you see no new revision is created. Replicas update do not create any new revision.

 

But, when you update the image, it will create new revision.

 

Let’s update the image of jboss/wildfly to below one.

jboss/wildfly:19.1.0.Final

$kubectl edit deployment domain-service-deployment
deployment.apps/domain-service-deployment edited

Now after applying the new image version, you can observe that new Revision entry gets created.

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

You can get what is happened in a revision by executing below command.

 

kubectl rollout history deployment domain-service-deployment --revision=2
$kubectl rollout history deployment domain-service-deployment --revision=2
deployment.apps/domain-service-deployment with revision #2
Pod Template:
  Labels:	app=domain-service
	author=krishna
	pod-template-hash=59b87b89d
	serviceType=webservice
  Containers:
   domain-service-container:
    Image:	jboss/wildfly:19.1.0.Final
    Port:	<none>
    Host Port:	<none>
    Environment:	<none>
    Mounts:	<none>
  Volumes:	<none>
Whenever a new revision created, deployment creates new replicaset to take care of the deployment.
$kubectl get replicaset
NAME                                   DESIRED   CURRENT   READY   AGE
domain-service-deployment-59b87b89d    3         3         3       4m3s
domain-service-deployment-6d8455f97c   0         0         0       13m
You can use -o wide option to know, which image is handled by replicaset.
$kubectl get replicaset -o wide
NAME                                   DESIRED   CURRENT   READY   AGE     CONTAINERS                 IMAGES                       SELECTOR
domain-service-deployment-59b87b89d    3         3         3       4m54s   domain-service-container   jboss/wildfly:19.1.0.Final   app=domain-service,pod-template-hash=59b87b89d
domain-service-deployment-6d8455f97c   0         0         0       14m     domain-service-container   jboss/wildfly                app=domain-service,pod-template-hash=6d8455f97c

You can go to previous history by executing undo command.

$kubectl rollout undo deployment domain-service-deployment
deployment.apps/domain-service-deployment rolled back

You can confirm the undo by querying replicaset.

$kubectl get replicaset -o wide
NAME                                   DESIRED   CURRENT   READY   AGE     CONTAINERS                 IMAGES                       SELECTOR
domain-service-deployment-59b87b89d    0         0         0       6m31s   domain-service-container   jboss/wildfly:19.1.0.Final   app=domain-service,pod-template-hash=59b87b89d
domain-service-deployment-6d8455f97c   3         3         2       16m     domain-service-container   jboss/wildfly                app=domain-service,pod-template-hash=6d8455f97c

You can even go to specific revision by specifying --to-revision option.

$kubectl rollout undo deployment domain-service-deployment --to-revision=2
deployment.apps/domain-service-deployment rolled back

$kubectl get replicaset -o wide
NAME                                   DESIRED   CURRENT   READY   AGE     CONTAINERS                 IMAGES                       SELECTOR
domain-service-deployment-59b87b89d    3         3         3       7m57s   domain-service-container   jboss/wildfly:19.1.0.Final   app=domain-service,pod-template-hash=59b87b89d
domain-service-deployment-6d8455f97c   0         0         0       17m     domain-service-container   jboss/wildfly                app=domain-service,pod-template-hash=6d8455f97c

These undos and upgrades will create new revisions.

$kubectl rollout history deployment domain-service-deployment
deployment.apps/domain-service-deployment 
REVISION  CHANGE-CAUSE
3         <none>
4         <none>

Previous                                                    Next                                                    Home

Kubernetes: Deployment Strategies: Rollback and update

When you initiate a deployment task, a new rollout get initiated. Every new rollout creates a deployment revision.


When you initiate another deployment task, new deployment revision ‘Revision 2’ gets created.

Why revisions?

Revisions are used to keep track of the deployment and made the rollbacks easy.

 

Recreate Strategy

Delete all the Pods and recreate the Pods again with a specified image. In this strategy, Application downtime exists.

 

Rolling Update

In this strategy, instead of deleting all the Pods in one shot, we take down one pod at a time. Once a Pod is deleted, a newer Pod with the Specified image gets created. It is the default deployment strategy in Kubernetes.

 

Let’s see, how can we perform an update and undo operations using Deployment Object.

 

Step 1: Create a new deployment.

 

notificationServiceDeployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: notification-service-deployment
  labels:
    app: notification-service-deployment
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: notification-service
      labels:
        app: notification-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: notification-service-container
        image: jboss/wildfly:10.1.0.Final

  replicas: 10
  selector:
    matchLabels:
      app: notification-service

Let’s create deployment by executing the below command.

kubectl create -f notificationServiceDeployment.yml

$kubectl create -f notificationServiceDeployment.yml 
deployment.apps/notification-service-deployment created

Check the rollout status

Check the rollout status by executing the below command.

kubectl rollout status deployment/my-deployment

$kubectl rollout status deployment/notification-service-deployment
Waiting for deployment "notification-service-deployment" rollout to finish: 0 of 10 updated replicas are available...
Waiting for deployment "notification-service-deployment" rollout to finish: 1 of 10 updated replicas are available...
Waiting for deployment "notification-service-deployment" rollout to finish: 2 of 10 updated replicas are available...
Waiting for deployment "notification-service-deployment" rollout to finish: 3 of 10 updated replicas are available...
Waiting for deployment "notification-service-deployment" rollout to finish: 4 of 10 updated replicas are available...
Waiting for deployment "notification-service-deployment" rollout to finish: 5 of 10 updated replicas are available...
Waiting for deployment "notification-service-deployment" rollout to finish: 6 of 10 updated replicas are available...
Waiting for deployment "notification-service-deployment" rollout to finish: 7 of 10 updated replicas are available...
Waiting for deployment "notification-service-deployment" rollout to finish: 8 of 10 updated replicas are available...
Waiting for deployment "notification-service-deployment" rollout to finish: 9 of 10 updated replicas are available...
deployment "notification-service-deployment" successfully rolled out

Check rollout history

Execute the below command to see rollout history.

kubectl rollout history deployment/my-deployment

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

As you see Revision 1 is created for the deployment.

 

When you initiate a deployment, Kubernetes automatically create a Replicaset for us.

$kubectl get all
NAME                                                  READY   STATUS    RESTARTS   AGE
pod/notification-service-deployment-cdf5795d4-7pw86   1/1     Running   0          7m23s
pod/notification-service-deployment-cdf5795d4-98th2   1/1     Running   0          7m23s
pod/notification-service-deployment-cdf5795d4-9g8lg   1/1     Running   0          7m23s
pod/notification-service-deployment-cdf5795d4-jjzvq   1/1     Running   0          7m23s
pod/notification-service-deployment-cdf5795d4-mgdpr   1/1     Running   0          7m23s
pod/notification-service-deployment-cdf5795d4-n8kpj   1/1     Running   0          7m23s
pod/notification-service-deployment-cdf5795d4-pkrg9   1/1     Running   0          7m23s
pod/notification-service-deployment-cdf5795d4-q4zhw   1/1     Running   0          7m23s
pod/notification-service-deployment-cdf5795d4-zqx76   1/1     Running   0          7m23s
pod/notification-service-deployment-cdf5795d4-zrbqs   1/1     Running   0          7m23s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   28d

NAME                                              READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/notification-service-deployment   10/10   10           10          7m23s

NAME                                                        DESIRED   CURRENT   READY   AGE
replicaset.apps/notification-service-deployment-cdf5795d4   10        10        10      7m23s

Step 2: Let’s update the jboss/wildfly to some other version jboss/wildfly:15.0.0.Final.

 

When you initiate updation, Kubernetes create another replica set to deploy pods with new revision.

 

Let’s confirm this experimentally.

$kubectl get replicaset
NAME                                        DESIRED   CURRENT   READY   AGE
notification-service-deployment-cdf5795d4   10        10        10      8m26s

As you see the output there is one replicaset present now.

$kubectl set image deployment/notification-service-deployment notification-service-container=jboss/wildfly:15.0.0.Final
deployment.apps/notification-service-deployment image updated

Check the rollout status

$kubectl rollout status deployment/notification-service-deployment
Waiting for deployment "notification-service-deployment" rollout to finish: 5 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 5 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 5 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 6 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 6 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 6 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 6 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 6 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 8 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 9 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 9 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 9 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 9 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 9 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "notification-service-deployment" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "notification-service-deployment" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "notification-service-deployment" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "notification-service-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "notification-service-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "notification-service-deployment" rollout to finish: 8 of 10 updated replicas are available...
Waiting for deployment "notification-service-deployment" rollout to finish: 9 of 10 updated replicas are available...
deployment "notification-service-deployment" successfully rolled out

Check the rollout history.

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

As you see the output, 2nd revision gets created.

 

When you query replicasets, you confirm that two replicasets get created. Whenever you initiate a deployment, a new replicaset created to take care of the deployment of the Pods.

$kubectl get replicaset -o wide
NAME                                         DESIRED   CURRENT   READY   AGE     CONTAINERS                       IMAGES                       SELECTOR
notification-service-deployment-5b9b7ffc68   10        10        10      3m46s   notification-service-container   jboss/wildfly:15.0.0.Final   app=notification-service,pod-template-hash=5b9b7ffc68
notification-service-deployment-cdf5795d4    0         0         0       16m     notification-service-container   jboss/wildfly:10.1.0.Final   app=notification-service,pod-template-hash=cdf5795d4

As you see currently active replica set pointing to the image ‘jboss/wildfly:15.0.0.Final’.

 

Step 3: You can confirm the rolling update strategy by describing the deployment.

$kubectl describe deployment notification-service-deployment
Name:                   notification-service-deployment
Namespace:              default
CreationTimestamp:      Fri, 05 Jun 2020 10:10:46 +0530
Labels:                 app=notification-service-deployment
                        author=krishna
                        serviceType=webservice
Annotations:            deployment.kubernetes.io/revision: 2
Selector:               app=notification-service
Replicas:               10 desired | 10 updated | 10 total | 10 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=notification-service
           author=krishna
           serviceType=webservice
  Containers:
   notification-service-container:
    Image:        jboss/wildfly:15.0.0.Final
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   notification-service-deployment-5b9b7ffc68 (10/10 replicas created)
Events:
  Type    Reason             Age                    From                   Message
  ----    ------             ----                   ----                   -------
  Normal  ScalingReplicaSet  18m                    deployment-controller  Scaled up replica set notification-service-deployment-cdf5795d4 to 10
  Normal  ScalingReplicaSet  6m41s                  deployment-controller  Scaled up replica set notification-service-deployment-5b9b7ffc68 to 3
  Normal  ScalingReplicaSet  6m41s                  deployment-controller  Scaled down replica set notification-service-deployment-cdf5795d4 to 8
  Normal  ScalingReplicaSet  6m41s                  deployment-controller  Scaled up replica set notification-service-deployment-5b9b7ffc68 to 5
  Normal  ScalingReplicaSet  5m40s                  deployment-controller  Scaled down replica set notification-service-deployment-cdf5795d4 to 7
  Normal  ScalingReplicaSet  5m40s                  deployment-controller  Scaled up replica set notification-service-deployment-5b9b7ffc68 to 6
  Normal  ScalingReplicaSet  5m37s                  deployment-controller  Scaled down replica set notification-service-deployment-cdf5795d4 to 6
  Normal  ScalingReplicaSet  5m37s                  deployment-controller  Scaled up replica set notification-service-deployment-5b9b7ffc68 to 7
  Normal  ScalingReplicaSet  5m35s                  deployment-controller  Scaled down replica set notification-service-deployment-cdf5795d4 to 5
  Normal  ScalingReplicaSet  5m35s                  deployment-controller  Scaled up replica set notification-service-deployment-5b9b7ffc68 to 8
  Normal  ScalingReplicaSet  5m29s (x7 over 5m35s)  deployment-controller  (combined from similar events): Scaled down replica set notification-service-deployment-cdf5795d4 to 0

As you see the Events: section of the output, old deployment replica set scaled-down, while new deployment replica set scaled up.

 

Step 4: Rollback to the previous deployment.

 

Let’s query replicasets first.

$kubectl get replicaset -o wide
NAME                                         DESIRED   CURRENT   READY   AGE   CONTAINERS                       IMAGES                       SELECTOR
notification-service-deployment-5b9b7ffc68   10        10        10      16m   notification-service-container   jboss/wildfly:15.0.0.Final   app=notification-service,pod-template-hash=5b9b7ffc68
notification-service-deployment-cdf5795d4    0         0         0       28m   notification-service-container   jboss/wildfly:10.1.0.Final   app=notification-service,pod-template-hash=cdf5795d4

As you see replicaset ‘notification-service-deployment-5b9b7ffc68’ which points to the image ‘jboss/wildfly:15.0.0.Final’ is active.

 

Now when I rollback the deployment to the previous state, the deployment destroys the pods in new replica set and creates pods in old replica set.

 

You can rollback to the previous deployment by executing below command

kubectl rollout undo deployment/notification-service-deployment

$kubectl rollout undo deployment/notification-service-deployment
deployment.apps/notification-service-deployment rolled back

Query for replica sets again.

$kubectl get replicaset -o wide
NAME                                         DESIRED   CURRENT   READY   AGE   CONTAINERS                       IMAGES                       SELECTOR
notification-service-deployment-5b9b7ffc68   0         0         0       19m   notification-service-container   jboss/wildfly:15.0.0.Final   app=notification-service,pod-template-hash=5b9b7ffc68
notification-service-deployment-cdf5795d4    10        10        10      32m   notification-service-container   jboss/wildfly:10.1.0.Final   app=notification-service,pod-template-hash=cdf5795d4

You can confirm that the older replica set has 10 Pods, where a new replica set has 0 pods.

 

Now, you can see revision number 3 gets created.

$kubectl rollout history deployment/notification-service-deployment
deployment.apps/notification-service-deployment 
REVISION  CHANGE-CAUSE
2         <none>
3         <none>

Step 5: You can even update the deployment definition file and apply the changes using ‘kubectl apply’ command.

 

For example, I changed the image to 19.1.0.

image: jboss/wildfly:19.1.0.Final

 

notificationServiceDeployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: notification-service-deployment
  labels:
    app: notification-service-deployment
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: notification-service
      labels:
        app: notification-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: notification-service-container
        image: jboss/wildfly:19.1.0.Final

  replicas: 10
  selector:
    matchLabels:
      app: notification-service

Apply the changes using the below command.

$kubectl apply -f notificationServiceDeployment.yml 
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
deployment.apps/notification-service-deployment configured

Execute rollout status command to see the rollout status.

$kubectl apply -f notificationServiceDeployment.yml 
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
deployment.apps/notification-service-deployment configured
$kubectl rollout status deployment/notification-service-deployment
Waiting for deployment "notification-service-deployment" rollout to finish: 5 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 5 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 5 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 5 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 5 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 6 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 6 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 6 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 6 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 6 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 7 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 8 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 8 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 8 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 9 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 9 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 9 out of 10 new replicas have been updated...
Waiting for deployment "notification-service-deployment" rollout to finish: 3 old replicas are pending termination...
Waiting for deployment "notification-service-deployment" rollout to finish: 3 old replicas are pending termination...
Waiting for deployment "notification-service-deployment" rollout to finish: 3 old replicas are pending termination...
Waiting for deployment "notification-service-deployment" rollout to finish: 3 old replicas are pending termination...
Waiting for deployment "notification-service-deployment" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "notification-service-deployment" rollout to finish: 2 old replicas are pending termination...
Waiting for deployment "notification-service-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "notification-service-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "notification-service-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "notification-service-deployment" rollout to finish: 8 of 10 updated replicas are available...
Waiting for deployment "notification-service-deployment" rollout to finish: 9 of 10 updated replicas are available...
deployment "notification-service-deployment" successfully rolled out

Now you can see three replica sets.

$kubectl get replicasets
NAME                                         DESIRED   CURRENT   READY   AGE
notification-service-deployment-5b9b7ffc68   0         0         0       87m
notification-service-deployment-6b49cf4b55   10        10        10      99s
notification-service-deployment-cdf5795d4    0         0         0       100m

Now you can see another revision 4 is created.

$kubectl rollout history deployment/notification-service-deployment
deployment.apps/notification-service-deployment 
REVISION  CHANGE-CAUSE
2         <none>
3         <none>
4         <none>

As you see CHANGE-CAUSE is set to none.

 

Let’s use the option --record to record the change cause.

 

Let’s update the definition file to point to the latest image of wildfly.

 

notificationServiceDeployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: notification-service-deployment
  labels:
    app: notification-service-deployment
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: notification-service
      labels:
        app: notification-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: notification-service-container
        image: jboss/wildfly

  replicas: 10
  selector:
    matchLabels:
      app: notification-service

Let’s apply the changes using --record option.

$kubectl apply -f notificationServiceDeployment.yml --record
deployment.apps/notification-service-deployment configured

Now when you execute rollout history command, you can see the change cause against the revision 5.

$kubectl rollout history deployment/notification-service-deployment
deployment.apps/notification-service-deployment 
REVISION  CHANGE-CAUSE
2         <none>
3         <none>
4         <none>
5         kubectl apply --filename=notificationServiceDeployment.yml --record=true


Previous                                                    Next                                                    Home

Sunday, 26 July 2020

Kubernetes: Deployments

Deployment is a Kubernetes object, it provides declarative updates for the Pods and ReplicaSets.

 

Deployment will take care of the following features.

a.   Scalability

b.   Deployment Strategies

c.    Make sure that the desired number of Pods are always available.

 

Deployment uses the labels to check the availability of a Pod.

 

For example, you may want to replace all the Pods in a ReplicaSet with a new version of the image, you can do this via Deployment Object. You may want to rollback all your changes, you can do this via Deployment Object.

Using the Deployment object, we can perform rolling updates of the application seamlessly (Zero downtime for the applications). In real-world applications, you do not create a Pod and ReplicaSet definition file. You create a deployment definition file only, which in turn create replica sets and Pods

 

Let’s create a deployment.

 

Step 1: Create ‘employeeServiceDeployment.yml’ file.

 

employeeServiceDeployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: employee-service-deployment
  labels:
    app: employee-service-deployment
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: employee-service
      labels:
        app: employee-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: employee-service-container
        image: jboss/wildfly

  replicas: 5
  selector:
    matchLabels:
      app: employee-service

template:

It has the Pod specification.

 

replicas: 5

Specifies the number of copies of Pod to run. Here 5 Pods of the application ‘jboss/wildfly’ will run.

 

selector:

    matchLabels:

      app: employee-service

Kubernetes use matchLabels to identify how labels are identified against a Pod.


Step 2: Create a deployment from the definition file.

$kubectl create -f employeeServiceDeployment.yml 
deployment.apps/employee-service-deployment created

Step 3: List all the deployments.

$kubectl get deployments
NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
employee-service-deployment   3/5     5            3           15s

As you see the output, deployment with name ‘employee-service-deployment’ is created and 3 Pods out of 5 are up and running.

 

Wait for some time and list deployments again. You will see that all the Pods are up and running.

$kubectl get deployments
NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
employee-service-deployment   5/5     5            5           72s

Deployment automatically creates a replicaset. To confirm this, list all the replicasets.

$kubectl get replicaset
NAME                                    DESIRED   CURRENT   READY   AGE
employee-service-deployment-dff6c9cdf   5         5         5       3m15s

Query Pods.

$kubectl get pods
NAME                                          READY   STATUS    RESTARTS   AGE
employee-service-deployment-dff6c9cdf-2wrg9   1/1     Running   0          2m6s
employee-service-deployment-dff6c9cdf-8m7cr   1/1     Running   0          2m6s
employee-service-deployment-dff6c9cdf-kv9ck   1/1     Running   0          2m6s
employee-service-deployment-dff6c9cdf-pbfpr   1/1     Running   0          2m6s
employee-service-deployment-dff6c9cdf-s4nh4   1/1     Running   0          2m6s

Step 4: To know more information about the deployment, execute the command ‘kubectl describe deployment’.

$kubectl describe deployment
Name:                   employee-service-deployment
Namespace:              default
CreationTimestamp:      Fri, 05 Jun 2020 00:02:53 +0530
Labels:                 app=employee-service-deployment
                        author=krishna
                        serviceType=webservice
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=employee-service
Replicas:               5 desired | 5 updated | 5 total | 5 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=employee-service
           author=krishna
           serviceType=webservice
  Containers:
   employee-service-container:
    Image:        jboss/wildfly
    Port:         <none>
    Host Port:    <none>
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   employee-service-deployment-dff6c9cdf (5/5 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  11m   deployment-controller  Scaled up replica set employee-service-deployment-dff6c9cdf to 5

Step 5: You can delete a deployment by executing the below command.

 

kubectl delete deployment {deploymentName}

$kubectl get deployment
NAME                          READY   UP-TO-DATE   AVAILABLE   AGE
employee-service-deployment   5/5     5            5           13m
$
$kubectl delete deployment employee-service-deployment
deployment.apps "employee-service-deployment" deleted
$
$kubectl get deployment
No resources found in default namespace.



Previous                                                    Next                                                    Home

Saturday, 25 July 2020

Kubernetes: ReplicaSet can adopt previously created Pods

Replicaset manages the Pods that are not created as part of the execution of this ReplicaSet definition file. If any existing Pods matches to the selector definition, then ReplicaSet considers those Pods also into consideration while creating the replicas.



Previous                                                    Next                                                    Home

Kubernetes: Delete ReplicaSet but not the Pods managed by it

If you delete a ReplicaSet or ReplicationController, all the Pods managed by them will get deleted by default. But in some cases, you just want to delete the ReplicaSet or ReplicationController alone, and not the Pods.

 

You can do this by setting --cascade to false.

 

Let’s see it with an example.

 

Step 1: Define ReplicaSet manifest file.

 

replicaSetLabels.yml

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: employee-service-replica-set
  labels:
    app: employee-service-replica-set
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: employee-service
      labels:
        app: employee-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: employee-service-container
        image: jboss/wildfly

  replicas: 5
  selector:
    matchLabels:
      app: employee-service

Step 2: Create ReplicaSet from the above definition file.

$kubectl create -f replicaSetLabels.yml 
replicaset.apps/employee-service-replica-set created

Query for replicaset and pods.

$kubectl get rs
NAME                           DESIRED   CURRENT   READY   AGE
employee-service-replica-set   5         5         3       18s
Query for Pods.

$kubectl get pods
NAME                                 READY   STATUS    RESTARTS   AGE
employee-service-replica-set-8gw6n   1/1     Running   0          37s
employee-service-replica-set-b4jrb   1/1     Running   0          37s
employee-service-replica-set-tg2hf   1/1     Running   0          37s
employee-service-replica-set-tmz22   1/1     Running   0          37s
employee-service-replica-set-zhkwx   1/1     Running   0          37s

Step 2: let's delete replicaset alone.

$kubectl delete rs employee-service-replica-set --cascade=false
replicaset.apps "employee-service-replica-set" deleted

Let’s query for the Pods and replicaset to confirm that Pods are not deleted.

$kubectl get rs
No resources found in default namespace.
$
$kubectl get pods
NAME                                 READY   STATUS    RESTARTS   AGE
employee-service-replica-set-8gw6n   1/1     Running   0          4m8s
employee-service-replica-set-b4jrb   1/1     Running   0          4m8s
employee-service-replica-set-tg2hf   1/1     Running   0          4m8s
employee-service-replica-set-tmz22   1/1     Running   0          4m8s
employee-service-replica-set-zhkwx   1/1     Running   0          4m8s




Previous                                                    Next                                                    Home

Friday, 24 July 2020

ReplicaSet: Changing the Pod template do not have any impact on currently running nodes

You can modify ReplicaSet Pod template at any time. This modification of Pod template does not reflect on the currently running pods, these changes will be reflected on new pods.

 

How can I make sure that old pods reflected with a new pod template?

Just delete them, then ReplicaSet automatically creates new Pods with the updated template.

 

Let’s confirm this with an example.

 

replicaSetLabels.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: employee-service-replica-set
  labels:
    app: employee-service-replica-set
    author: krishna
    serviceType: webservice
spec:
  template:
    metadata:
      name: employee-service
      labels:
        app: employee-service
        author: krishna
        serviceType: webservice
    spec:
      containers:
      - name: employee-service-container
        image: jboss/wildfly

  replicas: 5
  selector:
    matchLabels:
      app: employee-service

Create ReplicaSet from the above definition file.

$kubectl create -f replicaSetLabels.yml
replicaset.apps/employee-service-replica-set created

You can query Pods and their labels by executing the below command.

$kubectl get pods --show-labels
NAME                                 READY   STATUS    RESTARTS   AGE   LABELS
employee-service-replica-set-gkbhg   1/1     Running   0          31s   app=employee-service,author=krishna,serviceType=webservice
employee-service-replica-set-qhpcx   1/1     Running   0          31s   app=employee-service,author=krishna,serviceType=webservice
employee-service-replica-set-xbtjb   1/1     Running   0          31s   app=employee-service,author=krishna,serviceType=webservice
employee-service-replica-set-xhh2h   1/1     Running   0          31s   app=employee-service,author=krishna,serviceType=webservice
employee-service-replica-set-xtnv4   1/1     Running   0          31s   app=employee-service,author=krishna,serviceType=webservice

Now, let’s edit the ReplicaSet definition and update the author from ‘krishna’ to ‘Shiney’ in the Pod template section (spec.template.matadata.labels.author).

 

You can edit the replica set by executing the below command.

kubectl edit rc {my_replica_set}

 

Example

kubectl edit rs employee-service-replica-set

 

Change the author key from ‘krishna’ to ‘Shiney’.

$kubectl edit rs employee-service-replica-set
replicaset.apps/employee-service-replica-set edited
Query the Pods again to confirm that this label change will not reflect on currently running pods.
$kubectl get pods --show-labels
NAME                                 READY   STATUS    RESTARTS   AGE     LABELS
employee-service-replica-set-gkbhg   1/1     Running   0          2m31s   app=employee-service,author=krishna,serviceType=webservice
employee-service-replica-set-qhpcx   1/1     Running   0          2m31s   app=employee-service,author=krishna,serviceType=webservice
employee-service-replica-set-xbtjb   1/1     Running   0          2m31s   app=employee-service,author=krishna,serviceType=webservice
employee-service-replica-set-xhh2h   1/1     Running   0          2m31s   app=employee-service,author=krishna,serviceType=webservice
employee-service-replica-set-xtnv4   1/1     Running   0          2m31s   app=employee-service,author=krishna,serviceType=webservice

Let’s delete the pod ‘employee-service-replica-set-gkbhg’.

$kubectl delete pod employee-service-replica-set-gkbhg
pod "employee-service-replica-set-gkbhg" deleted

Query the Pods again.

$kubectl get pods --show-labels
NAME                                 READY   STATUS    RESTARTS   AGE     LABELS
employee-service-replica-set-qhpcx   1/1     Running   0          3m26s   app=employee-service,author=krishna,serviceType=webservice
employee-service-replica-set-rj6kn   1/1     Running   0          35s     app=employee-service,author=Shiney,serviceType=webservice
employee-service-replica-set-xbtjb   1/1     Running   0          3m26s   app=employee-service,author=krishna,serviceType=webservice
employee-service-replica-set-xhh2h   1/1     Running   0          3m26s   app=employee-service,author=krishna,serviceType=webservice
employee-service-replica-set-xtnv4   1/1     Running   0          3m26s   app=employee-service,author=krishna,serviceType=webservice

From the output, you can confirm that the author ‘Shiney’ reflected for newly created Pod.




Previous                                                    Next                                                    Home