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".
- Run minikube (in this scenario, K8s runs on WSL2- Ubuntu 20.04) ("minikube start")
- Create Yaml file (recreate-deployment.yaml) in your directory and copy the below definition into the file:
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
- Create Yaml file (rolling-deployment.yaml) in your directory and copy the below definition into the file:
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
- Run deployment:
- Watching pods' status (on linux: "watch kubectl get pods", on win: "kubectl get pods -w")
- Watching replica set's status (on linux: "watch kubectl get rs", on win: "kubectl get rs -w")
- Update image version ("kubectl set image deployment rcdeployment nginx=httpd"), after new replicaset and pods are created, old ones are deleted.
- With "recreate" strategy, pods are terminated:
- New pods are creating:
- New replicaset created:
- Delete this deployment:
- Run deployment (rolling-deployment.yaml):
- Watching pods' status (on linux: "watch kubectl get pods", on win: "kubectl get pods -w")
- Watching replica set's status (on linux: "watch kubectl get rs", on win: "kubectl get rs -w")
- 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
- New pods are creating with new version:
- New replicaset created:
- Run new deployment version:
- New pods are creating with new version:
- New replicaset created:
- To show history of the deployments (important: --record should be used to add old deployment versions in the history list):
- To show/describe the selected revision:
- Rollback to the revision=1 (with undo: "kubectl rollout undo deployment rolldeployment --to-revision=1"):
- Pod status:
- Replicaset revision=1:
- It is possible to return from revision=1 to revision=2 (with undo: "kubectl rollout undo deployment rolldeployment --to-revision=2"):
- It is also to pause rollout:
- While rollback to the revision=3 from revision=2, it was paused:
- Resume the pause of rollout of deployment:
- Now deployment's revision is 3:
-
It is also possible to see the logs of rollout with:
- "kubectl rollout status deployment rolldeployment -w"
-
Delete deployment: