Deploy Springboot to GKE from scratch in 7 minutes

Sakul Montha
6 min readApr 22, 2020

--

Hi I’m Gique, Today I will share how to deploy springboot to Google Kubernetes Engine (GKE) from scratch…

> Google Kubernetes Engine

Google Kubernetes Engine known as GKE is an enterprise-grade platform for containerized applications, including stateful and stateless, AI and ML, Linux and Windows, complex and simple web apps, API, and backend services. Leverage industry-first features like four-way auto-scaling and no-stress management. Link

Prerequisite

  • Maven / Gradle
  • mvnw or gradlew
  • docker
  • kubenetes
  • Google cloud account
  • IDEA I prefer IntelliJ

If you have everything, let’s do it!!

> Initial Springboot

Go to spring.io to initialze your project

Initializr springboot project

You can choose maven or gradle, Java or Kotlin. Up to you then click Generate your project and then extract it.

Now we have Springboot project. Let’s import project to your IDEA

Import project / project structure

Let’s do the unit test

// GkeApplicationTests@ExtendWith(SpringExtension.class)
@SpringBootTest
class GkeApplicationTests {

@InjectMocks
GkeApplication app;

private MockMvc mvc;

@BeforeEach
public void setUp() {
MockitoAnnotations.initMocks(this);
app = new GkeApplication();
mvc = MockMvcBuilders.standaloneSetup(app)
.build();
}

@DisplayName("Test get hello with PathVariable success")
@Test
void testGetHelloWithPathVariableSuccess() throws Exception {
MvcResult mvcResult = mvc.perform(get("/hello/Gique"))
.andExpect(status().isOk())
.andExpect(content().string("Hello Gique"))
.andReturn();
}
}

Let’s Write the code

// GkeApplication@SpringBootApplication
@RestController
public class GkeApplication {

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

@GetMapping("hello/{name}")
public String hello(@Valid @PathVariable("name") String name) {
return "Hello " + name;
}
}

Run: mvn spring-boot:run then try to access it with default port localhost:8080/hello/Gique

Congratulations! You’ve started spring boot already.

> Docker

For this step, I will lead you to create simpleDockerfile as below

// DockerfileFROM openjdk:8
VOLUME /tmp
RUN mkdir /application
COPY . /application
WORKDIR /application
RUN /application/mvnw install #if you use gradle change it to /application/gradlew build
RUN mv /application/target/*.jar /application/app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/application/app.jar"]

$ docker build --no-cache -t gkeappdemo . Everything’s gonna be ok!!!

If it’s success, try to run docker with this command $ docker run -it --rm -p 8080:8080 gkeappdemo and try to access it with default port localhost:8080/hello/Gique again.

> Google Cloud Platform

Therefore, when you wanna use GKE you must have GCP account. This section, I will lead you to create a project on Google Cloud Platform (GCP).
Go to console.cloud.google and click create new project

Create new project

This section will lead you to create an image file to use on EKS.

Container Registry

Go to Container Registry then click Enable

Enable container registry

When you want to push image file to GCP, it’s necessary to have permission to “Create buckets storage”. So, you must login first using the command
$ gcloud auth login . To ensure that you’re working on the project with the right account, you can set the account using this command $ gcloud config set account '${email}'.

gcloud auth login

After-that, You’re now having permission to push image to “Container Registry” then run the command as below

// Go to your project directory 
$ cd /Users/${your_account}/Project/gke/
$ gcloud builds submit --tag=gcr.io/gke-application-demo/gkeappdemo:v1 .

Wait… Yeah everything is success

Push image to Container Registry success
docker image on Container Registry

Kubernetes Cluster

Earlier we created image file for running on production.
Now, we need to create a cluster because Google Kubernetes Engine lets you create Kubernetes clusters to host. Let’s go to Console Kubernetes to Enable Kubernetes Engine API

Enable Kubernetes Engine API
// Command
$ gcloud container clusters create gke-application-demo-cluster --num-nodes=3 --region=asia-southeast1-a
$ gcloud config set container/cluster gke-application-demo-cluster

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

cluster gke-application-demo-cluster

You can find cluster list by running$ gcloud container images list . Now you’ve got a Kubernetes Cluster but, it’s inaccessible because you haven’t deploy image to Kubernetes Cluster.

Deploy to Kubernetes Cluster

Congratulations, It’s almost complete already. So, this section will lead you to deploy your image to Kubernetes Cluster.

$ kubectl run gkeappdemo --image=gcr.io/gke-application-demo/gkeappdemo:v1 --port 8080$ kubectl get pods$ kubectl expose deployment gkeappdemo --type=LoadBalancer --port 80 --target-port 8080$ kubectl get service

For the first line, you need to change “gke-application-demo” and “gkeappdemo” to your own GCP project.

Waiting external-ip until finish

Compute Engine and Kubernetes clusters
Congratulations!! you can do it.

Update your application

When you have any changes you can push new image anytime.

$ gcloud builds submit --tag=gcr.io/gke-application-demo/gkeappdemo:v2 .$ kubectl set image deployment/gkeappdemo gkeappdemo=gcr.io/gke-application-demo/gkeappdemo:v2$ kubectl get service
gkeappdemo version 2

If you want to rollback

kubectl set image deployment/gkeappdemo gkeappdemo=gcr.io/gke-application-demo/gkeappdemo:v1

> Conclusion

Actually, This is some of many method to deploy your image to GKE. There are many ways to deploy to GKE such as Cloud Code in IntelliJ which you can use it also.

It’s not difficult, Is it? When I start GKE, I found a problem almost every single command. So, that is a reason why I wrote it. This arcticle made for GKE newbie like me and I hope you can make it and enjoy.

It isn’t necessary to use Spring boot. You can make anythings as an image file to deploy Kubernetes Cluster as well.

You can clone this project source code from Github

--

--

Sakul Montha
Sakul Montha

Chief Product Officer, a man who’s falling in love with the galaxy.

No responses yet