Kubernetes Remote Development in Java Using Kubernetes Maven Plugin

Introduction

In this article, we’re going to look at some pain points while developing Java applications on top of Kubernetes. We’re going to look at newly added functionality in Eclipse JKube’s Kubernetes Maven Plugin that allows your application running on your local machine to get exposed in Kubernetes Cluster.

If you haven’t heard about Eclipse JKube or Kubernetes Maven Plugin, I’d suggest you read the following DZone articles first:

Target Audience:

Current Solutions

What Is Eclipse JKube Kubernetes Remote Development

Our team at Eclipse Cloud Tooling is focused on creating tools that ease developer activity and development workflow across distributed services. While working and testing on Kubernetes Maven Plugin, we noticed that repeatedly building and deploying applications to Kubernetes while developing locally isn’t the most effective way of working. 

In v1.10.1 of Kubernetes Maven Plugin, we added a new goal k8s:remote-dev . This goal tries to ease java developer workflow across distributed services via:

Why Kubernetes Remote Development?

Let’s consider a scenario where we’re writing a joke microservice that tries to fetch joke strings from other microservices. Here is a diagram for you to get a better understanding:

Joke Microservice

Figure 1: Simple Joke application using two existing services


Custom Joke Service is our main application which has one endpoint /random-joke. It depends on two other microservices ChuckNorris and Jokes via /chuck-norris and /joke endpoints, respectively. The user requests a joke using /random-joke endpoint, and our application fetches a joke string from one of the two microservices randomly.

In order to develop and test our application, we need access to these dependent ChuckNorris and Jokes services, respectively. Let’s see what the developer’s workflow would look like:

Exposing Remote Kubernetes Services Locally

Let’s assume you have two applications already running in Kubernetes Cluster on which your current application is dependent:

 
$ kubectl get svc
NAME                     TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE
service1                 NodePort    10.101.224.227   <none>        8080:31878/TCP   113s
service2                 NodePort    10.101.224.227   <none>        8080:31879/TCP   113s


Let us expose these remote services running in Kubernetes Cluster to our local machine. Here is a diagram for you to better understand:

Eclipse JKube's Remote Dev goal/task simplifying developing with remote services

Figure 2: JKube's remote development simplifying remote development


In order to do that, we need to provide XML configuration to our plugin for exposing these services:

XML
 
      <plugin>
        <groupId>org.eclipse.jkube</groupId>
        <artifactId>kubernetes-maven-plugin</artifactId>
        <version>${jkube.version}</version>
        <configuration>
          <remoteDevelopment>
            <remoteServices>
              <remoteService>
                <hostname>service1</hostname> <!-- Name of Service -->
                <port>8080</port>                 <!-- Service port -->
                <localPort>8081</localPort>       <!-- Local Port where to expose -->
              </remoteService>
              <remoteService>
                <hostname>service2</hostname>  <!-- Name of Service -->
                <port>8080</port>           <!-- Service Port -->
                <localPort>8082</localPort> <!-- Local Port where to expose -->
              </remoteService>
            </remoteServices>
          </remoteDevelopment>
        </configuration>
      </plugin>


The above configuration is doing these two things:

Run Kubernetes Remote Development goal:

Shell
 
$ mvn k8s:remote-dev
[INFO] Scanning for projects...
[INFO] 
[INFO] -----------< org.eclipse.jkube.demos:random-jokes-generator >-----------
[INFO] Building random-jokes-generator 1.0.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- kubernetes-maven-plugin:1.10.1:remote-dev (default-cli) @ random-jokes-generator ---
[INFO] k8s: Waiting for JKube remote development Pod [jkube-remote-dev-9cafc1e1-054b-4fab-8f4e-b4345056478e] to be ready...
[INFO] k8s: JKube remote development Pod [jkube-remote-dev-9cafc1e1-054b-4fab-8f4e-b4345056478e] is ready
[INFO] k8s: Opening remote development connection to Kubernetes: jkube-remote-dev-9cafc1e1-054b-4fab-8f4e-b4345056478e:54252


[INFO] k8s: Kubernetes Service service1:8080 is now available at local port 8081

[INFO] k8s: Kubernetes Service service2:8080 is now available at local port 8082


Try accessing services available locally on ports:

Shell
 

$ curl localhost:8081/
Chuck Norris's OSI network model has only one layer - Physical.

$ curl localhost:8082/
Why do Java programmers have to wear glasses? Because they don't C#.


As you can see, You are able to access Kubernetes services service1 and service2 locally on ports 8081 and 8082, respectively.

Conclusion

In this article, you learned about Eclipse JKube’s Kubernetes Maven Plugin’s remote development goal and how you can expose your local applications to the Kubernetes cluster and vice versa.

In case you’re interested in knowing more about Eclipse JKube, you can check these links:

 

 

 

 

Top