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.
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 Docker image and activates 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 on the described operations.
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
1.3.70 version of Kotlin.
@Controller implementation responsible for handling HTTP API requests.
Let’s take a look on 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 application before deploying it on GKE. The credentials are stored in file
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
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.