Skip to content

kubectl Command Reference

kubectl is the CLI for interacting with Kubernetes clusters. This page covers the commands used most often in day-to-day operations.

Terminal window
# View current context (which cluster you're connected to)
kubectl config current-context
# List all contexts
kubectl config get-contexts
# Switch context
kubectl config use-context my-cluster
# Set default namespace for current context
kubectl config set-context --current --namespace=my-namespace
Terminal window
kubectl get namespaces
kubectl create namespace staging
kubectl delete namespace staging
# All commands accept -n to target a namespace
kubectl get pods -n staging
kubectl get pods --all-namespaces # or -A
Terminal window
kubectl get pods
kubectl get pods -o wide # include node and IP
kubectl describe pod my-pod
kubectl logs my-pod
kubectl logs my-pod -c my-container # specific container in multi-container pod
kubectl logs my-pod --previous # logs from last crash
kubectl logs my-pod -f # follow (tail -f equivalent)
kubectl exec -it my-pod -- /bin/sh
kubectl delete pod my-pod
kubectl get pods -w # watch for changes
Terminal window
kubectl get deployments
kubectl describe deployment my-app
kubectl apply -f deployment.yaml
kubectl delete deployment my-app
kubectl scale deployment my-app --replicas=5
kubectl set image deployment/my-app my-app=myrepo/my-app:2.0
kubectl rollout status deployment/my-app
kubectl rollout history deployment/my-app
kubectl rollout undo deployment/my-app
kubectl rollout undo deployment/my-app --to-revision=2
kubectl rollout pause deployment/my-app
kubectl rollout resume deployment/my-app
Terminal window
kubectl get services
kubectl describe service my-service
kubectl get endpoints my-service
kubectl get ingress
kubectl describe ingress my-ingress
Terminal window
kubectl get configmaps
kubectl describe configmap my-config
kubectl get secrets
kubectl describe secret my-secret
# Decode a secret value
kubectl get secret my-secret -o jsonpath='{.data.password}' | base64 --decode
Terminal window
# Apply a manifest
kubectl apply -f manifest.yaml
# Apply everything in a directory
kubectl apply -f ./k8s/
# Delete from manifest
kubectl delete -f manifest.yaml
# Dry run (preview what would be applied)
kubectl apply -f manifest.yaml --dry-run=client
# Generate YAML for a resource
kubectl get deployment my-app -o yaml
Terminal window
# Open a shell in a running pod
kubectl exec -it my-pod -- /bin/bash
# Copy files to/from a pod
kubectl cp my-pod:/etc/config/app.conf ./app.conf
kubectl cp ./local-file.txt my-pod:/tmp/
# Forward a local port to a pod or service
kubectl port-forward pod/my-pod 8080:8080
kubectl port-forward service/my-service 8080:80
# Get events (useful for diagnosing failures)
kubectl get events --sort-by='.lastTimestamp'
kubectl get events -n my-namespace
Terminal window
kubectl top nodes
kubectl top pods
kubectl top pods --sort-by=memory
Terminal window
kubectl get pods -o json
kubectl get pods -o yaml
kubectl get pods -o wide
kubectl get pods -o jsonpath='{.items[*].metadata.name}'
# Custom columns
kubectl get pods -o custom-columns='NAME:.metadata.name,STATUS:.status.phase'
Terminal window
# Filter by label
kubectl get pods -l app=my-app
kubectl get pods -l 'env in (prod, staging)'
# Add a label
kubectl label pod my-pod version=v2
# Remove a label
kubectl label pod my-pod version-
ActionCommand
Apply manifestkubectl apply -f file.yaml
Get all resourceskubectl get all
Shell into podkubectl exec -it pod -- sh
View logskubectl logs pod
Port forwardkubectl port-forward svc/svc 8080:80
Rollback deploymentkubectl rollout undo deployment/app
Scale deploymentkubectl scale deployment app --replicas=3