Kubernetes Commands Reference
This reference guide provides commonly used kubectl commands organized by category, with examples and explanations.
Cluster Information
Section titled “Cluster Information”Cluster Status
Section titled “Cluster Status”# Get cluster infokubectl cluster-info
# Check component statuskubectl get componentstatuses
# Get nodes with detailskubectl get nodes -o wide
# Node detailskubectl describe node <node-name>API Resources
Section titled “API Resources”# List all API resourceskubectl api-resources
# List API resources with specific verbskubectl api-resources --verbs=list,get
# Get API versionskubectl api-versions
# Explain resourcekubectl explain <resource>kubectl explain pod.spec.containersResource Management
Section titled “Resource Management”Basic Operations
Section titled “Basic Operations”# Create resourceskubectl create -f <file-or-dir>
# Apply manifests (create or update)kubectl apply -f <file-or-dir>
# Delete resourceskubectl delete -f <file-or-dir>
# Scale resourceskubectl scale deployment <name> --replicas=<count>
# Edit resourceskubectl edit <resource> <name>
# Replace resourcekubectl replace -f <file>
# Patch resourcekubectl patch <resource> <name> --type=json -p='[{"op":"replace","path":"/spec/replicas","value":3}]'Viewing Resources
Section titled “Viewing Resources”# Get resources of a specific typekubectl get <resource-type>
# Get all resourceskubectl get all -n <namespace>
# Get resources with more detailskubectl get <resource-type> -o wide
# Watch for changeskubectl get <resource-type> -w
# Format output as YAMLkubectl get <resource-type> <name> -o yaml
# Custom columnskubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase,NODE:.spec.nodeName
# Sortingkubectl get pods --sort-by=.metadata.creationTimestamp
# Filtering with field selectorskubectl get pods --field-selector=status.phase=Running
# Filtering with label selectorskubectl get pods -l app=nginx,tier=frontendResource Details
Section titled “Resource Details”# Describe resourceskubectl describe <resource-type> <name>
# Get specific fields using jsonpathkubectl get <resource> <name> -o jsonpath='{.spec.replicas}'
# View resource hierarchykubectl get deployment <name> -o jsonpath='{.metadata.ownerReferences}'Pods and Containers
Section titled “Pods and Containers”Pod Operations
Section titled “Pod Operations”# Create pod from imagekubectl run <name> --image=<image>
# Get pod logskubectl logs <pod-name>kubectl logs <pod-name> -c <container-name>kubectl logs -f <pod-name> # Follow logskubectl logs <pod-name> --tail=100 # Last 100 lineskubectl logs <pod-name> --since=1h # Logs from last hour
# Execute commands in containerskubectl exec -it <pod-name> -- /bin/bashkubectl exec <pod-name> -- <command>
# Copy files to/from podskubectl cp <file> <pod-name>:/<path>kubectl cp <pod-name>:/<path> <local-path>
# Port forwardingkubectl port-forward <pod-name> <local-port>:<pod-port>
# Get pod resource usagekubectl top pod <pod-name>Pod Troubleshooting
Section titled “Pod Troubleshooting”# Force delete podkubectl delete pod <pod-name> --force --grace-period=0
# Check pod eventskubectl get events --field-selector involvedObject.name=<pod-name>
# Get previous container logskubectl logs <pod-name> --previous
# Debug with ephemeral container (K8s 1.23+)kubectl debug <pod-name> -it --image=busyboxDeployment Management
Section titled “Deployment Management”# Create deploymentkubectl create deployment <name> --image=<image>
# Update deployment imagekubectl set image deployment/<name> <container>=<new-image>
# View rollout statuskubectl rollout status deployment/<name>
# Rollout historykubectl rollout history deployment/<name>
# Rollback to previous versionkubectl rollout undo deployment/<name>kubectl rollout undo deployment/<name> --to-revision=<revision>
# Pause/Resume rolloutkubectl rollout pause deployment/<name>kubectl rollout resume deployment/<name>
# Restart deploymentkubectl rollout restart deployment/<name>Service Management
Section titled “Service Management”# Create servicekubectl expose deployment <name> --port=<port> --target-port=<target-port>
# Get endpointskubectl get endpoints <service-name>
# Test service DNSkubectl run -it --rm debug --image=busybox -- nslookup <service-name>
# Test service connectionkubectl run -it --rm debug --image=busybox -- wget -O- <service-name>:<port>Namespace Operations
Section titled “Namespace Operations”# Create namespacekubectl create namespace <name>
# Set default namespacekubectl config set-context --current --namespace=<namespace>
# List resources across all namespaceskubectl get <resource> --all-namespaces
# Delete namespace and all contentskubectl delete namespace <name>
# Get resources that aren't namespacedkubectl api-resources --namespaced=falseConfig and Context
Section titled “Config and Context”# View kubeconfigkubectl config view
# Get contextskubectl config get-contexts
# Switch contextkubectl config use-context <context-name>
# Rename contextkubectl config rename-context <old-name> <new-name>
# Delete contextkubectl config delete-context <context-name>
# Set cluster/user/namespace for a contextkubectl config set-context <context-name> --cluster=<cluster-name> --user=<user-name> --namespace=<namespace>Resource Quotas and Limits
Section titled “Resource Quotas and Limits”# View resource quotaskubectl get resourcequota -n <namespace>
# Describe quota usagekubectl describe resourcequota <quota-name> -n <namespace>
# Get limit rangeskubectl get limitrange -n <namespace>RBAC Management
Section titled “RBAC Management”# Create service accountkubectl create serviceaccount <name>
# Create rolekubectl create role <name> --verb=get,list,watch --resource=pods
# Create rolebindingkubectl create rolebinding <name> --role=<role-name> --serviceaccount=<namespace>:<sa-name>
# Check permissionskubectl auth can-i <verb> <resource> --as=system:serviceaccount:<namespace>:<serviceaccount>ConfigMaps and Secrets
Section titled “ConfigMaps and Secrets”# Create configmapkubectl create configmap <name> --from-file=<path> --from-literal=key=value
# Create secretkubectl create secret generic <name> --from-file=<path> --from-literal=key=value
# Decode secretkubectl get secret <name> -o jsonpath='{.data.<key>}' | base64 --decodeBatch Operations
Section titled “Batch Operations”# Delete all pods in a namespacekubectl delete pods --all -n <namespace>
# Apply manifests recursivelykubectl apply -f <directory> --recursive
# Get all resources with a specific labelkubectl get all -l app=<app-label>
# Delete resources based on labelkubectl delete all -l app=<app-label>Cluster Administration
Section titled “Cluster Administration”# Drain node for maintenancekubectl drain <node-name> --ignore-daemonsets
# Cordon/uncordon nodekubectl cordon <node-name> # Mark node as unschedulablekubectl uncordon <node-name> # Mark node as schedulable
# Taint nodeskubectl taint nodes <node-name> key=value:effect
# Manage static pods# Edit files in /etc/kubernetes/manifests/ on the node