Running a Python Application on Kubernetes

By Joannah Nanjekye

Kubernetes is an open source platform that features deployment, maintenance and scaling mechanisms that help us simplify the management of containerized Python applications while giving us portability, extensibility and self-healing capabilities for our applications.

Whether you want to run a simple Python application or a complex one, Kubernetes can quickly and efficiently help you deploy and scale your applications, seamlessly roll out new features while limiting resources to only required resources. In this article, I will cover a holistic process of deploying a simple Python application to Kubernetes. The topics I will cover include:

Creating Python container images

Publishing the container images to an image registry

Working with Persistent Volume

Deploying the Python application to Kubernetes

Requirements

To seamlessly follow through, you will need the following:

Docker

Docker is an open platform to build and ship distributed applications. To install Docker on Debian/Ubuntu:

One of the requirements to deploy to Kubernetes is a containerized application. I will quickly start with a discussion on containerizing Python applications.

Containerization at a Glance

Containerization involves enclosing an application in a container with its own operating system. Using this option instead of full machine virtualization gives us the advantage of being able to run an application on any machine without worrying about dependencies. Roman Gaponov has a good article that you can reference for more information.

We will start with creating a container image for our Python code.

Creating a Python Container Image

To create these images, we will use Docker, a popular software solution that allows us to deploy applications inside isolated Linux software containers.

Docker is able to automatically build images using instructions from a Docker file, such as the following for our Python application:

This Docker file contains instructions to run our sample Python code, using the Python 3.5 development environment. We can now build the Docker image from these instructions using this command:

docker build -t k8s_python_sample_code .

This command creates a Docker image for our Python application.

Publishing the Container Images

We can publish our Python container image to different private/public cloud repositories like Dockerhub, AWS ECR, Google Container Registry, etc. For purposes of this tutorial, we shall use Dockerhub.

Before publishing the image, we need to tag it to a version.

docker tag k8s_python_sample_code:latest k8s_python_sample_code:0.1

Once this is done, push the image to the cloud repository. Using a Docker registry other than Dockerhub to store images requires you to add that container registry to the local Docker daemon and Kubernetes Docker daemons. You can look up this information for the different cloud registries. We shall use Dockerhub in this example.

You can verify whether your application is running by inspecting the running services.

kubectl get services

May Kubernetes set you free from deployment hustles!!!

About the Author

Joannah Nanjekye is from Uganda, a software engineer, conference speaker and a proud FOSS (Free and Open Source Software) contributor who presented at PyCon ZA in South Africa in 2016 and 2017. She shares her knowledge on implementation for Python 2 and 3 support from experiences on her work on open source projects. She worked as a software developer for Laboremus Uganda and Fintech Uganda before pursuing a career as an Aeronautical Engineer with a bias in Avionics at Kenya Aeronautical College. She is a proud Rails Girls Summer of Code alumnae and was mentored into FOSS development during her time as a scholar.

Learn more about Python from the author’s recent book, Python 2 and 3 Compatibility. Get your copy today and discover clean ways to write code that will run on both Python 2 and 3, including detailed examples of how to convert existing Python 2-compatible code to code that will run reliably on both Python 2 and 3.