This project demonstrates the use of Docker and Kubernetes to deploy services for two different languages: Python and Go. The project includes:
- A simple Hello World Flask app running inside a Docker container.
- A Ping-Pong service where:
- Python and Go both respond to the
/pingendpoint with a "pong" message.
- Python and Go both respond to the
- A combined Docker setup running both Python and Go services on different ports.
The docker setup was already done in my pc so I proceeded to the next task.
The Hello World Flask app demonstrates a simple web application running inside a Docker container. The app listens for HTTP requests on port 5000 and returns a message "Hello, Docker!". This example showcases how to build a basic Python web app and containerize it using Docker, followed by deploying it to a Kubernetes cluster
In this I have implied two steps:-
We can directly write the below command in the terminal
% docker run hello-world
In the hello-world-docker folder we have all the folders to build a docker image and deploying it on the kubernetes
1.) The first file which is app.py. It contains the Flask code for the server on port 5000 which will show Hello, Docker! on the screen.
2.) The second file which is the Dockerfile. It is designed to containerize a Python application build with Flask.
- Base Image
- The Dockerfile uses the
python:3.9-slimimage as the base
- The Dockerfile uses the
- Working Directory
- The working directory inside the container is set to
/app.
- The working directory inside the container is set to
- Copy Application Files
- The
app.pyis copied into the container
- The
- Install Dependencies
- The
pip install flaskcommand install Flask
- The
- Expose port
- The port 5000 is exposed
- Run the application
- The application is started with the command
python app.py
- The application is started with the command
3.) The third file is deployment.yaml file. This file is used to define and deploy the application to a Kubernetes cluster.
- API Version
- The file uses
apps/v1, the stable API version for deploying workloads in Kubernetes.
- The file uses
- Kind
- The kind is
Deployment, which ensures that the desired number of application replicas are always running.
- The kind is
- Metadata
name: Specifies the name of the deployment (hello-world-deployment).
- Spec
- Replicas: Sets the number of application instances to 1.
- Selector: Matches Pods with the label
app: hello-world. - Template:
- Metadata: Defines labels applied to the Pods created by this deployment.
- Spec:
- Containers:
-name: The container's name ishello-world.
-image: The Docker image to use, specified ashello-world-docker.
-port: Exposes port 5000 on the container for external access.
- Containers:
4.) The fourth file is service.yaml file. This file is used to expose the application to external traffic in the Kubernetes cluster.
- API Version
- The file uses
apps/v1, the stable API version for deploying workloads in Kubernetes.
- The file uses
- Kind
- The kind is
Service, which provides network access to a set of Pods.
- The kind is
- Metadata
name: The name of the service ishello-world-service.
- Spec
- Type: The service type is LoadBalancer, which exposes the application to external traffic through a cloud provider's load balancer.
- Selector: The service routes traffic to Pods with the label
app: hello-world. - Ports
- protocol: TCP
- port: The service listens on port 5000.
- targetPort: Traffic received on port 5000 is forwarded to the application running on the same port in the Pod.
docker build -t hello-world-docker:latest .docker login
docker tag hello-world-docker:latest <your-dockerhub-username>/hello-world-docker:latest
docker push <your-dockerhub-username>/hello-world-docker:latestkubectl apply -f deployment.yaml
kubectl apply -f service.yaml- Now we should be able to access through (http://localhost:5000) on the browser.
- This will expose the Flask app on port
5000locally. You can access the application in your browser or usingcurl:curl http://localhost:5000
You should receive a response:
Hello, Docker!
The Ping-Pong service provides two separate services:
- A Python Flask service that responds with
"pong"to the/pingendpoint. - A Go service that responds with
"pong"to the/pingendpoint.These services are built and containerized separately, using individual Dockerfiles for Python and Go, and then deployed to Kubernetes. - A Python and Go service that responds with
"pong from Python"and"pong from Go"to the endpoint/pingon two different ports.
The Python setup is in the folder named ping-pong-project, the Go setup is in the folder named ping-pong-project-go and the combined setup is in the folder ping-pong.
The main structure of all the these folders are:-
serverfolderserver.pyormain.gofile is there in which the code for the server is there in python and go respectively.Dockerfileis there which is designed to containerize the application.
k8sfolderk8s-deployment.yamlfile is there which contains the code for deployment and service of the kubernetes.
Makefilewhich is to automate repetitive tasks and to simplify the process of building, managing, and deploying software projects.- Build the Go server (
make build-go). - Build the Docker image (
make build-docker-go). - Push the Docker image to the registry (
make push-docker-go). - Deploy the app to Kubernetes (
make deploy).
- Build the Go server (
make build-pythonormake build-gobased on which folder you are accessing. For the combined Python and Go it will bemake build-pythonthenmake build-go.make build-docker-pythonormake build-docker-gobased on your choice.make push-docker-pythonormake push-docker-gobased on your choice. For combinedmake push-pythonthenmake push-go.make deploy
Now based on the ports defined on running curl: curl http://localhost:port we will get the output as pong.