Lab 06 - Finding your way around kubectl

Lab 06 - Finding your way around kubectl

Infodiscovery

Goal: never need the internet to answer a Kubernetes question again. Chapter: Finding your way

Each task states what you should end up seeing. Try it yourself first; the box holds one working answer.


Task 1. List every kind of object this cluster knows about, and find out whether ingresses is namespaced and what its short name is.

kubectl api-resources
kubectl api-resources | grep -i ingress

ingresses ing networking.k8s.io/v1 true Ingress - short name ing, and true means namespaced.


Task 2. List only the kinds that are not namespaced. You should see nodes, persistentvolumes, storageclasses, namespaces themselves.

kubectl api-resources --namespaced=false

Task 3. Without leaving the terminal, find out which fields go under a Pod’s spec.containers[].resources, and what spec.restartPolicy accepts.

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

explain is generated from the API server you are talking to, so it is always correct for your version.


Task 4. Find the minimum required fields of an Ingress rule - the ones marked required.

kubectl explain ingress.spec.rules
kubectl explain ingress.spec.rules.http.paths --recursive

Look for -required- in the field descriptions.


Task 5. You want to create a Deployment imperatively but do not remember the flags. Find the built-in examples.

kubectl create deployment --help

The Examples: block at the bottom of any --help is usually the fastest answer in the whole toolchain.


Task 6. Create a Pod named explorer running nginx:1.27, then print only its IP address and the node it landed on - no other output.

kubectl run explorer --image nginx:1.27
kubectl get pod explorer -o jsonpath='{.status.podIP} {.spec.nodeName}{"\n"}'

or, more readably:

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

Task 7. Show the full stored object for explorer, including the defaults the API server filled in that you never wrote. Find terminationGracePeriodSeconds and dnsPolicy.

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

Everything you did not specify was defaulted by the API server - this is why get -o yaml is much longer than the manifest you applied.


Task 8. Show the events for explorer: scheduled, image pulled, started.

kubectl describe pod explorer          # Events are at the bottom
kubectl get events --sort-by=.lastTimestamp | tail -10

Task 9. Print the nginx access log of explorer, then follow it live while you curl the Pod from a throwaway Pod.

kubectl logs explorer
kubectl logs -f explorer &
kubectl run tmp --image busybox --restart=Never -it --rm -- \
  wget -qO- --timeout=3 "$(kubectl get pod explorer -o jsonpath='{.status.podIP}')"
kill %1

Task 10. Find out whether you are allowed to delete nodes in this cluster.

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

Clean up

kubectl delete pod explorer
Tip

Four commands answer almost everything: api-resources (what exists), explain (what fields), describe (what happened), logs (what the app said).