Develop and deploy Python APIs using Kubernetes and Docker

image.png

Introduction

Docker is one of the most popular containerization technologies. It's an easy-to-use, easy-to-use tool for developers, and it's also smooth and easy to use, making it more popular than other similar technologies. Since its first open source release in March 2013, Docker has attracted the attention of developers and operations engineers, with Docker users downloading over 105 billion containers and 5.8 million containers on Docker Hub, according to Docker Inc. Docked. The project's Github has over 32,000 stars.

Today, Docker has become so mainstream. Over 100,000 third-party projects with this technology- It is also true that there is a growing demand for developers with containerization skills who are using.

This article first describes how to use Docker to containerize your application, and then how to use Docker Compose to run it in your development environment. Use the Python API as the main app.

In MetricFire, Docker, Kubernetes , Can help you monitor your Python setup. To see how it works, please book a demo [https://www.metricfire.com/demo/?utm_source=blog&utm_medium=Qiita&utm_campaign=Japan&utm_content=Develop%20and%20Deploy%20a%20Python] % 20API% 20with% 20Kubernetes% 20and% 20Docker) Please.

Development environment setup

Before we get started, we'll install some requirements. We will use the Mini Python API developed in Flask. Flask is a Python framework and a great choice for quickly prototyping APIs, and our applications are developed using Flask. If you are new to Python, here are the steps to create this API.

First, create a Python virtual environment to separate the dependencies from the rest of the system dependencies. Before that, you need PIP, a popular Python package manager.

Installation is very easy. Try running the following two commands.

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py

For reference, Python 3 must be installed. To check this, type:

​python --version​

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

​pip install virtualenv​

You can find other installation methods by following the official guide. Next, create and activate a project in the folder where you want to create the virtual environment. It also creates an app folder and a file called app.py.

mkdir app
cd app
python3 -m venv venv
. venv/bin/activate
mkdir code
cd code
touch app.py

Create a simple API to display the weather for a particular city. For example, suppose you want to view the weather in London. Must be requested using the route:

/london/uk

You need to use PIP to install Python dependencies called "flask" and "requests". We will use them later:

pip install flask requests​

Don't forget to "Freeze" the dependencies in a file called requirements.txt. This file will later be used to install the app's dependencies into the container.

pip freeze > requirements.txt​

The requirements file looks like this:

certifi==2019.9.11
chardet==3.0.4
Click==7.0
Flask==1.1.1
idna==2.8
itsdangerous==1.1.0
Jinja2==2.10.3
MarkupSafe==1.1.1
requests==2.22.0
urllib3==1.25.7
Werkzeug==0.16.0

The initial code of the API is

from flask import Flask
app = Flask(__name__)

@app.route('/')
def index():
    return 'App Works!'

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000)

To test, you need to run python app.py and go to http: //127.0.0.1:5000/. "AppWorks!" Is displayed. On a web page. Be sure to create an account on the same website and generate an API key as we will be using data from openweathermap.org.

Next, we need to add some useful code to the API to display the weather data for a particular city.

@app.route('/<string:city>/<string:country>/')
def weather_by_city(country, city):
    url = 'https://samples.openweathermap.org/data/2.5/weather'
    params = dict(
        q=city + "," + country,
        appid= API_KEY,
    )
    response = requests.get(url=url, params=params)
    data = response.json()
    return data

And the whole code looks like this:

from flask import Flask
import requests

app = Flask(__name__)

API_KEY = "b6907d289e10d714a6e88b30761fae22"

@app.route('/')
def index():
    return 'App Works!'

@app.route('/<string:city>/<string:country>/')
def weather_by_city(country, city):

    url = 'https://samples.openweathermap.org/data/2.5/weather'
    params = dict(
        q=city + "," + country,
        appid= API_KEY,
    )

    response = requests.get(url=url, params=params)
    data = response.json()
    return data

if __name__ == '__main__':
    app.run(host="0.0.0.0", port=5000)

127.0.0.1:5000/london/ukにアクセスすると、次のようなJSONが表示されるはずです。

{
  "base": "stations",
  "clouds": {
    "all": 90
  },
  "cod": 200,
  "coord": {
    "lat": 51.51,
    "lon": -0.13
  },
...

The Mini API is working. Let's containerize using Docker.

Create an app container using Docker

Let's create a container for the API. The first step is to create a Dockerfile. A Dockerfile is a descriptive text file that contains various steps and instructions that the Docker daemon must follow to build an image. After building the image, you can run the container.

Dockerfile always starts with a FROM instruction.

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /app
WORKDIR /app
COPY requirements.txt /app
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . /app
EXPOSE 5000
CMD [ "python", "app.py" ]

In the above file, I did the following:

  1. Use the base image "python: 3"
  2. Set PYTHONUNBUFFERED to 1. Setting PYTHONUNBUFFERED to 1 allows log messages to be dumped to a stream without buffering.
  3. Create a folder / app and set it as workdir.
  4. Copy the requirements and use it to install all the dependencies.
  5. Copy all the files that make up your application, the app.py file, to workdir.
  6. Since the app uses this port, finally expose port 5000 and launch the python command with app.py as an argument. This will start the API when the container starts.

After creating the Dockerfile, you need to build the Dockerfile with the image name and the tags you choose. In this example, we will use "weather" as the name and "v1" as the tag.

docker build -t weather:v1 .

Make sure you are building from within the folder that contains the Dockerfile and app.py files.

After building the container, you can run it using:

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

The container runs in the background because it uses the -d option. The container is called "weather" (-name weather). We also mapped host port 5000 to exposed container port 5000, so port 5000 is also reachable.

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

docker ps

You should see an output that is very similar to the following output.

CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS                    NAMES
0e659e41d475        weather:v1          "python app.py"     About a minute ago   Up About a minute   0.0.0.0:5000->5000/tcp   weather

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

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

If the last command needs to return JSON:

{
  "base": "stations",
  "clouds": {
    "all": 90
  },
  "cod": 200,
  "coord": {
    "lat": 51.51,
    "lon": -0.13
...
}

Using Docker Compose for development

Docker Compose is a Docker Inc for defining and running multi-container Docker applications. An open source tool developed by. Docker Compose is also a tool intended for use in development environments. This allows the container to be automatically reloaded when the code is updated, without having to manually restart the container or rebuild the image with every change. Without Compose, it would be frustrating to develop using only Docker containers.

The implementation uses the "docker-compose.yaml" file.

This is the "docker-compose.yaml" file used by the API.

version: '3.6'
services:
  weather:
    image: weather:v1
    ports:
      - "5000:5000"
    volumes:
      - .:/app

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

You can also use a Dockerfile instead of an image. I already have a Dockerfile, so I recommend this in this case.

version: '3.6'
services:
  weather:
    build: .
    ports:
      - "5000:5000"
    volumes:
      - .:/app

Then run "docker-compose up" to start running the service, or run "docker-compose up --build" to build and then run.

Summary

In this post, I learned how to create a Docker container for the Mini Python API and used Docker Compose to create a development environment. If you're using another programming language like Go or Rails, you usually follow the same steps, with a few minor differences. MetricFire Free demo Reservation and monitored by MetricFire Please contact us if it suits your environment.

Recommended Posts

Develop and deploy Python APIs using Kubernetes and Docker
Python development flow using Poetry, Git and Docker
Develop, run, and deploy AWS Lambda remotely using lambda-uploader
Try using Kubernetes Client -Python-
Authentication using tweepy-User authentication and application authentication (Python)
Clustering and visualization using Python and CytoScape
Build and try an OpenCV & Python environment in minutes using Docker
From Python to using MeCab (and CaboCha)
Using venv in Windows + Docker environment [Python]
Behind the flyer: Using Docker with Python
Using Python and MeCab with Azure Databricks
[FX] Hit oanda-API in Python using Docker
Develop slack bot in python using chat.postMessage
I'm using tox and Python 3.3 with Travis-CI
I tried updating Google Calendar with CSV appointments using Python and Google APIs
Three things I was addicted to when using Python and MySQL with Docker
Head orientation estimation using Python and OpenCV + dlib
I tried web scraping using python and selenium
Notes on installing Python3 and using pip on Windows7
I tried object detection using Python and OpenCV
Create a web map using Python and GDAL
[Python3] Automatic sentence generation using janome and markovify
Try using tensorflow ① Build python environment and introduce tensorflow
Create a Mac app using py2app and Python3! !!
Build PyPy and Python execution environment with Docker
Try using ChatWork API and Qiita API in Python
Start using Python
python at docker
Scraping using Python
Initial settings for using Python3.8 and pip on CentOS8
Searching for pixiv tags and saving illustrations using Python
Extendable skeletons for Vim using Python, Click and Jinja2
Try creating a compressed file using Python and zlib
Aggregate Git logs using Git Python and analyze associations using Orange
Building a Docker working environment for R and Python
(Python) Try to develop a web application using Django
Send and receive Gmail via the Gmail API using Python
Implementing a generator using Python> link> yield and next ()> yield
Install and develop Git, VSCode, Docker on Chrome OS
Get and automate ASP Datepicker control using Python and Selenium
Read and write NFC tags in python using PaSoRi
Speech transcription procedure using Python and Google Cloud Speech API
Get files from Linux using paramiko and scp [Python]
HTTP server and HTTP client using Socket (+ web browser) --Python3