What it does
kubectl sends requests to the Kubernetes API server. It reads the cluster address and your credentials from a kubeconfig file (~/.kube/config or $KUBECONFIG). One file can hold several contexts (cluster + user + namespace).
You work in two styles: imperative (kubectl create, run, scale, expose — fast, great in the CKA exam) and declarative (kubectl apply -f with YAML in Git — the GitOps way). --dry-run=client -o yaml turns an imperative command into YAML you can edit.
How to write it
kubectl [COMMAND] [TYPE] [NAME] [flags]
kubectl apply -f FILE|DIR|URLMain parts
Common commands
getTYPE[NAME]- List resources.
describeTYPENAME- Details and events (first step of debugging).
apply-fFILE- Create or update from YAML.
create/run/expose- Create resources imperatively.
deleteTYPENAME/-fFILE- Delete resources.
logsPOD[-c C] [-f] [--previous]- Container logs.
exec-itPOD--sh- Run a command in a container.
editTYPENAME- Edit a live object in your editor.
scale/autoscale- Change replicas / create an HPA.
rolloutstatus|history|undo|restart- Manage deployments.
port-forwardPOD|svc/NAMEL:R- Forward a local port into the cluster.
topnodes|pods- CPU and memory use (needs metrics-server).
cordon/uncordon/drainNODE- Node maintenance.
taint/label/annotate- Change metadata.
authcan-iVERBRES[--as USER]- Check RBAC rights.
configget-contexts|use-context|set-context- Work with kubeconfig.
explainTYPE.FIELD- Built-in docs for YAML fields.
debug- Start a debug container in a pod or on a node.
diff-fFILE- Show what apply would change.
Options 10
Most used
Global flags
Nothing matches this filter.
Examples 11
$ run as a normal user# run as root (or with sudo)
1See the cluster
kubectl cluster-info kubectl get nodes -o wide kubectl get pods -A2Create a deployment and a service fast (CKA style)
kubectl create deployment shop --image=ghcr.io/s0sta/shop:1.4.0 --replicas=3 -n shop kubectl expose deployment shop --port=80 --target-port=8080 -n shop kubectl get deploy,rs,pods,svc -n shop3Generate YAML, edit, apply
kubectl create deployment api --image=nginx:1.27 --dry-run=client -o yaml > api.yaml vim api.yaml kubectl apply -f api.yaml kubectl diff -f api.yaml4Debug a pod that does not start
kubectl describe pod shop-7c9d6b8f5-x2k4p -n shop kubectl logs shop-7c9d6b8f5-x2k4p -n shop --previous kubectl get events -n shop --sort-by=.lastTimestamp kubectl debug -it shop-7c9d6b8f5-x2k4p -n shop --image=busybox:1.36 --target=shop5Roll out a new version and roll back
kubectl set image deployment/shop shop=ghcr.io/s0sta/shop:1.5.0 -n shop kubectl rollout status deployment/shop -n shop kubectl rollout history deployment/shop -n shop kubectl rollout undo deployment/shop -n shop6Reach a service from your laptop
kubectl port-forward svc/shop 8080:80 -n shop curl http://localhost:8080/health7Check and test RBAC
kubectl auth can-i create deployments -n shop --as=system:serviceaccount:shop:ci kubectl create role reader --verb=get,list,watch --resource=pods -n shop kubectl create rolebinding ci-reader --role=reader --serviceaccount=shop:ci -n shop8Read values with jsonpath
kubectl get nodes -o jsonpath='{.items[*].status.addresses[?(@.type=="InternalIP")].address}' kubectl get secret db -n shop -o jsonpath='{.data.password}' | base64 -d9Maintain a node
kubectl cordon worker2 kubectl drain worker2 --ignore-daemonsets --delete-emptydir-data kubectl uncordon worker210Switch cluster and namespace
kubectl config get-contexts kubectl config use-context home-k3s kubectl config set-context --current --namespace=shop11Learn a field
kubectl explain pod.spec.securityContext --recursive | head -30
Nothing matches this filter.
Watch out
- Check the context before
deleteorapply:kubectl config current-context. kubectl get secret -o yamlshows base64 — that is not encryption.- A kubeconfig is a credential. Keep it
chmod 600, never in Git.
Files
~/.kube/config- Default kubeconfig (clusters, users, contexts).
/etc/kubernetes/admin.conf- kubeadm admin kubeconfig on control-plane nodes.
Tips to remember
- Exam speed:
alias k=kubectl,export do="--dry-run=client -o yaml",source <(kubectl completion bash). - Use
kubectl get … -o wideto see node and IP at once.
Exit codes
0 | Success. |
1 | Error (not found, forbidden, invalid YAML …). |