This project demonstrates how to deploy a simple Flask application that displays "Hello, World!" using Kubernetes. It covers the process of creating a Flask app, containerizing it with Docker, and deploying it to a Kubernetes cluster.
- Docker installed and configured
- Kubernetes cluster (local or cloud-based)
- kubectl command-line tool installed and configured
- Python 3.9 or later
kubernetes-flask-hello-world/
├── app.py
├── Dockerfile
├── requirements.txt
└── kubernetes-deployment.yaml
-
Clone the repository:
git clone <repository-url> cd kubernetes-flask-hello-world
-
Build the Docker image:
docker build -t flask-k8s-hello-world .This command builds a Docker image from your Dockerfile. The
-tflag tags the image with the nameflask-k8s-hello-world. The.at the end specifies that the Dockerfile is in the current directory. -
Tag the Docker image:
docker tag flask-k8s-hello-world your-docker-username/flask-k8s-hello-world
This step tags your image with your Docker Hub username, preparing it for pushing to Docker Hub. Replace
your-docker-usernamewith your actual Docker Hub username. -
Push the image to Docker Hub:
docker push your-docker-username/flask-k8s-hello-world
This command uploads your Docker image to Docker Hub, making it accessible for your Kubernetes cluster to pull. Ensure you're logged in to Docker Hub (
docker login) before running this command. -
Deploy to Kubernetes:
kubectl apply -f kubernetes-deployment.yaml
This command creates or updates the resources defined in your
kubernetes-deployment.yamlfile in your Kubernetes cluster.Note: If already deployed, you can delete the existing deployment and redeploy: Check the status of the pods:
kubectl get pods
This shows the current running pods in your cluster. Delete the deployment:
kubectl delete -f kubernetes-deployment.yaml
This removes the existing deployment and associated resources.
Redeploy the application:
kubectl apply -f kubernetes-deployment.yaml
This creates a fresh deployment with your updated configuration or image.
-
Access the application:
kubectl port-forward service/flask-hello-world 8080:80
This command forwards traffic from your local machine's port 8080 to port 80 of the
flask-hello-worldservice in your Kubernetes cluster.Open a web browser and navigate to
http://localhost:8080You should now see your Flask application's "Hello, World!" message in your web browser.
The app.py file contains a simple Flask application that returns an HTML header with "Hello, World!".
The Dockerfile defines how to build the Docker image for our Flask application.
The kubernetes-deployment.yaml file defines both the Deployment and Service for our application.
-
Ensure your Kubernetes cluster is running and kubectl is configured correctly.
-
Apply the Kubernetes configuration:
kubectl apply -f kubernetes-deployment.yaml
-
Check the status of your pods:
kubectl get pods
-
Check the status of your service:
kubectl get services flask-hello-world
Use port forwarding to access the application:
kubectl port-forward service/flask-hello-world 8080:80Then open a web browser and go to http://localhost:8080
To remove the deployment and service:
kubectl delete -f kubernetes-deployment.yamlIf you encounter issues:
-
Check pod status:
kubectl get pods
-
View pod logs:
kubectl logs <pod-name>
-
Describe the service:
kubectl describe service flask-hello-world