Site icon UnixArena

Kubernetes – CKA Exam – Sample question 3

CKA

CKA

Here is yet another interesting and useful sample question for the CKA exam. The Certified Kubernetes Administration exam is a performance-based exam, unlike other professional exams. Hence, you need to make your hands dirty by building a new Kubernetes cluster and trying all the sample questions. In UnixArena, I have shared sample questions and detailed step by step executions in previous articles. If you find any useful resources, kindly share them in the comments section.

In this article, let’s see how Kubernetes deployment does the rolling updates and rollback. From the exam point of view, you need to complete the sequence of tasks.

Sample Question:

Create a new deployment as follows:

1. Create a new deployment file using imperative command and edit the file if required.

[root@kmaster ~]# kubectl create deploy web-app --image=nginx:1.11.10-alpine --dry-run=client -o yaml > app.yaml
[root@kmaster ~]# kubectl create -f app.yaml
[root@kmaster ~]# cat app.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web-app
  name: web-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: web-app
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web-app
    spec:
      containers:
      - image: nginx:1.11.10-alpine
        name: nginx
        resources: {}
status: {}

[root@kmaster ~]# kubectl get deployment -R
NAME        READY   UP-TO-DATE   AVAILABLE   AGE
web-app   1/1     1            1           41s
[root@kmaster ~]#

2. Update the number of replicas to 3. Edit the “app.yaml” file and set the replica count to 3.

[root@kmaster ~]# vi app.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web-app
  name: web-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web-app
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web-app
    spec:
      containers:
      - image: nginx:1.11.10-alpine
        name: nginx
        resources: {}
status: {}
[root@kmaster ~]#



[root@kmaster ~]# kubectl apply -f app.yaml
deployment.apps/web-app configured

[root@kmaster ~]# kubectl get deployment
NAME        READY   UP-TO-DATE   AVAILABLE   AGE
web-app   1/3     3            1           6m26s

[root@kmaster ~]# kubectl get deployment
NAME        READY   UP-TO-DATE   AVAILABLE   AGE
web-app   3/3     3            3           6m30s
[root@kmaster ~]#

3. Update the nginx image version to the newer one.

[root@kmaster ~]# vi app.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: web-app
  name: web-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web-app
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: web-app
    spec:
      containers:
      - image: nginx:1.11.13-alpine
        name: nginx
        resources: {}
status: {}
[root@kmaster ~]#

4. Verify the default deployment method before pushing the update.

[root@kmaster ~]# kubectl describe deployment web-app |egrep "StrategyType|RollingUpdateStrategy"
StrategyType:           RollingUpdate
RollingUpdateStrategy:  25% max unavailable, 25% max surge

5. Push the deployment rolling update using kubectl apply command.

[root@kmaster ~]# kubectl apply -f app.yaml
deployment.apps/web-app configured

6. Validate the rolling update.

[root@kmaster ~]# kubectl get deployment
NAME        READY   UP-TO-DATE   AVAILABLE   AGE
web-app   3/3     2            3           8m10s

[root@kmaster ~]# kubectl get deployment
NAME        READY   UP-TO-DATE   AVAILABLE   AGE
web-app   3/3     3            3           8m22s
[root@kmaster ~]#

[root@kmaster ~]#  kubectl get po |grep web-app
web-app-bff47cd8c-lzpqr   1/1     Running       0          10s
web-app-bff47cd8c-qk92m   1/1     Running       0          24s
web-app-bff47cd8c-w2z49   1/1     Running       0          18s
web-app-fc7875d8-mr9kz    0/1     Terminating   0          2m7s
[root@kmaster ~]#

7. Here is the last task. Just revert to the older version of nginx. Use “kubectl edit” command to update the image version.

[root@kmaster ~]# kubectl edit deployment web-app
deployment.apps/web-app edited


# Update the image version.
    spec:
      containers:
      - image: nginx:1.11.10-alpine

[root@kmaster ~]# kubectl get deployment
NAME        READY   UP-TO-DATE   AVAILABLE   AGE
web-app   3/3     3            3           9m29s

[root@kmaster ~]#  kubectl get po |grep web-app
web-app-bff47cd8c-lzpqr   0/1     Terminating   0          78s
web-app-fc7875d8-48trz    1/1     Running       0          11s
web-app-fc7875d8-d5v5z    1/1     Running       0          12s
web-app-fc7875d8-ls4fc    1/1     Running       0          14s
[root@kmaster ~]#

You could also roll back using the following command.

[root@kmaster ~]# kubectl rollout undo deploy web-app
deployment.apps/web-app rolled back

[root@kmaster ~]# kubectl get deployment
NAME        READY   UP-TO-DATE   AVAILABLE   AGE
web-app   3/3     3            3           24m

[root@kmaster ~]# kubectl get pods |grep web-app
web-app-bff47cd8c-5w589   1/1     Running       0          12s
web-app-bff47cd8c-7t8bq   1/1     Running       0          11s
web-app-bff47cd8c-959wx   1/1     Running       0          14s
web-app-fc7875d8-48trz    0/1     Terminating   0          15m
[root@kmaster ~]#

We have deployed a new application and scaled it to 3 pods. Updated the image with the new version using deployment’s rolling update. Finally, we have rolled back to the older version.

Hope this article is informative to you.

Exit mobile version