Context & namespaces
bash
kubectl config get-contexts
kubectl config use-context <name>
kubectl config set-context --current --namespace=<ns>
kubectl get namespacesReading state
bash
kubectl get pods
kubectl get pods -o wide
kubectl get pods -l app=docktionary
kubectl get all
kubectl describe pod <name>
kubectl get deployment <name> -o yamlApplying & deleting
bash
kubectl apply -f deployment.yaml
kubectl apply -f ./manifests/ # a whole directory
kubectl delete -f deployment.yaml
kubectl delete pod <name>
kubectl diff -f deployment.yaml # see what apply would changeGenerating a manifest instead of writing one by hand
--dry-run=client -o yaml turns any imperative kubectl create/kubectl run command into a starting manifest — nothing is sent to the cluster, you just get the YAML it would have applied, to edit and save as a real file.
bash
kubectl create deployment docktionary-app \
--image=docktionary/app:1.4.0 \
--dry-run=client -o yaml > deployment.yaml
kubectl create service clusterip docktionary-app \
--tcp=80:3000 \
--dry-run=client -o yaml > service.yamlLogs & debugging
bash
kubectl logs <pod>
kubectl logs -f <pod> -c <container> # follow one container in a multi-container pod
kubectl logs --previous <pod> # logs from before the last crash
kubectl exec -it <pod> -- sh
kubectl cp <pod>:/path/in/pod ./local # copy a file out of a running pod
kubectl describe pod <pod> # events: why is this Pod Pending/CrashLoopBackOff?
kubectl top pods
kubectl top nodesScaling & rollouts
bash
kubectl scale deployment/<name> --replicas=5
kubectl rollout status deployment/<name>
kubectl rollout undo deployment/<name>
kubectl set image deployment/<name> <container>=<image>:<tag>Networking shortcuts
bash
kubectl port-forward svc/<name> 8080:80
kubectl get endpoints <service>
kubectl run debug --rm -it --image=busybox -- sh # throwaway debug podOutput formats worth knowing
bash
kubectl get pods -o json
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phaseLooking up what a field actually does
kubectl explain prints the API documentation for any resource or field, straight from the cluster's own OpenAPI schema — faster than searching the web when you just need to know what a field under spec.strategy is called or accepts.
bash
kubectl explain deployment.spec.strategy
kubectl explain pod.spec.containers.resources --recursive