Part 5: Making Watson Microservice using Python, Docker, and Flask

Creating a Python Microservice using Flask

We already have a Python application we wrote back in part 3.  This application takes in a text string and submits the text to IBM Watson to return an analysis of the text. The next step in building our application would be to take this piece of code and turn it into a Microservice.

The first step of creating a microservice from our code is to expose the functionality through some sort of interface. We will use Flask to expose our functionality via a HTTP (1.1. we will work on a 2.0 architecture update in a future post). Additionally we will set our BlueMix credentials from environment variables where the Python is executed. We update our code below

As you can see, we set our BlueMix credentials from environment variables. Additionally we remove the main execution routine on this file. Now we create a new file called app.py. This file will serve up our Flask application and call into our tone analysis script. We will expose two endpoints. /status will send us the value RUNNING. We will use this to health check the micro service.  /analyze will call our analysis function. It will expect a GET with a URL parameter “text” or a POST with the same parameter name.  We set up these two routing methods, then use the __main__ routine pattern to start a Flask server on the default port of 5000.

Set your envionment variables with your Bluemix credentials and the URL endpoint for the tone analysis service and then go ahead and startup your application with:

You should now be able to see the service is running at http://localhost:5000/status  and then you can try an analysis using http://localhost:5000/analyze?text=im really happy guys. Congratulations, you have added the python scaffolding to make the tone analysis routine into a stand alone service with an HTTP interface.

Packaging your Python Microservice with Docker

The next step will be to package our microservice so it is easily scalable and deployed. If we have to stand up a server, install all the software, and setup all the initial daemons to start to make this service available for use… we’re going to have a bad time.

For this, we use Docker. Docker will create a redeployable image that we can deploy over and over again without having to do all the administrative things to get our microservice up and running. For this we must first create a Dockerfile. The contents of the Dockerfile pulls down an ubuntu base image, updates the packages, installs python and pip, installs the python packages, deploys the code and starts the application. Before we build the docker file we need to have python give us our package requirements. We do this with the following

This outputs the following file

Next we build the Dockerfile

Now we have docker build our image

Docker will go about building our image and creating it as the package named tone:latest

Finally we create a file env.list with our environment variables we want to set inside the container. These will be our bluemix creds and endpoints. The env.list file is in the format KEY=VALUE

Finally start up our container with the following command

You should now be able to see the service is running at http://localhost:5000/status  and then you can try an analysis using http://localhost:5000/analyze?text=im really happy guys.

Now scale it up a little. Run another container side by side this one on port 5001

You should now be able to see the service is running at http://localhost:5001/status  and then you can try an analysis using http://localhost:5001/analyze?text=im really happy guys.

Concluding Remarks

From this you can see how we are going to create a cluster of units able to run tone analysis simultaneously. With docker we are going to be able to create these ephemeral nodes as we need them and tear them down after we are done with them. This allows us to create a dynamic allocation of resources based on what aspect of our application is needed. Packaging our application with Docker is what enables this.

Also take note of how we made the secrets involved configurable. This will be important when we come to understanding secrets management products such as Hashicorp’s Vault specifically as it pertains to microservices.

Source Code

The source code for this posting is available at https://github.com/allengeer/toneservice