kubectl and Kubernetes Objects

The CKA and CKAD are performance exams under a clock, so speed matters more than breadth. These are the commands that come up, plus the order to debug a pod that will not start.

Everyday commands

CommandWhat it does
kubectl get pods -AEvery pod in every namespace
kubectl get pods -o wideAdds node and IP columns
kubectl describe pod NAMEEvents and state. The first debugging step
kubectl logs NAME -c containerLogs from one container
kubectl logs NAME --previousLogs from the last crashed instance
kubectl exec -it NAME -- shShell inside a running container
kubectl apply -f file.yamlDeclarative create or update
kubectl delete pod NAME --forceRemove a stuck pod
kubectl get events --sort-by=.metadata.creationTimestampRecent cluster events
kubectl top nodesNode CPU and memory. Needs metrics-server

Speed under a clock

CommandWhy it saves time
kubectl run nginx --image=nginx --dry-run=client -o yamlGenerates a manifest to edit
kubectl create deploy web --image=nginx --replicas=3Imperative deployment in one line
kubectl expose deploy web --port=80Creates the service without writing YAML
kubectl explain pod.spec.containersField reference without leaving the terminal
alias k=kubectlSet this first, in every session
kubectl config set-context --current --namespace=NSStops repeating -n

Core objects

ObjectWhat it is for
PodOne or more containers sharing a network namespace
ReplicaSetKeeps a number of identical pods running
DeploymentManages ReplicaSets, gives rolling updates
StatefulSetStable identity and storage per pod
DaemonSetOne pod per node
Job / CronJobRun to completion, once or on a schedule
ServiceStable address in front of pods
IngressHTTP routing into the cluster
ConfigMap / SecretNon-secret and secret configuration
PersistentVolumeClaimA request for storage
NetworkPolicyPod-level firewall rules
RBAC Role / RoleBindingPermissions, and who holds them

Pod not starting: debug order

StatusUsual causeCheck
PendingNo node can schedule itdescribe pod, look at Events for taints or resources
ImagePullBackOffImage name or registry authImage tag, imagePullSecrets
CrashLoopBackOffContainer exits immediatelylogs --previous, then the command and args
CreateContainerConfigErrorMissing ConfigMap or SecretThe referenced object exists in the namespace
Running but not ReadyReadiness probe failingProbe path, port and initial delay
Terminating foreverFinalizer or stuck volumedescribe pod, check finalizers

Exams covered

Now test yourself

Memorizing a table is a start. Practice questions are what make it stick, and every answer carries the full explanation.