CKAD Exam Practice Exercise : Core Concepts
Tags: kubernetes, developer, devops, cloud
Published: 2020-06-07 12:40:02

# Core Concepts (13%)


## Practice questions based on these concepts

* Understand Kubernetes API Primitives
* Create and Configure Basic Pods

## Exercise

<summary>List all the namespaces in the cluster</summary>
```console

kubectl get namespaces

kubectl get ns
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/7q36p19kfzdvlzlnt52o.png)

<summary>List all the pods in all namespaces</summary>
```
kubectl get po --all-namespaces
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gw7ddhq84irtd4k8vsoo.png)
<summary>List all the pods in the particular namespace</summary>


```
kubectl get po -n <namespace name>
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/629d0sxrxjpd72m4m589.png)

<summary>List all the services in the particular namespace</summary>


```
kubectl get svc -n <namespace name>
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/eoqb91z2lvbcfvg7jnlv.png)


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


```
kubectl get pods -o=jsonpath="{.items[*]['metadata.name', 'metadata.namespace']}"
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/l0ceftz4euru4jtpnyln.png)


<summary>Create an rabbitmq pod in a default namespace and verify the pod running</summary>


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

// List the pod
kubectl get po
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/wd7ty4ctdkbkrknkdrq2.png)

<summary>Create the nginx pod with a yaml file</summary>


```
// 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
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/csl98cgt9msrojrjiknh.png)


<summary>Output the yaml file of the pod you just created</summary>


```
kubectl get po nginx -o yaml
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jp860bmv3wy9uh47gcau.png)


<summary>Output the yaml file of the pod you just created without the cluster-specific information</summary>


```
kubectl get po nginx -o yaml --export
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ons92k4dhvh1bj0kxziu.png)



<summary>Get the complete details of the pod you just created</summary>


```
kubectl describe pod nginx
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/de2edri8ue0abu12ee6k.png)



<summary>Delete the pod you just created</summary>


```
kubectl delete po nginx

kubectl delete -f nginx-pod.yaml
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2ws36hztajiz22gdhowx.png)



<summary>Delete the pod you just created without any delay (force delete)</summary>


```
kubectl delete po nginx --grace-period=0 --force
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tfrbzu78uo21i4zw4w42.png)



<summary>Create the nginx pod with version 1.17.4 and expose it on port 80</summary>


```
kubectl run nginx --image=nginx:1.17.4 --restart=Never --port=80
```
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/vs30g8kaole8jozf79f2.png)



<summary>Change the Image version to 1.15-alpine for the pod you just created and verify the image version is updated</summary>


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




<summary>Change the Image version back to 1.17.1 for the pod you just updated and observe the changes</summary>


```
kubectl set image pod/nginx nginx=nginx:1.17.1

kubectl describe po nginx

kubectl get po nginx -w # watch it
```




<summary>Check the Image version without the describe command</summary>


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




<summary>Create the nginx pod and execute the simple shell on the pod</summary>


```
// creating a pod
kubectl run nginx --image=nginx --restart=Never

// exec into the pod
kubectl exec -it nginx /bin/sh
```




<summary>Get the IP Address of the pod you just created</summary>


```
kubectl get po nginx -o wide
```



<summary>Create a busybox pod and run command ls while creating it and check the logs</summary>


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

kubectl logs busybox
```




<summary>If pod crashed check the previous logs of the pod</summary>


```
kubectl logs busybox -p
```




<summary>Create a busybox pod with command sleep 3600</summary>


```
kubectl run busybox --image=busybox --restart=Never -- /bin/sh -c "sleep 3600"
```




<summary>Check the connection of the nginx pod from the busybox pod</summary>


```
kubectl get po nginx -o wide

// check the connection
kubectl exec -it busybox -- wget -o- <IP Address>
```




<summary>Create a busybox pod and echo message ‘How are you’ and delete it manually</summary>


```
kubectl run busybox --image=nginx --restart=Never -it -- echo "How are you"

kubectl delete po busybox
```




<summary>Create a busybox pod and echo message ‘How are you’ and have it deleted immediately</summary>


```
// notice the --rm flag
kubectl run busybox --image=nginx --restart=Never -it --rm -- echo "How are you"
```




<summary>Create an nginx pod and list the pod with different levels of verbosity</summary>


```
// 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
```




<summary>List the nginx pod with custom columns POD_NAME and POD_STATUS</summary>


```
kubectl get po -o=custom-columns="POD_NAME:.metadata.name, POD_STATUS:.status.containerStatuses[].state"
```




<summary>List all the pods sorted by name</summary>


```
kubectl get pods --sort-by=.metadata.name
```




<summary>List all the pods sorted by created timestamp</summary>


```
kubectl get pods--sort-by=.metadata.creationTimestamp
```

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