Deploying a Spring Boot application on Google Kubernetes Engine

In this section, we will learn how to deploy a Spring Boot application in Google Kubernetes Engine.


1.  A little bit of Background

Google Kubernetes Engine

The most scalable and fully automated Kubernetes service
Put your containers on autopilot, eliminating the need to manage nodes or capacity and reducing cluster costs—with little to no cluster operations expertise required.

Spring Boot

Spring Boot makes it easy to create stand-alone, production-grade Spring-based Applications that you can "just run". 


2. Creating a simple spring boot web application

First, open the Spring initializr https://start.spring.io/ 

Then, Provide the Group and Artifact name. We have provided Group name com.knf.dev.demo and Artifact spring-boot-hello-world. Here I selected the Maven project - language Java 11 - Spring Boot 2.7.8 and add Spring web dependency.


Then, click on the Generate button. When we click on the Generate button, it starts packing the project in a .zip(spring-boot-hello-world) file and downloads the project. Then, Extract the Zip file. 

Then, import the project on your favourite IDE.


Final Project directory:


pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knf.dev.demo</groupId>
<artifactId>spring-boot-hello-world</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-hello-world</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>


Create Hello Controller

package com.knf.dev.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

@GetMapping("/hello")
public String getMessage()
{
return "Hello World!";
}
}


Application.java

package com.knf.dev.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}


3. Create a New Repo and Upload Files on GitHub

First, sign in to Github https://github.com/ 

Then, create a new repository "spring-boot-hello-world". 

Then, upload the source code from your local machine to the newly created Github repo.


4. Create a GCP Project

First, Sign into the Google console at https://console.cloud.google.com.

You can create a new project by first selecting the project dropdown in the top left and selecting "New Project".


Next, specify your GCP Project name and Project ID.

Then Click on the "CREATE" button.


5. Build the Docker-image for Spring Application & push the docker image to Google's Container Registry

Start the Google Cloud Shell in the browser.
Button to activate cloud shell is marked in the below image.


Next, clone the git repository:

git clone https://github.com/knowledgefactory4u/spring-boot-hello-world.git

Change the directory to spring-boot-hello-world

cd spring-boot-hello-world


Create the JAR deployable for the app.

mvn -DskipTests package

You will see the following similar output in your cloud shell:


Enable Container Registry to store the container image that we will create.

gcloud services enable containerregistry.googleapis.com

You will see the following similar output in your cloud shell:



Next, use Jib to create the container image and push it to the Container Registry.

export GOOGLE_CLOUD_PROJECT=`gcloud config list --format="value(core.project)"`
mvn -DskipTests com.google.cloud.tools:jib-maven-plugin:build \ -Dimage=gcr.io/$GOOGLE_CLOUD_PROJECT/spring-boot-hello-app:v1

If everything goes fine, you will see the following similar output in your cloud shell:


Check whether the image is pushed in Google Container Registry

From cloud console, search for "Container Registry" like below and click on "Container Registry" button.


You will see the image name which has been pushed.


6. Create Kubernetes Cluster

First, make sure that the related API features are enabled.

gcloud services enable compute.googleapis.com container.googleapis.com

You will see the following similar output in your cloud shell:


Then, create a cluster by executing below command.

gcloud container clusters create spring-boot-hello-cluster --num-nodes=2 --region=asia-southeast1-a

You will see the following similar output in your cloud shell:

You can change the cluster name, number of nodes and region as you wanted. But, if you don’t know which region to select, you can find more information from this region link.

Now you’ve got a Kubernetes Cluster.

From cloud console, search for "Kubernetes Engine" like below and click on "Kubernetes Engine" button.

You can see the name of your Kubernetes Cluster.


7. Deploy Spring Application on Kubernetes

Create a Deployment for Spring App docker image:

kubectl create deployment spring-boot-hello-app \ --image=gcr.io/$GOOGLE_CLOUD_PROJECT/spring-boot-hello-app:v1


To View the Deployment that we just created:

kubectl get deployments

You will see the following similar output in your cloud shell:



To View, the Spring Application Pod instances created by the deployment:
kubectl get pods

You will see the following similar output in your cloud shell:


Now we have a Container running under the control of Kubernetes, now it's time to make it accessible to the outside world.


8. Allow External Traffic

In Cloud Shell, you can expose the Pod to the public internet by creating a Kubernetes LoadBalancer service.

kubectl create service loadbalancer spring-boot-hello-app --tcp=8080:8080


Check the Service that we just created,

kubectl get services

You will see the following similar output in your cloud shell:

Note: If the EXTERNAL-IP is missing, then wait a few minutes and try again.

Verify that Spring Boot App is running,


Next section: Deploy a Spring Boot application in Google Cloud App Engine - Click here!

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java Stream API - How to convert List of objects to another List of objects using Java streams?

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Java, Spring Boot Mini Project - Library Management System - Download

ReactJS, Spring Boot JWT Authentication Example

Spring Boot + Mockito simple application with 100% code coverage

Top 5 Java ORM tools - 2024

Java - Blowfish Encryption and decryption Example

Spring boot video streaming example-HTML5

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete