Difficulty: Beginner
Estimated Time: 20 minutes

Spring Boot is a great way to write an application in Java. This scenario shows you how to create a Spring Boot application and run it in Kubernetes with as little fuss and bother as possible. And there's no YAML. To do this we need to do three things:

  1. Create a Spring Boot application
  2. Containerize it, and push the container to a registry
  3. Deploy it to Kubernetes

You will need a few minutes of time. If you have credentials for a public docker registry, like Dockerhub or Google Container Registry, that will help, but it won't stop you from completing the scenario.

We don't cover all the features of Spring and Spring Boot. For that you could go to the Spring guides or Spring project homepages.

We also don't cover all the options available for building containers. There is a Spring Boot topical guide covering some of those options in more detail.

When it comes to deploying the application to Kubernetes, there are far too many options to cover them all in one tutorial. We can look at one or two, but the emphasis will be on getting something working as quickly as possible, not on how to deploy to a production cluster.

Source code for this guide is in Github. It is deployed in Katacoda where you can run the code interactively in your browser.

Well done! You built an app, containerized it and pushed it to Kubernetes. Here are the links again to the Spring guides and Spring project homepages. And the Spring Boot Docker covering some more of the containerization options.

Getting Started with Spring Boot on Kubernetes

Step 1 of 3

Create a Spring Boot Application

NOTE: A Kubernetes cluster will start for you in the terminal on the right (if you are on a phone or a small device, rotate it to landscape to see the terminal). When the prompt appears you can proceed with the tutorial.

The first thing we will do is create a Spring Boot application. If you have one you prefer to use already in github, you could clone it in the terminal (git and java are installed already). Or you can create an application from scratch using start.spring.io:

curl https://start.spring.io/starter.tgz -d dependencies=webflux,actuator | tar -xzvf -

You can then build the application:

./mvnw install

NOTE: It will take a couple of minutes the first time, but then once the dependencies are all cached it will be fast.

And you can see the result of the build. If the build was successful, you should see a JAR file, something like this:

ls -l target/*.jar

-rw-r--r-- 1 root root 19463334 Nov 15 11:54 target/demo-0.0.1-SNAPSHOT.jar

The JAR is executable:

java -jar target/*.jar

The app has some built in HTTP endpoints by virtue of the "actuator" dependency we added when we downloaded the project. So you will see something like this in the logs on startup:

...
2019-11-15 12:12:35.333  INFO 13912 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
2019-11-15 12:12:36.448  INFO 13912 --- [           main] o.s.b.web.embedded.netty.NettyWebServer  : Netty started on port(s): 8080
...

So you can curl the endpoints:

curl localhost:8080/actuator | jq .

{
  "_links": {
    "self": {
      "href": "http://localhost:8080/actuator",
      "templated": false
    },
    "health-path": {
      "href": "http://localhost:8080/actuator/health/{*path}",
      "templated": true
    },
    "health": {
      "href": "http://localhost:8080/actuator/health",
      "templated": false
    },
    "info": {
      "href": "http://localhost:8080/actuator/info",
      "templated": false
    }
  }
}

Finally:

echo "Send Ctrl+C to kill the application"