Kubernetes Cheat Sheet
A comprehensive reference of Kubernetes (kubectl) commands with examples and usage instructions. Find the command you need using the search bar or browse by category.
kubectl version
Cluster ManagementDisplay Kubernetes client and server version information
Syntax:
kubectl version [--client] [--short]
Examples:
kubectl version
Show both client and server versionskubectl version --client
Show only client versionkubectl version --short
Show versions in short formatNotes:
Useful for checking kubectl and cluster compatibility
kubectl cluster-info
Cluster ManagementDisplay cluster information and service endpoints
Syntax:
kubectl cluster-info [dump]
Examples:
kubectl cluster-info
Show cluster master and services infokubectl cluster-info dump
Dump detailed cluster state for debuggingNotes:
Shows master and services running in the cluster
kubectl config view
Cluster ManagementShow merged kubeconfig settings
Syntax:
kubectl config view [--minify] [--flatten]
Examples:
kubectl config view
Display current kubeconfigkubectl config view --minify
Show only current context configkubectl config get-contexts
List all available contextsNotes:
Use to inspect and manage kubectl configuration
kubectl config current-context
Cluster ManagementDisplay the current-context
Syntax:
kubectl config current-context
Examples:
kubectl config current-context
Show current active contextkubectl config use-context minikube
Switch to minikube contextkubectl config set-context --current --namespace=production
Set namespace for current contextNotes:
Essential for managing multiple clusters
kubectl get pods
Pod ManagementList pods in the current namespace
Syntax:
kubectl get pods [NAME] [--namespace=NAMESPACE] [OPTIONS]
Examples:
kubectl get pods
List all pods in current namespacekubectl get pods -A
List pods in all namespaceskubectl get pods -o wide
Show additional info like node names and IPskubectl get pods --selector app=nginx
List pods with specific labelkubectl get pods --field-selector status.phase=Running
List only running podsNotes:
Most commonly used command for checking pod status
kubectl describe pod
Pod ManagementShow detailed information about a specific pod
Syntax:
kubectl describe pod POD_NAME [--namespace=NAMESPACE]
Examples:
kubectl describe pod nginx-pod
Show detailed pod informationkubectl describe pod nginx-pod -n production
Describe pod in specific namespaceNotes:
Essential for troubleshooting pod issues and events
kubectl logs
Pod ManagementPrint logs from a pod or container
Syntax:
kubectl logs POD_NAME [-c CONTAINER] [OPTIONS]
Examples:
kubectl logs nginx-pod
Show logs from single-container podkubectl logs nginx-pod -c web-server
Show logs from specific containerkubectl logs -f nginx-pod
Follow log output in real-timekubectl logs --previous nginx-pod
Show logs from previous container instancekubectl logs --tail=100 nginx-pod
Show last 100 lines of logsNotes:
Critical for debugging applications and monitoring
kubectl exec
Pod ManagementExecute commands in a running container
Syntax:
kubectl exec POD_NAME [-c CONTAINER] -- COMMAND [args...]
Examples:
kubectl exec nginx-pod -- ls /
Execute command in podkubectl exec -it nginx-pod -- bash
Interactive shell accesskubectl exec nginx-pod -c web-server -- env
Execute in specific containerkubectl exec nginx-pod -- cat /etc/hostname
Read file from containerNotes:
Use -it flags for interactive sessions
kubectl delete pod
Pod ManagementDelete pods by name or label selector
Syntax:
kubectl delete pod POD_NAME [OPTIONS]
Examples:
kubectl delete pod nginx-pod
Delete specific podkubectl delete pods --selector app=nginx
Delete pods by labelkubectl delete pod nginx-pod --force --grace-period=0
Force delete pod immediatelykubectl delete pods --all
Delete all pods in namespaceNotes:
Use --force only when necessary; prefer graceful deletion
kubectl get services
Service ManagementList services in the current namespace
Syntax:
kubectl get services [NAME] [--namespace=NAMESPACE] [OPTIONS]
Examples:
kubectl get services
List all serviceskubectl get svc
Short form - list serviceskubectl get svc -A
List services in all namespaceskubectl get svc -o wide
Show additional service detailsNotes:
Services expose pods and provide stable network endpoints
kubectl describe service
Service ManagementShow detailed information about a service
Syntax:
kubectl describe service SERVICE_NAME [--namespace=NAMESPACE]
Examples:
kubectl describe service nginx-service
Show service details and endpointskubectl describe svc nginx-service
Short form - describe serviceNotes:
Shows endpoints, selectors, and port configurations
kubectl expose
Service ManagementExpose a resource as a new Kubernetes service
Syntax:
kubectl expose TYPE NAME --port=PORT [OPTIONS]
Examples:
kubectl expose deployment nginx --port=80
Expose deployment as servicekubectl expose pod nginx --port=80 --type=NodePort
Expose pod with NodePort typekubectl expose deployment web --port=80 --target-port=8080
Map service port to different container portkubectl expose deployment nginx --port=80 --type=LoadBalancer
Expose with LoadBalancer typeNotes:
Creates services to make pods accessible
kubectl port-forward
Service ManagementForward local port to a pod or service
Syntax:
kubectl port-forward TYPE/NAME LOCAL_PORT:REMOTE_PORT
Examples:
kubectl port-forward pod/nginx 8080:80
Forward local port 8080 to pod port 80kubectl port-forward service/nginx 8080:80
Forward to service instead of podkubectl port-forward deployment/nginx 8080:80
Forward to deploymentkubectl port-forward nginx 8080:80 --address 0.0.0.0
Allow external connectionsNotes:
Useful for local development and debugging
kubectl get deployments
Deployment ManagementList deployments in the current namespace
Syntax:
kubectl get deployments [NAME] [--namespace=NAMESPACE] [OPTIONS]
Examples:
kubectl get deployments
List all deploymentskubectl get deploy
Short form - list deploymentskubectl get deploy -A
List deployments in all namespaceskubectl get deploy -o wide
Show additional deployment infoNotes:
Deployments manage ReplicaSets and pod updates
kubectl create deployment
Deployment ManagementCreate a new deployment
Syntax:
kubectl create deployment NAME --image=IMAGE [OPTIONS]
Examples:
kubectl create deployment nginx --image=nginx
Create simple nginx deploymentkubectl create deployment web --image=nginx --replicas=3
Create deployment with 3 replicaskubectl create deployment api --image=node:14 --port=3000
Create deployment with exposed portNotes:
Quick way to create deployments; use YAML for complex configurations
kubectl scale
Deployment ManagementScale the number of replicas in a deployment
Syntax:
kubectl scale deployment NAME --replicas=COUNT
Examples:
kubectl scale deployment nginx --replicas=5
Scale nginx deployment to 5 replicaskubectl scale deploy nginx --replicas=0
Scale down to 0 (effectively pause)kubectl scale --replicas=3 -f deployment.yaml
Scale using deployment fileNotes:
Horizontal scaling for handling load changes
kubectl rollout status
Deployment ManagementCheck the rollout status of a deployment
Syntax:
kubectl rollout status deployment/NAME [OPTIONS]
Examples:
kubectl rollout status deployment/nginx
Check deployment rollout progresskubectl rollout status deploy nginx --watch
Watch rollout status continuouslyNotes:
Monitor deployment updates and ensure successful rollouts
kubectl rollout history
Deployment ManagementView rollout history of a deployment
Syntax:
kubectl rollout history deployment/NAME [OPTIONS]
Examples:
kubectl rollout history deployment/nginx
Show deployment revision historykubectl rollout history deploy nginx --revision=2
Show details of specific revisionNotes:
Track deployment changes and revision information
kubectl rollout undo
Deployment ManagementRollback a deployment to a previous revision
Syntax:
kubectl rollout undo deployment/NAME [OPTIONS]
Examples:
kubectl rollout undo deployment/nginx
Rollback to previous revisionkubectl rollout undo deploy nginx --to-revision=2
Rollback to specific revisionNotes:
Quick way to recover from problematic deployments
kubectl get configmaps
ConfigMaps & SecretsList ConfigMaps in the current namespace
Syntax:
kubectl get configmaps [NAME] [--namespace=NAMESPACE] [OPTIONS]
Examples:
kubectl get configmaps
List all ConfigMapskubectl get cm
Short form - list ConfigMapskubectl get cm app-config -o yaml
Show ConfigMap in YAML formatNotes:
ConfigMaps store non-confidential configuration data
kubectl create configmap
ConfigMaps & SecretsCreate a ConfigMap from literal values or files
Syntax:
kubectl create configmap NAME [OPTIONS]
Examples:
kubectl create configmap app-config --from-literal=key1=value1
Create from literal valueskubectl create configmap app-config --from-file=config.properties
Create from filekubectl create configmap app-config --from-file=configs/
Create from directorykubectl create configmap app-config --from-literal=db_host=localhost --from-literal=db_port=5432
Multiple literal valuesNotes:
Multiple ways to create ConfigMaps from different sources
kubectl get secrets
ConfigMaps & SecretsList Secrets in the current namespace
Syntax:
kubectl get secrets [NAME] [--namespace=NAMESPACE] [OPTIONS]
Examples:
kubectl get secrets
List all secretskubectl get secret app-secret -o yaml
Show secret in YAML (base64 encoded)kubectl get secret app-secret -o jsonpath='{.data}'
Extract secret dataNotes:
Secrets store sensitive data like passwords and tokens
kubectl create secret
ConfigMaps & SecretsCreate a Secret from literal values or files
Syntax:
kubectl create secret TYPE NAME [OPTIONS]
Examples:
kubectl create secret generic app-secret --from-literal=username=admin
Create generic secret from literalkubectl create secret generic app-secret --from-file=credentials.txt
Create secret from filekubectl create secret docker-registry regcred --docker-server=server --docker-username=user --docker-password=pass
Create Docker registry secretkubectl create secret tls tls-secret --cert=cert.crt --key=cert.key
Create TLS secretNotes:
Different secret types for various use cases
kubectl get namespaces
Namespace OperationsList all namespaces in the cluster
Syntax:
kubectl get namespaces [NAME] [OPTIONS]
Examples:
kubectl get namespaces
List all namespaceskubectl get ns
Short form - list namespaceskubectl get ns --show-labels
Show namespace labelsNotes:
Namespaces provide scope for names and resource isolation
kubectl create namespace
Namespace OperationsCreate a new namespace
Syntax:
kubectl create namespace NAME [OPTIONS]
Examples:
kubectl create namespace production
Create production namespacekubectl create ns staging
Short form - create namespaceNotes:
Namespaces help organize and isolate cluster resources
kubectl config set-context
Namespace OperationsSet namespace for current context
Syntax:
kubectl config set-context --current --namespace=NAMESPACE
Examples:
kubectl config set-context --current --namespace=production
Switch to production namespacekubectl config set-context --current --namespace=default
Switch back to default namespaceNotes:
Changes default namespace for all subsequent commands
kubectl delete namespace
Namespace OperationsDelete a namespace and all its resources
Syntax:
kubectl delete namespace NAME [OPTIONS]
Examples:
kubectl delete namespace test-env
Delete namespace and all resourceskubectl delete ns old-project
Short form - delete namespaceNotes:
WARNING: Deletes ALL resources in the namespace permanently
kubectl describe node
Debugging & LogsShow detailed information about cluster nodes
Syntax:
kubectl describe node NODE_NAME
Examples:
kubectl describe node
Describe all nodeskubectl describe node worker-1
Describe specific nodekubectl get nodes -o wide
List nodes with additional infoNotes:
Useful for checking node capacity, conditions, and allocated resources
kubectl get events
Debugging & LogsList recent cluster events
Syntax:
kubectl get events [--namespace=NAMESPACE] [OPTIONS]
Examples:
kubectl get events
List events in current namespacekubectl get events -A
List events in all namespaceskubectl get events --sort-by=.metadata.creationTimestamp
Sort events by creation timekubectl get events --field-selector type=Warning
Show only warning eventsNotes:
Essential for troubleshooting cluster issues and pod problems
kubectl top nodes
Debugging & LogsDisplay resource usage of nodes
Syntax:
kubectl top nodes [NODE_NAME] [OPTIONS]
Examples:
kubectl top nodes
Show CPU and memory usage of all nodeskubectl top node worker-1
Show usage of specific nodeNotes:
Requires metrics-server to be installed in the cluster
kubectl top pods
Debugging & LogsDisplay resource usage of pods
Syntax:
kubectl top pods [POD_NAME] [--namespace=NAMESPACE] [OPTIONS]
Examples:
kubectl top pods
Show CPU and memory usage of podskubectl top pods -A
Show usage across all namespaceskubectl top pods --sort-by=cpu
Sort pods by CPU usagekubectl top pods --sort-by=memory
Sort pods by memory usageNotes:
Monitor resource consumption to identify performance issues
kubectl apply
Resource ManagementApply a configuration to a resource by filename or stdin
Syntax:
kubectl apply -f FILENAME [OPTIONS]
Examples:
kubectl apply -f deployment.yaml
Apply configuration from YAML filekubectl apply -f configs/
Apply all YAML files in directorykubectl apply -f https://raw.githubusercontent.com/user/repo/main/app.yaml
Apply from URLkubectl apply --dry-run=client -f deployment.yaml
Test apply without making changesNotes:
Preferred method for managing Kubernetes resources declaratively
kubectl get all
Resource ManagementList many resource types
Syntax:
kubectl get all [--namespace=NAMESPACE] [OPTIONS]
Examples:
kubectl get all
List pods, services, deployments, etc.kubectl get all -A
List resources in all namespaceskubectl get all --selector app=nginx
List resources with specific labelNotes:
Quick overview of main resources; doesn't include everything
kubectl delete
Resource ManagementDelete resources by filename, stdin, resource and name, or label selector
Syntax:
kubectl delete TYPE NAME [OPTIONS]
Examples:
kubectl delete -f deployment.yaml
Delete resources defined in YAML filekubectl delete deployment nginx
Delete specific deploymentkubectl delete pods --selector app=nginx
Delete pods by label selectorkubectl delete all --selector app=nginx
Delete all resources with labelNotes:
Be careful with delete operations, especially with selectors
kubectl label
Resource ManagementAdd or update labels on resources
Syntax:
kubectl label TYPE NAME KEY=VALUE [OPTIONS]
Examples:
kubectl label pods nginx environment=production
Add label to podkubectl label pods nginx environment=staging --overwrite
Update existing labelkubectl label pods nginx environment-
Remove label from podkubectl label nodes worker-1 role=database
Label a nodeNotes:
Labels are key-value pairs for organizing and selecting resources
kubectl annotate
Resource ManagementAdd or update annotations on resources
Syntax:
kubectl annotate TYPE NAME KEY=VALUE [OPTIONS]
Examples:
kubectl annotate pods nginx description='Web server pod'
Add annotation to podkubectl annotate pods nginx description='Updated description' --overwrite
Update annotationkubectl annotate pods nginx description-
Remove annotationNotes:
Annotations store arbitrary metadata that tools can read
βΈοΈ What is Kubernetes?
Kubernetes (K8s) is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. Originally developed by Google and now maintained by the Cloud Native Computing Foundation (CNCF), Kubernetes has become the de facto standard for container orchestration in modern cloud-native environments.
π Core Features
- β Container Orchestration: Automated deployment and scaling of containers
- β Self-Healing: Automatic restart, replacement, and rescheduling of failed containers
- β Service Discovery & Load Balancing: Built-in networking and traffic distribution
- β Rolling Updates: Zero-downtime application deployments and rollbacks
- β Storage Orchestration: Automatic mounting and management of storage systems
- β Configuration Management: Centralized secrets and configuration handling
π‘ Why Use Kubernetes?
- β’ Scalability: Horizontal and vertical scaling based on demand
- β’ High Availability: Multi-zone deployment and automatic failover
- β’ Resource Efficiency: Optimal resource utilization across clusters
- β’ Platform Agnostic: Runs on any infrastructure - cloud, on-premise, hybrid
- β’ Vendor Neutral: No lock-in to specific cloud providers
- β’ Ecosystem: Rich ecosystem of tools and integrations
π Key Kubernetes Concepts
Pod
The smallest deployable unit in Kubernetes. Contains one or more containers that share storage, network, and runtime specifications.
Service
An abstraction that defines a logical set of Pods and a policy to access them. Provides stable network endpoints for dynamic Pod environments.
Deployment
Manages ReplicaSets and provides declarative updates for Pods. Handles rolling updates, scaling, and rollback operations.
Namespace
Virtual clusters backed by the same physical cluster. Provides scope for names and enables resource quotas and access control.
ConfigMap
Stores non-confidential data in key-value pairs. Allows decoupling of configuration artifacts from image content.
Secret
Stores sensitive data such as passwords, OAuth tokens, and SSH keys in a more secure way than putting it verbatim in Pod specifications.
ποΈ Kubernetes Architecture
Control Plane
- β’ API Server: Central management entity
- β’ etcd: Distributed key-value store
- β’ Scheduler: Assigns pods to nodes
- β’ Controller Manager: Runs controller processes
Worker Nodes
- β’ kubelet: Node agent that communicates with control plane
- β’ kube-proxy: Network proxy for service abstraction
- β’ Container Runtime: Docker, containerd, or CRI-O
- β’ Pods: Running application containers
π³ Kubernetes vs Docker
Kubernetes
- β’ Container orchestration platform
- β’ Manages multiple containers across multiple hosts
- β’ Provides service discovery, load balancing
- β’ Automatic scaling and self-healing
- β’ Complex setup but powerful features
- β’ Production-grade container management
Docker
- β’ Container runtime and platform
- β’ Focuses on individual container lifecycle
- β’ Docker Swarm provides basic orchestration
- β’ Simpler for single-host deployments
- β’ Easy to learn and get started
- β’ Great for development and simple deployments
Complementary Technologies: Docker creates containers, Kubernetes orchestrates them at scale
π οΈ Kubernetes Ecosystem
Core Tools
- β’ kubectl: Command-line interface for Kubernetes
- β’ kubeadm: Tool for bootstrapping clusters
- β’ kubelet: Node agent for container management
- β’ Helm: Package manager for Kubernetes applications
- β’ Kustomize: Configuration management tool
Ecosystem Partners
- β’ Prometheus: Monitoring and alerting system
- β’ Grafana: Observability and visualization
- β’ Istio: Service mesh for microservices
- β’ Ingress Controllers: External access management
- β’ CI/CD Tools: Jenkins, GitLab, ArgoCD integration
π Getting Started with Kubernetes
1. Local Development
Start with minikube
or kind
β’
Install kubectl CLI tool β’ Practice with basic pod and service deployments
2. First Application
kubectl create deployment nginx --image=nginx
β’ kubectl expose deployment nginx --port=80
β’ Learn basic kubectl commands
3. Production Learning
Explore managed services (GKE, EKS, AKS) β’ Learn YAML manifest creation β’ Practice with Helm charts and CI/CD integration
β‘ Kubernetes Use Cases
Microservices
- β’ Service-to-service communication
- β’ Independent scaling per service
- β’ Circuit breaker patterns
- β’ A/B testing and canary deployments
Cloud Native Apps
- β’ Auto-scaling based on metrics
- β’ Multi-region deployments
- β’ Disaster recovery automation
- β’ Infrastructure as Code
Enterprise
- β’ Legacy application modernization
- β’ Resource optimization and cost control
- β’ Compliance and security automation
- β’ Multi-tenant environments
Kubernetes Revolution: Kubernetes has transformed how we deploy and manage applications at scale. From simple web apps to complex distributed systems, it provides the foundation for modern cloud-native development. This cheat sheet is your guide to mastering kubectl commands and Kubernetes concepts! βΈοΈ
Pro Kubernetes Tips
Essential kubectl Workflow
kubectl apply -f deployment.yaml
- Deploy your applicationkubectl get pods
- Check pod statuskubectl logs -f pod-name
- Follow application logskubectl describe pod pod-name
- Debug issueskubectl port-forward pod-name 8080:80
- Test locallykubectl scale deployment nginx --replicas=3
- Scale application
Kubernetes Best Practices
- Use specific image tags, avoid
latest
- Set resource requests and limits for containers
- Use namespaces to organize and isolate resources
- Implement health checks with readiness and liveness probes
- Store sensitive data in Secrets, not ConfigMaps
- Use kubectl dry-run before applying changes