Monday, April 1, 2019

How to setup rolling update in kubernetes deployments


Please note that below rolling update exercise is only applicable if you maintaining container image versioning for each deployment. That means you cannot perform rolling update for two deployments with same version number.

If you are looking for a way to do this while keeping the same version number, please check this post J

First we need add rolling update strategy to the deployment yaml.

Below is the required part to be added to the yaml file,

strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1 # this is limit to create extra pods while rolling update
      maxUnavailable: 1 # this is the limit that can unavailable during rolling update
   minReadySeconds: 25 # this is the minimum time to start your application

Below is a sample deployment yaml file with above strategy,

apiVersion: apps/v1
kind: Deployment
metadata:
  name: test
  labels:
    app: test
spec:
  replicas: 2
  selector:
    matchLabels:
      app: test
   strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
   minReadySeconds: 25
  template:
    metadata:
      labels:
        app: test
        version: v1
    spec:
      imagePullSecrets:
      - name:
      containers:
      - name: test
        image: test:1.0.0  # different versioning number is a must to perform rolling update    
        ports:
          - name:
            containerPort: 8080

Now apply the deployment with –record flag.

$ kubectl.exe apply -f <your deployment name>.yml --record

Now you can run below command to start rolling update. You must mention a different version number here.

$ kubectl set image deployment test test=test:1.0.1 --record

Now you can check rollout status using below command,

$ kubectl.exe rollout status deployment <your deployment name>

Also you can check rollout history by using below command,

$ kubectl.exe rollout history deployment <your deployment name>

You can use below commands to undo roll outs,

Undo to previous deployment,

$ kubectl rollout undo deployment <your deployment name>

Undo to a specific roll out revision,

$ kubectl rollout undo deployment <deployment> --to-revision=<revision Number that you get from rollout history command (1,2,…)>

References,


Kubernetes Rolling update / restart without changing the version of pods

With the default kubernetes we cannot maintain rolling update while keeping the same container version but there is a workaround for this,

This can be done by adding a patch label to the deployment. Once the patch updated, pods getting rolling restart by pulling images,

$ kubectl patch deployment <your deployment name> -p '{"spec":{"template":{"spec":{"containers":[{"name":"<your container name","env":[{"name":"LAST_ROLLOUT","value":"'$(date +%s)'"}]}]}}}}'

You can test your deployment in kubernetes for the new label,

$ kubectl.exe get deployment <your deployment name> -o yaml
spec:
  template:
...
    spec:
      containers:
      - env:
        - name: LAST_ROLLOUT
          value: "1553772196"
       

You can get pods list to check whether new ones created and existing ones terminated,
$ kubectl.exe get pods
NAME                                           READY     STATUS             RESTARTS   AGE
test-776885db4c-rdb6j                  2/2       Running            0          5h
test-84fdbf5cc8-wv4qv                  0/2       PodInitializing    0          4s

$ kubectl.exe get pods
NAME                                           READY     STATUS             RESTARTS   AGE
test-776885db4c-rdb6j                  0/2       Terminating        0          5h
test-84fdbf5cc8-wv4qv                  2/2       Running              1          40s

Now you can check rollout status using below command,

$ kubectl.exe rollout status deployment <your deployment name>
deployment "<your deployment name>" successfully rolled out

Also you can check rollout history by using below command,
$ kubectl.exe rollout history deployment <your deployment name>
deployments "<your deployment name>"
REVISION  CHANGE-CAUSE
1         kubectl.exe patch deployment <your deployment name> --patch={"spec":{"template":{"spec":{"containers":[{"name":"<your deployment name>","env":[{"name":"LAST_ROLLOUT","value":"1553772196"}]}]}}}}

You can use below commands to undo roll outs,

Undo to previous deployment,

$ kubectl rollout undo deployment <your deployment name>

Undo to a specific roll out revision,

$ kubectl rollout undo deployment <deployment> --to-revision=<revision Number that you get from rollout history command (1,2,…)>

References,

https://github.com/kubernetes/kubernetes/issues/27081