⎈ Kubernetes Basics & Command Cheatsheet – A Beginner’s Guide
Kubernetes (also called K8s) sounds complex when you hear it for the first time. But once you break it down, it’s just a powerful tool to manage your containerized apps.
Whether you're a backend developer, DevOps learner, or someone exploring cloud-native tech – this guide will help you get started with the basics of Kubernetes and a practical kubectl command cheatsheet.
What is Kubernetes?
In simple terms:
Kubernetes is a tool to manage and deploy containerized applications (like Docker containers).
It takes care of scaling, load balancing, rolling updates, and even auto-healing.
Originally built by Google, now managed by the Cloud Native Computing Foundation (CNCF).
Why use Kubernetes?
Here’s what makes Kubernetes so popular in the industry:
Auto-scaling: Handles traffic spikes smoothly.
Self-healing: Crashed containers restart automatically.
Load balancing: Routes traffic smartly to different pods.
Rolling updates: Updates without downtime.
Portability: Runs on cloud, on-premise, or hybrid environments.
Declarative setup: Everything is written in YAML, so easy to version control.
Core Concepts
Pod – The smallest deployable unit in K8s. It wraps around one or more containers (usually just one).
Node – A virtual or physical machine that runs your pods. A cluster has many nodes.
Cluster – A group of nodes managed by Kubernetes. This is your full infrastructure.
Deployment – Defines how many replicas (copies) of a pod to run. Handles rolling updates too.
ReplicaSet – Maintains a set number of pod replicas. It's usually auto-managed by Deployments.
Service – Exposes your pod(s) to the network. Can be internal (ClusterIP) or external (NodePort, LoadBalancer).
Namespace – Like folders in Kubernetes. Used to group and isolate resources (dev, test, prod, etc.).
ConfigMap – Stores config data in key-value format (non-sensitive values like ENV vars).
Secret – Like ConfigMap, but for sensitive data (API keys, passwords), stored securely.
Volume – Used to store persistent data. Stays even if the pod crashes or restarts.
PersistentVolume (PV) – A piece of storage in the cluster provided by an admin.
PersistentVolumeClaim (PVC) – A user’s request for storage, like “I want 5GB”.
StatefulSet – Like a Deployment, but used when pod identity matters. Useful for databases, Kafka, etc.
Ingress – Handles HTTP and HTTPS traffic. It lets you route external traffic to internal services using clean URLs and domains.
DaemonSet – Ensures a pod runs on every node (or selected ones). Useful for log collectors, monitoring agents, etc.
Job – Runs a task once to completion (like a script or batch job).
CronJob – Same as Job but runs on a schedule (like a Linux cron).
Horizontal Pod Autoscaler (HPA) – Scales pods automatically based on CPU/memory usage.
Kubectl Cheatsheet (Commands You’ll Actually Use)
Here’s a quick list of kubectl commands for daily usage, debugging, and managing resources.
Cluster and Nodes
# Show cluster endpoint and master details
kubectl cluster-info
# List all nodes in the cluster
kubectl get nodes
# Show details (capacity, conditions, labels) for a node
kubectl describe node <node-name>
# View CPU and memory usage for nodes
kubectl top node
Pods
# List all pods in the current namespace
kubectl get pods
# Show pods with extra info like IP and Node name
kubectl get pods -o wide
# Describe pod details, events, and status
kubectl describe pod <pod-name>
# View logs of the default container in a pod
kubectl logs <pod-name>
# View logs for a specific container in multi-container pods
kubectl logs <pod-name> -c <container-name>
# Follow pod logs in real time
kubectl logs -f <pod-name>
# Open an interactive terminal (bash) inside a running pod
kubectl exec -it <pod-name> -- bash
# Delete a pod manually
kubectl delete pod <pod-name>
# Copy files from pod to local machine
kubectl cp <pod-name>:/path/in/pod /local/path
Deployments and Rollouts
# List all deployments
kubectl get deployments
# Create a deployment using the nginx image
kubectl create deployment nginx --image=nginx
# Scale the deployment to 3 pods
kubectl scale deployment nginx --replicas=3
# Show rollout status of a deployment
kubectl rollout status deployment nginx
# Restart the deployment (useful after config changes)
kubectl rollout restart deployment nginx
# Roll back to previous deployment revision
kubectl rollout undo deployment nginx
# Delete the deployment
kubectl delete deployment nginx
Services and Networking
# List all services
kubectl get svc
# Expose a deployment as a NodePort service on port 80
kubectl expose deployment nginx --type=NodePort --port=80
# Show backend pods linked to a service
kubectl get endpoints
# Forward a local port to a pod or service for testing
kubectl port-forward svc/nginx 8080:80
ConfigMaps and Secrets
# Create a ConfigMap from literal key-value pair
kubectl create configmap my-config --from-literal=key=value
# List ConfigMaps
kubectl get configmaps
# View ConfigMap details
kubectl describe configmap my-config
# Create a secret with a literal key-value (password)
kubectl create secret generic my-secret --from-literal=password=1234
# List all secrets
kubectl get secrets
# View details of a secret
kubectl describe secret my-secret
YAML and Resource Management
# Apply resources from a YAML file
kubectl apply -f app.yaml
# Delete resources defined in a YAML file
kubectl delete -f app.yaml
# Show what would change without applying it
kubectl diff -f app.yaml
# List all resources in the current namespace (pods, svc, deployments)
kubectl get all
Namespaces
# List all namespaces
kubectl get namespaces
# Create a new namespace
kubectl create namespace dev-env
# Set default namespace for kubectl context
kubectl config set-context --current --namespace=dev-env
Debugging and Troubleshooting
# Describe a pod to check for events, errors, restarts
kubectl describe pod <pod-name>
# View cluster-wide events sorted by time
kubectl get events --sort-by=.metadata.creationTimestamp
# View raw YAML of a pod definition
kubectl get pod <pod-name> -o yaml
# List all pods across all namespaces
kubectl get pods -A
Metrics (if metrics-server is installed)
# Show CPU and memory usage for all pods
kubectl top pod
# Show resource usage per container inside each pod
kubectl top pod --containers
Free Resources to Practice Kubernetes
Play with Kubernetes (Free browser-based playground): https://labs.play-with-k8s.com/
Kubernetes Official Docs: https://kubernetes.io/docs/
Lens – GUI for Kubernetes (Desktop App)
Final Thoughts
Learning Kubernetes is more about practice than theory. Start small – run your first container in a pod, deploy Nginx, apply a YAML file. As you face errors, you’ll get better at debugging.
Use this cheatsheet whenever you're stuck or preparing for a DevOps/cloud interview.
Let us know if you'd like a follow-up on:
Setting up Kubernetes with Minikube or Kind
Writing your first deployment YAML from scratch
Real-world interview questions from Kubernetes basics