Simple Spring Boot microservice deployed in Kubernetes using Docker and Nexus — Part 1

Razvan Simion
4 min readApr 10, 2019

--

Spring boot, Docker, Nexus, Kubernetes

In this short story we will point the basic steps to create a simple Spring Boot application, create a docker image with this application, push the docker image to Nexus and deploy the image to Kubernetes.

This story will be divided in two parts. In this first part we will create the Spring app, Build the docker image and push the image to Nexus.

Start a spring boot project

https://start.spring.io/

Init spring boot project

Check the project content

Check spring boot init content

Create a simple REST resource

[project path]/src/main/java/[project base package]/web/rest/DemoResource.java

package ro.trc.service.demo.web.rest;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(“/demo”)
public class DemoResource {

@GetMapping(“/status”)
public ResponseEntity<String> getList() {
return new ResponseEntity<>(“OK”, HttpStatus.OK);
}
}
Create rest resource

Build the project

gradlew clean bootJar

Run and test the project

IDE runner or java -jar [project path]/dist/libs/spring-microservice-docker-[version].jar. You can also create a Gradle bootRun task if you want.

Run and test the project

Create a Gradle task to build docker image and push to Nexus

We will use the Jib gradle plugin. Details about this plugin can be found at: https://github.com/GoogleContainerTools/jib/tree/master/jib-gradle-plugin

Rename the Gradle root project (optional)

In settings.gradle change the rootProject.name to:

rootProject.name = 'spring-microservice-docker'

Create Gradle task for docker

Create a file called docker.gradle in [project path]/gradle/docker.gradle

jib {

from {

image = "openjdk:11-jre-slim-stretch"

}

to {

image = "[nexus ip]:[nexus port]/spring-microservice-docker:$version"

auth {

username = [nexus user, for example ‘demo’]

password = [nexus password, for example ‘demo’]

}

}

container {

workingDirectory = "/"

entrypoint = ["sh", "-c", "chmod +x /entrypoint.sh && sync && . /entrypoint.sh"]

ports = ["8080"]

environment = [

SPRING_OUTPUT_ANSI_ENABLED: "ALWAYS",

DEMO_SLEEP: "0"

]

useCurrentTimestamp = true

}

allowInsecureRegistries = true

extraDirectory = file('src/main/jib')

}

Add Jib Gradle plugin dependency

build.gradle file contents

plugins {

id 'org.springframework.boot' version '2.2.0.BUILD-SNAPSHOT'

id 'java'

id "com.google.cloud.tools.jib" version "1.0.2"

}



group = 'ro.trc.service'

version = '0.0.1'

sourceCompatibility = '11'



apply plugin: 'io.spring.dependency-management'

apply from: "gradle/docker.gradle"



repositories {

mavenCentral()

gradlePluginPortal()

maven { url 'https://repo.spring.io/snapshot' }

maven { url 'https://repo.spring.io/milestone' }

}



dependencies {

implementation 'org.springframework.boot:spring-boot-starter-web'

testImplementation 'org.springframework.boot:spring-boot-starter-test'

}

Create entrypoint script

Create a file entrypoint.sh in [project path]/src/main/jib/entrypoint.sh

#!/bin/sh



echo "The application will start in ${DEMO_SLEEP}s..." && sleep ${DEMO_SLEEP}

exec java ${JAVA_OPTS} -noverify -XX:+AlwaysPreTouch -Djava.security.egd=file:/dev/./urandom -cp /app/resources/:/app/classes/:/app/libs/* "ro.trc.service.demo.DemoApplication" "$@"

Setup local Docker daemon for Nexus registry

If your nexus registry is exposed in unsecured way, you must change the Docker Daemon Settings.

Docker daemon setup

Publish docker image to Nexus

Run Jib Task

gradlew clean bootJar jib
Run jib task

Check Nexus Repository

Check nexus repository

Test the docker image

Login to Nexus Docker registry

From terminal:

docker login [nexus ip]:[nexus port]

Pull and run the Image

docker run -d -p 8080:8080 [nexus ip]:[nexus port]/spring-microservice-docker:0.0.1

Identify the container identifier

docker ps

Check the docker container logs

docker logs  [container id]
Image Docker logs

Check the application rest resource from the container

Check application

Push the project to Github

git initgit add ./git commit -m “Initial Commit”git remote add origin https://github.com/RazvanSimion/spring-microservice-docker.gitgit push origin master
Github project

The git project can be found at: https://github.com/RazvanSimion/spring-microservice-docker

We will show the Kubernetes deployment in the second part of the story (Part 2).

--

--