Deploying a Flask Application on Kubernetes using Docker Desktop
- Sharon Rajendra Manmothe

- 22 hours ago
- 3 min read
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:
Docker Desktop (latest version)
Kubernetes enabled within Docker Desktop
Kubectl command-line tool (installed automatically with Docker Desktop)
Basic understanding of Docker and YAML configuration files
3. Enabling Kubernetes in Docker Desktop
Open Docker Desktop on your system.
Navigate to Settings → Kubernetes.
Select Enable Kubernetes.
Click Apply and Restart.
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
Create a new project directory, for example, flask-k8s-app.
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
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
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
Apply the deployment configuration:
kubectl apply -f deployment.yaml
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
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
Apply the service configuration:
kubectl apply -f service.yaml
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.

$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.



Comments