Amplify Cloud Native development using Spring Boot, Docker & Kubernetes in VS Code!

LaxmiKumar Reddy Sammeta
5 min readJan 19, 2021
Photo by Kozjat

Cloud-native Micro-service development being the focus area, we will discuss and build a sample end-to-end Java application by leveraging the benefits of super cool tech-stack such as:

  • Visual Studio Code as piggybacking IDE
  • Spring Boot
  • Docker
  • Kubernetes

VS Code as single IDE

Far too long, Java developers did give oversight of picking up and exploring VS Code for Java development. When I stumbled upon this IDE, I felt this editor has decent enough support. However, we will uncover VS Code features for all-inclusive development of our goal in step-by-step manner.

Once you have downloaded the IDE into your laptop, you can search and download the necessary Extensions that are listed below.

VS Code Extensions Search and download
  • Java Extension Pack
  • Spring Initializr
  • Spring Boot Extension Pack
  • Docker
  • Docker Desktop
  • Remote Containers
  • Kubernetes
  • YAML

Best part? If you are too hooked to other popular IDEs such as Eclipse or IntelliJ, there is an Extension available that does the keybindings.

Let’s hit the high spots!

The Spring Initializr allows you to search for dependencies and create new Spring boot project. Open the Command Palette (⇧⌘P) and type Spring Initializr to start generating new SB project. Once done, go to pom.xml to check the dependencies. You can add new dependencies by searching in Command Palette. I built the REST GET service which takes-in the any website’s URL as parameter, opens connection to the URL and responds with appropriate message such the website is up or down etc. Below is the full code.

package com.sammeta.gusto.springboot.gustosb;
import java.io.IOError;import java.io.IOException;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;@RestControllerpublic class URLHeartBeatMonitor{private static final String template = "The URL: %s is %s; Response Message is %s";private static final String template_failure = "The URL: %s is %s";
@GetMapping("/heartbeat" )
public String getURLHeartBeatStatus(@RequestParam(value = "url", defaultValue = "https://www.google.com") String url) {String returnMsg = "";String respMessage = "";try {URL urlObj = new URL(url);HttpURLConnection conn = (HttpURLConnection) urlObj.openConnection();conn.setRequestMethod("GET");conn.connect();respMessage = conn.getResponseMessage();int respCodeCat = conn.getResponseCode() / 100;if (respCodeCat != 2) {returnMsg = String.format(template, url, "DOWN", respMessage);} else {returnMsg = String.format(template, url, "UP", respMessage);}} catch(MalformedURLException e) {returnMsg = String.format(template_failure, url, "MALFORMED", respMessage);;} catch(IOException e) {returnMsg = String.format(template_failure, url, "DOWN", respMessage);}return returnMsg;}}

That’s all. You are done with meaty part of the project.

You can explore other extension ~ Spring Boot Dashboard that offers great ease to manage all Springboot apps in your workspace to Start, Stop and debug projects.

Run this service now. Then, go access the URL in your browser.

http://localhost:8080/heartbeat?url=https://www.google.com

Here is the response:

It looks good! You can test other scenarios as well.

Let’s dockerize this Spring Boot app!

We had installed the Docker extensions already into the IDE. They help build, manage and deploy the Containerized apps straight off VS code.

Go to Command Palette, type Docker Image… then, it prompts few options. Pick them appropriately, it would build Dockerfile for you. Something close to below:

FROM openjdk:8-jdk-alpineVOLUME /tmpARG JAVA_OPTSENV JAVA_OPTS=$JAVA_OPTSCOPY target/gusto-sb-0.0.1-SNAPSHOT.jar gustosb.jarEXPOSE 8080# For Spring-Boot project, use the entrypoint below to reduce Tomcat startup time.ENTRYPOINT exec java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar gustosb.jar

Build Image

Make sure you have the artifact (.jar) built under /target directory. If not, go to EXPLORER -> MAVEN -> Right click on the Project, click on ‘Package’. Then, go check if you have .jar existing in there.

Also make sure Docker Desktop is running in local. Run:

docker — version

Next up, go the Dockerfile and click the Build Image option or run the command:

docker build — pull — rm -f “Dockerfile” -t gustosb:latest “.” <

The docker build consumes little high time. In my case, it took good 2 minutes to build the image. The screenshot below explains the same.

Push Image to Docker Hub Registry

You can view the newly built image under IMAGES tab under Docker Section. Now, it is the time to push it to DockerHub registry.

Command:

docker push slkreddysite/gustosb:latest <

Alternatively, you can right-click on the image and select Push. After some time, you see the image under selected registry.

The push refers to repository [docker.io/slkreddysite/gustosb]

Image pushed to DH Registry

Run the Image

Select the Image, right-click and click on the Run. Post which, you can access the service like you did earlier.

docker run — rm -d -p 8080:8080/tcp slkreddysite/gustosb:latest <

Hope this is decently well-rounded guide to get started. As the continuation, in my next article, I will explain how we can pull this image into a docker container in a Kubernetes Pod environment and expose it to public IP, how to configure replica sets, deployments and so on. We will also touch-base on Service Mesh pattern with some really cool and practical examples.

Stay tuned and stay safe!

--

--