Running Kotlin Microservice on Google Kubernetes Engine


In this article I’ll guide you through the steps required for building and running simple Kotlin microservice on Google Kubernetes Engine. We will use such and framework like Spring Boot, Skaffold and Jib.

Example

The sample Kotlin application as always is available on GitHub. Here’s the URL to the repository: https://github.com/piomin/sample-spring-kotlin-microservice.git.

1. Configure kubectl to connect GKE

Before the exercise I have created a project My First Project and a single-node Kubernetes cluster on Google Cloud Platform. You can login to your using web console available under address https://console.cloud.google.com. Then you should navigate to your cluster available as Kubernetes Engine service and copy command responsible for configuring kubectl to connect with your cluster. After running gcloud command responsible for it you may check out a newly created kubectl context by running command kubectl config get-context. It displays all previously used kubectl contexts and marks the currently used context.
All the described steps are visible below.

gcloud-new-1

2. Add Skaffold to the project

A perfect solution that simplifies building and deploying applications on Kubernetes is Skaffold. For JVM-based applications you may use it together with Jib Maven Plugin. Both these tools are provided by Google. Skaffold is a command line tool that facilitates continuous development for Kubernetes applications. Jib is designed to build optimized Docker and OCI images for your Java applications without a Docker daemon.
To enable Skaffold for a project we need to create skaffold.yaml file in the root directory. Skaffold has many configuration settings, but a standard scenario does not require much. Here’s the skaffold.yaml for our current project. It sets the name of Docker image and activates Jib plugin.

guide-to-running-kotlin-on-gke-skaffold

We also need to add Jib Plugin to our Maven or Gradle configuration file. Here’s pom.xml for our project.

guide-to-running-kotlin-on-gke-jib

If you put all the Kubernetes manifests in k8s directory you don’t need to provide any other configuration. Let’s take a look on the described operations.

gcloud-new-7

3. Create Kotlin application

We are building a simple web application written in Kotlin. It is built on top of Spring Boot. It exposes HTTP API for managing Person objects, Swagger API documentation and Spring Boot Actuator endpoints. We are using the newest stable version of Spring Boot 2.2.6.RELEASE, and 1.3.70 version of Kotlin.

guide-to-running-kotlin-on-gke-springboot

Here’s the @Controller implementation responsible for handling HTTP API requests.

guide-to-running-kotlin-on-gke-controller

Let’s take a look on more details.

gcloud-new-6

4. Build/Deploy with Skaffold and Jib

Since we finished an implementation of the sample Kotlin application we may build and deploy it on Google Kubernetes Engine. Thanks to Skaffold we just need to run command skaffold dev --port-forward in the project root directory. Before running that command you should have configured credentials to your Docker Hub repository. Skaffold try to push the Docker image with application before deploying it on GKE. The credentials are stored in file $HOME_DIR/.docker/config.json.

gcloud-new-4

5. Verify on Google Cloud

If Skaffold command finished succesfully, we may verify the results. The following video illustrates that new image piomin/sample-spring-kotlin-microservice with application has been pushed to my Docker Hub repository, and a new deployment and service has been created on GKE. Thanks to Skaffold --port-forward we may easily test locally application running on Google Cloud Platform. We may call Actuator endpoint http://localhost:8080/actuator/info.

gcloud-new-5

Conclusion

Kotlin simplifies and speeds up building Spring Boot applications. Skaffold and Jib simplify building and running containers on Kubernetes, also when you are using remote cluster available on Google Cloud. With that powerful stack of tools you can easily develop and run microservices in cloud environment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.