Deploy Springboot to GKE from scratch in 7 minutes

> 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

> Initial Springboot

Go to spring.io to initialze your project

Initializr springboot project
Import project / project structure
// 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();
}
}
// 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;
}
}

> 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"]

> 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

Container Registry

Go to Container Registry then click Enable

Enable container registry
gcloud auth login
// Go to your project directory 
$ cd /Users/${your_account}/Project/gke/
$ gcloud builds submit --tag=gcr.io/gke-application-demo/gkeappdemo:v1 .
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
cluster gke-application-demo-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
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
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.

--

--

Get the Medium app

Sakul Montha

Sakul Montha

Development Director, a man who’s falling in love with the galaxy.