DEV Community

Cover image for CKAD Exam Practice Exercise : Core Concepts
Vijay Daswani
Vijay Daswani

Posted on

CKAD Exam Practice Exercise : Core Concepts

Core Concepts (13%)

Practice questions based on these concepts

  • Understand Kubernetes API Primitives
  • Create and Configure Basic Pods

Exercise

List all the namespaces in the cluster


kubectl get namespaces

kubectl get ns
Enter fullscreen mode Exit fullscreen mode

Alt Text

List all the pods in all namespaces

kubectl get po --all-namespaces
Enter fullscreen mode Exit fullscreen mode

Alt Text

List all the pods in the particular namespace

kubectl get po -n <namespace name>
Enter fullscreen mode Exit fullscreen mode

Alt Text

List all the services in the particular namespace

kubectl get svc -n <namespace name>
Enter fullscreen mode Exit fullscreen mode

Alt Text

List all the pods showing name and namespace with a json path expression

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

Alt Text

Create an rabbitmq pod in a default namespace and verify the pod running

// creating a pod
kubectl run rabbitmq --image=rabbitmq --restart=Never

// List the pod
kubectl get po
Enter fullscreen mode Exit fullscreen mode

Alt Text

Create the 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
Enter fullscreen mode Exit fullscreen mode

Alt Text

Output the yaml file of the pod you just created

kubectl get po nginx -o yaml
Enter fullscreen mode Exit fullscreen mode

Alt Text

Output the yaml file of the pod you just created without the cluster-specific information

kubectl get po nginx -o yaml --export
Enter fullscreen mode Exit fullscreen mode

Alt Text

Get the complete details of the pod you just created

kubectl describe pod nginx
Enter fullscreen mode Exit fullscreen mode

Alt Text

Delete the pod you just created

kubectl delete po nginx

kubectl delete -f nginx-pod.yaml
Enter fullscreen mode Exit fullscreen mode

Alt Text

Delete the pod you just created without any delay (force delete)

kubectl delete po nginx --grace-period=0 --force
Enter fullscreen mode Exit fullscreen mode

Alt Text

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
Enter fullscreen mode Exit fullscreen mode

Alt Text

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-alpine

kubectl describe po nginx

// another way it will open vi editor and change the version
kubeclt edit po nginx

kubectl describe po nginx
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Check the Image version without the describe command

kubectl get po nginx -o jsonpath='{.spec.containers[].image}{"\n"}'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Get the IP Address of the pod you just created

kubectl get po nginx -o wide
Enter fullscreen mode Exit fullscreen mode

Create a busybox pod and run command ls while creating it and check the logs

kubectl run busybox --image=busybox --restart=Never -- ls

kubectl logs busybox
Enter fullscreen mode Exit fullscreen mode

If pod crashed check the previous logs of the pod

kubectl logs busybox -p
Enter fullscreen mode Exit fullscreen mode

Create a busybox pod with command sleep 3600

kubectl run busybox --image=busybox --restart=Never -- /bin/sh -c "sleep 3600"
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

List all the pods sorted by name

kubectl get pods --sort-by=.metadata.name
Enter fullscreen mode Exit fullscreen mode

List all the pods sorted by created timestamp

kubectl get pods--sort-by=.metadata.creationTimestamp
Enter fullscreen mode Exit fullscreen mode

Multi Container Pod Section based exercise to be continued in the next post.

Top comments (0)