Running Apache Brooklyn on Cloud Foundry

In previous posts we’ve spoken about the Brooklyn Service Broker, which makes use of Apache Brooklyn to deploy services such as MySQL or MongoDB for your Cloud Foundry applications. In this post, we’ll show you how to run Apache Brooklyn itself as a Cloud Foundry Application; run the Brooklyn Service Broker using this Brooklyn instance; then finally, we’ll register the service broker with Cloud Foundry, giving you a fully Cloud Foundry hosted mechanism for delivering services to your Cloud foundry developers.

Packaging a dist zip for the Java Buildpack

The first thing we need to do is download the Apache Brooklyn distribution:

$ wget http://mirrors.muzzy.org.uk/apache/brooklyn/apache-brooklyn-0.8.0-incubating/apache-brooklyn-0.8.0-incubating-bin.tar.gz

$ tar zxvf apache-brooklyn-0.8.0-incubating-bin.tar.gz

 

The Java buildpack allows us to upload a Dist Zip, so to do this we need two subdirectories: a bin folder, containing a start script; a lib folder containing the brooklyn distribution; and a brooklyn.properties file. The tree will look a bit like this:

.
├── bin
│ └── start
├── brooklyn.properties
└── lib
├── brooklyn
│ ├── DISCLAIMER
│ ├── LICENSE
│ ├── NOTICE
│ ├── README.md
│ ├── bin
│ │ ├── brooklyn
│ │ ├── brooklyn.bat
│ │ └── brooklyn.ps1
│ ├── conf
│ │ ├── brooklyn
│ │ │ └── default.catalog.bom
│ │ └── logback.xml
│ ├── lib
│ │ ├── brooklyn
│ │ │ ├── ...
│ │ ├── dropins
│ │ └── patch
│ └── pid_java
└── spring-boot-1.3.3.RELEASE.jar

Let’s start with the start script, which calls out to brooklyn, setting the port to be the one set by Cloud Foundry in the $PORT variable, and setting the location of the brooklyn.properties file. We put this start script in the bin folder.

#!/usr/bin/env bash

# Set APP_HOME
SAVED="`pwd`"
cd "`dirname \"$0\"`/.." >&-
APP_HOME="`pwd -P`"
cd "$SAVED" >&-

# Set BROOKLYN_PROPERTIES
BROOKLYN_PROPERTIES=$APP_HOME/brooklyn.properties
chmod 600 $BROOKLYN_PROPERTIES

# Launch Brooklyn
$APP_HOME/lib/brooklyn/bin/brooklyn launch -p $PORT --    localBrooklynProperties 
$BROOKLYN_PROPERTIES

Next, we need to put the brooklyn distribution into the lib folder. (Brooklyn already follows this dist format, and I’m sure you can move some files about, but just dropping it into the lib folder is simplest.) We also put a spring-boot-1.3.3.RELEASE.jar into lib so that the DistZip container is chosen by the java buildpack (I’m sure this can be avoided, but I couldn’t find a way).

Lastly, we need to alter the lib/brooklyn/bin/brooklyn script in the distribution, so that it uses the installed java runtime, replacing the last line:

exec java ${JAVA_OPTS} -cp "${INITIAL_CLASSPATH}" org.apache.brooklyn.cli.Main "$@"

with

if [ -n "$JAVA_HOME" ] ; then
    JAVACMD="$JAVA_HOME/bin/java"
else
    JAVACMD="java"
fi

exec $JAVACMD ${JAVA_OPTS} -cp "${INITIAL_CLASSPATH}" org.apache.brooklyn.cli.Main "$@"

Next, we set up a username and password for brooklyn and an IaaS location in brooklyn.properties to deploy the services to. (There is a template brooklyn.properties, for a greater range of options):

brooklyn.webconsole.security.users=brooklyn
brooklyn.webconsole.security.user.brooklyn.password=brooklyn

brooklyn.location.jclouds.aws-ec2.identity = 
brooklyn.location.jclouds.aws-ec2.credential = 
brooklyn.location.named.AWS\ West=aws-ec2:eu-west-1

Finally, we just need to zip up the project, and push to Cloud Foundry:

$ zip -r brooklyn-cf-dist.zip  brooklyn-cf-dist
$ cf push brooklyn -p brooklyn-cf-dist.zip
...
requested state: started
instances: 1/1
usage: 1G x 1 instances
urls: brooklyn.apps.cloudsoft.com
last uploaded: Tue Mar 1 12:35:14 UTC 2016

 state     since                    cpu    memory         disk
#0   running   2016-03-01 12:35:58 PM   0.0%   410.5M of 1G   190.5M of 1G

Deploy the Brooklyn Service Broker

We’ve discussed how to deploy the Brooklyn Service Broker in previous posts, so without dwelling on the details, we need to create a manifest file with the details of the Brooklyn Server:

applications:
- name: Brooklyn-Service-Broker
  memory: 1G
  env:
    BROOKLYN_URI: http://brooklyn.apps.cloudsoft.com
    BROOKLYN_USERNAME: brooklyn
    BROOKLYN_PASSWORD: brooklyn
    BROOKLYN_LOCATION: AWS West
    BROOKLYN_NAMESPACE: br
    SECURITY_USER_NAME: robert
    SECURITY_USER_PASSWORD: password
    SPRING_PROFILES_ACTIVE: size-plan

Then simply push:

$ cf push brooklyn-service-broker -p brooklyn-broker-0.1.8.war
...
requested state: started
instances: 1/1
usage: 1G x 1 instances
urls: brooklyn-service-broker.apps.cloudsoft.com
last uploaded: Tue Mar 1 12:52:31 UTC 2016

 state     since                    cpu    memory         disk
#0   running   2016-03-01 12:52:31 PM   0.0%   410.5M of 1G   188.6M of 1G

Register the broker

Finally, you can now register the service broker in the Org of your choosing to provide developers services deployed from the brooklyn server.

$ cf create-service-broker brooklyn robert password http://brooklyn-service-broker.apps.cloudsoft.com

Conclusion

We have seen in past posts how to deploy the brooklyn service broker to bring Apache Brooklyn deployed services into Cloud Foundry. We still had one question to answer, though, where to run Brooklyn itself. In this post we have seen how to deploy Apache Brooklyn to Cloud Foundry, making it simple to set up for use with the Brooklyn Service Broker.