This is the last blog of a series that showed how to deploy, configure and use the Pivotal Cloud Foundry Runtime environment and developers console.
One of the cool parts of CF is the ability to install services, Hadoop, Mysql, Mongodb, and then deploy applications that attach themselves to these services. So if you wrote an analytic application you could deploy it to CF and bind it to the Hadoop service. This is all done in with automation, elasticity and dynamic tools provided by PivotalCF. Each time you deploy and app a dynamic environment is provided by CF to run the app using warden containers. Each app you push also needs a framework to run in, this is provided by buildpacks. By default PivotalCF has four system buildpacks:
- Java for applications written in Java, Grails, Play, Spring or any other JVM-based language or framework
- Node.js for applications written in Node or JavaScript
- Rubyfor applications written in Ruby, Rack, Rails or Sinatra
- Gofor applications written in Go
You can also use custom buildpacks created by the community. My good friend Matt Cowger has an example of deploying a Python Flask app using a custom build back. The basics are that during the deploy you retrieve the framework for your app from github. For this blog I'll show you how to deploy a simple "Hello World" application using the Ruby framework.
First, I'm using CentOS so I have to install Ruby on my machine:
sudo yum install ruby
sudo yum install gcc g++ make automake autoconf curl-devel openssl-devel zlib-devel httpd-devel apr-devel apr-util-devel sqlite-devel
sudo yum install ruby-rdoc ruby-devel
Once Ruby is installed I will use Gem for package building:
sudo yum install rubygems
For this app I will be using Sinatra, a DSL for quickly creating web applications in Ruby with minimal effort.
gem install bundle sinatra
Now I need to create the app. I'm logged in as the user "Jim" and have created a directory for my app in /home/jim/ruby . There are 4 files I need to create in this directoryfor this app to run
Config.ru - Contains the settings for deploying the app
require /home/jim/ruby
run Sinatra::Application
env.rb - This is the actual application
require 'rubygems'
require 'sinatra'
configure do
disable :protection
end
get '/' do
host = ENV['VCAP_APP_HOST']
port = ENV['VCAP_APP_PORT']
"<h1>Hello World!</h1><h2> I am in the Cloud! via: #{host}:#{port}</h2>"
end
get '/env' do
res = ''
ENV.each do |k, v|
res << "#{k}: #{v}<br/>"
end
res
end
Gemfile - Contains the information on the gems used by this app
source 'https://rubygems.org'
gem 'sinatra'
manifest.yml - This file tells cf push what to do with the app. By having a manifest you dont have to enter all sorts of options when deploying, the options are contained within this file.
applications:
- name: nifty
memory: 256M
command: 'ruby env.rb'
Once these files have been created I have to create a bundle. The bundle command creates a Gemfile.lock file that contains all the versions needed to run this app
The command is simply "bundle", run at the command line in the directory containing your app.
Above is the Gemfile.lock file that was created after running the bundle command.
Now we are ready to deploy our app. We'll need to connect to the api server and login
cf api https://api.cto.emc.local --skip-ssl-validation
cf login
Once the the api is set and you are logged in all you have to do is push the app
cf push Darkside
Login to the developers console and you can see the app running in the Developers space
Next we can verify the app is running by using a web browser to the FQDN of the app
Comments