This is a simple Flask application that shows how apps on CloudFoundry works.
Apply for an Cloud Foundry account by signing up for a free trial at Pivotal Web Services.
Install the CF CLI according to the OS you're using.
The Python buildpack requires extra configuration files below:
-
Procfile
This file specifies the command to start the app.
-
requirements.txt
This file illustrates the additional python packages not included by the buildpack by default.
-
runtime.txt
This file is to specify the python runtime, eg, python-2.7.10.
Open the command prompt and login the CF
cf login -a api.run.pivotal.io
Download the sample project
git clone https://github.com/ichbinblau/cf-flask-test.git
Upload the project to CF. I was unable to use the auto detected python buildpack provided by CF here thereby specifying another working buildpack by -b.
cd cf-flask-test
cf push flask-test-theresa -b https://github.com/cloudfoundry/cf-buildpack-python.git -m 128m -i 1
At the end of the execution, you will see messages below and are able to access the app by the url flask-test-theresa.cfapps.io
requested state: started
instances: 1/1
usage: 128M x 1 instances
urls: flask-test-theresa.cfapps.io
last uploaded: Thu Nov 12 05:21:11 UTC 2015
stack: cflinuxfs2
buildpack: https://github.com/cloudfoundry/cf-buildpack-python.git
If the upload fails, you may check the error the command:
cf logs flask-test-theresa --recent
You may scale the app horizontally by simply increasing the number of instances. Incoming requests to your application are automatically load balanced across all instances of your application, and each instance handles tasks in parallel with every other instance.
cf scale flask-test-theresa -i 6
Check the app health status, and you will see that there are 6 instances are up and running now.
cf app flask-test-theresa
Run the below commands to create a cf db service and bind it with the web app
cf create-service cleardb spark flask-test-db // creates a ClearDB MySQL service
cf bind-service flask-test-theresa flask-test-db
Add database operation logics in hello.py, and test by accessing flask-test-theresa.cfapps.io/users
def get_user():
initial_db()
u = User('theresa', '[email protected]')
db_session.add(u)
db_session.commit()
return 'The first user is ' + db_session.query(User).first().name
https://docs.cloudfoundry.org/devguide/deploy-apps/cf-scale.html