Deploy Vert.x applications onto Kubernetes using Eclipse JKube

Rohan Kumar
5 min readAug 7, 2020

--

Note: This blog is a part of blog Series: Deploying Java applications onto Kubernetes using Eclipse JKube

Deploying Vert.x applications onto Kubernetes using Eclipse JKube

Today in this blog, I would be talking about a project called Eclipse JKube and how it helps in deploying Vert.x applications onto Kubernetes without any problems.

We will be deploying one of Vert.x quickstarts hosted in their examples repository onto Kubernetes. But first we need to make sure our Kubernetes Cluster is running.

Preparing Kubernetes Environment:

You can either use your own cluster or install a single node Kubernetes Cluster like Minikube. I am using minikube so I started it using this command:

minikube start

To use minikube ‘s local docker daemon, I used this command:

# To point your shell to minikube's docker-daemon, run:
eval $(minikube -p minikube docker-env)

Getting Vert.x Quickstarts(Vert.x MusicStore Application):

We need to deploy one Vert.x project onto Kubernetes. I’ll be using one of the famous Music Store projects’s mentioned in vert-x3/vertx-awesome:

You can clone this repository and go to web-api-service-example demo project:

git clone https://github.com/tsegismont/vertx-musicstore.git
cd vertx-musicstore/

Setting up the database:

This application relies on Postgres and MongoDB databases. So we would need to set them up first. Since we’re deploying application onto Kubernetes, let’s deploy these databases onto Kubernetes too. You would need to apply these two yaml files onto your Kubernetes cluster:

PostgreSQL Kubernetes manifest:

PostgreSQL YAML manifest

MongoDB Kubernetes manifest:

MongoDB Yaml manifest
~/work/repos/vertx-musicstore : $ kubectl create -f postgres.yml
configmap/postgres-config created
persistentvolume/postgres-pv-volume created
persistentvolumeclaim/postgres-pv-claim created
deployment.apps/postgres created
service/postgres created
~/work/repos/vertx-musicstore : $ kubectl create -f mongodb.yml
deployment.apps/mongodb created
service/mongo created

You’ll notice that these manifests have create Deployment and Service for your databases with which you would be able to access them from outside of Kubernetes. You can also check trying to access them using commandline psql and mongo :

Accessing PostGreSQL running in Kubernetes
Accessing MongoDB running inside Kubernetes

Now we’re left with two things, modifying code to point to the databases running inside Kubernetes and then deploying main application onto Kubernetes.

In order to point our application to correct database we would need to find database ports and host. These databases are created with Service of type NodePort which means that they are exposed to host machine via a port. You can check what port that is by checking services:

~/work/repos/vertx-musicstore : $ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 16d
mongo NodePort 10.106.38.202 <none> 27017:30292/TCP 3m21s
postgres NodePort 10.107.38.197 <none> 5432:30033/TCP 4m11s
~/work/repos/vertx-musicstore : $ minikube ip
192.168.39.145

So after inspecting Kubernetes services, we can see that our databases are running with the following URLs:

  • PostgreSQL : 192.168.39.145:30033/
  • MongoDB: 192.168.39.145:30292/

Now that we know where our databases are running we can now modify code to point them to the right URL. Hence we’ll modify code (just 4 lines in 2 files) to configure databases.

PostgreSQL: We will checkout DatasourceConfig class which is storing PostgreSQL configuration. We’ll modify its constructor like this:

url = datasourceConfig.getString("url", "jdbc:postgresql://192.168.39.145:30033/musicdb");
user = datasourceConfig.getString("user", "postgresadmin");
password = datasourceConfig.getString("password", "admin123");

MongoDB: We will modify mongodb connection string inside MusicStoreVerticle class:

String connectionString = config()
.getJsonObject("mongo", new JsonObject())
.getString("url", "mongodb://192.168.39.145:30292");

We’ll compile project afterwards:

mvn clean install

Deploying on Kubernetes with Eclipse JKube:

Now that we have set up our databases, we just need to deploy this onto Kubernetes with the help of Eclipse JKube’s Kubernetes Maven Plugin . We’ll be using the plugin for these three things:

  • k8s:build -> Build Container image of application (Docker/JIB)
  • k8s:resource -> Generate Kubernetes Manifests
  • k8s:apply -> Apply those Kubernetes Manifests onto Kubernetes

Note: You’d also need to delete Dockerfile inside root directory used for docker builds since it collides with Eclipse JKube’s default opinionated build strategy.

Delete Dockerfile first(which is for docker compose):

rm Dockerfile

Run Eclipse JKube’s Kubernetes Maven Plugin goals:

mvn org.eclipse.jkube:kubernetes-maven-plugin:build \  
> org.eclipse.jkube:kubernetes-maven-plugin:resource \
> org.eclipse.jkube:kubernetes-maven-plugin:apply \
-Djkube.enricher.jkube-service.type=NodePort

This would build docker image inside minikube, create Kubernetes manifests inside target/classes/META-INF/jkube and apply them onto Kubernetes cluster:

Eclipse JKube deploying Vert.x MusicStore into Kubernetes

You can check for your application pods and services:

~/work/repos/vertx-musicstore : $ kubectl get pods
kubNAME READY STATUS RESTARTS AGE
mongodb-855bf5d4db-j7b79 1/1 Running 0 34m
musicstore-5f47cf8df5-zxz8g 1/1 Running 0 73s
postgres-7ffd788bc9-xjpjz 1/1 Running 0 34m
~/work/repos/vertx-musicstore : $ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 16d
mongo NodePort 10.106.38.202 <none> 27017:30292/TCP 34m
musicstore NodePort 10.99.233.244 <none> 8080:31998/TCP 75s
postgres NodePort 10.107.38.197 <none> 5432:30033/TCP 35m

You can access your application running inside Kubernetes with minikube service musicstore , it would open up your application in your default browser.

~/work/repos/vertx-musicstore : $ minikube service musicstore
|-----------|------------|-------------|-----------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|------------|-------------|-----------------------------|
| default | musicstore | http/8080 | http://192.168.39.145:31998 |
|-----------|------------|-------------|-----------------------------|
🎉 Opening service default/musicstore in default browser...
Vert.x Music Store running inside Kubernetes
Commenting after creating user in Vert.x Music store

Conclusion:

I hope I was able to give you an overview of Eclipse JKube and how ease it makes working on top of Kubernetes. Most of the time in this blog was spent on setting up database :-) but that’s okay.

Please checkout project website for more information about project:

Thanks for taking time to read!

--

--

No responses yet