Both Lagom and Kubernetes are gaining popularity quite fast. Lagom is an open source framework for building reactive microservice systems in Java/Scala. And, Kubernetes (or K8s in short) is an open-source system for automating deployment, scaling, and management of containerized applications. Together they make an excellent stack for developing Reactive microservices of production grade.
We have already seen a lot of blogs on Lagom on this site, like:
- Data Persistence in Lagom
- Unit Testing vs Component Testing in Lagom
- A Beginner’s Guide to Deploying a Lagom Service Without ConductR
- And many more…
In this blog post, we will take a closer look at the steps helping us deploy our Lagom application, built using Java & Maven, to Kubernetes.
We all know that Lagom is a distributed microservices framework. It means it can be deployed over a cluster of machines where all of them will interact with each other for handling requests, R/W data, or maintaining state. For this Lagom takes help of Akka Clustering which provides a fault-tolerant decentralized peer-to-peer based cluster with no single point of failure. Since the cluster is peer-to-peer based, the communication between all machines has to be ensured. Now, how to do that let’s see through our Lagom Restaurant example.
Step 1: Add Akka Cluster Manager & Bootstrap dependencies
We have to start with adding Akka Cluster Manager & Bootstrap dependencies in the pom.xml file:
Akka Management is a suite of tools for operating Akka Clusters. Whereas, Akka Cluster Bootstrap helps to form a cluster by using Akka Discovery to discover peer nodes. It is an alternative to configuring static seed-nodes in dynamic deployment environments such as on Kubernetes or AWS. It is built on the flexibility of Akka Discovery, leveraging a range of discovery mechanisms depending on the environment we want to run our cluster in. This leads us to our next step, i.e., adding Akka Discovery suite.
Step 2: Add the Akka Discovery suite
Akka Discovery provides a simple interface around various ways of locating services, such as DNS or using configuration or key-value stores like zookeeper, consul etc. For our Lagom example, we will use the DNS method to locate services over Kubernetes. So, we have to add the following dependency in the pom.xml:
Step 3: Add the Reactive Lib tool
Reactive Lib (reactive-lib) is a component of Lightbend Orchestration for Kubernetes which is a developer-centric suite of tools that helps us deploy Reactive Platform applications to Kubernetes or DC/OS. So, we have to add the following dependency in the pom.xml:
Now, we are all set in terms dependencies required to run our Lagom application on Kubernetes.
Step 4: Update Configuration
Next step is to configure our Lagom application. For this, we have to add the following content to the application.conf file:
Above configurations will let Lagom application know that it has to use Kubernetes API (kubernetes-api) for Akka Discovery and other details.
Step 5: Bind Akka Cluster Manager & Bootstrap to Module
At last, we have to bind Akka Cluster Manager and Cluster in our Lagom application’s module file. Like this:
Now, we are all set in terms of code. The only part left is to create a Docker image of our Lagom application and deploy it on Kubernetes. For demo purpose, we are going to use Minikube.
Step 6: Start Minikube
Above command will delete any old started Minikube and start a new instance of Minikube with 2GB memory. Also, it will export some environment variables required for Minikube.
Step 7: Create Docker Image
Now, we have to create the Docker image of our Lagom application. In our Lagom Restaurant example, we are using Maven Docker Plugin to ease our work:
Here we are building docker image with Kubernetes profile. It is done in order to make application flexible enough to be deployed on any environment, like, Marathon, DC/OS, or AWS.
Step 8: Run Lagom Application
The last step is to run the Lagom application’s image inside Minikube. For this first, we have to apply Role-Based Access Control over K8s. This will let us exercise fine-grained control over how users access the API resources running on our K8s cluster.
So, just apply RBAC via the following command:
Next, we have to create our Lagom application’s configuration:
And apply it using the following command:
This will start our Lagom application which we can verify by hitting http://192.168.99.100:31001/menu.
The Akka Cluster Management & Bootstrap and Reactive Lib suite(s) have made it very simple to deploy our Lagom application(s) on Kubernetes or DC/OS. All it requires is few configurations/dependencies and we are good to go.
The above process was for Java/Maven Lagom application(s). For readers looking for Scala/SBT Lagom application(s), please refer to the blog post written by Yannick De Turck: Lagom 1.4 and Kubernetes Orchestration. It explains the process very nicely. And, for readers looking for an example, please refer to code repo: lagom-on-k8s.
I hope you found this blog post informative. If you have any feedback then please leave a comment below.