Sign inSign up

sfiorini/python-k8s-sample

By sfiorini

Updated over 3 years ago

Sample template for deploying a Python application to Kubernetes

Image
0

888

sfiorini/python-k8s-sample repository overview

Python K8s Sample

Description

This repository contains a sample template for deploying a Python application to Kubernetes.

Usage

Installing

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.


Dependencies

You will need to make sure that the following dependencies are installed on your system:


Setup Development Environment
  • 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


Run in a Docker container
  • 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.


Run in Kubernetes
  • 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.


Folder structure

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

Makefile commands

See below a complete list of make commands for installation and maintenance of the application:

CommandSectionDescription
dependencies-installDevelopmentInstall required dependencies
runDevelopmentRun application service
bump-version-patchDevelopmentBumps version's patch
bump-version-minorDevelopmentBumps version's minor
bump-version-majorDevelopmentBumps version's major
build-imageDockerBuild and tag Docker image
publish-imageDockerPublish Docker image to registry
create-run-containerDockerCreate and run Container from Docker image
startDockerStart Docker Container
stopDockerStop Docker Container
remove-containerDockerRemove Docker Container
remove-imageDockerRemove Docker Image
docker-installDockerCreate Docker Image, Container and Start Container
docker-uninstallDockerStop Container, Remove Container and Remove Image
install-ingress-controllerKubernetesInstall NGINX Ingress Controller
deploy-k8s-deploymentKubernetesDeploy application's pod
deploy-k8s-serviceKubernetesDeploy application's service
deploy-k8s-ingressKubernetesDeploy application's ingress
delete-k8s-deploymentKubernetesRemove application's pod
delete-k8s-serviceKubernetesRemove application's service
delete-k8s-ingressKubernetesRemove application's ingress
k8s-installKubernetesFull install of application on cluster
k8s-uninstallKubernetesFull uninstall of application from cluster

References

Tag summary

Content type

Image

Digest

sha256:a5e5d7a71

Size

329 MB

Last updated

over 3 years ago

docker pull sfiorini/python-k8s-sample