A quick reference for kubectl and Kubernetes. Covers contexts, core resources, pod operations, deployments, namespaces, debugging, and config.
Context & Cluster
| Command | Description |
|---|---|
| kubectl config get-contexts | List all contexts |
| kubectl config current-context | Show the active context |
| kubectl config use-context <ctx> | Switch to a context |
| kubectl config set-context --current --namespace=<ns> | Set default namespace for current context |
| kubectl cluster-info | Show cluster endpoint info |
| kubectl version | Show client and server versions |
| kubectl api-resources | List all available resource types |
Core Resource Commands
| Command | Description |
|---|---|
| kubectl get <resource> | List resources (pods, deployments, svc, …) |
| kubectl get <resource> -o wide | List with extra columns (node, IP, …) |
| kubectl get <resource> -o yaml | Output full YAML definition |
| kubectl get all -n <ns> | List all resource types in a namespace |
| kubectl describe <resource> <name> | Show detailed state and events |
| kubectl apply -f <file.yaml> | Create or update resources from file |
| kubectl apply -f <dir/> | Apply all manifests in a directory |
| kubectl delete -f <file.yaml> | Delete resources defined in a file |
| kubectl delete <resource> <name> | Delete a specific resource by name |
| kubectl edit <resource> <name> | Open resource in editor for live editing |
Namespaces & Label Selectors
| Command | Description |
|---|---|
| kubectl get ns | List all namespaces |
| kubectl create ns <name> | Create a namespace |
| kubectl delete ns <name> | Delete a namespace and all its resources |
| kubectl get pods -n <ns> | List pods in a specific namespace |
| kubectl get pods -A | List pods across all namespaces |
| kubectl get pods -l app=web | Filter by label selector |
| kubectl get pods -l env=prod,tier=frontend | Filter by multiple labels |
| kubectl label pod <name> env=prod | Add or update a label on a resource |
Pod Operations
| Command | Description |
|---|---|
| kubectl logs <pod> | Print pod logs |
| kubectl logs <pod> -f | Stream/follow pod logs |
| kubectl logs <pod> -c <container> | Logs from a specific container in a pod |
| kubectl logs <pod> --previous | Logs from the previous (crashed) container |
| kubectl exec -it <pod> -- sh | Open a shell in a running pod |
| kubectl exec -it <pod> -c <c> -- sh | Shell into a specific container |
| kubectl exec <pod> -- <cmd> | Run a one-off command in a pod |
| kubectl port-forward pod/<pod> 8080:80 | Forward local port 8080 to pod port 80 |
| kubectl port-forward svc/<svc> 8080:80 | Forward local port to a service port |
| kubectl cp <pod>:<src> <dst> | Copy file from pod to local |
| kubectl cp <src> <pod>:<dst> | Copy file from local to pod |
Deployments & Scaling
| Command | Description |
|---|---|
| kubectl get deployments | List deployments |
| kubectl scale deploy/<name> --replicas=3 | Scale a deployment to 3 replicas |
| kubectl rollout status deploy/<name> | Watch rollout progress |
| kubectl rollout history deploy/<name> | Show rollout history |
| kubectl rollout undo deploy/<name> | Roll back to the previous revision |
| kubectl rollout undo deploy/<name> --to-revision=2 | Roll back to a specific revision |
| kubectl set image deploy/<name> app=image:v2 | Update the container image |
| kubectl rollout restart deploy/<name> | Trigger a rolling restart |
| kubectl autoscale deploy/<name> --min=2 --max=10 --cpu-percent=80 | Create an HPA |
Services & Networking
| Command | Description |
|---|---|
| kubectl get svc | List services |
| kubectl expose deploy/<name> --port=80 --type=ClusterIP | Expose a deployment as a service |
| kubectl get ingress | List ingress resources |
| kubectl get endpoints | Show endpoints (pod IPs behind a service) |
ConfigMaps & Secrets
| Command | Description |
|---|---|
| kubectl get configmaps | List ConfigMaps |
| kubectl describe configmap <name> | Show ConfigMap data |
| kubectl create configmap <name> --from-literal=KEY=val | Create ConfigMap from literal |
| kubectl create configmap <name> --from-file=<file> | Create ConfigMap from a file |
| kubectl get secrets | List Secrets |
| kubectl create secret generic <name> --from-literal=KEY=val | Create a generic Secret |
| kubectl create secret docker-registry <name> --docker-server=… --docker-username=… --docker-password=… | Create image pull Secret |
| kubectl get secret <name> -o jsonpath="{.data.KEY}" | base64 -d | Decode a secret value |
Debugging Workflow
| Command | Description |
|---|---|
| kubectl get pods | 1. List pods — spot CrashLoopBackOff / Pending states |
| kubectl describe pod <name> | 2. Inspect events and conditions for root cause |
| kubectl logs <pod> --previous | 3. Read logs from the last crashed container |
| kubectl exec -it <pod> -- sh | 4. Shell in to inspect filesystem or run diagnostics |
| kubectl get events --sort-by=.lastTimestamp | View cluster events sorted by time |
| kubectl top pods | Show CPU/memory usage for pods (metrics-server required) |
| kubectl top nodes | Show CPU/memory usage for nodes |
| kubectl run tmp --image=busybox --rm -it --restart=Never -- sh | Spin up a throwaway debug pod |
Common YAML Structure
| Field | Description |
|---|---|
| apiVersion | API group + version, e.g. apps/v1 or v1 |
| kind | Resource type: Pod, Deployment, Service, … |
| metadata.name | Name of the resource |
| metadata.namespace | Namespace (defaults to default) |
| metadata.labels | Key-value pairs for selection and organisation |
| spec.selector.matchLabels | Deployment: selects the pods it manages |
| spec.template | Deployment: pod template (metadata + spec) |
| spec.containers[].image | Container image, e.g. nginx:1.27 |
| spec.containers[].ports[].containerPort | Port the container listens on |
| spec.containers[].env | List of environment variables for the container |
| spec.containers[].resources.requests/limits | CPU and memory requests and limits |
| spec.containers[].livenessProbe | Health check — restarts container on failure |
| spec.containers[].readinessProbe | Readiness check — gates traffic to the pod |