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.

36 commands found
Filter by category:

kubectl version

Cluster Management

Display Kubernetes client and server version information

Syntax:

kubectl version [--client] [--short]

Examples:

kubectl version Show both client and server versions
kubectl version --client Show only client version
kubectl version --short Show versions in short format

Notes:

Useful for checking kubectl and cluster compatibility

kubectl cluster-info

Cluster Management

Display cluster information and service endpoints

Syntax:

kubectl cluster-info [dump]

Examples:

kubectl cluster-info Show cluster master and services info
kubectl cluster-info dump Dump detailed cluster state for debugging

Notes:

Shows master and services running in the cluster

kubectl config view

Cluster Management

Show merged kubeconfig settings

Syntax:

kubectl config view [--minify] [--flatten]

Examples:

kubectl config view Display current kubeconfig
kubectl config view --minify Show only current context config
kubectl config get-contexts List all available contexts

Notes:

Use to inspect and manage kubectl configuration

kubectl config current-context

Cluster Management

Display the current-context

Syntax:

kubectl config current-context

Examples:

kubectl config current-context Show current active context
kubectl config use-context minikube Switch to minikube context
kubectl config set-context --current --namespace=production Set namespace for current context

Notes:

Essential for managing multiple clusters

kubectl get pods

Pod Management

List pods in the current namespace

Syntax:

kubectl get pods [NAME] [--namespace=NAMESPACE] [OPTIONS]

Examples:

kubectl get pods List all pods in current namespace
kubectl get pods -A List pods in all namespaces
kubectl get pods -o wide Show additional info like node names and IPs
kubectl get pods --selector app=nginx List pods with specific label
kubectl get pods --field-selector status.phase=Running List only running pods

Notes:

Most commonly used command for checking pod status

kubectl describe pod

Pod Management

Show detailed information about a specific pod

Syntax:

kubectl describe pod POD_NAME [--namespace=NAMESPACE]

Examples:

kubectl describe pod nginx-pod Show detailed pod information
kubectl describe pod nginx-pod -n production Describe pod in specific namespace

Notes:

Essential for troubleshooting pod issues and events

kubectl logs

Pod Management

Print logs from a pod or container

Syntax:

kubectl logs POD_NAME [-c CONTAINER] [OPTIONS]

Examples:

kubectl logs nginx-pod Show logs from single-container pod
kubectl logs nginx-pod -c web-server Show logs from specific container
kubectl logs -f nginx-pod Follow log output in real-time
kubectl logs --previous nginx-pod Show logs from previous container instance
kubectl logs --tail=100 nginx-pod Show last 100 lines of logs

Notes:

Critical for debugging applications and monitoring

kubectl exec

Pod Management

Execute commands in a running container

Syntax:

kubectl exec POD_NAME [-c CONTAINER] -- COMMAND [args...]

Examples:

kubectl exec nginx-pod -- ls / Execute command in pod
kubectl exec -it nginx-pod -- bash Interactive shell access
kubectl exec nginx-pod -c web-server -- env Execute in specific container
kubectl exec nginx-pod -- cat /etc/hostname Read file from container

Notes:

Use -it flags for interactive sessions

kubectl delete pod

Pod Management

Delete pods by name or label selector

Syntax:

kubectl delete pod POD_NAME [OPTIONS]

Examples:

kubectl delete pod nginx-pod Delete specific pod
kubectl delete pods --selector app=nginx Delete pods by label
kubectl delete pod nginx-pod --force --grace-period=0 Force delete pod immediately
kubectl delete pods --all Delete all pods in namespace

Notes:

Use --force only when necessary; prefer graceful deletion

kubectl get services

Service Management

List services in the current namespace

Syntax:

kubectl get services [NAME] [--namespace=NAMESPACE] [OPTIONS]

Examples:

kubectl get services List all services
kubectl get svc Short form - list services
kubectl get svc -A List services in all namespaces
kubectl get svc -o wide Show additional service details

Notes:

Services expose pods and provide stable network endpoints

kubectl describe service

Service Management

Show detailed information about a service

Syntax:

kubectl describe service SERVICE_NAME [--namespace=NAMESPACE]

Examples:

kubectl describe service nginx-service Show service details and endpoints
kubectl describe svc nginx-service Short form - describe service

Notes:

Shows endpoints, selectors, and port configurations

kubectl expose

Service Management

Expose 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 service
kubectl expose pod nginx --port=80 --type=NodePort Expose pod with NodePort type
kubectl expose deployment web --port=80 --target-port=8080 Map service port to different container port
kubectl expose deployment nginx --port=80 --type=LoadBalancer Expose with LoadBalancer type

Notes:

Creates services to make pods accessible

kubectl port-forward

Service Management

Forward 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 80
kubectl port-forward service/nginx 8080:80 Forward to service instead of pod
kubectl port-forward deployment/nginx 8080:80 Forward to deployment
kubectl port-forward nginx 8080:80 --address 0.0.0.0 Allow external connections

Notes:

Useful for local development and debugging

kubectl get deployments

Deployment Management

List deployments in the current namespace

Syntax:

kubectl get deployments [NAME] [--namespace=NAMESPACE] [OPTIONS]

Examples:

kubectl get deployments List all deployments
kubectl get deploy Short form - list deployments
kubectl get deploy -A List deployments in all namespaces
kubectl get deploy -o wide Show additional deployment info

Notes:

Deployments manage ReplicaSets and pod updates

kubectl create deployment

Deployment Management

Create a new deployment

Syntax:

kubectl create deployment NAME --image=IMAGE [OPTIONS]

Examples:

kubectl create deployment nginx --image=nginx Create simple nginx deployment
kubectl create deployment web --image=nginx --replicas=3 Create deployment with 3 replicas
kubectl create deployment api --image=node:14 --port=3000 Create deployment with exposed port

Notes:

Quick way to create deployments; use YAML for complex configurations

kubectl scale

Deployment Management

Scale 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 replicas
kubectl scale deploy nginx --replicas=0 Scale down to 0 (effectively pause)
kubectl scale --replicas=3 -f deployment.yaml Scale using deployment file

Notes:

Horizontal scaling for handling load changes

kubectl rollout status

Deployment Management

Check the rollout status of a deployment

Syntax:

kubectl rollout status deployment/NAME [OPTIONS]

Examples:

kubectl rollout status deployment/nginx Check deployment rollout progress
kubectl rollout status deploy nginx --watch Watch rollout status continuously

Notes:

Monitor deployment updates and ensure successful rollouts

kubectl rollout history

Deployment Management

View rollout history of a deployment

Syntax:

kubectl rollout history deployment/NAME [OPTIONS]

Examples:

kubectl rollout history deployment/nginx Show deployment revision history
kubectl rollout history deploy nginx --revision=2 Show details of specific revision

Notes:

Track deployment changes and revision information

kubectl rollout undo

Deployment Management

Rollback a deployment to a previous revision

Syntax:

kubectl rollout undo deployment/NAME [OPTIONS]

Examples:

kubectl rollout undo deployment/nginx Rollback to previous revision
kubectl rollout undo deploy nginx --to-revision=2 Rollback to specific revision

Notes:

Quick way to recover from problematic deployments

kubectl get configmaps

ConfigMaps & Secrets

List ConfigMaps in the current namespace

Syntax:

kubectl get configmaps [NAME] [--namespace=NAMESPACE] [OPTIONS]

Examples:

kubectl get configmaps List all ConfigMaps
kubectl get cm Short form - list ConfigMaps
kubectl get cm app-config -o yaml Show ConfigMap in YAML format

Notes:

ConfigMaps store non-confidential configuration data

kubectl create configmap

ConfigMaps & Secrets

Create 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 values
kubectl create configmap app-config --from-file=config.properties Create from file
kubectl create configmap app-config --from-file=configs/ Create from directory
kubectl create configmap app-config --from-literal=db_host=localhost --from-literal=db_port=5432 Multiple literal values

Notes:

Multiple ways to create ConfigMaps from different sources

kubectl get secrets

ConfigMaps & Secrets

List Secrets in the current namespace

Syntax:

kubectl get secrets [NAME] [--namespace=NAMESPACE] [OPTIONS]

Examples:

kubectl get secrets List all secrets
kubectl get secret app-secret -o yaml Show secret in YAML (base64 encoded)
kubectl get secret app-secret -o jsonpath='{.data}' Extract secret data

Notes:

Secrets store sensitive data like passwords and tokens

kubectl create secret

ConfigMaps & Secrets

Create 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 literal
kubectl create secret generic app-secret --from-file=credentials.txt Create secret from file
kubectl create secret docker-registry regcred --docker-server=server --docker-username=user --docker-password=pass Create Docker registry secret
kubectl create secret tls tls-secret --cert=cert.crt --key=cert.key Create TLS secret

Notes:

Different secret types for various use cases

kubectl get namespaces

Namespace Operations

List all namespaces in the cluster

Syntax:

kubectl get namespaces [NAME] [OPTIONS]

Examples:

kubectl get namespaces List all namespaces
kubectl get ns Short form - list namespaces
kubectl get ns --show-labels Show namespace labels

Notes:

Namespaces provide scope for names and resource isolation

kubectl create namespace

Namespace Operations

Create a new namespace

Syntax:

kubectl create namespace NAME [OPTIONS]

Examples:

kubectl create namespace production Create production namespace
kubectl create ns staging Short form - create namespace

Notes:

Namespaces help organize and isolate cluster resources

kubectl config set-context

Namespace Operations

Set namespace for current context

Syntax:

kubectl config set-context --current --namespace=NAMESPACE

Examples:

kubectl config set-context --current --namespace=production Switch to production namespace
kubectl config set-context --current --namespace=default Switch back to default namespace

Notes:

Changes default namespace for all subsequent commands

kubectl delete namespace

Namespace Operations

Delete a namespace and all its resources

Syntax:

kubectl delete namespace NAME [OPTIONS]

Examples:

kubectl delete namespace test-env Delete namespace and all resources
kubectl delete ns old-project Short form - delete namespace

Notes:

WARNING: Deletes ALL resources in the namespace permanently

kubectl describe node

Debugging & Logs

Show detailed information about cluster nodes

Syntax:

kubectl describe node NODE_NAME

Examples:

kubectl describe node Describe all nodes
kubectl describe node worker-1 Describe specific node
kubectl get nodes -o wide List nodes with additional info

Notes:

Useful for checking node capacity, conditions, and allocated resources

kubectl get events

Debugging & Logs

List recent cluster events

Syntax:

kubectl get events [--namespace=NAMESPACE] [OPTIONS]

Examples:

kubectl get events List events in current namespace
kubectl get events -A List events in all namespaces
kubectl get events --sort-by=.metadata.creationTimestamp Sort events by creation time
kubectl get events --field-selector type=Warning Show only warning events

Notes:

Essential for troubleshooting cluster issues and pod problems

kubectl top nodes

Debugging & Logs

Display resource usage of nodes

Syntax:

kubectl top nodes [NODE_NAME] [OPTIONS]

Examples:

kubectl top nodes Show CPU and memory usage of all nodes
kubectl top node worker-1 Show usage of specific node

Notes:

Requires metrics-server to be installed in the cluster

kubectl top pods

Debugging & Logs

Display resource usage of pods

Syntax:

kubectl top pods [POD_NAME] [--namespace=NAMESPACE] [OPTIONS]

Examples:

kubectl top pods Show CPU and memory usage of pods
kubectl top pods -A Show usage across all namespaces
kubectl top pods --sort-by=cpu Sort pods by CPU usage
kubectl top pods --sort-by=memory Sort pods by memory usage

Notes:

Monitor resource consumption to identify performance issues

kubectl apply

Resource Management

Apply 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 file
kubectl apply -f configs/ Apply all YAML files in directory
kubectl apply -f https://raw.githubusercontent.com/user/repo/main/app.yaml Apply from URL
kubectl apply --dry-run=client -f deployment.yaml Test apply without making changes

Notes:

Preferred method for managing Kubernetes resources declaratively

kubectl get all

Resource Management

List 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 namespaces
kubectl get all --selector app=nginx List resources with specific label

Notes:

Quick overview of main resources; doesn't include everything

kubectl delete

Resource Management

Delete 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 file
kubectl delete deployment nginx Delete specific deployment
kubectl delete pods --selector app=nginx Delete pods by label selector
kubectl delete all --selector app=nginx Delete all resources with label

Notes:

Be careful with delete operations, especially with selectors

kubectl label

Resource Management

Add or update labels on resources

Syntax:

kubectl label TYPE NAME KEY=VALUE [OPTIONS]

Examples:

kubectl label pods nginx environment=production Add label to pod
kubectl label pods nginx environment=staging --overwrite Update existing label
kubectl label pods nginx environment- Remove label from pod
kubectl label nodes worker-1 role=database Label a node

Notes:

Labels are key-value pairs for organizing and selecting resources

kubectl annotate

Resource Management

Add 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 pod
kubectl annotate pods nginx description='Updated description' --overwrite Update annotation
kubectl annotate pods nginx description- Remove annotation

Notes:

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

  1. kubectl apply -f deployment.yaml - Deploy your application
  2. kubectl get pods - Check pod status
  3. kubectl logs -f pod-name - Follow application logs
  4. kubectl describe pod pod-name - Debug issues
  5. kubectl port-forward pod-name 8080:80 - Test locally
  6. kubectl 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