kubernetes deployment

kubernetes pod update strategies

ReplicaSet FTW

  1. update template
  2. delete legacy kubernetes pods
  3. wait

Drawback

Service interruption while the kubernetes ReplicaSet creates the kubernetes pods with the new template.

ReplicaSet + kubernetes service

  1. create a new kubernetes ReplicaSet dedicated to the new version
  2. when all kubernetes pods are READ, change the selector in the kubernetes service
  3. delete the old kubernetes ReplicaSet

Drawback

Needs to have both versions at a time, hence twice the resource consumption.

Manual rolling update

Drawback

Lots of manual operations, hence error prone.

Deployment

Enter Deployment:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: foobar
spec:
  replicas: 3
  selector:
    matchLabels:
      app: foobar
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: foobar
    spec:
      containers:
        - name: app
          image: foobar/k8s-training-deploy: v1
# checking if deployment went well
$ kubectl apply -f foobar-deployment-v1.yml
$ kubectl rollout status deployment foobar
Waiting for deployment "foobar" rollout to finish: 0 of 3 updated replicas are available...
deployment foobar successfully rolled out
 
$ kubectl get deploy -l app=foobar
 
$ kubectl get rs -l app=foobar
 
$ kubectl get po -l app=foobar
# update version
$ kubectl apply -f foobar-deployment-v2.yml --record
deployment.apps/foobar configured
 
$ kubectl get rs -l app=foobar
 

# get history
kubectl rollout history deploy frontend
 
# rollback
kubectl rollout undo deployment frontend ‐‐to‐revision= 20

Controlling the “Rollout”

  • maxSurge (default 25%): how many kubernetes pod instances greater than targeted replicas is allowed
    • e.g. for nb_replicas=4, there won’t be more than 5 kubernetes pods in READY
  • maxUnavailable (default 25%): how many kubernetes pod instances can be unavailable than targeted replicas is allowed
    • e.g. for nb_replicas=4, there won’t be less than 3 kubernetes pods in READY
spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 30%
      maxUnavailable: 0

Example 1: replicas=3, maxSurge=1, maxUnavailable=1

Example 2: replicas=3, maxSurge=1, maxUnavailable=0

Re-creation strategy

If you can’t have both versions at the same time, there is another strategy: Recreate

  1. delete existing kubernetes pods
  2. create new kubernetes pods
spec:
  strategy:
    type: Recreate

There is a service interruption!