Advanced

☸️ Kubernetes Intro

Manage many containers at once. Scale your app automatically.

πŸ“– What is Kubernetes?

Kubernetes (often called K8s) is a tool that manages many Docker containers. It decides where each container runs, restarts them if they crash, and scales them up or down when traffic changes.

πŸ’‘ Simple Example:
Imagine you have a pizza shop. One chef (Docker container) can only make so many pizzas. Kubernetes is the manager who hires more chefs when it gets busy and sends them home when it is quiet.

πŸ€” Why Use Kubernetes?

  • Self-healing: If a container crashes, Kubernetes starts a new one.
  • Scaling: Add more containers when traffic is high.
  • Load balancing: Spread traffic across all containers.
  • Rolling updates: Update your app with zero downtime.
  • Works anywhere: AWS, Azure, Google Cloud, or your own server.

🌍 Real Client Example

Client: A ticket-selling website for concerts.

Problem: When a popular concert goes on sale, 100,000 people visit at once. The single server crashes.

Solution with Kubernetes:

  1. Kubernetes notices the high traffic.
  2. It starts 20 more containers in seconds.
  3. Traffic is spread across all containers.
  4. When the sale ends, it shuts down extra containers.

Result: The site stays online. The client sells all tickets. No crashes.

πŸ”‘ Key Concepts

Word Simple Meaning
PodThe smallest unit. Usually one container.
NodeA worker machine (physical or virtual).
ClusterA group of nodes managed by Kubernetes.
DeploymentTells Kubernetes how many pods to run.
ServiceGives pods a stable address.
IngressRoutes outside traffic to services.

✍️ Your First Kubernetes File

This YAML file tells Kubernetes to run 3 copies of your app:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: myapp:latest
        ports:
        - containerPort: 80

What this does:

  • replicas: 3 β€” Run 3 copies of the app.
  • image: myapp:latest β€” Use your Docker image.
  • containerPort: 80 β€” The app listens on port 80.

⌨️ Basic kubectl Commands

CommandWhat It Does
kubectl apply -f app.yamlCreate or update resources.
kubectl get podsList all pods.
kubectl get servicesList all services.
kubectl logs <pod-name>See logs from a pod.
kubectl delete -f app.yamlDelete resources.

βœ… What You Learned

  • Kubernetes manages many Docker containers.
  • It self-heals, scales, and balances traffic.
  • Pods, nodes, clusters, deployments, and services are the key words.
  • You use kubectl to manage Kubernetes.