Amazon EKS — kubectl Cheat Sheet

3 minute read

Essential kubectl commands organized by task.


Cluster Info

# Cluster endpoint and services
kubectl cluster-info

# API server version
kubectl version --short

# List API resources
kubectl api-resources

# Check current context
kubectl config current-context

# Switch context
kubectl config use-context arn:aws:eks:us-east-1:123456789012:cluster/my-cluster

# List all contexts
kubectl config get-contexts

Nodes

# List nodes
kubectl get nodes
kubectl get nodes -o wide

# Node details
kubectl describe node <node-name>

# Node resource usage
kubectl top nodes

# Node labels
kubectl get nodes --show-labels

# Add label
kubectl label node <node-name> env=production

# Taint a node
kubectl taint nodes <node-name> dedicated=gpu:NoSchedule

# Cordon (prevent scheduling)
kubectl cordon <node-name>

# Drain (evict pods and cordon)
kubectl drain <node-name> --ignore-daemonsets --delete-emptydir-data

# Uncordon
kubectl uncordon <node-name>

Pods

# List pods
kubectl get pods -n <namespace>
kubectl get pods -A                      # All namespaces
kubectl get pods -o wide                 # Show node placement
kubectl get pods --show-labels

# Pod details
kubectl describe pod <pod-name> -n <namespace>

# Pod logs
kubectl logs <pod-name> -n <namespace>
kubectl logs <pod-name> -n <namespace> -c <container>   # Specific container
kubectl logs <pod-name> -n <namespace> --previous        # Previous crash
kubectl logs <pod-name> -n <namespace> -f                # Follow/stream
kubectl logs <pod-name> -n <namespace> --tail=100        # Last 100 lines

# Exec into pod
kubectl exec -it <pod-name> -n <namespace> -- /bin/sh
kubectl exec -it <pod-name> -n <namespace> -c <container> -- /bin/bash

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

# Delete pod
kubectl delete pod <pod-name> -n <namespace>
kubectl delete pod <pod-name> -n <namespace> --grace-period=0 --force   # Force

# Ephemeral debug container
kubectl debug -it <pod-name> --image=busybox:latest --target=<container>

# Run a temporary pod
kubectl run debug --image=nicolaka/netshoot --restart=Never -it --rm -- /bin/bash

Deployments

# List deployments
kubectl get deployments -n <namespace>

# Deployment details
kubectl describe deployment <name> -n <namespace>

# Scale
kubectl scale deployment <name> -n <namespace> --replicas=3

# Update image
kubectl set image deployment/<name> <container>=<image>:<tag> -n <namespace>

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

# Rollout history
kubectl rollout history deployment/<name> -n <namespace>

# Rollback
kubectl rollout undo deployment/<name> -n <namespace>
kubectl rollout undo deployment/<name> -n <namespace> --to-revision=2

# Restart (rolling)
kubectl rollout restart deployment/<name> -n <namespace>

Services & Networking

# List services
kubectl get svc -n <namespace>
kubectl get svc -A

# Service details
kubectl describe svc <name> -n <namespace>

# Endpoints
kubectl get endpoints <svc-name> -n <namespace>

# Ingress
kubectl get ingress -A
kubectl describe ingress <name> -n <namespace>

# Port forward (local access)
kubectl port-forward svc/<name> 8080:80 -n <namespace>
kubectl port-forward pod/<name> 8080:80 -n <namespace>

# DNS test
kubectl exec -it <pod> -- nslookup <service>.<namespace>.svc.cluster.local

# Network policies
kubectl get networkpolicies -A

ConfigMaps & Secrets

# List
kubectl get configmaps -n <namespace>
kubectl get secrets -n <namespace>

# View content
kubectl get configmap <name> -n <namespace> -o yaml
kubectl get secret <name> -n <namespace> -o jsonpath='{.data.<key>}' | base64 -d

# Create from literal
kubectl create configmap my-config --from-literal=key1=value1 -n <namespace>
kubectl create secret generic my-secret --from-literal=password=s3cur3 -n <namespace>

# Create from file
kubectl create configmap my-config --from-file=config.yaml -n <namespace>

Storage

# Persistent Volume Claims
kubectl get pvc -n <namespace>
kubectl describe pvc <name> -n <namespace>

# Persistent Volumes
kubectl get pv

# Storage Classes
kubectl get storageclass

Events & Debugging

# Events (sorted by time)
kubectl get events -n <namespace> --sort-by=.lastTimestamp
kubectl get events -A --sort-by=.lastTimestamp | tail -30

# Warning events only
kubectl get events -A --field-selector type=Warning

# RBAC check
kubectl auth can-i get pods -n production
kubectl auth can-i '*' '*' --all-namespaces            # Am I cluster-admin?
kubectl auth can-i list pods --as=system:serviceaccount:default:my-sa

# API resources in use
kubectl get all -n <namespace>

Output Formatting

# JSON path
kubectl get pods -o jsonpath='{.items[*].metadata.name}'

# Custom columns
kubectl get pods -o custom-columns='NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName'

# YAML output
kubectl get pod <name> -o yaml

# JSON output
kubectl get pod <name> -o json

# Sort by
kubectl get pods --sort-by=.metadata.creationTimestamp

Updated: