Contents


Create a scalable and fault-tolerant REST endpoint using Flask and Python

Write compelling, state-of-the-art web apps and REST services

Comments

This tutorial was written using a previous version of the IBM Cloud interface. Given the rapid evolution of technology, some illustrations may have changed.

Learn how to deploy a simple Flask application with an AngularJS user interface to IBM Cloud using the Cloud Foundry command-line tool. For this tutorial, we chose Flask over other frameworks like Django, Pyramid, and web2py because it is very lightweight and therefore easy to understand. For just writing up a REST endpoint it is a perfect fit. In addition, we also show you how a single REST endpoint can be used to multiplex between different functions.

To do this, you need to create a scalable and fault-tolerant REST endpoint using Flask and Python, and then test it using an AngularJS application. To help you get up and running with Python on IBM Cloud as quickly and easily as possible, we introduce the Flask web framework and show you how to use it in the open source Cloud Foundry command-line tool. Flask can open an HTTP listener and serve HTTP requests, so you don't have to write the entire web application yourself. With Flask, you can create a REST service in a couple of very simple steps, as you will see shortly. We will also add an AngularJS front end (single-page application) to it, to make the boilerplate complete. And everything will be done using the Cloud Foundry command-line tool.

In addition, we illustrate how a single REST endpoint can be used to multiplex between different functions. You will also see how third-party libraries required for your application can be automatically installed via the PIP package management system.

What you'll need to build your application

Run the appGet the code

Step 1. Clone and review the project

We have provided the complete application in a GIT repository, so all you need to do is to clone it to your local machine. Open a GIT command line (in Windows, right-click in your preferred directory and select "git bash here") and run the following command:

git clone https://github.com/mvonhe/dwflaskangular

This creates a folder named "dwflaskangular" in the current directory. In the folder contents, you will see the following files:

contents of folder dwflaskangular
  1. In the templates folder, you will see the index.html file — the user interface. Open it in your editor of choice (such as Notepad). Take a look at the code and determine where the REST call is made and the response is displayed. (Hint: search for the string $http.get.)
  2. The requirements.txt file tells the Python buildpack for Cloud Foundry which resources to install using the PIP Python Package Manager (in this case, Flask).
  3. The Procfile contains the command that starts the app. If you run your app locally, you can invoke the Python script via python upper.py. The same command will be executed automatically when your app is being deployed to IBM Cloud.
  4. Next, take a look at upper.py. A stand-alone Python interpreter is not sufficient for serving a REST endpoint or HTML content, so instead of implementing an HTTP stack on your own, you'll use a web framework (in this case, Flask). An instance of this framework is created with the command app = Flask(__name__).
    """Cloud Foundry test"""
    from flask import Flask, render_template, request
    import os
    
    app = Flask(__name__)
    
    if os.getenv("VCAP_APP_PORT"):
    	port = int(os.getenv("VCAP_APP_PORT"))
    else:
    	port = 8080
    
    @app.route('/', methods=['GET'])
    def display_template():
    	if "lower" in request.args:
    		return request.args["lower"].lower()	
    	elif "upper" in request.args:
    		return request.args["upper"].upper()
    	else: 			
    		return render_template('index.html')	
    
    if __name__ == '__main__':
    	app.run(host='0.0.0.0', port=port, debug=True)
  5. Cloud Foundry tells the application on which TCP port it should open the listener serving the HTTP request by passing an environment variable called "VCAP_APP_PORT" to the container. In Python, you can read and assign this port to a variable with the following command:

    port = int(os.getenv("VCAP_APP_PORT"))
  6. Now you can use this app reference to annotate the method display_template() in your script to expose it as a REST endpoint using the @app.route annotation. It does this if you provide either "lower" or "upper" as the GET request parameter; otherwise (specified by the "else" statement), the same endpoint will render and return the index.html file — specifically, the one containing the AngularJS application. This is an example of how you can multiplex between different functions on the same endpoint.

Step 2. Deploy the app to IBM Cloud

Now you are ready to push your application to IBM Cloud using the Cloud Foundry command-line tool: Open a command-line window and change directories (cd) to the "dwflaskangular" folder. To deploy your app to IBM Cloud, run the following command:

cf push <your app name> -m 128m

The app name (in this case, "rest_angularrkie") is used to create the subdomain of your app's URL. Therefore the app name must be unique. If you want to make sure that your URL has not already been taken, you can just open "<yourPlannedAppName>.mybluemix.net” in a browser. If you see the following message, the URL is still available:
"404 Not Found: Requested route ('<yourPlannedAppName>.mybluemix.net') does not exist."

The "-m" option specifies the amount of memory that the Python script is allowed to use — in this case, 128MB, which is more than sufficient for a simple Python REST application. If successful, the tail of the output in your command line should look similar to this:

Tail of the output
Tail of the output

Step 3. Test your app

  1. In a web browser, go to the URL of your application; you can find it at the bottom of the output. (In this instance, it would be "rest-angularrkie.mybluemix.net." Note that the underlines "_" are converted into dashes "-".)
  2. Enter some text and click Submit.
  3. You should see your input upper-cased and lower-cased. Your input is read by AngularJS and sent to the REST endpoint twice using two different parameters that make the REST endpoint behave differently.

Step 4 (optional). Make your application fault tolerant and dynamically scalable

When working with microservices and REST endpoints, you need to dynamically assign resources to your application based on the load generated. Cloud Foundry supports the addition and removal of parallel application containers in real time. IBM Cloud offers a free service called Auto-Scaling, which, when attached to an application, will monitor response time, throughput, CPU usage, and main memory consumption for your application. Auto-Scaling will also dynamically add or remove additional containers based on scale policies. In this example, you'll see how this can be done in a few simple steps. In order to make your application fault tolerant, at least two instances of your app must be running. (We will cover how to implement cross-data-center fault tolerance in a future tutorial.)

  1. Log in to IBM Cloud, find your Python application using the dashboard, and select it. Then click ADD A SERVICE OR API. Python app, rest_angularkkie
    Python app, rest_angularkkie
    Add a service or API
    Add a service or API

    Search for the Auto-Scaling service and click on it. Auto-Scaling Service
    Auto-Scaling Service
  2. Keep the defaults and click on CREATE. Add a service
    Add a service
  3. Click RESTAGE and wait until the application has restaged. Restage application
    Restage application
  4. Click on the Auto-Scaling service icon. Auto-Scaling service icon
    Auto-Scaling service icon
  5. Click on CREATE AUTO-SCALING POLICY. Create auto-scaling policy
    Create auto-scaling policy
  6. Change the minimum number of application instances to "2." Edit auto-scaling policy
    Edit auto-scaling policy
  7. Click on SAVE.
  8. In your command-line window, type cf apps. You will see that there are now two instances running: Apps output
    Apps output

Congratulations! Your application is now fault tolerant because there are two instances running and the load is distributed among them using the Cloud Foundry router; if one instance fails, the other will take over. In addition, if main memory usage surpasses 80%, additional instances will be created and the load will be distributed evenly among all instances. And of course, when they are no longer necessary, the number of instances will be reduced.

Conclusion

This tutorial showed you how to implement and deploy a Flask application using the Cloud Foundry command-line tool, what files are needed, and which commands are used to do so. Since this application runs in a Cloud Foundry container, services like auto-scaling, monitoring, and fail-over can be plugged into the application. In the final step, you saw how easy this is for the auto-scaling service.

In future tutorials, we will cover the installation of libraries that need to be compiled using a C compiler, but this tutorial should enable you to start writing compelling, state-of-the-art web applications and REST services.


Downloadable resources


Related topics


Comments

Sign in or register to add and subscribe to comments.

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Cloud computing
ArticleID=1029866
ArticleTitle=Create a scalable and fault-tolerant REST endpoint using Flask and Python
publish-date=07052017