Kubernetes – practise exercise – 1

Reading Time: 3 mins

After completing CKA and CKAD certifications, I thought of writing few posts with exercises that can be used by anyone preparing for K8s certifications. I will try my best to cover from basics to complicated ones with both imperative and declarative way wherever possible. This is not same as actual exam and created just for practice, this might help you a lot while preparing as I have covered most of the topics that is part of exam curriculum.

Tip: Make use of imperative commands wherever possible in exam.

  1. Create a new pod app-1 with nginx image and list the pods in default namespace.
syntax: kubectl run <pod-name> --image=<image name>

create the pod: kubectl run app-1 --image=nginx

List the pods: kubectl get pods

declarative: save the below code into yaml file and run the command kubectl create -f <file-name>.yaml


2. Delete the pod app-1 we just created:

syntax: kubectl delete pod <pod name>

kubectl delete pod app-1

3. Create a new namespace amazon:

syntax: kubectl create ns <name space>

kubectl create ns amazon

declarative: save the below code into yaml file and run the command kubectl create -f <file-name>.yaml

4.create a new pod app-2 with nginx image in amazon namespace

syntax: kubectl run <pod name> --image=<image name> -n <name space>

kubectl run app-2 --image=nginx -n amazon


5. List the pods in amazon namespace

 syntax:      kubectl get pods -n <name space>

kubectl get pods -n amazon

6. Create a new pod app-3 with nginx image and tag 3

syntax:  kubectl run <pod name> --image=<image name>:<tag>

kubectl run app-3 --image=nginx:3


apiVersion: v1
kind: Pod
name: app-3
- name: app-3
image: nginx:3

7. Create a new pod app-4 with image nginx:12345, check the status of the pod created.

imperative: kubectl run app-4 --image=nginx:12345

                   kubectl get pods app-4

8. Update the pod app-4 to use correct image nginx

                   kubectl set image pod/app-4 app-4=nginx

#search for image nginx:12345 and replace it with nginx , save and exit.


kubectl edit pod app-4


9. List pods from all namespaces:

kubectl get pods --all-namespaces

10. List services from all namespaces:

kubectl get svc --all-namespaces

11. List services from default namespace:

kubectl get svc

12. Get complete details of pod app-4

kubectl describe pod app-4

13. Check pods from all namespaces with node names they are located and IP address of pods: 

kubectl get pods --all-namespaces -o wide

14. Output the pod app-4 to /tmp directory as output-app4.yaml: 

kubectl get pod app-4 -o yaml > /tmp/output-app4.yaml

Tip: https://kubernetes.io/docs/reference/kubectl/cheatsheet/   Refer to this URL for more examples.

15. List all the pods showing name and namespace with  jsonpath expression:

kubectl get pods -o=jsonpath="{.items[*]['metadata.name', 'metadata.namespace']}"

16. Create the pod app-5 with nginx image and expose it on port 80

kubectl run app-5 --image=nginx --port=80

17.  Create a pod app-6 with busy box image and command sleep 4800: 

kubectl run app-6 --image=busybox -- /bin/sh -c "sleep 4800"

18. Create a pod app-7 with busy box image and run command hostname while creating, check the logs of pod app-7: 

kubectl run app-7 --image=busybox --restart=Never -- hostname


20. Connect to the pod app-6 and check hostname

kubectl exec -it app-6 -- sh
/ # hostname

21. Check the Image version of pod app-7 using jsonpath: 

kubectl get po app-7 -o jsonpath='{.spec.containers[].image}{"\n"}'