-
Notifications
You must be signed in to change notification settings - Fork 515
/
ckad_questions_answers
1527 lines (1145 loc) · 44.4 KB
/
ckad_questions_answers
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
==========================k8s questions ============================
https://medium.com/bb-tutorials-and-thoughts/practice-enough-with-these-questions-for-the-ckad-exam-2f42d1228552
=================================================================
BASIC CORE CONCEPT Questions
=================================================================
1. List all the namespaces in the cluster
kubectl get namespaces
kubectl get ns
2. List all the pods in all namespaces
kubectl get po --all-namespaces
3. List all the pods in the particular namespace
kubectl get po -n <namespace name>
4. List all the services in the particular namespace
kubectl get svc -n <namespace name>
5. List all the pods showing name and namespace with a json path expression
kubectl get pods -o=jsonpath="{.items[*]['metadata.name', 'metadata.namespace']}"
6. Create an nginx pod in a default namespace and verify the pod running
// creating a pod
kubectl run nginx --image=nginx --restart=Never// List the pod
kubectl get po
7. Create the same nginx pod with a yaml file
// get the yaml file with --dry-run flag
kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx-pod.yaml// cat nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}// create a pod
kubectl create -f nginx-pod.yaml
8. Output the yaml file of the pod you just created
kubectl get po nginx -o yaml
9. Output the yaml file of the pod you just created without the cluster-specific information
kubectl get po nginx -o yaml --export
10. Get the complete details of the pod you just created
kubectl describe pod nginx
11. Delete the pod you just created
kubectl delete po nginxkubectl delete -f nginx-pod.yaml
12. Delete the pod you just created without any delay (force delete)
kubectl delete po nginx --grace-period=0 --force
13. Create the nginx pod with version 1.17.4 and expose it on port 80
kubectl run nginx --image=nginx:1.17.4 --restart=Never --port=80
14. Change the Image version to 1.15-alpine for the pod you just created and verify the image version is updated
kubectl set image pod/nginx nginx=nginx:1.15-alpinekubectl describe po nginx// another way it will open vi editor and change the version
kubeclt edit po nginxkubectl describe po nginx
15. Change the Image version back to 1.17.1 for the pod you just updated and observe the changes
kubectl set image pod/nginx nginx=nginx:1.17.1
kubectl describe po nginx
kubectl get po nginx -w # watch it
16. Check the Image version without the describe command
kubectl get po nginx -o jsonpath='{.spec.containers[].image}{"\n"}'
17. Create the nginx pod and execute the simple shell on the pod
// creating a pod
kubectl run nginx --image=nginx --restart=Never// exec into the pod
kubectl exec -it nginx /bin/sh
18. Get the IP Address of the pod you just created
kubectl get po nginx -o wide
19. Create a busybox pod and run command ls while creating it and check the logs
kubectl run busybox --image=busybox --restart=Never -- lskubectl logs busybox
20. If pod crashed check the previous logs of the pod
kubectl logs busybox -p
21. Create a busybox pod with command sleep 3600
kubectl run busybox --image=busybox --restart=Never -- /bin/sh -c "sleep 3600"
22. Check the connection of the nginx pod from the busybox pod
kubectl get po nginx -o wide// check the connection
kubectl exec -it busybox -- wget -o- <IP Address>
23. Create a busybox pod and echo message ‘How are you’ and delete it manually
kubectl run busybox --image=nginx --restart=Never -it -- echo "How are you"kubectl delete po busybox
24. Create a busybox pod and echo message ‘How are you’ and have it deleted immediately
// notice the --rm flag
kubectl run busybox --image=nginx --restart=Never -it --rm -- echo "How are you"
25. Create an nginx pod and list the pod with different levels of verbosity
// create a pod
kubectl run nginx --image=nginx --restart=Never --port=80// List the pod with different verbosity
kubectl get po nginx --v=7
kubectl get po nginx --v=8
kubectl get po nginx --v=9
26. List the nginx pod with custom columns POD_NAME and POD_STATUS
kubectl get po -o=custom-columns="POD_NAME:.metadata.name, POD_STATUS:.status.containerStatuses[].state"
27. List all the pods sorted by name
kubectl get pods --sort-by=.metadata.name
28. List all the pods sorted by created timestamp
kubectl get pods--sort-by=.metadata.creationTimestamp
===================================================================
Multi-Container Pods
====================================================================
Practice questions based on these concepts
Understand multi-container pod design patterns (eg: ambassador, adaptor, sidecar)
29. Create a Pod with three busy box containers with commands “ls; sleep 3600;”, “echo Hello World; sleep 3600;” and “echo this is the third container; sleep 3600” respectively and check the status
// first create single container pod with dry run flag
kubectl run busybox --image=busybox --restart=Never --dry-run -o yaml -- bin/sh -c "sleep 3600; ls" > multi-container.yaml// edit the pod to following yaml and create it
kubectl create -f multi-container.yaml
kubectl get po busybox
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: busybox
name: busybox
spec:
containers:
- args:
- bin/sh
- -c
- ls; sleep 3600
image: busybox
name: busybox1
resources: {}
- args:
- bin/sh
- -c
- echo Hello world; sleep 3600
image: busybox
name: busybox2
resources: {}
- args:
- bin/sh
- -c
- echo this is third container; sleep 3600
image: busybox
name: busybox3
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
multi-container pod
30. Check the logs of each container that you just created
kubectl logs busybox -c busybox1
kubectl logs busybox -c busybox2
kubectl logs busybox -c busybox3
31. Check the previous logs of the second container busybox2 if any
kubectl logs busybox -c busybox2 --previous
32. Run command ls in the third container busybox3 of the above pod
kubectl exec busybox -c busybox3 -- ls
33. Show metrics of the above pod containers and puts them into the file.log and verify
kubectl top pod busybox --containers// putting them into file
kubectl top pod busybox --containers > file.log
cat file.log
34. Create a Pod with main container busybox and which executes this “while true; do echo ‘Hi I am from Main container’ >> /var/log/index.html; sleep 5; done” and with sidecar container with nginx image which exposes on port 80. Use emptyDir Volume and mount this volume on path /var/log for busybox and on path /usr/share/nginx/html for nginx container. Verify both containers are running.
// create an initial yaml file with this
kubectl run multi-cont-pod --image=busbox --restart=Never --dry-run -o yaml > multi-container.yaml// edit the yml as below and create it
kubectl create -f multi-container.yamlkubectl get po multi-cont-pod
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: multi-cont-pod
name: multi-cont-pod
spec:
volumes:
- name: var-logs
emptyDir: {}
containers:
- image: busybox
command: ["/bin/sh"]
args: ["-c", "while true; do echo 'Hi I am from Main container' >> /var/log/index.html; sleep 5;done"]
name: main-container
resources: {}
volumeMounts:
- name: var-logs
mountPath: /var/log
- image: nginx
name: sidecar-container
resources: {}
ports:
- containerPort: 80
volumeMounts:
- name: var-logs
mountPath: /usr/share/nginx/html
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
multi-container.yaml
35. Exec into both containers and verify that main.txt exist and query the main.txt from sidecar container with curl localhost
// exec into main container
kubectl exec -it multi-cont-pod -c main-container -- sh
cat /var/log/main.txt// exec into sidecar container
kubectl exec -it multi-cont-pod -c sidecar-container -- sh
cat /usr/share/nginx/html/index.html// install curl and get default page
kubectl exec -it multi-cont-pod -c sidecar-container -- sh
# apt-get update && apt-get install -y curl
# curl localhost
===========================================================
Pod Design
============================================================
Practice questions based on these concepts
Understand how to use Labels, Selectors and Annotations
Understand Deployments and how to perform rolling updates
Understand Deployments and how to perform rollbacks
Understand Jobs and CronJobs
36. Get the pods with label information
kubectl get pods --show-labels
37. Create 5 nginx pods in which two of them is labeled env=prod and three of them is labeled env=dev
kubectl run nginx-dev1 --image=nginx --restart=Never --labels=env=dev
kubectl run nginx-dev2 --image=nginx --restart=Never --labels=env=dev
kubectl run nginx-dev3 --image=nginx --restart=Never --labels=env=devkubectl run nginx-prod1 --image=nginx --restart=Never --labels=env=prod
kubectl run nginx-prod2 --image=nginx --restart=Never --labels=env=prod
38. Verify all the pods are created with correct labels
kubeclt get pods --show-labels
39. Get the pods with label env=dev
kubectl get pods -l env=dev
40. Get the pods with label env=dev and also output the labels
kubectl get pods -l env=dev --show-labels
41. Get the pods with label env=prod
kubectl get pods -l env=prod
42. Get the pods with label env=prod and also output the labels
kubectl get pods -l env=prod --show-labels
43. Get the pods with label env
kubectl get pods -L env
44. Get the pods with labels env=dev and env=prod
kubectl get pods -l 'env in (dev,prod)'
45. Get the pods with labels env=dev and env=prod and output the labels as well
kubectl get pods -l 'env in (dev,prod)' --show-labels
46. Change the label for one of the pod to env=uat and list all the pods to verify
kubectl label pod/nginx-dev3 env=uat --overwrite
kubectl get pods --show-labels
47. Remove the labels for the pods that we created now and verify all the labels are removed
kubectl label pod nginx-dev{1..3} env-
kubectl label pod nginx-prod{1..2} env-kubectl get po --show-labels
48. Let’s add the label app=nginx for all the pods and verify
kubectl label pod nginx-dev{1..3} app=nginx
kubectl label pod nginx-prod{1..2} app=nginxkubectl get po --show-labels
49. Get all the nodes with labels (if using minikube you would get only master node)
kubectl get nodes --show-labels
50. Label the node (minikube if you are using) nodeName=nginxnode
kubectl label node minikube nodeName=nginxnode
51. Create a Pod that will be deployed on this node with the label nodeName=nginxnode
kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > pod.yaml// add the nodeSelector like below and create the pod
kubectl create -f pod.yaml
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
nodeSelector:
nodeName: nginxnode
containers:
- image: nginx
name: nginx
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
52. Verify the pod that it is scheduled with the node selector
kubectl describe po nginx | grep Node-Selectors
53. Verify the pod nginx that we just created has this label
kubectl describe po nginx | grep Labels
54. Annotate the pods with name=webapp
kubectl annotate pod nginx-dev{1..3} name=webapp
kubectl annotate pod nginx-prod{1..2} name=webapp
55. Verify the pods that have been annotated correctly
kubectl describe po nginx-dev{1..3} | grep -i annotations
kubectl describe po nginx-prod{1..2} | grep -i annotations
56. Remove the annotations on the pods and verify
kubectl annotate pod nginx-dev{1..3} name-
kubectl annotate pod nginx-prod{1..2} name-kubectl describe po nginx-dev{1..3} | grep -i annotations
kubectl describe po nginx-prod{1..2} | grep -i annotations
57. Remove all the pods that we created so far
kubectl delete po --all
58. Create a deployment called webapp with image nginx with 5 replicas
kubectl create deploy webapp --image=nginx --dry-run -o yaml > webapp.yaml// change the replicas to 5 in the yaml and create it
kubectl create -f webapp.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: webapp
name: webapp
spec:
replicas: 5
selector:
matchLabels:
app: webapp
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: webapp
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
59. Get the deployment you just created with labels
kubectl get deploy webapp --show-labels
60. Output the yaml file of the deployment you just created
kubectl get deploy webapp -o yaml
61. Get the pods of this deployment
// get the label of the deployment
kubectl get deploy --show-labels// get the pods with that label
kubectl get pods -l app=webapp
62. Scale the deployment from 5 replicas to 20 replicas and verify
kubectl scale deploy webapp --replicas=20kubectl get po -l app=webapp
63. Get the deployment rollout status
kubectl rollout status deploy webapp
64. Get the replicaset that created with this deployment
kubectl get rs -l app=webapp
65. Get the yaml of the replicaset and pods of this deployment
kubectl get rs -l app=webapp -o yaml
kubectl get po -l app=webapp -o yaml
66. Delete the deployment you just created and watch all the pods are also being deleted
kubectl delete deploy webappkubectl get po -l app=webapp -w
67. Create a deployment of webapp with image nginx:1.17.1 with container port 80 and verify the image version
kubectl create deploy webapp --image=nginx:1.17.1 --dry-run -o yaml > webapp.yaml// add the port section and create the deployment
kubectl create -f webapp.yaml// verify
kubectl describe deploy webapp | grep Image
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: webapp
name: webapp
spec:
replicas: 1
selector:
matchLabels:
app: webapp
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: webapp
spec:
containers:
- image: nginx:1.17.1
name: nginx
ports:
- containerPort: 80
resources: {}
status: {}
68. Update the deployment with the image version 1.17.4 and verify
kubectl set image deploy/webapp nginx=nginx:1.17.4kubectl describe deploy webapp | grep Image
69. Check the rollout history and make sure everything is ok after the update
kubectl rollout history deploy webappkubectl get deploy webapp --show-labels
kubectl get rs -l app=webapp
kubectl get po -l app=webapp
70. Undo the deployment to the previous version 1.17.1 and verify Image has the previous version
kubectl rollout undo deploy webappkubectl describe deploy webapp | grep Image
71. Update the deployment with the image version 1.16.1 and verify the image and also check the rollout history
kubectl set image deploy/webapp nginx=nginx:1.16.1kubectl describe deploy webapp | grep Imagekubectl rollout history deploy webapp
72. Update the deployment to the Image 1.17.1 and verify everything is ok
kubectl rollout undo deploy webapp --to-revision=3kubectl describe deploy webapp | grep Imagekubectl rollout status deploy webapp
73. Update the deployment with the wrong image version 1.100 and verify something is wrong with the deployment
kubectl set image deploy/webapp nginx=nginx:1.100kubectl rollout status deploy webapp (still pending state)kubectl get pods (ImagePullErr)
74. Undo the deployment with the previous version and verify everything is Ok
kubectl rollout undo deploy webapp
kubectl rollout status deploy webappkubectl get pods
75. Check the history of the specific revision of that deployment
kubectl rollout history deploy webapp --revision=7
76. Pause the rollout of the deployment
kubectl rollout pause deploy webapp
77. Update the deployment with the image version latest and check the history and verify nothing is going on
kubectl set image deploy/webapp nginx=nginx:latestkubectl rollout history deploy webapp (No new revision)
78. Resume the rollout of the deployment
kubectl rollout resume deploy webapp
79. Check the rollout history and verify it has the new version
kubectl rollout history deploy webappkubectl rollout history deploy webapp --revision=9
80. Apply the autoscaling to this deployment with minimum 10 and maximum 20 replicas and target CPU of 85% and verify hpa is created and replicas are increased to 10 from 1
kubectl autoscale deploy webapp --min=10 --max=20 --cpu-percent=85kubectl get hpakubectl get pod -l app=webapp
81. Clean the cluster by deleting deployment and hpa you just created
kubectl delete deploy webappkubectl delete hpa webapp
=================================================
82. Create a Job with an image node which prints node version and also verifies there is a pod created for this job
kubectl create job nodeversion --image=node -- node -vkubectl get job -w
kubectl get pod
83. Get the logs of the job just created
kubectl logs <pod name> // created from the job
84.Output the yaml file for the Job with the image busybox which echos “Hello I am from job”
kubectl create job hello-job --image=busybox --dry-run -o yaml -- echo "Hello I am from job"
85. Copy the above YAML file to hello-job.yaml file and create the job
kubectl create job hello-job --image=busybox --dry-run -o yaml -- echo "Hello I am from job" > hello-job.yamlkubectl create -f hello-job.yaml
86. Verify the job and the associated pod is created and check the logs as well
kubectl get job
kubectl get pokubectl logs hello-job-*
87. Delete the job we just created
kubectl delete job hello-job
88. Create the same job and make it run 10 times one after one
kubectl create job hello-job --image=busybox --dry-run -o yaml -- echo "Hello I am from job" > hello-job.yaml// edit the yaml file to add completions: 10
kubectl create -f hello-job.yaml
apiVersion: batch/v1
kind: Job
metadata:
creationTimestamp: null
name: hello-job
spec:
completions: 10
template:
metadata:
creationTimestamp: null
spec:
containers:
- command:
- echo
- Hello I am from job
image: busybox
name: hello-job
resources: {}
restartPolicy: Never
status: {}
89. Watch the job that runs 10 times one by one and verify 10 pods are created and delete those after it’s completed
kubectl get job -w
kubectl get pokubectl delete job hello-job
90. Create the same job and make it run 10 times parallel
kubectl create job hello-job --image=busybox --dry-run -o yaml -- echo "Hello I am from job" > hello-job.yaml// edit the yaml file to add parallelism: 10
kubectl create -f hello-job.yaml
apiVersion: batch/v1
kind: Job
metadata:
creationTimestamp: null
name: hello-job
spec:
parallelism: 10
template:
metadata:
creationTimestamp: null
spec:
containers:
- command:
- echo
- Hello I am from job
image: busybox
name: hello-job
resources: {}
restartPolicy: Never
status: {}
91. Watch the job that runs 10 times parallelly and verify 10 pods are created and delete those after it’s completed
kubectl get job -w
kubectl get pokubectl delete job hello-job
92. Create a Cronjob with busybox image that prints date and hello from kubernetes cluster message for every minute
kubectl create cronjob date-job --image=busybox --schedule="*/1 * * * *" -- bin/sh -c "date; echo Hello from kubernetes cluster"
93. Output the YAML file of the above cronjob
kubectl get cj date-job -o yaml
94. Verify that CronJob creating a separate job and pods for every minute to run and verify the logs of the pod
kubectl get job
kubectl get pokubectl logs date-job-<jobid>-<pod>
95. Delete the CronJob and verify all the associated jobs and pods are also deleted.
kubectl delete cj date-job// verify pods and jobs
kubectl get po
kubectl get job
============================================
State Persistence
==========================================================
96. List Persistent Volumes in the cluster
kubectl get pv
97. Create a hostPath PersistentVolume named task-pv-volume with storage 10Gi, access modes ReadWriteOnce, storageClassName manual, and volume at /mnt/data and verify
kubectl create -f task-pv-volume.yamlkubectl get pv
apiVersion: v1
kind: PersistentVolume
metadata:
name: task-pv-volume
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/mnt/data"
98. Create a PersistentVolumeClaim of at least 3Gi storage and access mode ReadWriteOnce and verify status is Bound
kubectl create -f task-pv-claim.yamlkubectl get pvc
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: task-pv-claim
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
99. Delete persistent volume and PersistentVolumeClaim we just created
kubectl delete pvc task-pv-claim
kubectl delete pv task-pv-volume
100. Create a Pod with an image Redis and configure a volume that lasts for the lifetime of the Pod
// emptyDir is the volume that lasts for the life of the podkubectl create -f redis-storage.yaml
apiVersion: v1
kind: Pod
metadata:
name: redis
spec:
containers:
- name: redis
image: redis
volumeMounts:
- name: redis-storage
mountPath: /data/redis
volumes:
- name: redis-storage
emptyDir: {}
101. Exec into the above pod and create a file named file.txt with the text ‘This is called the file’ in the path /data/redis and open another tab and exec again with the same pod and verifies file exist in the same path.
// first terminal
kubectl exec -it redis-storage /bin/sh
cd /data/redis
echo 'This is called the file' > file.txt//open another tab
kubectl exec -it redis-storage /bin/sh
cat /data/redis/file.txt
102. Delete the above pod and create again from the same yaml file and verifies there is no file.txt in the path /data/redis
kubectl delete pod rediskubectl create -f redis-storage.yaml
kubectl exec -it redis-storage /bin/sh
cat /data/redis/file.txt // file doesn't exist
103. Create PersistentVolume named task-pv-volume with storage 10Gi, access modes ReadWriteOnce, storageClassName manual, and volume at /mnt/data and Create a PersistentVolumeClaim of at least 3Gi storage and access mode ReadWriteOnce and verify status is Bound
kubectl create -f task-pv-volume.yaml
kubectl create -f task-pv-claim.yamlkubectl get pv
kubectl get pvc
104. Create an nginx pod with containerPort 80 and with a PersistentVolumeClaim task-pv-claim and has a mouth path "/usr/share/nginx/html"
kubectl create -f task-pv-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: task-pv-pod
spec:
volumes:
- name: task-pv-storage
persistentVolumeClaim:
claimName: task-pv-claim
containers:
- name: task-pv-container
image: nginx
ports:
- containerPort: 80
name: "http-server"
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: task-pv-storage
==============================================================
Configuration
============================================================
Understand ConfigMaps
Understand SecurityContexts
Define an application’s resource requirements
Create & Consume Secrets
Understand ServiceAccounts
105. List all the configmaps in the cluster
kubectl get cm
or
kubectl get configmap
106. Create a configmap called myconfigmap with literal value appname=myapp
kubectl create cm myconfigmap --from-literal=appname=myapp
107. Verify the configmap we just created has this data
// you will see under datakubectl get cm -o yaml
or
kubectl describe cm
108. delete the configmap myconfigmap we just created
kubectl delete cm myconfigmap
109. Create a file called config.txt with two values key1=value1 and key2=value2 and verify the file
cat >> config.txt << EOF
key1=value1
key2=value2
EOFcat config.txt
110. Create a configmap named keyvalcfgmap and read data from the file config.txt and verify that configmap is created correctly
kubectl create cm keyvalcfgmap --from-file=config.txtkubectl get cm keyvalcfgmap -o yaml
111. Create an nginx pod and load environment values from the above configmap keyvalcfgmap and exec into the pod and verify the environment variables and delete the pod
// first run this command to save the pod yml
kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx-pod.yml// edit the yml to below file and create
kubectl create -f nginx-pod.yml// verify
kubectl exec -it nginx -- envkubectl delete po nginx
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
envFrom:
- configMapRef:
name: keyvalcfgmap
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
112. Create an env file file.env with var1=val1 and create a configmap envcfgmap from this env file and verify the configmap
echo var1=val1 > file.env
cat file.envkubectl create cm envcfgmap --from-env-file=file.env
kubectl get cm envcfgmap -o yaml --export
113. Create an nginx pod and load environment values from the above configmap envcfgmap and exec into the pod and verify the environment variables and delete the pod
// first run this command to save the pod yml
kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx-pod.yml// edit the yml to below file and create
kubectl create -f nginx-pod.yml// verify
kubectl exec -it nginx -- envkubectl delete po nginx
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
containers:
- image: nginx
name: nginx
resources: {}
env:
- name: ENVIRONMENT
valueFrom:
configMapKeyRef:
name: envcfgmap
key: var1
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
114. Create a configmap called cfgvolume with values var1=val1, var2=val2 and create an nginx pod with volume nginx-volume which reads data from this configmap cfgvolume and put it on the path /etc/cfg
// first create a configmap cfgvolume
kubectl create cm cfgvolume --from-literal=var1=val1 --from-literal=var2=val2// verify the configmap
kubectl describe cm cfgvolume// create the config map
kubectl create -f nginx-volume.yml// exec into the pod
kubectl exec -it nginx -- /bin/sh// check the path
cd /etc/cfg
ls
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: nginx
name: nginx
spec:
volumes:
- name: nginx-volume
configMap:
name: cfgvolume
containers:
- image: nginx
name: nginx
resources: {}
volumeMounts:
- name: nginx-volume
mountPath: /etc/cfg
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
115. Create a pod called secbusybox with the image busybox which executes command sleep 3600 and makes sure any Containers in the Pod, all processes run with user ID 1000 and with group id 2000 and verify.
// create yml file with dry-run
kubectl run secbusybox --image=busybox --restart=Never --dry-run -o yaml -- /bin/sh -c "sleep 3600;" > busybox.yml// edit the pod like below and create
kubectl create -f busybox.yml// verify
kubectl exec -it secbusybox -- sh
id // it will show the id and group
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: secbusybox
name: secbusybox
spec:
securityContext: # add security context
runAsUser: 1000
runAsGroup: 2000
containers:
- args:
- /bin/sh
- -c
- sleep 3600;
image: busybox
name: secbusybox
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
116. Create the same pod as above this time set the securityContext for the container as well and verify that the securityContext of container overrides the Pod level securityContext.
// create yml file with dry-run
kubectl run secbusybox --image=busybox --restart=Never --dry-run -o yaml -- /bin/sh -c "sleep 3600;" > busybox.yml// edit the pod like below and create
kubectl create -f busybox.yml// verify
kubectl exec -it secbusybox -- sh
id // you can see container securityContext overides the Pod level
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: secbusybox
name: secbusybox
spec:
securityContext:
runAsUser: 1000
containers:
- args:
- /bin/sh
- -c
- sleep 3600;
image: busybox
securityContext:
runAsUser: 2000
name: secbusybox
resources: {}
dnsPolicy: ClusterFirst
restartPolicy: Never
status: {}
117. Create pod with an nginx image and configure the pod with capabilities NET_ADMIN and SYS_TIME verify the capabilities
// create the yaml file
kubectl run nginx --image=nginx --restart=Never --dry-run -o yaml > nginx.yml// edit as below and create pod
kubectl create -f nginx.yml// exec and verify
kubectl exec -it nginx -- sh
cd /proc/1
cat status// you should see these values
CapPrm: 00000000aa0435fb
CapEff: 00000000aa0435fb
apiVersion: v1