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.
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 a Docker image and activates a Jib plugin.
We also need to add Jib Plugin to our Maven or Gradle configuration file. Here’s pom.xml
for our project.
If you put all the Kubernetes manifests in k8s
directory you don’t need to provide any other configuration. Let’s take a look at the described operations.
3. Create Kotlin microservice
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.
Here’s the @Controller
implementation responsible for handling HTTP API requests.
Let’s take a look at more details.
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 the application before deploying it on GKE. The credentials are stored in file $HOME_DIR/.docker/config.json
.
5. Verify on Google Kubernetes Engine
If Skaffold command finished successfully, we may verify the results. The following video illustrates that a new image piomin/sample-spring-kotlin-microservice
with the application has been pushed to my Docker Hub repository, and 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
.
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 a remote cluster available on Google Cloud. With that powerful stack of tools, you can easily develop and run microservices in the cloud environment.
Leave a Reply