top of page

How to use Kubernetes

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.


$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button

$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button.

$50

Product Title

Product Details goes here with the simple product description and more information can be seen by clicking the see more button. Product Details goes here with the simple product description and more information can be seen by clicking the see more button.

Recommended Products For This Post
 
 
 

Comments


© 2023 by newittrendzzz.com 

  • Facebook
  • Twitter
  • Instagram
bottom of page