Develop and Deploy a Python API with Kubernetes and Docker

January 30, 2020

Table of Contents

  1. Introduction
  2. Setting Up a Development Environment
  3. Using Docker to Create a Container for our App
  4. Using Docker Compose for Development
  5. Conclusion

1. Introduction

Docker is one of the most popular containerization technologies. It is a simple to use, developer-friendly tool, and has advantages over other similar technologies that make using it smooth and easy. Since its first open-source release in March 2013, Docker has gained attention from developers and ops engineers. According to Docker Inc., Docker users have downloaded over 105 billion containers and dockerized 5.8 million containers on Docker Hub. The project has over 32K stars on Github.

Docker has since become mainstream. More than 100K 3rd-party projects are using this technology, and developers with containerization skills are in increasing demand.

This article is the first of a two-part series. In this blog post, you will discover how to use Docker to containerize an application, then how to run it on development environments using Docker Compose. We are going to use a Python API as our main app. The second part will focus more on orchestration, more specifically on how to deploy the same app to Kubernetes.

Check out the MetricFire free trial to see how we can help monitor your Docker, Kubernetes and Python setups.

2. Setting Up a Development Environment

We are going to install some requirements before starting. We will use a mini Python API here developed in Flask. Flask is a Python framework and is an excellent choice to rapidly prototype an API. Our application will be developed using Flask. If you are not accustomed to Python, you can see the steps to create this API below.

Start by creating a Python virtual environment to keep our dependencies isolated from the rest of the system dependencies. Before this, we will need PIP, a popular Python package manager.

The installation is quite easy - you need to execute the following two commands:

<p>CODE:https://gist.github.com/denshirenji/aa6fc331526d7f534f23a4d66e1d96bb.js</p>

For your information, you should have Python 3 installed. You can verify this by typing:

​python --version​

After installing PIP, use the following command to install the virtual environment:

​pip install virtualenv​

You can follow the official guide to find other installation alternatives. Next, create a project for your folder in which you should create a virtual environment, then activate it. Also, create a folder for the app and a file called app.py.

<p>CODE:https://gist.github.com/denshirenji/8232dd429afb0acd8439ad101dabefd8.js</p>

We are going to build a simple API that shows the weather for a given city. For example, we want to show the weather in London; we should request it using the route:​

/london/uk

You need to install the Python dependencies called "flask" and "requests" using PIP. We are going to use them later on:

pip install flask requests​

Don't forget to "freeze" your dependencies in a file called requirements.txt. This file will be used later to install our app dependencies in the container:

pip freeze > requirements.txt​

This is what the requirements file looks like:

​<p>CODE:https://gist.github.com/denshirenji/13f510a10d1379edd6914eab7d4e2c84.js</p>

This is the API initial code:

<p>CODE:https://gist.github.com/denshirenji/f7e144b2356fd430cd288749013a9bfc.js</p>

In order to test it, you need to run python app.py and visit http://127.0.0.1:5000/. You should see "App Works!" on the web page. We are going to use data from openweathermap.org, so make sure to create an account on the same website and generate an API key.

Now, we need to add the convenient code to make the API show weather data about a given city:

<p>CODE:https://gist.github.com/denshirenji/70139199c17e50501f8402f433453cb0.js</p>

The overall code looks like:

<p>CODE:https://gist.github.com/denshirenji/23944baff5297260e863685ec7752524.js</p>

Now if you visit 127.0.0.1:5000/london/uk, you should be able to see a JSON similar to the following one:

<p>CODE:https://gist.github.com/denshirenji/7cd73f4bf15d9240b3a50a0e2ef8dc4a.js</p>

Our mini API is working. Let's containerize it using Docker.

3. Using Docker to Create a Container for our App

Let's create a container for our API; the first step is creating a Dockerfile. Dockerfile is an instructive text file containing the different steps and instructions that the Docker Daemon should follow to build an image. After building the image, we will be able to run the container.

A Dockerfile always starts with the FROM instructions:

<p>CODE:https://gist.github.com/denshirenji/7408c986d10733e22b692a667271294f.js</p>

In the above file, we did the following things:

  1. We are using a base image called "python:3".
  2. We also set PYTHONUNBUFFERED to 1. Setting PYTHONUNBUFFERED to 1 allows log messages to be dumped to the stream instead of being buffered.
  3. We also created the folder /app, and we set it as a workdir.
  4. We copied the requirements, then used it to install all the dependencies.
  5. We copied all of the files composing our application, namely the app.py file to the workdir.
  6. We finally exposed the port 5000, since our app will use this port, and we launched the python command with our app.py as an argument. This will start the API when the container starts.

After creating the Dockerfile, we need to build it using an image name and a tag of our choice. In our case, we will use "weather" as a name and "v1" as a tag:

docker build -t weather:v1 .

Make sure that you are building from inside the folder containing the Dockerfile and the app.py file.

After building the container, you can run it using:

docker run -dit --rm -p 5000:5000 --name weather weather:v1

The container will run in the background since we use the -d option. The container is called "weather" (--name weather). It's also reachable on port 5000 since we mapped the host port 5000 to the exposed container port 5000.

If we want to confirm the creation of the container, we can use:

docker ps

You should be able to see a very similar output to the following one:

<p>CODE:https://gist.github.com/denshirenji/973b6e13ac7bb1a4240cef1b61361d22.js</p>

You should be able to query the API now. Let's test it using CURL.

curl http://0.0.0.0:5000/london/uk/

If the last command should return a JSON:

<p>CODE:https://gist.github.com/denshirenji/fed95260c1a2eece94d0dc091c869df1.js</p>

4. Using Docker Compose for Development

Docker Compose is an open-source tool developed by Docker Inc. for defining and running multi-container Docker applications. Docker Compose is also a tool that is meant to be used in development environments, since it allows auto-reloading your container when you update your code, without making you restart your containers manually or rebuilding your image after each change. Without Compose developing using only Docker containers would be frustrating.

For the implementation part, we are going to use a "docker-compose.yaml" file.

This is the "docker-compose.yaml" file we are using with our API:

<p>CODE:https://gist.github.com/denshirenji/76eebd8cdf444e1885a7f8a9c3c0b475.js</p>

You can see in the above file that I configured the service "weather" to use the image "weather:v1". Map the host port 5000 to the container port 5000 and mount the current folder to the "/app" folder inside the container.

It is also possible to use a Dockerfile instead of an image. This would be recommended in our case since we already have the Dockerfile.

<p>CODE:https://gist.github.com/denshirenji/6f2c6109d07989ece7d3266fe177de79.js</p>

Now simply run "docker-compose up" to start running the service or "docker-compose up --build" to build it then run it.

5. Conclusion

In this post, we have seen how to create a Docker container for a mini Python API, and we used Docker Compose to create a development environment. If you are using another programming language, like Go or Rails, you will generally follow the same steps except for some minor differences. Check out the MetricFire free trial to see if MetricFire can fit into your monitoring environment.

In the second part of this tutorial we are going to discover more details about Docker and Docker Compose, but we are mainly going to see how to use Kubernetes and deploy our API to GKE (Google Kubernetes Engine).

Related Posts

GET FREE MONITORING FOR 14 DAYS