Cheat sheet

57 commandes essentielles, prêtes à copier.

Context & discovery

kubectl config get-contextsList contexts and identify the active one.
kubectl config use-context <context>Switch the active cluster context.
kubectl api-resources -o wideDiscover kinds, short names, scope and verbs.
kubectl explain <kind>.<field> --recursiveRead the server-aware schema.
kubectl cluster-infoShow control-plane and core service endpoints.

Get & format

kubectl get pods -A -o wideSee placement, IPs, readiness and nodes.
kubectl get pods -l app=web --show-labelsFilter and inspect selector labels.
kubectl get pods -o custom-columns='NAME:.metadata.name,NODE:.spec.nodeName'Produce a focused table.
kubectl get secret s -o jsonpath='{.data.key}' | base64 -dDecode one authorized Secret value.
kubectl get events -A --sort-by=.lastTimestampRead recent cluster events in order.

Generate YAML

kubectl create deployment web --image=nginx:1.27 --dry-run=client -o yamlGenerate a Deployment skeleton.
kubectl run tmp --image=busybox:1.36 --restart=Never --dry-run=client -o yamlGenerate a Pod skeleton.
kubectl expose deployment web --port=80 --target-port=8080 --dry-run=client -o yamlGenerate a Service skeleton.
kubectl create job task --image=busybox:1.36 --dry-run=client -o yaml -- dateGenerate a Job skeleton.
kubectl create cronjob task --image=busybox:1.36 --schedule="*/5 * * * *" --dry-run=client -o yaml -- dateGenerate a CronJob skeleton.

Mutate

kubectl apply -f file.yamlReconcile declarative configuration.
kubectl apply --dry-run=server -f file.yamlValidate with server schema and admission.
kubectl diff -f file.yamlPreview declarative changes.
kubectl patch deployment web -p '{"spec":{"replicas":3}}'Change a focused field.
kubectl edit deployment webEdit the live object.

Rollouts

kubectl set image deployment/web web=nginx:1.27Change a workload image.
kubectl rollout status deployment/webWait for rollout completion.
kubectl rollout history deployment/webInspect revisions.
kubectl rollout undo deployment/webReturn to the previous revision.
kubectl scale deployment/web --replicas=4Set desired replica count.

Debug workloads

kubectl describe pod <pod>Inspect status, conditions and Events.
kubectl logs <pod> -c <container> --previousRead the previous crashed instance.
kubectl exec -it <pod> -c <container> -- shRun a process in a container.
kubectl debug -it pod/<pod> --image=nicolaka/netshoot --target=<container>Attach an ephemeral diagnostic container.
kubectl port-forward service/web 8080:80Test a Service locally.

Nodes

kubectl describe node <node>Read conditions, capacity, allocations and Events.
kubectl cordon <node>Prevent new scheduling.
kubectl drain <node> --ignore-daemonsets --delete-emptydir-dataEvict managed workloads for maintenance.
kubectl uncordon <node>Resume scheduling.
systemctl status kubeletCheck node agent state.
journalctl -u kubelet -n 100 --no-pagerRead recent kubelet logs.

Networking

kubectl get service,endpointslice -ACompare frontends with ready backends.
kubectl run netcheck --rm -it --restart=Never --image=nicolaka/netshoot -- bashLaunch a disposable network toolbox.
kubectl get networkpolicy -AInventory network isolation.
kubectl -n kube-system get pods -l k8s-app=kube-dnsInspect CoreDNS Pods.

Storage

kubectl get storageclass,pv,pvc -AInspect provisioning and binding chain.
kubectl describe pvc <claim> -n <ns>Read claim binding Events.

RBAC

kubectl auth can-i <verb> <resource> -n <ns>Test your effective permission.
kubectl auth can-i --list --as=<user> -n <ns>Review an impersonated identity.
kubectl create role reader --verb=get,list,watch --resource=pods -n teamGenerate namespace rules.
kubectl create rolebinding reader --role=reader --serviceaccount=team:reader -n teamBind a ServiceAccount.

kubeadm

kubeadm token create --print-join-commandGenerate a worker join command.
kubeadm upgrade planInspect supported control-plane upgrade.
kubeadm certs check-expirationReview kubeadm-managed certificate dates.

etcd

ETCDCTL_API=3 etcdctl snapshot status backup.db -w tableValidate snapshot metadata.

Packaging

helm template release ./chart -f values.yamlRender a chart locally.
helm list -AInventory releases.
kubectl kustomize overlays/prodRender a Kustomize overlay.

Security

trivy image --severity HIGH,CRITICAL <image>Scan an image.
cosign verify <image@digest> <identity flags>Verify artifact signature identity.
sudo ss -lntupInventory listening network services.
journalctl -u falco --since "10 minutes ago"Review runtime alerts.