Kubectl Cheatsheet


Configuration and Context Management

  • Get current context
kubectl config current-context
  • List all contexts
kubectl config get-contexts
  • Switch to a different context
kubectl config use-context <context-name>
  • Set default namespace for current context
kubectl config set-context --current --namespace=<namespace>
  • View kubeconfig
kubectl config view
  • Get cluster information
kubectl cluster-info

Pod Management

  • List all pods
kubectl get pods
  • List pods in specific namespace
kubectl get pods -n <namespace>
  • List pods with more details
kubectl get pods -o wide
  • Create a pod from YAML file
kubectl apply -f <pod.yaml>
  • Create a simple pod
kubectl run <pod-name> --image=<image-name>
  • Delete a pod
kubectl delete pod <pod-name>
  • Get pod details
kubectl describe pod <pod-name>
  • Get pod logs
kubectl logs <pod-name>
  • Follow pod logs
kubectl logs -f <pod-name>
  • Execute commands in a pod
kubectl exec -it <pod-name> -- /bin/bash

Deployment Management

  • Create a deployment
kubectl create deployment <deployment-name> --image=<image-name>
  • List all deployments
kubectl get deployments
  • Get deployment details
kubectl describe deployment <deployment-name>
  • Scale a deployment
kubectl scale deployment <deployment-name> --replicas=<number>
  • Update deployment image
kubectl set image deployment/<deployment-name> <container-name>=<new-image>
  • Rollback deployment
kubectl rollout undo deployment/<deployment-name>
  • Check rollout status
kubectl rollout status deployment/<deployment-name>
  • Delete a deployment
kubectl delete deployment <deployment-name>

Service Management

  • Create a service
kubectl expose deployment <deployment-name> --type=<service-type> --port=<port>
  • List all services
kubectl get services
  • Get service details
kubectl describe service <service-name>
  • Delete a service
kubectl delete service <service-name>
  • Port forward to a service
kubectl port-forward service/<service-name> <local-port>:<service-port>

Namespace Management

  • List all namespaces
kubectl get namespaces
  • Create a namespace
kubectl create namespace <namespace-name>
  • Delete a namespace
kubectl delete namespace <namespace-name>
  • Get resources in all namespaces
kubectl get <resource-type> --all-namespaces

ConfigMaps and Secrets

  • Create ConfigMap from literal values
kubectl create configmap <configmap-name> --from-literal=<key>=<value>
  • Create ConfigMap from file
kubectl create configmap <configmap-name> --from-file=<file-path>
  • List ConfigMaps
kubectl get configmaps
  • Create Secret from literal values
kubectl create secret generic <secret-name> --from-literal=<key>=<value>
  • Create Secret from file
kubectl create secret generic <secret-name> --from-file=<file-path>
  • List Secrets
kubectl get secrets
  • Decode a secret
kubectl get secret <secret-name> -o jsonpath="{.data.<key>}" | base64 --decode

Resource Inspection and Debugging

  • Get all resources
kubectl get all
  • Get resources with labels
kubectl get <resource-type> -l <label-key>=<label-value>
  • Watch resource changes
kubectl get <resource-type> --watch
  • Get resource in YAML format
kubectl get <resource-type> <resource-name> -o yaml
  • Get resource in JSON format
kubectl get <resource-type> <resource-name> -o json
  • Edit a resource
kubectl edit <resource-type> <resource-name>
  • Get events
kubectl get events
  • Get events sorted by timestamp
kubectl get events --sort-by='.metadata.creationTimestamp'

Node Management

  • List all nodes
kubectl get nodes
  • Get node details
kubectl describe node <node-name>
  • Cordon a node (mark as unschedulable)
kubectl cordon <node-name>
  • Uncordon a node
kubectl uncordon <node-name>
  • Drain a node
kubectl drain <node-name> --ignore-daemonsets

Persistent Volumes and Claims

  • List Persistent Volumes
kubectl get pv
  • List Persistent Volume Claims
kubectl get pvc
  • Create PVC from file
kubectl apply -f <pvc.yaml>
  • Delete PVC
kubectl delete pvc <pvc-name>

Labels and Annotations

  • Add label to resource
kubectl label <resource-type> <resource-name> <key>=<value>
  • Remove label from resource
kubectl label <resource-type> <resource-name> <key>-
  • Add annotation to resource
kubectl annotate <resource-type> <resource-name> <key>=<value>
  • Remove annotation from resource
kubectl annotate <resource-type> <resource-name> <key>-

Resource Management with Files

  • Apply configuration from file
kubectl apply -f <file.yaml>
  • Apply configurations from directory
kubectl apply -f <directory>/
  • Apply with recursive directory search
kubectl apply -R -f <directory>/
  • Delete resources from file
kubectl delete -f <file.yaml>
  • Dry run before applying
kubectl apply -f <file.yaml> --dry-run=client

Troubleshooting and Debugging

  • Check resource usage of nodes
kubectl top nodes
  • Check resource usage of pods
kubectl top pods
  • Get pod logs from previous container instance
kubectl logs <pod-name> --previous
  • Get logs from specific container in pod
kubectl logs <pod-name> -c <container-name>
  • Copy files to/from pod
kubectl cp <pod-name>:<path> <local-path>
kubectl cp <local-path> <pod-name>:<path>
  • Run debugging pod
kubectl run debug-pod --rm -i --tty --image=busybox -- /bin/sh

Common Utilities

  • Get kubectl version
kubectl version
  • Get API resources
kubectl api-resources
  • Get API versions
kubectl api-versions
  • Explain resource fields
kubectl explain <resource-type>
  • Explain specific field
kubectl explain <resource-type>.<field>
  • Set output format
kubectl get <resource-type> -o wide
kubectl get <resource-type> -o yaml
kubectl get <resource-type> -o json
  • Use custom columns
kubectl get pods -o custom-columns=NAME:.metadata.name,STATUS:.status.phase
  • Get help for command
kubectl <command> --help