Develop and Deploy a Python API with Kubernetes and Docker

Aymen El Amri
The MetricFire Blog
3 min readFeb 27, 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

Part II of this article can be read here.

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. Also, if you’re ready to move on to part 2 of Developing and Deploying a Python API in Docker and Kubernetes, check out the second article here.

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:

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.

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‍

To finish reading this article, check out the full post on the MetricFire website. You can also check out part 2 of this article on the MetricFire blog.

--

--