☸️ KUBERNETES BASIC
☸️ Kubernetes (K8s) – Complete Core Notes
Container Orchestration Architecture, Cluster Nodes, Pods Lifecycle, Deployments, Declarative YAML aur Kubectl CLI ka comprehensive reference guide.
1️⃣ Kubernetes Kya Hai?
Definition: Kubernetes (jise K8s bhi kaha jata hai) ek open-source container orchestration platform hai jise Google ne develop kiya tha. Yeh hazaron containers ko automatically deploy, scale, balance aur manage karta hai.
- Container Orchestration: Containerized applications ke deployment aur networking ko automate karta hai.
- Auto-Scaling: Web traffic badhne par automatically new pods spawn karta hai aur traffic kam hone par scale-down kar deta hai.
- Self-Healing: Agar koi container ya pod crash ho jaye, toh K8s use bina human intervention ke instantly restart ya replace kar deta hai.
2️⃣ Kubernetes Ke Core Features
- Automated Rollouts & Rollbacks: Application me changes bina kisi downtime ke deploy ho jate hain; error aane par turant pichhle version par rollback ho jata hai.
- Service Discovery & Load Balancing: Har pod ko unique IP aur single DNS name assign karta hai aur incoming network traffic ko pods me distribute karta hai.
- Storage Orchestration: Local storage, public cloud storage (AWS EBS, GCP PD) ko automatically pods ke sath mount karta hai.
- Secret & Configuration Management: Passwords, API keys aur configurations ko bina container image dobara build kiye securely store aur update karta hai.
3️⃣ Kubernetes Cluster Architecture Components
| Component | Role & Architectural Layer | Description |
|---|---|---|
| Cluster | High-level Environment | Master (Control Plane) aur Worker Nodes ka collective group. |
| Control Plane (Master) | Management Brain | Cluster decisions (Scheduling, event response, cluster state) manage karta hai. |
| Node (Worker Machine) | Physical / Virtual Server | Actual physical server ya cloud VM jahan applications run hoti hain. |
| Pod | Smallest Execution Unit | Ek ya ek se zyada containers ka group jo shared storage aur network IP use karte hain. |
| Deployment | Declarative Controller | Pods ke desired number of replicas aur rolling updates ko maintain karta hai. |
| Service | Networking Abstraction | Dynamic pods ke group ko stable IP address aur DNS provide karke expose karta hai. |
4️⃣ Essential Kubectl CLI Commands
Kubernetes cluster se interact karne ke liye kubectl command line tool use kiya jata hai:
| Command | Syntax Preview | Kaam / Purpose |
|---|---|---|
kubectl get nodes |
kubectl get nodes -o wide |
Cluster me judhe sabhi active nodes ka status dekhna. |
kubectl get pods |
kubectl get pods -A |
Cluster me running pods ki list aur unka health status dekhna. |
kubectl apply |
kubectl apply -f deployment.yaml |
YAML configuration file se resources create ya update karna. |
kubectl describe |
kubectl describe pod <pod-name> |
Kisi specific pod ke detailed events aur logs troubleshoot karna. |
kubectl delete |
kubectl delete pod <pod-name> |
Kisi running resource ko terminate/delete karna. |
kubectl logs |
kubectl logs -f <pod-name> |
Container ke live application logs terminal par stream karna. |
5️⃣ Deploying a Container (Declarative YAML)
Kubernetes me resources hamesha declarative YAML files ke zariye define aur apply kiye jaate hain:
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app-deployment
labels:
app: web-app
spec:
replicas: 3
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
containers:
- name: web-container
image: nginx:latest
ports:
- containerPort: 80
Configuration apply karne ke liye run karein:
kubectl apply -f deployment.yaml
6️⃣ Scaling Pods & Self-Healing Simulation
Traffic demand ke hisab se CLI ya file se instances scale kiye jaate hain:
kubectl scale deployment web-app-deployment --replicas=5
deployment.apps/web-app-deployment scaled [3/3 pods healthy]
kubectl delete pod se manually kill bhi kar denge, toh Deployment controller turant 1 naya pod start kar dega taaki total replica count hamesha barabar rahe.