Deployment of Python Web Server to Cloud Foundry using MTA
This blog shows how you can deploy a hello world example to the Cloud Foundry stack using a Multi Target Application MTA deployment process.
I am also extending the example to show how to use the same framework to deploy run JupyterLab / Notebook on the CloudFoundry stack.
The idea is to use the Python code module/microservice as part of a larger deployment. Hence this example will show the deployment using a mta.yaml file.
- To start out with we will create a folder to house the necessary files called ‘py-service’
- The core of the functionality is a web server based on the Python Flask framework (https://flask.palletsprojects.com/).
- To model the service, create a file in the py-service directory called ‘first-service.py’ and poppulate it using the following code snippet:
from flask import Flask import os app = Flask(__name__) # Port number is required to fetch from env variable # http://docs.cloudfoundry.org/devguide/deploy-apps/environment-variable.html#PORT cf_port = os.getenv("PORT") # Only get method by default @app.route('/') def hello(): return 'Hello World' if __name__ == '__main__': if cf_port is None: app.run(host='0.0.0.0', port=5000, debug=True) else: app.run(host='0.0.0.0', port=int(cf_port), debug=True)
- The PORT variable is set by the cloud foundry environment and used to expose the application to the outside world
- The next step is to create a file called ‘Procfile’. This file identifies the starting point of the application. It contains a single line
web: python first-service.py. This line provides the command that is executed. the ‘web:’ specifies that it will provide a web server.
- To tell the Cloud Foundry Buildpack what Python version should be used, you need to create a file called ‘runtime.txt’. This file contains a simple line:
python-3.9. You can find the available realeases in here (https://github.com/cloudfoundry/python-buildpack/releases)
- The packages/dependenies can be specified in an environment.yml or requirements.txt file
- Some useful information can be found here: https://stackoverflow.com/questions/35245401/combining-conda-environment-yml-with-pip-requirements-txt
- In my case I consolidated all information in an environment.yml file
name: riz-inno-py-cf-env channels: - conda-forge dependencies: - pip ### to specfy additional pop based packages you can specify the following ###- pip: ### - docx ### - gooey ### - http://www.lfd.uci.edu/~gohlke/pythonlibs/bofhrmxk/opencv_python-3.1.0-cp35-none-win_amd64.whl - pytest #@ if you want to specify a version, you can use - Flask==1.0.2 - flask - numpy - matplotlib
- The last part is a mta.yaml file just like you would create it for other multi target applications.
- !!!The memory allocation for this example is tremedous (2gb for memory and at 3gb for disk) I have not done any digging where the consumption originates, but adding the conda package manager seems to take up a lot of overhead.
- You can find more details about available buildpacks in the SAP Help and on the Cloud Foundry page
- This is the mta.yaml code:
ID: riz.inno.py-cf _schema-version: '3.1' version: 1.0.0 modules: - name: riz-inno-py-cf type: python path: py-service/ parameters: memory: 2000M # Specifying theses quotas is important as they specify how much of the assigned entitlement is assigned once the module is started disk-quota: 3000M
- To build and deploy the application execute
cf deploy .\mta_archives\riz.inno.py-cf_1.0.0.mtar
- Be aware: Staging the application takes a fairly long time.
- To view the progress use a different terminal window and specfy:
cf logs riz-inno-py-cf
- At the end of the deployment the deployment process will show the URL for the deployed component. Open the URL in a browser and you should receive the text ‘Hello World’ in the browser window.
Run Jupyter Notebook in the Cloud Foundry stack
- jupyterlabto the end of the ‘environment.yml’ file
- Change ‘Procfile’ to have the following content
web: jupyter lab --ip 0.0.0.0 --port $PORT --no-browser
- To open Jupyter you need two pieces
- the host – Execute
cf app riz-inno-py-cf– under ‘routes:’ you can find the host name.
- The url/token. – Execute
cf logs riz-inno-py-cf --recent– copy the path including the token. Something like ‘/lab?token=131a072f38600b24698ea25c14431f1383054d542ce26117’
- Combine the host name with path and token to open up jupyter lab – Example URL: https://[host]/lab?token=131a072f38600b24698ea25c14431f1383054d542ce26117
I hope this post provided you a good overview of how you can run python apps in the Business Technology Platform Cloud Foundry stack.
Please share your feedback and thoughts in the comment section below.
You can find more relevant content by following the tag “Python”
Be the first to leave a comment
You must be Logged on to comment or reply to a post.