Sign In

Curriculum 5: Kubectl CLI Cheat Sheet

Core Commands Reference

15 min · 15 XP

kubectl Core Commands Cheat Sheet

This is your quick reference for the most commonly used kubectl commands. Bookmark this page and come back whenever you need a refresher.

Create and Apply

Use apply for declarative management and create for imperative one-off resources.

# Apply a manifest file (create or update)
kubectl apply -f deployment.yaml

# Apply all manifests in a directory
kubectl apply -f ./manifests/

# Create a resource imperatively
kubectl create deployment nginx --image=nginx

# Create a namespace
kubectl create namespace staging

# Create a configmap from literal values
kubectl create configmap app-config --from-literal=ENV=production --from-literal=LOG_LEVEL=info

# Create a secret
kubectl create secret generic db-creds --from-literal=password=s3cret

Get and Describe

Use get to list resources and describe for detailed information.

# List resources
kubectl get pods
kubectl get services
kubectl get deployments
kubectl get nodes

# Wide output with extra columns
kubectl get pods -o wide

# All namespaces
kubectl get pods -A

# YAML output for a specific resource
kubectl get deployment my-app -o yaml

# Filter by label
kubectl get pods -l app=nginx

# Detailed information about a specific resource
kubectl describe pod my-app-abc123
kubectl describe node worker-1
kubectl describe service my-service

Edit and Patch

Modify resources that are already running in the cluster.

# Open a resource in your default editor
kubectl edit deployment my-app

# Patch a specific field
kubectl patch deployment my-app -p '{"spec":{"replicas":5}}'

# Update the container image
kubectl set image deployment/my-app nginx=nginx:1.28

# Add or update labels
kubectl label pod my-pod version=v2

# Add or update annotations
kubectl annotate pod my-pod description="production workload"

Delete

Remove resources from the cluster.

# Delete a specific resource
kubectl delete pod my-pod
kubectl delete deployment my-app

# Delete using a manifest file
kubectl delete -f deployment.yaml

# Delete all pods in a namespace
kubectl delete pods --all -n staging

# Delete with grace period
kubectl delete pod my-pod --grace-period=0 --force

# Delete resources by label
kubectl delete pods -l app=old-app

Debug and Troubleshoot

Commands for investigating problems and understanding what is happening.

# View pod logs
kubectl logs my-pod
kubectl logs my-pod -c sidecar-container   # specific container
kubectl logs my-pod --previous              # previous instance
kubectl logs my-pod -f                      # follow/stream logs
kubectl logs -l app=nginx --all-containers  # logs by label

# Execute a command inside a running pod
kubectl exec my-pod -- ls /app
kubectl exec -it my-pod -- /bin/sh          # interactive shell

# Port forward to access a pod locally
kubectl port-forward pod/my-pod 8080:80
kubectl port-forward svc/my-service 8080:80

# Copy files to/from a pod
kubectl cp my-pod:/app/logs.txt ./logs.txt
kubectl cp ./config.json my-pod:/app/config.json

# View resource usage
kubectl top pods
kubectl top nodes

# Run a temporary debug pod
kubectl run debug --image=busybox -it --rm -- /bin/sh

Quick Reference Table

ActionCommand
List podskubectl get pods
Pod detailskubectl describe pod <name>
Pod logskubectl logs <name>
Shell into podkubectl exec -it <name> -- /bin/sh
Create deploymentkubectl create deployment <name> --image=<image>
Scale deploymentkubectl scale deployment <name> --replicas=<n>
Update imagekubectl set image deployment/<name> <container>=<image>
Rollbackkubectl rollout undo deployment/<name>
Apply manifestkubectl apply -f <file>
Delete resourcekubectl delete <type> <name>
Switch namespacekubectl config set-context --current --namespace=<ns>
All namespaceskubectl get <resource> -A
Port forwardkubectl port-forward <pod> <local>:<remote>
Current contextkubectl config current-context
Cluster infokubectl cluster-info

Key Takeaways

  • apply is for declarative management; create is for imperative one-offs
  • get lists resources; describe gives detailed information
  • logs, exec, and port-forward are your primary debugging tools
  • Use -A to search across all namespaces when you cannot find a resource
  • Keep this cheat sheet handy as you work through the rest of the course