Skip to content

Instantly share code, notes, and snippets.

@olibartfast
Last active November 19, 2025 21:15
Show Gist options
  • Select an option

  • Save olibartfast/5f58dd13dd6f2a33ebb67068a38d9d49 to your computer and use it in GitHub Desktop.

Select an option

Save olibartfast/5f58dd13dd6f2a33ebb67068a38d9d49 to your computer and use it in GitHub Desktop.
kubectl_cheat_sheet

🧰 Generic Useful kubectl Commands (with --kubeconfig & port-forwarding)

A compact set of everyday-use commands for inspecting deployments, pods, logs, services, events, configmaps, and port-forwarding. Replace /path/to/kubeconfig.yaml with your actual kubeconfig file.


πŸ“¦ Deployments

Get basic info

kubectl --kubeconfig=/path/to/kubeconfig.yaml get deployment <name> -n <namespace>

Describe a deployment (full debug info)

kubectl --kubeconfig=/path/to/kubeconfig.yaml describe deployment <name> -n <namespace>

Get deployment YAML

kubectl --kubeconfig=/path/to/kubeconfig.yaml get deployment <name> -n <namespace> -o yaml

Show rollout status

kubectl --kubeconfig=/path/to/kubeconfig.yaml rollout status deployment/<name> -n <namespace>

Undo last rollout

kubectl --kubeconfig=/path/to/kubeconfig.yaml rollout undo deployment/<name> -n <namespace>

🧩 Pods

List pods in a namespace

kubectl --kubeconfig=/path/to/kubeconfig.yaml get pods -n <namespace>

List pods created by a deployment (label selector)

kubectl --kubeconfig=/path/to/kubeconfig.yaml get pods -n <namespace> -l app=<appname>

Describe a pod

kubectl --kubeconfig=/path/to/kubeconfig.yaml describe pod <pod-name> -n <namespace>

Get full pod YAML (spec + volumes)

kubectl --kubeconfig=/path/to/kubeconfig.yaml get pod <pod-name> -n <namespace> -o yaml

πŸ“„ Logs

Simple logs from a pod

kubectl --kubeconfig=/path/to/kubeconfig.yaml logs <pod> -n <namespace>

Logs of pods selected by label (aggregates logs from all matching pods)

kubectl --kubeconfig=/path/to/kubeconfig.yaml logs -n <namespace> -l app=<appname>

Follow logs

kubectl --kubeconfig=/path/to/kubeconfig.yaml logs -f <pod> -n <namespace>

Logs for a specific container in a pod

kubectl --kubeconfig=/path/to/kubeconfig.yaml logs <pod> -c <container> -n <namespace>

πŸ“ ConfigMaps & Secrets

List ConfigMaps

kubectl --kubeconfig=/path/to/kubeconfig.yaml get configmap -n <namespace>

View ConfigMap YAML

kubectl --kubeconfig=/path/to/kubeconfig.yaml get configmap <name> -n <namespace> -o yaml

Describe ConfigMap

kubectl --kubeconfig=/path/to/kubeconfig.yaml describe configmap <name> -n <namespace>

Describe Secret (metadata)

kubectl --kubeconfig=/path/to/kubeconfig.yaml describe secret <name> -n <namespace>

View Secret (base64 decode) β€” caution: secrets are sensitive

kubectl --kubeconfig=/path/to/kubeconfig.yaml get secret <name> -n <namespace> -o jsonpath="{.data}" \
  | jq -r 'to_entries[] | "\(.key)=\(.value | @base64d)"'

πŸ”Œ Services

List services

kubectl --kubeconfig=/path/to/kubeconfig.yaml get svc -n <namespace>

Describe a service

kubectl --kubeconfig=/path/to/kubeconfig.yaml describe svc <name> -n <namespace>

πŸ” Events

List all events

kubectl --kubeconfig=/path/to/kubeconfig.yaml get events -n <namespace>

Filter events for a specific object

kubectl --kubeconfig=/path/to/kubeconfig.yaml get events -n <namespace> \
  --field-selector involvedObject.name=<name>

🧲 Debugging & Exec

Exec into a pod (shell)

kubectl --kubeconfig=/path/to/kubeconfig.yaml exec -it <pod> -n <namespace> -- /bin/bash
# or use /bin/sh if bash is not present

Port-forward to a pod (localPort:remotePort)

kubectl --kubeconfig=/path/to/kubeconfig.yaml port-forward pod/<pod-name> 8080:80 -n <namespace>

πŸ” Namespaces

List namespaces

kubectl --kubeconfig=/path/to/kubeconfig.yaml get ns

πŸ“€ Apply / Create / Delete Resources

Apply manifests in a folder

kubectl --kubeconfig=/path/to/kubeconfig.yaml apply -f ./deploy/

Delete a resource file

kubectl --kubeconfig=/path/to/kubeconfig.yaml delete -f <file.yaml>

Dry-run apply (client-side diff)

kubectl --kubeconfig=/path/to/kubeconfig.yaml apply -f <file.yaml> --dry-run=client -o yaml

πŸ” Useful tips

Export KUBECONFIG to avoid repeating the flag

export KUBECONFIG=/path/to/kubeconfig.yaml
# then omit --kubeconfig in following commands:
kubectl get pods -n <namespace>

Use a different kubeconfig for a single command (example)

KUBECONFIG=/path/to/kubeconfig.yaml kubectl get pods -n <namespace>

πŸšͺ Port-forwarding (expanded)

Port-forwarding is very handy for quickly accessing pods/services locally without exposing them via Service type=LoadBalancer/NodePort/Ingress.

1) Pod -> local port (single port)

kubectl --kubeconfig=/path/to/kubeconfig.yaml port-forward pod/<pod-name> 8080:80 -n <namespace>
# access locally at http://localhost:8080

2) Service -> local port (service forwards to a pod port)

kubectl --kubeconfig=/path/to/kubeconfig.yaml port-forward svc/<service-name> 8080:80 -n <namespace>

3) Multiple ports

kubectl --kubeconfig=/path/to/kubeconfig.yaml port-forward pod/<pod-name> 8080:80 8443:443 -n <namespace>

4) Port-forward a Deployment β€” pick a pod from the deployment

Pick a running pod from the deployment (one-liner to get a pod name):

POD=$(kubectl --kubeconfig=/path/to/kubeconfig.yaml get pod -n <namespace> -l app=<appname> -o jsonpath="{.items[0].metadata.name}")
kubectl --kubeconfig=/path/to/kubeconfig.yaml port-forward pod/$POD 8080:80 -n <namespace>

5) Run port-forward in background (simple)

Note: this launches in background but you should still capture logs or use a process supervisor for production usage.

kubectl --kubeconfig=/path/to/kubeconfig.yaml port-forward pod/<pod-name> 8080:80 -n <namespace> > /tmp/portforward.log 2>&1 &
echo $! > /tmp/portforward.pid
# to stop:
kill $(cat /tmp/portforward.pid)

6) When local port is already used β€” choose another local port

kubectl --kubeconfig=/path/to/kubeconfig.yaml port-forward pod/<pod-name> 18080:80 -n <namespace>

7) Debugging: confirm which pod is backing a service

kubectl --kubeconfig=/path/to/kubeconfig.yaml get endpoints <service-name> -n <namespace> -o yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment