Yash Verma / Cloud runbook

Kubernetes commands

29 commands from the cloud runbook. Fill in a placeholder once and every command uses it.

Get all pods across namespaces

Lists all pods across all namespaces in the cluster.

kubectl get pods --all-namespaces

pods, overview

Sort events by timestamp

Get recent events in a namespace sorted by their creation time.

kubectl get events --sort-by='.metadata.creationTimestamp'

events, debugging, cluster

Check node resource usage

Displays CPU and Memory usage for all nodes.

kubectl top nodes

metrics, nodes, performance

Force delete a stuck pod

Bypasses graceful termination to kill a pod.

Caution. Removes the pod from the API without waiting for the kubelet to confirm it stopped. A StatefulSet pod can end up running twice against the same volume. Try a normal delete first.

kubectl delete pod <pod-name> --grace-period=0 --force

pods, stuck, delete

Port forward to a service

Forwards local port to a service port.

kubectl port-forward svc/<service-name> 8080:80

network, debug

Decode a base64 secret

Fetches and decodes a base64 Kubernetes secret.

kubectl get secret <secret-name> -o jsonpath='{.data.<key>}' | base64 --decode

Prints the secret in plain text to your terminal and its scrollback.

security, secrets, base64

Get logs from previously crashed pod

Useful for checking why a pod crashed before it restarted.

kubectl logs <pod-name> --previous

logs, crash, debug

Check RBAC permissions

Check if your current user has permission to perform an action.

kubectl auth can-i create deployments --namespace dev

rbac, auth, permissions

Generate YAML manifest (dry run)

Generates the YAML for a resource without actually creating it.

kubectl create deployment nginx --image=nginx --dry-run=client -o yaml > nginx.yaml

yaml, manifest, generate

Safely drain a node

Cordons the node and evicts its pods before maintenance, respecting PodDisruptionBudgets.

Caution. --delete-emptydir-data deletes the data in emptyDir volumes on that node.

kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-data

node, maintenance, drain

Interactive shell in pod

Opens an interactive shell session inside a running pod.

kubectl exec -it <pod-name> -- /bin/sh

exec, shell, interactive

Restart a deployment

Triggers a rolling restart of all pods in a deployment.

kubectl rollout restart deployment/<deployment-name>

rollout, restart, deployment

Scale deployment

Sets the replica count of a deployment.

kubectl scale deployment/<deployment-name> --replicas=5

If a HorizontalPodAutoscaler manages the deployment, it will change the count back.

scale, replicas, capacity

Update image in deployment

Updates the container image of a deployment without editing YAML.

kubectl set image deployment/<deployment-name> <container-name>=<image-name>:<tag>

image, update, deploy

Explain a Kubernetes resource's fields

Shows the documentation and fields for a specific Kubernetes resource.

kubectl explain pod.spec.containers

explain, docs, api

View rollout history

Checks the revision history of a deployment.

kubectl rollout history deployment/<deployment-name>

rollout, history

Get running pod YAML

Exports the live YAML configuration of a running pod.

kubectl get pod <pod-name> -o yaml

yaml, export

Patch resource inline

Updates a field on a resource without an editor.

kubectl patch deployment <deployment-name> -p '{"spec": {"replicas": 3}}'

patch, update

Label a node

Adds or updates a label on a Kubernetes node for scheduling.

kubectl label node <node-name> env=production

label, node, scheduling

Taint a node

Adds a taint to a node to repel pods that don't tolerate it.

kubectl taint nodes <node-name> key=value:NoSchedule

taint, scheduling

Create ConfigMap from file

Creates a ConfigMap from a local file.

kubectl create configmap my-config --from-file=config.properties

configmap, config

Rollback a deployment

Rolls back a deployment to the previous revision.

kubectl rollout undo deployment/<deployment-name>

rollback, undo, deployment

List all kube contexts

Shows all available contexts in your kubeconfig.

kubectl config get-contexts

context, config, cluster

Switch kube context

Changes the active Kubernetes context.

kubectl config use-context <context-name>

context, switch

Top pods by CPU

Shows CPU and memory usage of pods sorted by CPU.

kubectl top pods --sort-by=cpu --all-namespaces

metrics, cpu, pods

Find pods whose last restart was an OOM kill

Lists pods where a container last terminated with OOMKilled.

kubectl get pods -A -o jsonpath='{range .items[*]}{.metadata.namespace}{"/"}{.metadata.name}{" "}{.status.containerStatuses[*].lastState.terminated.reason}{"\n"}{end}' | grep OOMKilled

oom, memory, restarts

Describe a pod

Shows a pod's status, container states, restart reasons and recent events.

kubectl describe pod <pod-name> -n <namespace>

pods, events, debugging

Watch a rollout until it finishes

Waits for a deployment rollout and exits non-zero if it fails.

kubectl rollout status deployment/<deployment-name> -n <namespace>

rollout, deploy

Cordon a node

Marks a node unschedulable; running pods stay where they are.

kubectl cordon <node-name>

nodes, maintenance