Coder Social home page Coder Social logo

cf-flask-test's Introduction

A Simple Python Flask app on Cloud Foundry

This is a simple Flask application that shows how apps on CloudFoundry works.

Prerequisites

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.

Files

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.

How to deploy the python web apps

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

How to scale it?

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

Create and bind mysql database

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

Test the database connection

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

Reference

https://docs.cloudfoundry.org/devguide/deploy-apps/cf-scale.html

cf-flask-test's People

Contributors

ichbinblau avatar

Stargazers

Srikanta Prasad (Sri) avatar David Caron avatar  avatar Yiwei Zhou avatar Maic Siemering avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.