Skip to content

Latest commit

 

History

History
88 lines (68 loc) · 2.92 KB

File metadata and controls

88 lines (68 loc) · 2.92 KB

kubectl Cheat Sheet

The commands from this guide on one page.


Every command this guide uses, on one page. Add -n <ns> to target a namespace, -A for all namespaces.

Inspect

kubectl get pods                       # list (pods, deploy, svc, rs, sts, ds, pvc, ingress, nodes…)
kubectl get pods -o wide               # + node and Pod IP
kubectl get pods -l app=web            # filter by label
kubectl get pods -w                    # watch live changes
kubectl get all -n demo                # most namespaced objects at once
kubectl describe pod <name>            # full detail + events (start debugging here)
kubectl get events --sort-by=.lastTimestamp
kubectl explain deployment.spec.replicas   # discover any field
kubectl describe node <name>           # labels, taints, capacity, conditions

Create / change / delete

kubectl apply -f file.yaml             # declarative create/update
kubectl apply -f manifests/dir/        # a whole directory
kubectl apply -k overlays/prod         # a Kustomize overlay
kubectl delete -f file.yaml            # remove what a manifest created
kubectl delete namespace demo          # remove a namespace and everything in it
kubectl create deploy web --image=nginx:1.27 --dry-run=client -o yaml > web.yaml   # generate a manifest

Debug

kubectl logs <pod>                     # add -f to follow
kubectl logs <pod> --previous          # the crashed container's logs
kubectl exec -it <pod> -- sh           # shell inside a container
kubectl port-forward <pod|svc>/<name> 8080:80   # reach it locally
kubectl get endpoints <svc>            # which Pods a Service actually fronts

Scale & roll out

kubectl scale deploy/web --replicas=5
kubectl autoscale deploy/web --cpu-percent=50 --min=2 --max=10
kubectl set image deploy/web web=nginx:1.28
kubectl rollout status deploy/web
kubectl rollout history deploy/web
kubectl rollout undo deploy/web [--to-revision=N]
kubectl get pdb
kubectl drain <node> --ignore-daemonsets --delete-emptydir-data
kubectl uncordon <node>

Scheduling & security

kubectl label node <node> disk=ssd
kubectl taint node <node> dedicated=lab:NoSchedule
kubectl taint node <node> dedicated=lab:NoSchedule-   # remove the taint
kubectl auth can-i list pods -n demo
kubectl auth can-i get secrets --as=system:serviceaccount:demo:app -n demo

Namespaces & contexts

kubectl config get-contexts
kubectl config set-context --current --namespace=demo   # stop typing -n
kubectl get namespaces

Cluster lifecycle

sudo kubeadm upgrade plan
sudo kubeadm certs check-expiration
sudo kubeadm certs renew all

💡 Prefer the live dashboard? k9s covers most of this with single keystrokes (d describe, l logs, s shell, Ctrl-D delete).


← Cluster Lifecycle (kubeadm) · ↑ Contents · Troubleshooting →