kubectl Cheat Sheet — Essential Commands

Sanjeev SharmaSanjeev Sharma
5 min read

Advertisement

kubectl Cheat Sheet — Essential Commands

Quick reference for the most commonly used kubectl commands in your DevOps workflow.

Introduction

kubectl is the primary tool for interacting with Kubernetes clusters. This cheat sheet covers essential operations.

Cluster Management

# Cluster information
kubectl cluster-info
kubectl version
kubectl get nodes
kubectl describe node node-name

# Context management
kubectl config view
kubectl config current-context
kubectl config use-context context-name
kubectl config set-context --current --namespace=production

# Get API resources
kubectl api-resources

Pods

# List pods
kubectl get pods
kubectl get pods -A
kubectl get pods --all-namespaces
kubectl get pods -n namespace-name
kubectl get pods -o wide

# Pod details
kubectl describe pod pod-name
kubectl get pod pod-name -o yaml
kubectl get pod pod-name -o json

# Logs
kubectl logs pod-name
kubectl logs pod-name -f
kubectl logs pod-name --tail=100
kubectl logs pod-name -c container-name

# Execute commands
kubectl exec -it pod-name -- /bin/bash
kubectl exec pod-name -- command

# Port forward
kubectl port-forward pod-name 3000:3000

# Copy files
kubectl cp pod-name:/path/to/file ./local-file
kubectl cp ./local-file pod-name:/path/to/file

Deployments

# List deployments
kubectl get deployments
kubectl get deploy

# Create deployment
kubectl create deployment myapp --image=myapp:1.0
kubectl apply -f deployment.yaml

# Update deployment
kubectl set image deployment/myapp myapp=myapp:2.0
kubectl set env deployment/myapp ENV_VAR=value
kubectl patch deployment myapp -p '{"spec":{"replicas":5}}'

# Scale deployment
kubectl scale deployment myapp --replicas=5

# Rollout
kubectl rollout status deployment/myapp
kubectl rollout history deployment/myapp
kubectl rollout undo deployment/myapp
kubectl rollout undo deployment/myapp --to-revision=2

# Delete deployment
kubectl delete deployment myapp

Services

# List services
kubectl get svc
kubectl get services

# Create service
kubectl expose deployment myapp --type=LoadBalancer --port=80 --target-port=3000

# Service details
kubectl describe service myapp
kubectl get svc -o yaml

# Port forward
kubectl port-forward service/myapp 8080:80

# Delete service
kubectl delete service myapp

ConfigMaps and Secrets

# ConfigMaps
kubectl create configmap app-config --from-literal=key=value
kubectl create configmap app-config --from-file=config.yaml
kubectl get configmap
kubectl describe configmap app-config
kubectl delete configmap app-config

# Secrets
kubectl create secret generic db-creds --from-literal=password=secret
kubectl create secret docker-registry regcred --docker-server=registry.com --docker-username=user
kubectl get secrets
kubectl describe secret db-creds
kubectl get secret db-creds -o yaml

Namespaces

# List namespaces
kubectl get ns

# Create namespace
kubectl create namespace production

# Delete namespace
kubectl delete namespace production

# Set default namespace
kubectl config set-context --current --namespace=production

# All namespaces
kubectl get pods --all-namespaces
kubectl get pods -A

Resource Management

# View resource usage
kubectl top nodes
kubectl top pods

# Resource limits
kubectl set resources deployment myapp --limits cpu=500m,memory=512Mi
kubectl set resources deployment myapp --requests cpu=250m,memory=256Mi

# Get resource quotas
kubectl get resourcequota
kubectl describe resourcequota quota-name

Labels and Selectors

# Label resources
kubectl label pod myapp app=frontend
kubectl label deployment myapp environment=production
kubectl label nodes node-name label-key=label-value

# View labels
kubectl get pods --show-labels
kubectl get pods -L app,environment

# Select by labels
kubectl get pods -l app=frontend
kubectl get pods -l app=frontend,environment=production
kubectl get pods -l "tier in (frontend,backend)"

# Remove labels
kubectl label pod myapp app-

Debugging and Troubleshooting

# Pod status and events
kubectl describe pod myapp
kubectl get pods -o wide

# Events
kubectl get events
kubectl get events --sort-by=.metadata.creationTimestamp

# Logs
kubectl logs pod-name
kubectl logs deployment/myapp --all-containers
kubectl logs --previous pod-name

# Check node status
kubectl get nodes
kubectl describe node node-name

# Debugging pod
kubectl debug pod-name -it --image=ubuntu

# Interactive debugging
kubectl run -it debug --image=ubuntu -- /bin/bash

Manifests

# Apply manifests
kubectl apply -f manifest.yaml
kubectl apply -f directory/
kubectl apply -k ./

# Dry run
kubectl apply -f manifest.yaml --dry-run=client
kubectl apply -f manifest.yaml --dry-run=server

# Get resources in YAML
kubectl get deployment myapp -o yaml
kubectl get pod myapp -o yaml > pod-export.yaml

# Edit resources
kubectl edit deployment myapp
kubectl edit configmap app-config

# Delete resources
kubectl delete -f manifest.yaml
kubectl delete deployment myapp
kubectl delete pod myapp

Advanced Queries

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

# JSONPath queries
kubectl get pod -o jsonpath='{.items[0].metadata.name}'
kubectl get pods -o jsonpath='{.items[*].metadata.name}'

# Watch resources
kubectl get pods --watch
kubectl get deployments --watch

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

Common Tasks

# Restart pods
kubectl rollout restart deployment/myapp

# Force delete pod
kubectl delete pod myapp --grace-period=0 --force

# Port forward multiple
kubectl port-forward svc/myapp 8080:80 9090:443

# SSH into cluster node (via pod)
kubectl run -it --image=ubuntu shell -- /bin/bash

# Network diagnostics
kubectl run -it --image=nicolaka/netshoot debug -- /bin/bash

Output Formats

# Available formats
kubectl get pods -o json
kubectl get pods -o yaml
kubectl get pods -o wide
kubectl get pods -o name
kubectl get pods -o custom-columns=...
kubectl get pods -o jsonpath=...

Useful Aliases

# Add to ~/.bashrc or ~/.zshrc
alias k=kubectl
alias kg='kubectl get'
alias kd='kubectl describe'
alias kl='kubectl logs'
alias ke='kubectl exec -it'
alias ka='kubectl apply -f'
alias kdel='kubectl delete'

FAQ

Q: How do I see all resources in a namespace? A: Use kubectl get all -n namespace-name or kubectl api-resources | awk '{print $1}' then query each.

Q: How do I export a resource definition? A: Use kubectl get resource-type resource-name -o yaml > file.yaml

Q: How do I quickly restart a pod? A: Use kubectl rollout restart deployment/name for all pods in deployment.

Advertisement

Sanjeev Sharma

Written by

Sanjeev Sharma

Full Stack Engineer · E-mopro