CloudFoundry: Deploying the spring-music webapp, Part 1

Cloud Foundry is an opinionated Platform-as-a-Service that allows you to manage applications at scale.  It supports multiple infrastructure platforms, and is able to standardize deployment, logging,  scaling, and routing in a way friendly to a continuous delivery pipeline.

This article is Part 1 of  a series on Cloud Foundry concepts:

In this particular article, we will install the command line interface for Cloud Foundry on Ubuntu and then use that to deploy the Spring Boot based spring-music project to a CF provider.

Selecting a Cloud Foundry Provider

For purposes of simplicity, I’m going to recommend you sign up for a free Cloud Foundry trial directly from Pivotal.  Click here to start.

Note that there are many Cloud Foundry providers [1,2], using Pivotal will just make the subsequent outputs more uniform with this article.

Pick an arbitrary name for your org, I choose “fleetest1”, and you should see something similar after you have verified your account and logged in.

Install Command Line Tool

Install the CF CLI which is a console based client application that communicates with the CF Cloud Controller hosted at Pivotal.

$ wget -q -O - https://packages.cloudfoundry.org/debian/cli.cloudfoundry.org.key | sudo apt-key add -
$ echo "deb http://packages.cloudfoundry.org/debian stable main" | sudo tee /etc/apt/sources.list.d/cloudfoundry-cli.list
$ sudo apt-get update
$ sudo apt-get install cf-cli

Then verify the CLI version, which should look similar to below:

$ cf --version
cf version 6.31.0+b35df905d.2017-09-15

Validate CLI

Now authenticate into your remote Pivotal CF org using the command ‘cf login’.  The API endpoint will by default point at ‘https://api.run.pivotal.io’ which is correct for Pivotal CF.  If you were using  another provider, you would need to use the ‘-a’ option to override the API endpoint.

$ cf login
API endpoint: https://api.run.pivotal.io
Email> fabian.lee@test1.com

Password> 
Authenticating...
OK

Targeted org fleetest1

Targeted space development
                
API endpoint:   https://api.run.pivotal.io (API version: 2.98.0)
User:           fabian.lee@test1.com
Org:            fleetest1
Space:          development

There are no applications currently loaded, so listing them with ‘cf apps’ results in an empty list.

$ cf apps
Getting apps in org fleetest1 / space development as fabian.lee@test1.com...
OK

No apps found

Build project

The spring-music project is a Spring Boot project that allows a user to browse and modify a collection of music.  By default it persists to an in-memory database, although it can very easily persist to Postgres, SQL Server, or Oracle.

In order to create the spring-music.jar that will be pushed to CloudFoundry, we need to build the project from source.  The project requires Java8, so the first step is to install Java8 on our Ubuntu host.

$ sudo add-apt-repository ppa:openjdk-r/ppa -y
$ sudo apt-get update
$ sudo apt-cache policy openjdk-8-jdk
$ sudo apt-get install openjdk-8-jdk -y
$ sudo update-alternatives --config java
$ sudo update-alternatives --config javac

$ /usr/bin/java -version
openjdk version "1.8.0_141"
OpenJDK Runtime Environment (build 1.8.0_141-8u141-b15-3~14.04-b15)
OpenJDK 64-Bit Server VM (build 25.141-b15, mixed mode)

Then we grab the source code of the project from github and assemble it using the included gradle scripts:

$ sudo apt-get install git -y
$ git clone https://github.com/fabianlee/spring-music.git
$ cd spring-music
$ ./gradlew clean assemble

...
BUILD SUCCESSFUL

This should result in a ‘build/libs/spring-music.jar’ file, which is used in the staging process by the buildpacks when creating the droplet.

Deploy app

The ‘manifest.yml’ file found in the root directory is used as the driving configuration for the CF push.

---
applications:
- name: spring-music
  memory: 1G
  random-route: true
  path: build/libs/spring-music.jar

This simple manifest provides a name and the path of the spring-music.jar used in the staging process.  The ‘random-route’ property makes sure that the deployment URL is unique by suffixing two random words to ensure uniqueness.

Go ahead and push the application:

$ cf push

You should see output saying that buildpacks and app packages are downloading and finally that the instance is starting.  At the end you will see something similar to below:

Showing health and status for app spring-music in org fleetest1 / space development as fabian.lee@test1.com...
OK

requested state: started
instances: 1/1
usage: 1G x 1 instances
urls: spring-music-unadmissive-schiffli.cfapps.io 
last uploaded: Thu Nov 9 20:20:48 UTC 2017 
stack: cflinuxfs2 
...

state since cpu memory disk details #0 running 2017-11-09 03:22:48 PM 94.9% 323.1M of 1G 171.3M of 1G

The last line tells us the service is running, and how much cpu/memory is being consumed by the application.

The ‘urls:’ line means that the application can be found at ‘https://spring-music-unadmissive-schiffli.cfapps.io’, which can be tested in the browser.

 

 

 

REFERENCES

https://pivotal.io/platform

https://docs.cloudfoundry.org/concepts/

https://pivotal.io/platform/pcf-tutorials/getting-started-with-pivotal-cloud-foundry/deploy-the-sample-app

https://github.com/scottfrederick/spring-music

https://github.com/cloudfoundry-samples/spring-music

http://cli.cloudfoundry.org/en-US/cf/ (command reference)

https://pivotal.io/platform/pcf-tutorials/getting-started-with-pivotal-cloud-foundry/deploy-the-sample-app (PCF 15 minute tutorial)