skip to content
k8s \
context
alias k=kubectlfewer keystrokes
source <(kubectl completion bash)tab completion
kubectl config get-contextsclusters you can reach, * = current
kubectl config use-context prodswitch cluster
kubectl config set-context --current --namespace=appdefault namespace
get & describe
kubectl get podsin the current namespace
kubectl get pods -o widenode + ip
kubectl get pods -Aall namespaces
kubectl get pods -l app=webby label
kubectl get pods -wwatch
kubectl get pod web -o yamlfull object, status included
kubectl describe pod webevents at the bottom
kubectl get events --sort-by=.metadata.creationTimestampnewest last
kubectl top podneeds metrics-server
apply & delete
kubectl apply -f app.yamlcreate or update to match the file
kubectl apply -k overlays/prodkustomize
kubectl diff -f app.yamlwhat would change
kubectl delete -f app.yamlremove what the file defines
kubectl edit deploy webopens $KUBE_EDITOR, applies on save
kubectl create deployment web --image=nginx --dry-run=client -o yaml > web.yamlyaml to start from
deployments
kubectl scale deploy web --replicas=3pod count
kubectl set image deploy/web web=registry.example.com/web:1.1container=image, starts a rollout
kubectl rollout status deploy/webwait until it's done
kubectl rollout history deploy/webrevisions
kubectl rollout undo deploy/webback to the previous revision
kubectl rollout restart deploy/webnew pods, same spec
kubectl expose deploy web --port=80 --target-port=8080service in front of the pods
logs & shell
kubectl logs -f webfollow
kubectl logs web --previousthe container that crashed
kubectl logs web -c sidecarpick the container
kubectl logs -f -l app=webevery pod with the label
kubectl exec -it web -- shshell in the pod
kubectl port-forward svc/web 8080:80localhost:8080 to the service
kubectl cp web:/tmp/dump.txt ./dump.txtcopy out, needs tar in the image
kubectl run tmp --rm -it --image=busybox --restart=Never -- shthrowaway pod
kubectl debug -it web --image=busybox --target=appdebug container next to app
config & secrets
kubectl create configmap app-cfg --from-file=config.yamlfile content as a key
kubectl create secret generic db --from-literal=password=s3cr3tkey=value secret
kubectl create secret tls web-tls --cert=tls.crt --key=tls.keycert + key, for ingress
kubectl get secret db -o jsonpath='{.data.password}' | base64 -dread one value
nodes
kubectl get nodes -o wideversion, ip, os
kubectl cordon worker-1no new pods
kubectl drain worker-1 --ignore-daemonsets --delete-emptydir-dataevict pods for maintenance
kubectl uncordon worker-1schedulable again
kubectl top nodecpu and memory use
access
kubectl auth whoamiwho the cluster thinks you are
kubectl auth can-i create deployments -n appyes or no
kubectl auth can-i --list --as=system:serviceaccount:app:ciwhat a service account may do