top of page

Scaling a Flask App (Docker vs Kubernetes)


Step 1: Create Simple Flask App

Create file: app.py

from flask import Flask
import socket

app = Flask(__name__)

@app.route("/")
def hello():
    return f"Hello from {socket.gethostname()}"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

Step 2: Dockerfile

Create file: Dockerfile

FROM python:3.9
WORKDIR /app
COPY . .
RUN pip install flask
CMD ["python", "app.py"]

Build image:

docker build -t flask-demo .

Run container:

docker run -d -p 5000:5000 flask-demo

Open browser:



To scale we must manually run:

docker run -d -p 5001:5000 flask-demo
docker run -d -p 5002:5000 flask-demo

Problems:

  • Manual scaling

  • Manual load balancing

  • Manual restart if crash




Step 3: Kubernetes Deployment

Enable Kubernetes (Docker Desktop or Minikube)

Create deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: flask-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: flask
  template:
    metadata:
      labels:
        app: flask
    spec:
      containers:
      - name: flask-container
        image: flask-demo
        imagePullPolicy: Never
        ports:
        - containerPort: 5000

Apply:

kubectl apply -f deployment.yaml

Check:

kubectl get pods

You’ll see 3 pods automatically created


Step 4: Create Service

Create service.yaml

apiVersion: v1
kind: Service
metadata:
  name: flask-service
spec:
  type: NodePort
  selector:
    app: flask
  ports:
    - port: 80
      targetPort: 5000
      nodePort: 30007

Apply:

kubectl apply -f service.yaml

Now open:

Refresh multiple times → You’ll see different hostnames

That proves: Load balancing working automatically

Step 5: Auto Scaling Demo

Increase replicas:

kubectl scale deployment flask-deployment --replicas=5

Check:

kubectl get pods

Now 5 pods running

Docker cannot do this automatically.

Practical Example 2

Self-Healing Demo


Delete one pod manually:

kubectl delete pod <pod-name>

Now run:

kubectl get pods

You will see:Kubernetes automatically creates new pod.


 
 
 

Kubernetes (K8s) is an open-source platform used to manage, scale, and run containerized applications automatically.


Kubernetes is a container orchestration tool that automates deployment, scaling, networking, and management of containers.

Kubernetes Architecture




When You Work Only With Docker

docker run myapp

What happens?

  1. Docker pulls image

  2. Creates container

  3. Runs on one machine only

  4. If it crashes → you restart manually

  5. If you want 3 containers → you run command 3 times

  6. No automatic load balancing


Kubernetes = Manages containers automatically across machines

In Docker you type:

docker run


In Kubernetes you type:

kubectl apply -f app.yaml


We need to start Kubernetes from Docker like below ,




API Server (Brain of Kubernetes)


Everything goes through API Server.


  • Accepts your YAML file

  • Stores configuration

  • Tells cluster what to do


YAML file


etcd (The Memory)

In Docker:There is no central memory storing desired state.

In Kubernetes


etcd stores everything

Example:

  1. You said: "I want 3 replicas"

  2. That info is stored in etcd


Even if master restarts → configuration is safe.


Scheduler (Decision Maker)

Now question:Where should the container run?

If you have:

  1. Worker Node 1

  2. Worker Node 2

Scheduler decides which node has CPU/RAM available.


Controllers (Auto-Healing System)

Controllers constantly check:

Is actual state matching desired state?

Example:

  1. You said 3 pods

  2. One pod crashes

  3. Controller sees only 2 running

  4. Automatically creates 1 new pod


This is called:

Self Healing

Docker alone cannot do this automatically.


Worker Node

Worker node is where actual containers run.

Each worker node has:


Kubelet (Node Manager)

Think of Kubelet as:

The manager of that machine

It:


Talks to API server

  1. Creates pods

  2. Ensures containers are running

  3. Reports health

Container Runtime (Docker)


Earlier Kubernetes used Docker directly.

Now it uses container runtimes like:

containerd

CRI-O

But concept same.

This actually runs the container.


Pod (Very Important)


In Docker:Container = smallest unit

In Kubernetes:Pod = smallest unit


Pod contains:

  1. One or more containers

  2. Shared network

  3. Shared storage


Normally:1 Pod = 1 container


kube-proxy (Networking)

Handles:

  1. Internal communication

  2. Load balancing between pods


If 3 pods running:Traffic is automatically distributed.


 
 
 

Create a folder:

simple-docker-app

Inside it, create 3 files:

simple-docker-app/
├── app.py
├── requirements.txt
└── Dockerfile

Step 2 — app.py

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello! This app is running inside a Docker container."

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)

Step 3 — requirements.txt

flask

Step 4 — Dockerfile

FROM python:3.10-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

CMD ["python", "app.py"]

Step 5 — Build Docker Image

Open terminal in this folder:

docker build -t simpleapp:v1 .

Check image:

docker images

Step 6 — Run Container

docker run -p 5000:5000 simpleapp:v1

Step 7 — Open in Browser

Open:

You will see:

Hello! This app is running inside a Docker container.


Container 1 — Nginx Website (HTML only)

Files

nginx-app/
└── index.html

index.html

<h1>Nginx running inside Docker</h1>

Dockerfile

FROM nginx:alpine
COPY index.html /usr/share/nginx/html/index.html

Build & Run

docker build -t nginxapp .
docker run -p 8080:80 nginxapp

Container 2 — Python Script Container (prints output)

Files

python-script/
├── script.py
└── Dockerfile
print("Python running inside Docker container")

Dockerfile

FROM python:3.10-slim
COPY script.py .
CMD ["python", "script.py"]

Build & Run

docker build -t pyscript .
docker run pyscript

Container 3 — Node.js Web App

Files

node-app/
├── app.js
└── Dockerfile

app.js

const http = require('http');
http.createServer((req,res)=>{
  res.write("Node.js app in Docker");
  res.end();
}).listen(3000);

Dockerfile

FROM node:18-alpine
COPY app.js .
CMD ["node", "app.js"]

Build & Run

docker build -t nodeapp .
docker run -p 3000:3000 nodeapp

Container 4 — MySQL Database Container

(No files needed)

Run directly

docker run -d \
  --name mysqlcontainer \
  -e MYSQL_ROOT_PASSWORD=root123 \
  -p 3306:3306 \
  mysql:8

Check running:

docker ps

 
 
 

© 2023 by newittrendzzz.com 

  • Facebook
  • Twitter
  • Instagram
bottom of page