Skip to content

Instantly share code, notes, and snippets.

@memoryonrepeat
Last active January 25, 2025 22:44
Show Gist options
  • Save memoryonrepeat/9c9ad1c0f2377c27550b3d00ad913c7a to your computer and use it in GitHub Desktop.
Save memoryonrepeat/9c9ad1c0f2377c27550b3d00ad913c7a to your computer and use it in GitHub Desktop.
k8s cheatsheet
  • Use kubectx for switching between contexts / namespaces
  • For all commands should specify --namespace <NAMESPACE> or --all-namespaces or kubectl will use default namespace
  • kubectl auth can-i create deployments --namespace <NAMESPACE> --> Check deployment permission
  • General grammar: kubectl <verb> <noun> [options]
  • https://www.digitalocean.com/community/tutorials/how-to-inspect-kubernetes-networking
  • kubectl get pods --namespace=<NAMESPACE> -o wide | grep <SERVICE> --> Get all pods running a service
  • kubectl exec -it <POD> --namespace <NAMESPACE> -- /bin/bash --> Open shell inside some pod. Can also do other commands
  • Port forwarding to access service from localhost (Helm chart message upon successful deployment)
1. Get the application URL by running these commands:
  export POD_NAME=$(kubectl get pods --namespace <NAMESPACE> -l "app.kubernetes.io/name=wm-server,app.kubernetes.io/instance=wm-server" -o jsonpath="{.items[0].metadata.name}")
  export CONTAINER_PORT=$(kubectl get pod --namespace <NAMESPACE> $POD_NAME -o jsonpath="{.spec.containers[0].ports[0].containerPort}")
  echo "Visit http://127.0.0.1:8080 to use your application"
  kubectl --namespace <NAMESPACE> port-forward $POD_NAME 8080:$CONTAINER_PORT
  • kubectl get pod $(kubectl get pods --namespace api | grep <SERVICE> | awk '{print $1}') --namespace api --> Check pod status after deploying service
  • Prepare some swiss army debugging container. Examples: netshoot , cdebug
  • Official cheatsheet
  • Debug running containers
  • Debugging
    • kubectl exec -it <pod_name> [-c container_name] -- /bin/bash --> Run /bin/bash in chosen pod / container
    • kubectl attach -it <pod_name> [-c container_name] --> Attach to the chosen pod / container to stream output
    • kubectl run -it busybox --image=busybox --> Spin up a new busybox debug container
    • kubectl debug
    • others techniques
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment