Contents

K8S Deployment Strategies

K8S study notes

1. Background

We always deploy the final version applications to the production env. There are several ways to ensure the release production stably and safety. Below sections are k8s deployment strategies.

2. The Strategies

There are 4 ways to do the production release. 1) Rolling-update. 2) Recreate. 3) Blue/Green. 4) Canary. Below is the details.

  • Recreate Update

    This is very clumsy way to update. Destory V1 application then create V2 application. Will hit service down issue in a period time.

  • Rolling update.

    This is the most commonly used.

    • Performing an automatic rolling update with a RC

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      
      apiVersion: v1
      kind: ReplicationController
      metadata:
       name: kubia-v1
      spec:
       replicas: 3
       template:
         metadata:
           name: kubia
           labels:
             app: kubia
         spec:
           containers:
           - image: luksa/kubia:v1
             name: nodejs
      ---
      apiVersion: v1
      kind: Service
      metadata:
       name: kubia
      spec:
       type: NodePort
       selector:
         app: kubia
       ports:
       - port: 80
         targetPort: 8080
         nodePort: 30007
      

      To run this app, will create a ReplicationController and NodePort service to enable to access the app externally. Then open another terminal to monitor the service.

      Then performing a rolling update with kubectl, will create version 2 of the app. Execute below command:

      1
      
      kubectl rolling-update kubia-v1 kubia-v2 --image=luksa/kubia:v2
      
    • Using Deployment for updating apps declaratively

      A deployment is a high-level resource meant for deploying applications and updating them declaratively, instead of doing it through a RC or RS, which are both considered lower-level concepts.

       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      
      apiVersion: apps/v1
      kind: Deployment
      metadata:
       name: kubia-v1
      spec:
       replicas: 3
       selector:
         matchLabels:
           app: kubia
       template:
         metadata:
           name: kubia
           labels:
             app: kubia
         spec:
           containers:
           - image: luksa/kubia:v1
             name: nodejs
      ---
      apiVersion: v1
      kind: Service
      metadata:
       name: kubia
      spec:
       type: NodePort
       selector:
         app: kubia
       ports:
       - port: 80
      

      Also to run this application, after completed, change the v1 deployment container to v2, then deploy with the new version.

      1
      
      kubectl apply -f kubia-deploy-v2.yaml
      

      After that, you can notice that there will reserve the V1 RS. So it’s very easy to rollback the version from V2 to V1.

      Use this command to roll back the deployment.

      1
      
      kubectl rollout undo deployment kubia
      
      1
      
      kubectl rollout history deployment kubia
      
      1
      
      kubectl rollout undo deployment kubia --to-revision=1
      
  • Blue / Green

    Actually this deployment needs V1 and V2 applications are existing. The client connect the Deployment through the labels.

  • Canary Deployment