deploying spring boot application in mesosphere

In this article I will illustrate how to deploy a spring boot application as a service in mesosphere. The prerequisite to follow this tutorial is a docker installation and access to artifact management repo.

Lets start with a docker installation in linux environment:

  1. Sudo apt-get update which updates the linux image
  2. sudo apt-get install apt-transport-https ca-certificates curl software-properties-common to install ca certificate to run apt over https
  3. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add – which adds the ca certificate to the linux machine
  4. sudo apt-key fingerprint 0EBFCD88 to verify the certificate
  5. sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable” which adds linux repo to apt-get so that we can download docker cli
  6. sudo apt-get update to sync the docker executable repo to apt repo
  7. sudo apt-get install docker-ce which installs docker cli
  8. sudo docker –version to verify docker cli installation

To login to an artifactory like jfrog artifactory or any other artifactory follow the below steps

  1. export DOCKER_OPTS+=” –insecure-registry ${ip_address_of_artifactory}”
  2. sudo docker login ${ip_address_of_artifactory}

which promts for a username and password

Once we have docker cli up and running and connected to a artifactory we can deploy a spring boot app in mesosphere.

Lets create a simple spring boot application as below and build a fat jar from the same.


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class })
@EnableScheduling
public class Application {

public static void main(String[] args) {

SpringApplication.run(Application.class, args);
}
}


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

@RequestMapping("/test")
public ResponseEntity<String> greeting(@RequestParam(value="name", defaultValue="Service is Working") String name) {
return ResponseEntity.status(org.springframework.http.HttpStatus.OK).body("Success");
}
}

Lets dockerize the spring boot app. Create a folder called as code where we will be adding the jar file we created above.

Below is the docker file which can be used to build a docker image. I am using mesosphere/spark as my base image we can use any other image as per your requirement. We are also exposing the port 8080 to the outside world to access the service.


FROM mesosphere/spark
WORKDIR /home/springbootapp
ADD /code/. .

EXPOSE 8080

Let’s build a docker image from the above docker file

  • sudo docker build -t springbootapp:1.0 .

Let’s tag the image with the artifactory url

  • docker tag springbootapp ${artifactory.path}:1.0

Push the image to artifactory

  • docker push ${artifactory.path}:1.0

Lets deploy the image we created above into mesosphere. Go the service tab in the mesosphere ui and create a new service using the below json file


{
"env": {
"spark.master.conf.mesos": "local[*]"
},
"labels": {
"HAPROXY_GROUP": "external",
"HAPROXY_0_VHOST": "/adarsh/test/springbootapp, springbootapp.com"
},
"id": "/adarsh/test/springbootapp",
"acceptedResourceRoles": [
"*"
],
"backoffFactor": 1.15,
"backoffSeconds": 1,
"cmd": "/usr/lib/jvm/jre1.8.0_131/bin/java -jar /home/springbootapp/spring-boot-app-0.0.1-SNAPSHOT.jar",
"container": {
"portMappings": [
{
"containerPort": 8080,
"hostPort": 0,
"labels": {
"VIP_0": "springbootapp.com:8080"
},
"protocol": "tcp",
"servicePort": 10304
}
],
"type": "DOCKER",
"volumes": [],
"docker": {
"image": "${artifactory.path}:1.0",
"forcePullImage": true,
"privileged": false,
"parameters": []
}
},
"cpus": 5,
"disk": 0,
"instances": 1,
"maxLaunchDelaySeconds": 30,
"mem": 24576,
"gpus": 0,
"networks": [
{
"mode": "container/bridge"
}
],
"requirePorts": false,
"upgradeStrategy": {
"maximumOverCapacity": 1,
"minimumHealthCapacity": 1
},
"killSelection": "YOUNGEST_FIRST",
"unreachableStrategy": {
"inactiveAfterSeconds": 0,
"expungeAfterSeconds": 0
},
"healthChecks": [],
"fetch": [],
"constraints": []
}

Click on run and review button to deploy the spring boot app. To test the spring boot app run the http://springbootapp.com/test on the browser.