Lab 03 - The Kubernetes API and kubectl

Lab 03 - The Kubernetes API and kubectl

Infodiscover

Goal: answer any Kubernetes question from the terminal - and see the HTTP behind kubectl. Chapter: The Kubernetes API and kubectl

Try each task first; the box holds one working answer.


Task 1. List every kind the cluster knows. Find the short name and API group of deployments, and whether it is namespaced.

kubectl api-resources | grep -i deploy

deployments deploy apps/v1 true Deployment


Task 2. List the kinds that are not namespaced.

kubectl api-resources --namespaced=false

Task 3. Which API group/versions does the server offer? Is there more than one version of anything?

kubectl api-versions

Task 4. Without the internet, find which fields go under spec.containers[].resources of a Pod, and what spec.restartPolicy accepts.

kubectl explain pod.spec.containers.resources
kubectl explain pod.spec.restartPolicy

Task 5. Show the HTTP requests kubectl sends for kubectl get pods -n kube-system. Expected: a GET https://...:6443/api/v1/namespaces/kube-system/pods.

kubectl get pods -n kube-system -v=6 2>&1 | grep GET

-v=8 also prints the response bodies.


Task 6. Read the list of Deployments in kube-system straight from the API path, without the get deploy command.

kubectl get --raw /apis/apps/v1/namespaces/kube-system/deployments | head -c 400; echo

Task 7. Open a local proxy on port 8001 and list the Pod names in kube-system with curl.

kubectl proxy --port 8001 &
curl -s localhost:8001/api/v1/namespaces/kube-system/pods | grep '"name": "' | head
kill %1

The proxy authenticates with your kubeconfig, so curl needs no certificate.


Task 8. Create a Pod explorer (nginx:1.27), then print only its IP and node.

kubectl run explorer --image nginx:1.27
kubectl get pod explorer -o custom-columns=IP:.status.podIP,NODE:.spec.nodeName

Task 9. Show the stored object of explorer and find two fields you never wrote: terminationGracePeriodSeconds and dnsPolicy.

kubectl get pod explorer -o yaml | grep -E 'terminationGracePeriod|dnsPolicy'

The API server filled in the defaults.


Task 10. Show the events of explorer, then its log.

kubectl describe pod explorer | tail -8
kubectl logs explorer

Task 11. Are you allowed to delete nodes? List everything you may do.

kubectl auth can-i delete nodes
kubectl auth can-i --list | head -20

Clean up

kubectl delete pod explorer
Tip

api-resources (what exists), explain (what fields), describe (what happened), logs (what the app said).