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.
Join the DZone community and get the full member experience.Join For Free
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 log in 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.
gcloud command responsible for it you may check out a newly created kubectl context by running the 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. A 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 the Docker image and activates the Jib plugin.
We also need to add the 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 a 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 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 a file
5. Verify on Google Cloud
If the Skaffold command finishes 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 have 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 clusters available on Google Cloud. With that powerful stack of tools, you can easily develop and run microservices in a cloud environment.
Published at DZone with permission of Piotr Mińkowski, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.