1. Introduction
Kubernetes (often abbreviated as K8s) is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications.
While Docker helps you create and run containers, Kubernetes helps you run and manage containers across multiple servers (a cluster) efficiently. It eliminates manual work such as restarting crashed containers, load balancing traffic, or scaling the number of containers up and down.
In short:
2. Why Kubernetes Exists
Running a few containers manually is easy. But managing hundreds or thousands of containers across multiple machines quickly becomes complex.
Kubernetes solves this by:
Automatically restarting failed containers
Distributing load across nodes
Scaling containers up/down based on demand
Managing rolling updates without downtime
3. Core Concepts
a. Cluster
A Kubernetes cluster is the foundation. It has two types of machines:
Master Node (Control Plane): Manages the cluster (scheduling, scaling, etc.)
Worker Nodes: Run the actual applications (containers)
b. Node
A node is a single machine (physical or virtual) that runs your containers.
Each node contains:
Kubelet (communicates with the master)
Container runtime (e.g., Docker, containerd)
Kube-proxy (manages networking)
c. Pod
The smallest deployable unit in Kubernetes.
A pod wraps one or more containers that share storage, network, and configuration.
You rarely run individual containers directly in Kubernetes — you run them inside pods.
Example:
A web app and its helper logging container run together in one pod.
d. Deployment
Defines how many pods you want, what image to use, and how to update them.
Kubernetes automatically keeps the desired number of pods running.
Example:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: web
image: nginx
ports:
- containerPort: 80
Run it:
kubectl apply -f deployment.yaml
e. Service
A service exposes pods to the network.
Pods are temporary — when they restart, their IPs change. A service provides a stable IP and DNS name to reach them.
Example:
apiVersion: v1
kind: Service
metadata:
name: web-service
spec:
type: NodePort
selector:
app: web
ports:
- port: 80
targetPort: 80
nodePort: 30080
f. Namespace
Used to divide cluster resources logically.
You can have different environments (e.g., dev, staging, prod) in the same cluster.
4. Kubernetes Architecture
Control Plane (Master Node Components):
API Server: Entry point for all commands (kubectl
talks to this).
Controller Manager: Ensures desired state (e.g., keeps 3 pods running).
Scheduler: Decides which node a pod should run on.
etcd: Key-value store for cluster configuration and state.
Worker Node Components:
Kubelet: Communicates with API server, manages pods on that node.
Kube-proxy: Manages network routing.
Container Runtime: Runs containers (Docker, containerd, etc.).
5. Basic Workflow
You define what you want (desired state) in a YAML file (e.g., 3 replicas of a web app).
You apply it:
kubectl apply -f deployment.yaml
Kubernetes schedules pods on nodes.
If a pod crashes, the controller recreates it.
If traffic increases, you scale replicas:
kubectl scale deployment web-app --replicas=5
The service ensures all pods are reachable through a single endpoint.
6. Common Kubernetes Objects
Object | Purpose |
---|
Pod | Smallest deployable unit containing one or more containers |
ReplicaSet | Ensures a specified number of pods are running |
Deployment | Manages ReplicaSets and rolling updates |
Service | Provides stable networking for pods |
ConfigMap | Stores configuration data |
Secret | Stores sensitive data (passwords, keys) |
Ingress | Manages external HTTP/HTTPS access |
Volume | Persists data beyond container lifecycle |
Namespace | Logical grouping of resources |
7. Networking in Kubernetes
Pod-to-Pod communication: Managed internally by the cluster.
Service: Exposes pods within or outside the cluster.
Ingress: Routes external HTTP/HTTPS requests to services.
Example ingress:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: web-ingress
spec:
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: web-service
port:
number: 80
8. Scaling and Load Balancing
Kubernetes can automatically scale pods based on CPU or memory usage using Horizontal Pod Autoscaler (HPA).
Example:
kubectl autoscale deployment web-app --min=2 --max=10 --cpu-percent=80
Load balancing happens automatically through Services, distributing traffic evenly across pods.
9. Storage and Persistence
Containers are temporary. To persist data, Kubernetes provides Volumes and PersistentVolumes (PVs).
Developers request storage via PersistentVolumeClaims (PVCs).
Example PVC:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: data-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
10. Monitoring and Logging
Kubernetes provides metrics and logging integration with tools like:
Command to check pod logs:
kubectl logs pod-name
11. Common Commands
Command | Description |
---|
kubectl get pods | List running pods |
kubectl get deployments | List deployments |
kubectl get services | List services |
kubectl describe pod pod-name | Show detailed info |
kubectl delete pod pod-name | Delete a pod |
kubectl apply -f file.yaml | Apply configuration |
kubectl exec -it pod-name -- bash | Access container shell |
12. Kubernetes vs Docker
Feature | Docker | Kubernetes |
---|
Focus | Containerization | Orchestration |
Scope | Single host | Multi-host cluster |
Scaling | Manual | Automatic |
Load Balancing | Manual setup | Built-in |
Fault Tolerance | None | Automatic pod recovery |
Note: Docker and Kubernetes are complementary, not competitors. Kubernetes runs Docker containers (or other runtimes) at scale.
13. Why Use Kubernetes
Self-healing: Automatically restarts failed containers.
Scalability: Scale up/down based on load.
Zero downtime: Rolling updates and rollbacks.
Portability: Works on any cloud or on-premise environment.
Automation: Manages resource allocation and health checks automatically.
14. Best Practices
Use Namespaces to separate environments.
Store configs in ConfigMaps and secrets in Secrets.
Use readiness and liveness probes for health checks.
Keep YAML files version-controlled.
Use Resource Limits to prevent a pod from consuming too many resources.
15. Summary
Kubernetes is the industry-standard platform for automating containerized applications.
It abstracts infrastructure complexity and ensures your apps are always available, scalable, and resilient.