Skip to content

Kubectl Command Cheatsheet

Quick Reference

Essential Commands Cheatsheet

Cluster Management

# Cluster info
kubectl cluster-info
kubectl get nodes -o wide
kubectl top nodes

# Context management
kubectl config get-contexts
kubectl config use-context <context>
kubectl config current-context

Pod Operations

# Get pods
kubectl get pods -A                                    # All namespaces
kubectl get pods -o wide                              # With node info
kubectl get pods --show-labels                        # With labels
kubectl get pods --field-selector status.phase=Running

# Describe and logs
kubectl describe pod <pod> -n <namespace>
kubectl logs <pod> -n <namespace> -f --tail=100
kubectl logs <pod> -c <container> --previous

# Execute commands
kubectl exec -it <pod> -n <namespace> -- /bin/bash
kubectl exec <pod> -n <namespace> -- env
kubectl cp <pod>:/path/file ./local-file -n <namespace>

Deployment Management

# Deployments
kubectl get deployments -A
kubectl describe deployment <name> -n <namespace>
kubectl scale deployment <name> --replicas=5 -n <namespace>
kubectl set image deployment/<name> app=image:v2 -n <namespace>

# Rollout management
kubectl rollout status deployment/<name> -n <namespace>
kubectl rollout history deployment/<name> -n <namespace>
kubectl rollout undo deployment/<name> -n <namespace>
kubectl rollout undo deployment/<name> --to-revision=3 -n <namespace>
kubectl rollout restart deployment/<name> -n <namespace>

Service & Networking

# Services
kubectl get svc -A
kubectl describe svc <name> -n <namespace>
kubectl get endpoints -A

# Port forwarding
kubectl port-forward svc/<service> 8080:80 -n <namespace>
kubectl port-forward pod/<pod> 8080:80 -n <namespace>
kubectl port-forward deployment/<deployment> 8080:80 -n <namespace>

# Network policies
kubectl get networkpolicies -A
kubectl describe networkpolicy <name> -n <namespace>

Troubleshooting

# Events and logs
kubectl get events --sort-by='.lastTimestamp' -A
kubectl get events --field-selector type=Warning

# Resource usage
kubectl top pods -n <namespace>
kubectl top pods --sort-by=cpu -A
kubectl top pods --sort-by=memory -A

# Debug commands
kubectl run debug --image=nicolaka/netshoot -it --rm
kubectl debug <pod> -it --image=busybox -n <namespace>

Argo CD Commands

# Port forward
kubectl port-forward svc/argocd-server -n argocd 8080:443

# Applications
argocd app list
argocd app get <app-name>
argocd app sync <app-name>
argocd app wait <app-name> --health
argocd app rollback <app-name> <revision>

# Repository management
argocd repo list
argocd repo add <repo-url> --username <user> --password <pass>

Helm Commands

# Repository management
helm repo add <name> <url>
helm repo update
helm repo list

# Chart operations
helm search repo <keyword>
helm show values <chart>
helm install <release> <chart> -n <namespace>
helm upgrade <release> <chart> -n <namespace>
helm rollback <release> <revision> -n <namespace>

# Release management
helm list -A
helm status <release> -n <namespace>
helm history <release> -n <namespace>
helm uninstall <release> -n <namespace>

Common Issues Quick Reference

Issue Symptoms Quick Fix
ImagePullBackOff Pod stuck in Pending Check image name, registry credentials, network access
CrashLoopBackOff Pod keeps restarting Check logs with --previous, verify env vars, check command
Pending Pod Pod not scheduled Check resource requests, node capacity, taints/tolerations
Service No Endpoints Service not working Verify selector labels match pod labels
Port Forward Fails Can't connect Ensure pod is Running, check container ports
Node NotReady Node offline Check kubelet logs, disk space, network connectivity
OOMKilled Container killed Increase memory limits, optimize application
Readiness Probe Failed Pod not ready Check endpoint health, adjust probe settings

Emergency Procedures

Cluster Recovery

# Check cluster health
kubectl get nodes
kubectl get pods -n kube-system

Application Recovery

# Scale down problematic deployment
kubectl scale deployment <name> --replicas=0 -n <namespace>

# Delete stuck resources
kubectl delete pod <pod> --grace-period=0 --force -n <namespace>

# Clear finalizers
kubectl patch <resource>/<name> -p '{"metadata":{"finalizers":[]}}' --type=merge