Sample template for deploying a Python application to Kubernetes
888
This repository contains a sample template for deploying a Python application to Kubernetes.
For the purpose of running the sample, we are basing this document on deploying on a local Kubernetes installation that is provided by Docker Desktop.
The project provides a makefile with all the commands needed to install and deploy the application.
You will need to make sure that the following dependencies are installed on your system:
Clone this repository
Copy .env.sample to .env and set the evironment variables as indicated in the help within the file.
Install dependencies:
make dependencies-install
or
pip3 install -r requirements.txt
Run application
make run
or
python3 src/api.py
Open browser at http://localhost:3000 and you should get the following response.
{
"message": "API Server is running successfully"
}
For the OpenAPI documentation, open browser at http://localhost:3000/docs
Create Docker image (Note: the following command adds 2 tags, as latest and the current version of the app):
docker image build . -t sfiorini/python-k8s-sample:v1.0.0 -t sfiorini/python-k8s-sample:latest
or
make build-image
Create and run Docker container:
docker run -d --name python-k8s-sample -p 3000:3000 sfiorini/python-k8s-sample:latest
or
make create-run-container
Open browser at http://localhost:3000 and you should get the following response.
{
"message": "API Server is running successfully"
}
For the OpenAPI documentation, open browser at http://localhost:3000/docs
Fore all other Docker commands, please see the Makefile reference below.
Create Docker image: See above section.
Publish Docker image to registry.
For this tutorial we assume publishing on Docker Hub, however the same script will work with all other compatible registries.
Registry account setup is beyond the scope of this documentation.
docker push sfiorini/python-k8s-sample --all-tags
or
make publish-image
Install NGINX Ingress Controller on Kubernetes cluster for Docker Desktop.
Note: This is a one time operation as part of setup for the cluster. For commmercial installation, more than likely the Ingress Controller is already installed by your administrator.
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.5.1/deploy/static/provider/cloud/deploy.yaml
or
make install-ingress-controller
Deploy application's pod.
Open ./kube/python-k8s-sample.yaml and replace ${PORT} with the port declared on your .env file (Default: 3000).
kubectl apply -f ./kube/python-k8s-sample.yaml
or
make deploy-k8s-deployment
Deploy application's service.
Open ./kube/python-k8s-sample-service.yaml and replace ${PORT} with the port declared on your .env file (Default: 3000).
kubectl apply -f ./kube/python-k8s-sample-service.yaml
or
make deploy-k8s-service
Deploy application's ingress.
Open ./kube/python-k8s-sample-ingress.yaml and replace ${PORT} with the port declared on your .env file (Default: 3000).
kubectl apply -f ./kube/python-k8s-sample-ingress.yaml
or
make deploy-k8s-ingress
Open browser at http://your-k8s-domain:3000 and you should get the following response.
NOTE: Docker Desktop adds domain kubernetes.docker.internal to the hosts file. Therefore your url will be http://kubernetes.docker.internal:3000.
{
"message": "API Server is running successfully"
}
Fore all other kubectl commands, please see the Makefile reference below.
Here's the project's folder structure:
/python-k8s-sample # Root directory.
|- .vscode/ # Folder containing config files to develop the app using VSCode.
|- kube/ # Kubernetes deployment yaml files.
|- .gitignore # Ignore patterns for git.
|- src # The location of source files for the Python application
|- Dockerfile # Metadata content (title, author...).
|- Makefile # Makefile used for building our documents.
|- README.md # Documentation (this file)
|- requirements.txt # Application's dependencies
See below a complete list of make commands for installation and maintenance of the application:
| Command | Section | Description |
|---|---|---|
| dependencies-install | Development | Install required dependencies |
| run | Development | Run application service |
| bump-version-patch | Development | Bumps version's patch |
| bump-version-minor | Development | Bumps version's minor |
| bump-version-major | Development | Bumps version's major |
| build-image | Docker | Build and tag Docker image |
| publish-image | Docker | Publish Docker image to registry |
| create-run-container | Docker | Create and run Container from Docker image |
| start | Docker | Start Docker Container |
| stop | Docker | Stop Docker Container |
| remove-container | Docker | Remove Docker Container |
| remove-image | Docker | Remove Docker Image |
| docker-install | Docker | Create Docker Image, Container and Start Container |
| docker-uninstall | Docker | Stop Container, Remove Container and Remove Image |
| install-ingress-controller | Kubernetes | Install NGINX Ingress Controller |
| deploy-k8s-deployment | Kubernetes | Deploy application's pod |
| deploy-k8s-service | Kubernetes | Deploy application's service |
| deploy-k8s-ingress | Kubernetes | Deploy application's ingress |
| delete-k8s-deployment | Kubernetes | Remove application's pod |
| delete-k8s-service | Kubernetes | Remove application's service |
| delete-k8s-ingress | Kubernetes | Remove application's ingress |
| k8s-install | Kubernetes | Full install of application on cluster |
| k8s-uninstall | Kubernetes | Full uninstall of application from cluster |
Content type
Image
Digest
sha256:a5e5d7a71…
Size
329 MB
Last updated
over 3 years ago
docker pull sfiorini/python-k8s-sample