kubectl essentials, manifests, troubleshooting order, RBAC and exam-speed tricks.
In the print dialog, choose "Save as PDF" as the destination.
Setup for speed
alias k=kubectl
source <(kubectl completion bash)
exportdo="--dry-run=client -o yaml"
k config get-contexts
k config use-context CONTEXT
k config set-context --current --namespace=shop
Always confirm the context with k config current-context before changing anything.
Read cluster state
k get nodes -o wide
k get pods -A
k get pods -n shop -o wide --show-labels
k get deploy,svc,ingress,pvc -n shop
k get events -n shop --sort-by=.lastTimestamp
k describe pod NAME -n shop
k explain pod.spec.containers.resources
k api-resources
k top pod -n shop
Create and change
k create deployment api --image=nginx:1.27 --replicas=3 $do > deploy.yaml
k run tmp --rm -it --image=busybox:1.36 --restart=Never -- sh
k expose deployment api --port=80 --target-port=8080
k scale deployment api --replicas=5
k set image deployment/api nginx=nginx:1.27.1
k apply -f deploy.yaml
k delete -f deploy.yaml
k label pod NAME tier=backend
k annotate pod NAME note=checked
k edit deployment api
Rollouts
k rollout status deployment/api
k rollout history deployment/api
k rollout undo deployment/api
k rollout undo deployment/api --to-revision=2
k rollout restart deployment/api
Debugging order
k get pods -n shop # status and restarts
k describe pod NAME -n shop # events at the bottom
k logs NAME -n shop --previous # logs of the crashed container
k logs NAME -c CONTAINER -n shop # a specific container
k exec -it NAME -n shop -- sh
k get endpoints SERVICE -n shop # empty means selector or readiness problem
k debug NAME -it --image=busybox:1.36 # ephemeral debug container
Service DNS name: SERVICE.NAMESPACE.svc.cluster.local.
RBAC
k create role reader --verb=get,list,watch --resource=pods -n shop
k create rolebinding rb --role=reader --serviceaccount=shop:app -n shop
k auth can-i list pods -n shop --as=system:serviceaccount:shop:app
k auth can-i --list -n shop
Nodes and maintenance
k cordon NODE # stop new scheduling
k drain NODE --ignore-daemonsets --delete-emptydir-data
k uncordon NODE
k taint nodes NODE dedicated=gpu:NoSchedule
k label node NODE disktype=ssd
Storage
Item
Note
PVC ReadWriteOnce
One node at a time
PVC ReadWriteMany
Needs a file-capable storage class
Reclaim Delete
Claim deletion deletes the disk
StatefulSet
Stable names and a volume per Pod
JSONPath and output
k get pods -o jsonpath='{.items[*].metadata.name}'
k get pod NAME -o jsonpath='{.status.containerStatuses[0].lastState}'
k get secret NAME -o jsonpath='{.data.password}' | base64 -d
k get pods -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeName