Kubernetes - ashwani-cse/next-gen-pizza-backend GitHub Wiki
π Server = A computer that runs your application.
Example:
-
You create a website β you need a server to host it.
-
Server = RAM + CPU + Storage, like your laptop, but for serving others.
Bare Metal Server = A real, physical machine.
(e.g., Dell/HP rack server sitting in a data center.)
Earlier, people bought 1 physical server for 1 application.
-
Wasted space (Server CPU 20% used, 80% idle).
-
Expensive.
-
Difficult to manage multiple applications.
To fix this waste, we invented Virtualization.
π Hypervisor = Special software that runs multiple virtual computers (VMs) on 1 physical server.
Examples of Hypervisors:
-
VMware vSphere
-
KVM
-
VirtualBox (you might have used this on your laptop!)
Now:
-
1 physical server β run 10 virtual machines β save money, better use.
π‘ Think: Hypervisor = "Magician" that shows 10 computers from 1 real computer.
VMs were good but:
-
Heavyweight β each VM needs a full OS (Linux/Windows).
-
Slow boot time (takes minutes).
-
High RAM and CPU usage.
In short: Overkill for small apps.
Now enters Containers (like Docker).
π Container = Light-weight alternative to VM.
-
No full OS inside.
-
Just your app + its dependencies.
-
Shares the main hostβs OS.
Result:
-
Starts in seconds.
-
Very little RAM/CPU needed.
-
Easy to move (portable).
Docker = Tool to create, run, and manage containers.
-
Package your app once β run anywhere (laptop, server, cloud).
-
Solves the famous "it works on my machine" problem.
Example:
docker run --name my-nginx -p 80:80 -d nginx
Above command downloads a web server (nginx
) container and runs it in seconds.
Okay, now imagine:
-
You have 5 containers β easy.
-
You have 500 containers β difficult!
Challenges:
-
How to start/stop them?
-
How to restart if they crash?
-
How to balance load (traffic)?
-
How to upgrade them without downtime?
-
How to monitor health?
Solution Needed β Some "manager" for containers.
π Kubernetes = A Container Orchestrator.
Means:
-
It automatically manages your containers.
-
It restarts them if they fail.
-
It can create thousands of containers on hundreds of servers.
-
It does scaling, load balancing, updates, health monitoring.
π‘ "Kubernetes = Army General managing all soldiers (containers)."
Step | Technology | Why it came |
---|---|---|
1 | Bare Metal (Physical Servers) | Needed to run apps |
2 | Hypervisor (VMware/KVM) | Solve hardware wastage |
3 | Virtual Machines (VMs) | Run multiple OS on one server |
4 | Containers (Docker) | Lighter, faster, portable apps |
5 | Kubernetes (K8s) | Manage 100s/1000s containers easily |
π Kubernetes = Manages your containers across many servers.
It uses a special structure:
Cluster β Nodes β Pods β Containers
-
Cluster = Whole system.
-
Node = A single machine (server).
-
Pod = Smallest unit in Kubernetes.
-
Container = Your app (inside Pod).
π Cluster = Group of machines (servers) working together.
In Kubernetes:
-
One machine is called Master Node (or Control Plane).
-
Other machines are Worker Nodes.
Cluster = Master Node + Worker Nodes.
π Node = A single machine (physical or virtual) inside the Kubernetes cluster.
Two types of nodes:
Node Type | Purpose |
---|---|
Master Node (Control Plane) | Controls the whole cluster. |
Worker Node | Runs your actual apps (containers). |
π‘ Think of Master as Manager and Workers as Laborers.
π Pod = Smallest unit Kubernetes manages.
-
A Pod = 1 or more Containers bundled together.
-
Pod shares:
-
Same IP Address
-
Same Storage
-
Same Network
-
Usually: 1 Pod = 1 Container (but not always).
π‘ Pod = Room where your app (container) lives.
π Container = Your actual application running inside a Pod.
Examples:
-
Nginx container (web server)
-
Redis container (database cache)
-
Spring Boot container (Java app)
- Cluster
- Master Node
- Controls everything
- Worker Node 1
- Pod 1 -> Container (App)
- Pod 2 -> Container (App)
- Worker Node 2
- Pod 3 -> Container (App)
- Pod 4 -> Container (App)
- Master Node
Imagine you are running an online store (Amazon-like):
-
5 Servers = Cluster
-
1 Server (Master) manages the others.
-
Other 4 Servers (Workers) run:
-
Pod 1: Website app
-
Pod 2: Payment app
-
Pod 3: Notification app
-
Pod 4: Database app
-
Kubernetes:
-
Will keep your store running even if 1 server goes down.
-
Will add more Pods automatically if too many people visit your site.
-
Will upgrade your app without downtime.
-
What is Kubernetes
-
Why it exists
-
Basic architecture: Cluster β Nodes β Pods β Containers
Component | Role |
---|---|
kube-apiserver | Brain: Accepts commands. |
kube-scheduler | Decides where to run pods. |
kube-controller-manager | Watches and fixes problems. |
etcd | Database: Saves cluster state. |
kubelet (on Worker Node) | Talks to Master, runs Pods. |
kube-proxy (on Worker Node) | Manages network traffic to Pods. |
- The user initiates an action using the
kubectl
command. - The request is sent to the
kube-apiserver
, which serves as the front-end for the Kubernetes control plane. - The
kube-apiserver
checks the request and may interact withetcd
to retrieve or store cluster state information, if necessary. - The request is forwarded to either:
-
kube-scheduler
(for scheduling Pods) or -
kube-controller-manager
(for managing replica sets or other cluster tasks).
-
- The
kube-scheduler
determines the most appropriate worker node and schedules the Pod to run on that node. - The
kubelet
on the worker node ensures the Pod and its containers are running and healthy as per the specification. -
kube-proxy
manages network rules to ensure proper communication between Pods and external services.
πΉ Role:
The Gatekeeper β All operations on the cluster pass through it.
πΉ How it works:
- You send a request (e.g.,
kubectl create deployment
). - API Server:
- Validates the request.
- Authenticates (Are you who you claim?)
- Authorizes (Are you allowed to do this?)
- Updates etcd (if needed).
- Notifies Controllers to take action.
πΉ Example:
kubectl apply -f app.yaml
Flow: Request β kube-apiserver β Validation β Stored in etcd β Controllers informed β Scheduler starts.
πΉ Role:
The Brain Database β A super reliable key-value store for all cluster data.
πΉ How it works:
- Stores everything: Pods, Services, ConfigMaps, Nodes.
- Uses Raft algorithm for consistency and leader election.
- If etcd survives, your cluster can be fully recovered.
πΉ Example: Create a Pod β Its details are saved inside etcd.
πΉ Role:
Maintains the Cluster's Health β Ensures the actual state matches the desired state.
πΉ How it works:
- Continuously watches the API server.
- If something is wrong, it fixes it automatically.
πΉ Key Controllers:
- ReplicationController: Maintain number of Pod copies.
- NodeController: Monitor node health.
- DeploymentController: Handle rolling updates, rollbacks.
- JobController: Manage one-time/batch jobs.
πΉ Example: A Pod crashes β ReplicationController detects β Creates a new Pod.
πΉ Role:
The Pod Placement Expert β Decides where new Pods should run.
πΉ How it works:
- Looks at:
- CPU/Memory availability
- Node Labels
- Taints/Tolerations
- Affinity Rules
- Picks the best Worker Node.
- Updates the Pod with the Node's name.
πΉ Example: Pod needs 4GB RAM β kube-scheduler finds a node that fits!
πΉ Role:
The Pod Caretaker β Talks to the API Server and manages Pods.
πΉ How it works:
- Receives tasks from API server.
- Pulls necessary container images.
- Starts and monitors containers inside Pods.
- Reports health status.
πΉ Example: A container crashes β kubelet restarts it automatically.
πΉ Role:
The Network Mastermind β Manages networking on the node.
πΉ How it works:
- Sets up iptables or IPVS rules.
- Routes:
- Pod-to-Pod
- Pod-to-Service
- External-to-Pod traffic
- Handles load balancing internally.
πΉ Example: Your web app is exposed via a Service β kube-proxy makes sure requests find the right Pod.
πΉ Role:
Smallest Deployable Unit β Runs one or more containers together.
πΉ How it works:
- Shared IP and Storage among containers.
- Managed completely by Kubernetes.
- Ephemeral: If a Pod dies, Kubernetes creates a new one (not resurrects the old).
πΉ Example: Frontend + Backend container needing tight communication β Same Pod!
πΉ Role:
The Real Applications β Actual software running inside Pods.
πΉ How it works:
- Isolated using cgroups and namespaces.
- Pulled from container registries (DockerHub, AWS ECR, etc.)
- Managed by container runtime (Docker, containerd, etc.)
πΉ Example: You can run manually:
docker run nginx
But in Kubernetes, containers run inside Pods automatically.