Create a RESTful API Service with Spring Boot, dockerize & deploy to Kubernetes

Spring Boot on Kubernetes

We’ll first create a new Spring Boot Application implementing a basic HTTP GET service endpoint. Once our application is up and running we can go ahead and create a docker image and launch a docker container to test our service out. And finally, we’ll take our docker image and deploy it to a Kubernetes Cluster for a production grade deployment.


We use the stock jre image from openjdk to run our application inside of:

FROM openjdk:8-jre-alpine

COPY build/libs/k8-byexamples-spring-rest-0.0.1-SNAPSHOT.jar /application.jar

CMD ["java", "-jar", "/application.jar"]

Makefile (optional)

I’ve created a simple Makefile to speed things up. Simply make all to build & push the docker image. Similarly you can make k8/install to install the kubernetes manifests:

VERSION ?= $(shell git rev-parse HEAD)
APP     ?= k8-byexamples-spring-rest

.PHONY: build

all: build push


   docker build -t $(IMAGE) .


   docker run -p 8080:8080 $(IMAGE)


   docker push $(IMAGE)


   kubectl apply -f manifests/


   kubectl delete -f manifests/

This is a simple REST controller that responds to HTTP POST requests (i.e.: https:///test/echo):

package com.example.k8byexamplesspringrest;

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

public class TestRestController {

    public ResponseEntity<?> testResponse(@RequestBody String body) {

        return new ResponseEntity<>("You said: " + body, HttpStatus.OK);



Source Code Repositories: