Kubernetes Cheat Sheet

Tushar PantTushar Pant
6 min read

Here’s a comprehensive Kubernetes cheat sheet, covering essential commands, objects, and concepts:


Basic Kubernetes Concepts

  • Cluster: A set of machines (nodes) that run containerized applications.

  • Node: A single machine in the cluster (physical/virtual).

  • Pod: The smallest deployable unit in Kubernetes (one or more containers).

  • Service: Exposes a set of Pods as a network service.

  • Deployment: Manages replica sets and ensures desired state.

  • Namespace: Provides scope for resources in a cluster.


Kubernetes Command Basics

CommandDescription
kubectl versionShow kubectl and cluster version
kubectl cluster-infoDisplay information about the cluster
kubectl get nodesList nodes in the cluster
kubectl get podsList pods in the default namespace
kubectl get servicesList all services
kubectl get deploymentsList all deployments
kubectl get namespacesList all namespaces
kubectl get eventsList events in the cluster
kubectl config viewShow kubectl config
kubectl config current-contextShow the current context

Viewing Resources

CommandDescription
kubectl get [resource]List specific resource type (e.g., pods, services, deployments)
kubectl describe [resource] [name]Detailed description of a resource
kubectl logs [pod-name]Get logs for a specific pod
kubectl logs [pod-name] -c [container-name]Get logs for a specific container in a pod
kubectl exec [pod-name] -- [command]Execute a command in a pod (like docker exec)

Creating and Managing Pods

CommandDescription
kubectl run [pod-name] --image=[image]Create a pod from an image
kubectl delete pod [pod-name]Delete a pod
kubectl scale deployment [name] --replicas=[n]Scale a deployment to n replicas
kubectl expose pod [pod-name] --port=[port]Expose a pod as a service

Working with Deployments

CommandDescription
kubectl create deployment [name] --image=[image]Create a deployment
kubectl rollout status deployment [name]Check the status of a deployment rollout
kubectl rollout undo deployment [name]Undo a deployment rollout
kubectl delete deployment [name]Delete a deployment

Services and Networking

CommandDescription
kubectl expose deployment [name] --type=[type] --port=[port]Expose a deployment as a service (types: ClusterIP, NodePort, LoadBalancer)
kubectl get svcList all services
kubectl describe svc [service-name]Get details of a service
kubectl delete svc [service-name]Delete a service

Namespaces

CommandDescription
kubectl create namespace [name]Create a new namespace
kubectl get namespacesList all namespaces
kubectl config set-context --current --namespace=[name]Set current namespace
kubectl delete namespace [name]Delete a namespace

ConfigMaps and Secrets

CommandDescription
kubectl create configmap [name] --from-literal=[key=value]Create a ConfigMap from literal values
kubectl create configmap [name] --from-file=[file-path]Create a ConfigMap from a file
kubectl get configmapsList ConfigMaps
kubectl describe configmap [name]Show ConfigMap details
kubectl delete configmap [name]Delete a ConfigMap
CommandDescription
kubectl create secret generic [name] --from-literal=[key=value]Create a Secret from literal values
kubectl get secretsList Secrets
kubectl describe secret [name]Show Secret details
kubectl delete secret [name]Delete a Secret

Volumes and Storage

CommandDescription
kubectl create -f [persistent-volume-file].yamlCreate a PersistentVolume from a YAML file
kubectl get pvList all PersistentVolumes
kubectl describe pv [pv-name]Show details of a PersistentVolume
kubectl create -f [persistent-volume-claim].yamlCreate a PersistentVolumeClaim
kubectl get pvcList all PersistentVolumeClaims
kubectl describe pvc [pvc-name]Show details of a PersistentVolumeClaim

Rolling Updates and Rollbacks

CommandDescription
kubectl rollout status deployment/[deployment-name]View status of a rolling update
kubectl set image deployment/[name] [container-name]=[new-image]Update container image
kubectl rollout history deployment/[deployment-name]View rollout history
kubectl rollout undo deployment/[deployment-name]Rollback to the previous deployment

Scaling Applications

CommandDescription
kubectl scale deployment [name] --replicas=[n]Scale the number of replicas
kubectl autoscale deployment [name] --min=[n] --max=[m] --cpu-percent=[percent]Autoscale a deployment based on CPU usage

Resource Quotas and Limits

CommandDescription
kubectl create -f [resource-quota].yamlCreate a ResourceQuota
kubectl get resourcequotasList ResourceQuotas
kubectl describe resourcequota [name]Show details of a ResourceQuota
kubectl delete resourcequota [name]Delete a ResourceQuota

Port Forwarding

CommandDescription
kubectl port-forward [pod-name] [local-port]:[remote-port]Forward local port to a pod’s port
kubectl port-forward service/[service-name] [local-port]:[remote-port]Forward local port to a service port

Debugging

CommandDescription
kubectl describe pod [pod-name]Get details of a specific pod (useful for troubleshooting)
kubectl logs [pod-name]View pod logs
kubectl exec -it [pod-name] -- /bin/shExecute a shell inside the pod
kubectl get eventsGet a list of events in the cluster

YAML Essentials

Pod Definition Example

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  namespace: default
spec:
  containers:
  - name: my-container
    image: nginx
    ports:
    - containerPort: 80

Deployment Example

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-container
        image: nginx
        ports:
        - containerPort: 80

Kubernetes Components

ComponentDescription
KubeletAgent that runs on nodes and ensures containers are running
Kube-ProxyManages network communication inside the cluster
etcdKey-value store for cluster data
API ServerFrontend for the Kubernetes control plane
Controller ManagerManages cluster controllers like deployments and replicas
SchedulerSchedules pods to run on available nodes

Other Useful Commands

CommandDescription
kubectl apply -f [file].yamlCreate/update resources using a YAML file
kubectl delete -f [file].yamlDelete resources defined in a YAML file
kubectl edit [resource] [name]Edit a resource in place
kubectl get allList all resources in the current namespace
kubectl top nodesShow resource usage by nodes
kubectl top podsShow resource usage by pods

This cheat sheet should give you a solid starting point for working with Kubernetes. Let me know if you'd like further details on any specific area!

0
Subscribe to my newsletter

Read articles from Tushar Pant directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Tushar Pant
Tushar Pant