Skip to content

Latest commit

 

History

History
205 lines (134 loc) · 8.31 KB

K8s-Rollout-Rollback.md

File metadata and controls

205 lines (134 loc) · 8.31 KB

LAB: K8s Rollout - Rollback

This scenario shows:

  • how to roll out deployments with 2 different strategy: recreate and rollingUpdate,
  • how to save/record deployments' revision while rolling out with "--record" (e.g. changing image):
    • imperative: "kubectl set image deployment rcdeployment nginx=httpd --record",
    • declerative, edit file: "kubectl edit deployment rolldeployment --record",
  • how to rollback (rollout undo) the desired deployment revisions:
    • "kubectl rollout undo deployment rolldeployment --to-revision=2",
  • how to pause/resume rollout:
    • pause: "kubectl rollout pause deployment rolldeployment",
    • resume: "kubectl rollout resume deployment rolldeployment",
  • how to see the status of rollout deployment:
    • "kubectl rollout status deployment rolldeployment -w".

Steps

  • Run minikube (in this scenario, K8s runs on WSL2- Ubuntu 20.04) ("minikube start")

image

apiVersion: apps/v1
kind: Deployment
metadata:
  name: rcdeployment
  labels:
    team: development
spec:
  replicas: 5                        # create 5 replicas
  selector:
    matchLabels:                     # labelselector of deployment: selects pods which have "app:recreate" labels
      app: recreate
  strategy:                          # deployment roll up strategy: recreate => Delete all pods firstly and create Pods from scratch.
    type: Recreate
  template:
    metadata:
      labels:                        # labels the pod with "app:recreate" 
        app: recreate
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

image

apiVersion: apps/v1
kind: Deployment
metadata:
  name: rolldeployment
  labels:
    team: development
spec:
  replicas: 10                     
  selector:
    matchLabels:                     # labelselector of deployment: selects pods which have "app:rolling" labels
      app: rolling
  strategy:
    type: RollingUpdate              # deployment roll up strategy: rollingUpdate => Pods are updated step by step, all pods are not deleted at the same time.
    rollingUpdate:                   
      maxUnavailable: 2              # shows the max number of deleted containers => total:10 container; if maxUnava:2, min:8 containers run in that time period
      maxSurge: 2                    # shows that the max number of containers    => total:10 container; if maxSurge:2, max:12 containers run in a time
  template:
    metadata:
      labels:                        # labels the pod with "app:rolling"
        app: rolling
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

image

  • Run deployment:

image

  • Watching pods' status (on linux: "watch kubectl get pods", on win: "kubectl get pods -w")

image

  • Watching replica set's status (on linux: "watch kubectl get rs", on win: "kubectl get rs -w")

image

  • Update image version ("kubectl set image deployment rcdeployment nginx=httpd"), after new replicaset and pods are created, old ones are deleted.

image

  • With "recreate" strategy, pods are terminated:

image

  • New pods are creating:

image

  • New replicaset created:

image

  • Delete this deployment:

image

  • Run deployment (rolling-deployment.yaml):

image

  • Watching pods' status (on linux: "watch kubectl get pods", on win: "kubectl get pods -w")

image

  • Watching replica set's status (on linux: "watch kubectl get rs", on win: "kubectl get rs -w")

image

  • Run: "kubectl edit deployment rolldeployment --record", it opens vim editor on linux to edit
  • Find image definition, press "i" for insert mode, change to "httpd" instead of "nginx", press "ESC", press ":wq" to save and exit

image

  • New pods are creating with new version:

image

  • New replicaset created:

image

  • Run new deployment version:

image

  • New pods are creating with new version:

image

  • New replicaset created:

image

  • To show history of the deployments (important: --record should be used to add old deployment versions in the history list):

image

  • To show/describe the selected revision:

image

  • Rollback to the revision=1 (with undo: "kubectl rollout undo deployment rolldeployment --to-revision=1"):

image

  • Pod status:

image

  • Replicaset revision=1:

image

  • It is possible to return from revision=1 to revision=2 (with undo: "kubectl rollout undo deployment rolldeployment --to-revision=2"):

image

  • It is also to pause rollout:

image

  • While rollback to the revision=3 from revision=2, it was paused:

image

  • Resume the pause of rollout of deployment:

image

  • Now deployment's revision is 3:

image

  • It is also possible to see the logs of rollout with:

    • "kubectl rollout status deployment rolldeployment -w"
  • Delete deployment:

image