Contexts and namespaces
kubectl config get-contexts # which clusters?kubectl config current-contextkubectl config use-context staging # switch clusterkubectl config set-context --current --namespace=shop # default namespacekubectl get pods -n kube-system # one namespacekubectl get pods -A # all namespacesSee what’s there
kubectl get pods # pods herekubectl get pods -o wide # with node and IPkubectl get pods -w # keep watchingkubectl get pods -l app=web # by labelkubectl get pods --show-labelskubectl get deploy,svc,ingress # several kinds at oncekubectl get all # the common kinds (not everything)kubectl get nodeskubectl api-resources # every kind, with short nameskubectl explain deployment.spec.strategy # docs for any fieldDebug a pod
kubectl describe pod web-7d9f8 # status and EVENTS: start herekubectl logs web-7d9f8 # current logskubectl logs web-7d9f8 --previous # logs from before the last crashkubectl logs -f web-7d9f8 -c sidecar # follow one containerkubectl logs -l app=web --tail=50 # from every pod with a labelkubectl exec -it web-7d9f8 -- sh # a shell insidekubectl debug -it web-7d9f8 --image=busybox --target=app # a debug containerkubectl debug node/worker-2 -it --image=ubuntu # a shell on a nodekubectl get events --sort-by=.lastTimestampkubectl port-forward pod/web-7d9f8 8080:80kubectl port-forward svc/web 8080:80Apply and change
kubectl apply -f app.yaml # create or update from a filekubectl apply -f ./k8s/ # every file in a folderkubectl apply --dry-run=server -f app.yaml # check without changing anythingkubectl diff -f app.yaml # what would change?kubectl delete -f app.yamlkubectl edit deploy web # edit live (and lost on next apply)kubectl label pod web-7d9f8 tier=frontkubectl annotate deploy web owner=opsThe Kubernetes Manifest Generator writes the YAML for an app.
Deployments and rollouts
kubectl create deployment web --image=nginx:1.27 --replicas=2kubectl set image deploy/web nginx=nginx:1.28 # new versionkubectl rollout status deploy/web # wait until it's donekubectl rollout history deploy/webkubectl rollout undo deploy/web # back to the previous onekubectl rollout restart deploy/web # new pods, same versionkubectl scale deploy/web --replicas=5kubectl autoscale deploy/web --min=2 --max=10 --cpu-percent=80Services and networking
kubectl expose deploy web --port=80 --target-port=8080kubectl get endpoints web # no IPs = no pod matches or is Readykubectl run tmp --rm -it --image=busybox -- wget -qO- http://web:80Config and secrets
kubectl create configmap settings --from-file=app.confkubectl create secret generic db --from-literal=user=app --from-file=password=./pw.txtkubectl get secret db -o jsonpath='{.data.password}' | base64 -dkubectl describe secret db # keys, without the valuesGet exact output
kubectl get pod web-7d9f8 -o yaml # the full objectkubectl get pods -o name # just names, for scriptskubectl get pods -o jsonpath='{.items[*].metadata.name}'kubectl get pods -o custom-columns=NAME:.metadata.name,NODE:.spec.nodeNamekubectl get pods --sort-by=.metadata.creationTimestampkubectl get pods --field-selector=status.phase=FailedResources and nodes
kubectl top nodes # needs metrics-serverkubectl top pods -A --sort-by=memorykubectl describe node worker-2 # capacity, conditions, what runs therekubectl cordon worker-2 # no new pods herekubectl drain worker-2 --ignore-daemonsetskubectl uncordon worker-2Clean up
kubectl delete pod web-7d9f8 # a Deployment makes a new onekubectl delete pods --field-selector=status.phase=Failedkubectl delete pod web-7d9f8 --grace-period=0 --force # stuck pods: last resort