top of page

1. Introduction

Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. Docker is used to build and run containers, while Kubernetes manages those containers across clusters. This document provides a structured guide to deploying a simple Flask web application on Kubernetes using Docker Desktop.

2. Prerequisites

Before beginning, ensure the following components are installed and configured:

  1. Docker Desktop (latest version)

  2. Kubernetes enabled within Docker Desktop

  3. Kubectl command-line tool (installed automatically with Docker Desktop)

  4. Basic understanding of Docker and YAML configuration files

3. Enabling Kubernetes in Docker Desktop

  1. Open Docker Desktop on your system.

  2. Navigate to Settings → Kubernetes.

  3. Select Enable Kubernetes.

  4. Click Apply and Restart.

  5. After installation, verify Kubernetes is running using the following commands in the terminal:

    kubectl version --client kubectl get nodes

    The output should confirm that a Kubernetes node named docker-desktop is ready.

4. Creating a Flask Application

  1. Create a new project directory, for example, flask-k8s-app.

  2. Inside this directory, create a file named app.py with the following content:

    from flask import Flask app = Flask(__name__) @app.route('/') def home(): return "Hello from Kubernetes with Docker!" if name == '__main__': app.run(host='0.0.0.0', port=5000)

5. Creating a Dockerfile

  1. In the same directory, create a file named Dockerfile with the following content:

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

  2. Build the Docker image using the command:

    docker build -t myflaskapp:1.0 .

  3. Verify the image creation:

    docker images

6. Testing the Docker Image

Run the container locally to ensure it works correctly before deploying to Kubernetes.

docker run -d -p 5000:5000 myflaskapp:1.0

Open a web browser and visit http://localhost:5000.The page should display “Hello from Kubernetes with Docker!”.

7. Creating a Kubernetes Deployment

  1. Create a file named deployment.yaml with the following content:

    apiVersion: apps/v1 kind: Deployment metadata: name: flask-deployment spec: replicas: 2 selector: matchLabels: app: flaskapp template: metadata: labels: app: flaskapp spec: containers: - name: flaskapp image: myflaskapp:1.0 ports: - containerPort: 5000

  2. Apply the deployment configuration:

    kubectl apply -f deployment.yaml

  3. Verify the deployment:

    kubectl get deployments kubectl get pods

    The output should show that two pods are running for the Flask deployment.

8. Exposing the Application as a Service

  1. Create a file named service.yaml with the following configuration:

    apiVersion: v1 kind: Service metadata: name: flask-service spec: type: LoadBalancer selector: app: flaskapp ports: - protocol: TCP port: 5000 targetPort: 5000

  2. Apply the service configuration:

    kubectl apply -f service.yaml

  3. Verify the service:

    kubectl get svc

    Note the external or localhost IP and port number. Open the application in a web browser using that address to confirm that it is accessible through Kubernetes.

9. Scaling the Application

Kubernetes can easily scale applications to handle additional load.

Increase the number of running pods to five using the following command:

kubectl scale deployment flask-deployment --replicas=5

Verify the scaling:

kubectl get pods

The output will show five running pods for the same deployment.

10. Monitoring and Managing the Deployment

To monitor and manage the running resources, use the following commands:

kubectl get all
kubectl logs <pod-name>

Replace <pod-name> with the actual name of a running pod to view its logs.

To clean up resources after testing:

kubectl delete -f service.yaml
kubectl delete -f deployment.yaml

11. Summary

This exercise demonstrated how to deploy a Dockerized Flask application on Kubernetes using Docker Desktop.The steps covered include enabling Kubernetes, creating a Docker image, deploying it to Kubernetes, exposing it as a service, and scaling it.Through this process, you have learned the essential workflow of integrating Docker containers with Kubernetes for local orchestration and testing.

 
 
 

Introduction

If you’re new to Docker and wondering how developers run applications so smoothly across different systems, this post is for you.In this first practical, we’ll walk through how to install Docker Desktop on Windows and run your very first container — the legendary “Hello World” container.


By the end of this guide, you’ll understand what Docker does and how it proves that your environment is ready for future DevOps experiments.


What is Docker?

Before jumping into the commands, let’s get the idea straight.

Docker is a tool that lets you run applications inside containers — small, isolated environments that include everything your app needs to run: code, libraries, and settings.

Think of a container as a mini-computer inside your computer — it runs consistently, no matter where you move it.


Objective

To install Docker Desktop on Windows and verify the setup by running a simple “Hello World” container.

Step 1: Check Your System Requirements

Before downloading, make sure your system is ready.

Windows 10 or 11 (64-bit)WSL 2 (Windows Subsystem for Linux) enabled

Hardware virtualization turned ON in BIOS

Tip: To check if virtualization is enabled, open Task Manager → Performance Tab → CPU.If you see “Virtualization: Enabled,” you’re good to go.

Step 2: Download Docker Desktop

  1. Visit the official Docker website:👉 https://www.docker.com/products/docker-desktop

  2. Click “Download for Windows (x86_64)”.

  3. Wait for the installer to finish downloading.

    ree

Step 3: Install Docker Desktop

  1. Run the installer (Docker Desktop Installer.exe).

  2. Keep the option “Use WSL 2 instead of Hyper-V” checked.

  3. Click Next → Install.

  4. Once the installation completes, launch Docker Desktop.

You’ll notice a little whale icon 🐳 appear in your Windows taskbar — that’s Docker running successfully!


Step 4: Verify Docker Installation

Open Command Prompt or PowerShell, and type:

docker --version

ree

You should see something like:

Docker version 27.0.2, build xxxxxxx

Next, check Docker details:

docker info

You’ll get details about your Docker client, server, containers, and images.


ree
This confirms that Docker is correctly installed and running in the background.

🧩 Step 5: Run Your First Container

Now comes the exciting part — running your first Docker container!

Type the following command:

docker run hello-world
ree

What happens:

  • Docker looks for an image called hello-world.

  • If it’s not found locally, it pulls it from Docker Hub (the official Docker image repository).

  • Then it creates and runs a container that prints a message.

Output:

Hello from Docker!
This message shows that your installation appears to be working correctly.

Congratulations!You’ve just run your first Docker container.


Step 6: Explore Docker Commands

Check all running containers:

docker ps
ree

Since the hello-world container stops immediately after running, you’ll see no active containers.

Now, check all containers (including stopped ones):

docker ps -a
ree

Example Output:

CONTAINER ID   IMAGE          STATUS                      NAMES
1a2b3c4d5e6f   hello-world    Exited (0) 2 minutes ago    quirky_torvalds
Docker automatically assigns fun names (like quirky_torvalds) to your containers!



 
 
 

1. Quick overview — what we’ll do and why


What we’ll build (high level)Two small microservices (Python + Flask) that run in separate Docker containers and communicate over Docker’s network.


user-service — serves user info at


order-service — calls user-service and returns user + order info


We orchestrate both with Docker Compose so they start together and can discover each other by service name.


Why this exercise matters


Demonstrates key microservices concepts: service isolation, inter-service communication, service discovery (via Docker Compose DNS), and reproducible deployments.

Shows how Docker networks enable containers to call each other by service name rather than IP.


Prepares students for real-world systems where services are independent, scalable, and managed via orchestration tools (Compose/Kubernetes).


Related real-world examples


E-commerce: auth-service, product-service, order-service, payment-service.

Social app: user-profile, feed, notification.

Monitoring: a service pushing metrics to a separate ingestion API.


2. Learning objectives (what students should learn)

By the end of the lab students will be able to:


  1. Containerize a Python Flask app using a Dockerfile.

  2. Write a docker-compose.yml to run multiple containers together.

  3. Understand and test how containers communicate by service name.

  4. Debug common container-networking and dependency issues.

  5. Extend services (add DB, logging, scaling) as next steps.


3. Prerequisites & checklist (what must be installed)


Docker Desktop for Windows (or Docker Engine + docker-compose). If on Windows, enable WSL2 if prompted.


Basic familiarity with Python (creating a small Flask app).


A terminal (PowerShell or CMD).


Text editor (VS Code recommended).




4. Project structure (create this folder tree)


Create a folder C:\microservices-demo (or your preferred path) and inside create:

microservices-demo/
│
├── user-service/
│   ├── app.py
│   ├── requirements.txt
│   └── Dockerfile
│
├── order-service/
│   ├── app.py
│   ├── requirements.txt
│   └── Dockerfile
│
└── docker-compose.yml

You’ll paste the provided file contents below into these files.



5. File contents — copy these exactly

user-service/app.py

from flask import Flask, jsonify

app = Flask(__name__)

@app.route('/user')
def get_user():
    # Simple static data for the demo
    return jsonify({"id": 1, "name": "Sharon", "role": "Developer"})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5001)

user-service/requirements.txt

flask==2.2.5

(pinning a minor version reduces differences during installs; not mandatory)


user-service/Dockerfile

# Use an official Python runtime as a parent image
FROM python:3.10-slim

# Set working directory
WORKDIR /app

# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Copy the app code
COPY . .

# Expose the port
EXPOSE 5001

# Start the app
CMD ["python", "app.py"]

order-service/app.py

from flask import Flask, jsonify
import requests

app = Flask(__name__)

@app.route('/order')
def get_order():
    # Call the user-service using the Docker Compose service name
    try:
        user_response = requests.get('http://user-service:5001/user', timeout=5)
        user = user_response.json()
    except Exception as e:
        # If user-service is not reachable, return an error message so students can debug
        return jsonify({"error": "Could not reach user-service", "details": str(e)}), 503

    order = {"order_id": 101, "item": "Laptop", "price": 55000}
    return jsonify({"user": user, "order": order})

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5002)

order-service/requirements.txt

flask==2.2.5
requests==2.31.0

order-service/Dockerfile

FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 5002
CMD ["python", "app.py"]

docker-compose.yml (root of project)

version: '3.8'
services:
  user-service:
    build: ./user-service
    container_name: user-service
    ports:
      - "5001:5001"
    restart: unless-stopped

  order-service:
    build: ./order-service
    container_name: order-service
    ports:
      - "5002:5002"
    depends_on:
      - user-service
    restart: unless-stopped


Notes:

  • depends_on ensures Docker Compose starts user-service first, but it does not wait for the service to be "ready" — only for the container to start. We'll mention how to handle readiness later.

  • container_name is optional — included here to make container names predictable in the lab.


6. Step-by-step commands (build, run, test)


Open a terminal at the project root (C:\microservices-demo).


  1. Build containers

    docker-compose build


    Output: Docker will download base images and build both images. Look for Successfully built messages.

    ree
  2. Start services (foreground logging)

    docker-compose up

    • This will stream logs from both containers. You should see Flask starting messages for both services.

    • Press Ctrl+C to stop (or open a new terminal and run docker-compose down).


  3. Start services (detached)

    docker-compose up -d

    • Runs containers in background. Then check status:

    docker-compose ps

    • You should see both user-service and order-service with STATE Up.


  4. Test endpoints (use browser, curl, or Postman)

  5. View logs

    docker-compose logs --tail=100 docker-compose logs order-service


  6. Stop & remove containers

    docker-compose down

    This removes containers and network but keeps images locally.


7. Verify what happened

  • When order-service calls http://user-service:5001/user it uses the Compose service name (user-service) as a DNS name. Docker Compose creates an isolated network where service names resolve to container IPs.


  • This avoids hardcoding IP addresses and mirrors how service discovery works in production (DNS, Envoy, Kubernetes services, etc.).



 
 
 

© 2023 by newittrendzzz.com 

  • Facebook
  • Twitter
  • Instagram
bottom of page