Deploying Spring Boot in Pivotal Cloud Foundry

A lot of focus on my previous blogs has been on how to build micro services with Spring Boot. In this blog, I am going to cover how easy it is to deploy your application in Pivotal Cloud Foundry (PCF).

Spring Boot Applications

It is definitely more fun to deploy something that can communicate with each other rather than an isolated service. Therefore, I have prepared two applications that we are going to deploy in Pivotal Cloud Foundry. We are going to deploy an admin server with a fancy GUI for monitoring our Spring Boot Application, and a client that will register to the server.

Admin Server

The code for the admin server can be found here. It is a very straight forward admin server running on Spring Boot 2.1.2 and Spring Boot Admin version 2.1.1. It is a very straight forward Admin server without much configuration, for more details regarding this awesome module, see their GitHub repository.

Admin Client

The code for the client that will register to the Admin Sever can be found here. The Admin Client is also running on Spring Boot 2.1.2 and 2.1.1 for the admin client module. It is a very basic application with a basic GUI from Thymeleaf.

Pivotal Cloud Foundry

Now that we have our applications ready that we want to deploy to Pivotal Cloud Foundry. Let’s get started.

First of all, obviously, you will have to register an account on Pivotal. As of writing, they have a very generous offer where you can register very easily, without entering any credit card details and get $87 of free credits which will be more than enough for our lab.

After that you have gotten yourself an account, you will need to download the Cloud Foundry client to your computer where you want to deploy from. The documentation on Pivotal does a good job in covering the installation process, it is very straight forward.

You can check that the installation was successful by opening a new terminal and entering cf -h

$ cf -h
cf version 6.42.0+0cba12168.2019-01-10, Cloud Foundry command line tool

After that, all we need to do is to authenticate yourself with the account that you previously signed up.

cf login -a https://api.run.pivotal.io

We are good to go!

Configuration

When deploying our application, we can provide the configuration on the command line. However, it is difficult to track what flags the application actually was deployed with if you do it this way. Instead, I would recommend creating a manifest.yml in the root directory of your Spring Boot application. Then we can take advantage of having our deployment configuration managed by a SCM. I have created a very basic example of this file in both the admin server and the admin client repository. They look like the following, but with different names obviously.

---
applications:
  - name: adminserver
    memory: 768M

There are a lot more stuff that you can configure here, but this is enough for us to deploy.

Deploy

Next, all we have to do is to deploy. We do that by compiling our Maven (or Gradle) project and then pointing out which JAR file we want to deploy.

cf push -p target/adminserver-1.0.0-SNAPSHOT.jar
cf push -p target/adminclient-1.0.0-SNAPSHOT.jar

I have created the following handy scripts to building, pushing, deploying and stopping my services.

#!/bin/bash
# file: rebuild-adminserver.sh

cd adminserver 
mvn clean install
cf push -p target/adminserver-1.0.0-SNAPSHOT.jar
cd ..
#!/bin/bash
# file: rebuild-adminclient.sh

cd adminclient
mvn clean install
cf push -p target/adminclient-1.0.0-SNAPSHOT.jar
cd ..
#!/bin/bash
# file: deploy.sh

cf start adminserver
cf start adminclient
#!/bin/bash
# file: stop.sh

cf stop adminserver
cf stop adminclient

Navigating to #/applications shows us that we have deployed the admin server, and that the admin client successfully registered to it.

Admin server online on Pivotal Cloud Foundry

When you are done with it, don’t forget to stop your services so that you don’t use up your credits!

Docker

While it is convenient that you can deploy your fat JAR directly, sometimes it is better to deploy in a container instead that you control. When you deploy the fat JAR then you don’t have the same control over the JRE and other OS configuration. Luckily for us, Pivotal Cloud Foundry fully supports deploying our docker containers. This can be done by specifying this in our manifest.yml file.

---
applications:
  - name: adminserver
    memory: 768M
    docker:
      image: docker-image-repository/docker-image-name

For more details, see the official documentation.

Conclusion

This has been a very basic example of how to deploy our applications in Pivotal Cloud Foundry. Naturally, there is a lot of configurations that you can do but that is beyond the scope of this blog. But as you clearly can see, it is extremely easy to deploy with the nice tools provided by Pivotal and Cloud Foundry. Next step would be to integrate this with our build environments such as Jenkins. So that when we have a successful build, then Jenkins upgrades the cloud environment with the latest versions.


Leave a Reply