DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Solving Four Kubernetes Networking Challenges
  • Optimizing Kubernetes Costs With FinOps Best Practices
  • Microservices With Apache Camel and Quarkus (Part 3)
  • An Overview of Popular Open-Source Kubernetes Tools

Trending

  • DZone's Article Submission Guidelines
  • How to Submit a Post to DZone
  • The "Just Read the Code" Myth Is Killing Your AI Workflow
  • Idempotency Under Concurrency: Why Your Application Check Isn't the Last Line of Defense

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Microservices
  4. Service Discovery From Within the Kubernetes Cluster

Service Discovery From Within the Kubernetes Cluster

Does Java Kubernetes Client live up to the promise of being a Java library to operate the Kubernetes cluster by accessing Kubernetes API? Find out here.

By 
Jan-Rudolph Bührmann
·
Jan. 17, 22 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
7.2K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

It is probably not common to write your own code to perform service discovery from within the Kubernetes cluster. It is better to delegate that responsibility to an ingress controller or a load balancer. However, ever the adventurer, I stumbled onto the Java Kubernetes Client.

The project was an investigation into using Spinnaker as a CI/CD tool for a new micro-services architecture. My adventures of getting Spinnaker to run on Minikube have been documented as a three-article series on DZone and can be accessed from my profile page. 

Once I had Spinnaker up and running, I had to come up with something to deploy to impress my boss. I decided to try my hand at GraphQL. Coding the two micro-services was fun, and GraphQL exceeded my expectations in its ability to provide data at the correct granularity. However, one aspect tested my patience exceedingly: how would the Book microservice know where to find the Author microservice in the unpredictable world of container-managed service orchestration? As I did not want to install a load-balancer or external service discoverer just for this, I decided to try my hand at internal service discovery.

The project wiki describes the client as follows: "Kubernetes Java Client is a Java library to operate the Kubernetes cluster by accessing Kubernetes API. " I still have to investigate whether the API lives up to the promise, but it solved the task at hand: 

Java
 
    public String determineK8sRootFromHttpTillPort(String svcName) throws IOException, ApiException {
        String result = null;
        System.out.println("Determining roor for " + svcName);
        ApiClient client = Config.defaultClient();
        Configuration.setDefaultApiClient(client);
        CoreV1Api api = new CoreV1Api(client);
        V1ServiceList listNamespacedService =
                api.listNamespacedService(
                        DEFAULT_NAME_SPACE,
                        null,
                        null,
                        null,
                        null,
                        null, // TODO use this for selections
                        Integer.MAX_VALUE,
                        null,
                        TIME_OUT_VALUE,
                        Boolean.FALSE);
        for (V1Service service : listNamespacedService.getItems()) {
            if (service.getMetadata().getName().equals(svcName)) {
                V1ServiceSpec spec = service.getSpec();
                String portNr = "#";

                result = "http://" + spec.getClusterIP() + ":" + spec.getPorts().get(0).getPort();
                System.out.println("Root determined " + result);
            }
        }

        return result;
    }

This is the Java equivalent of "kubectl get svc". Towards the bottom, one can see how the result is mined and combined towards the URL of the Kubernetes service, encapsulating all of the Author service pods behind it. The Book service can now determine its Author in the normal fashion. 

I would be very surprised if the Java Kubernetes Client does not live up to the promise quoted above.

Kubernetes microservice Service discovery cluster Discovery (law)

Opinions expressed by DZone contributors are their own.

Related

  • Solving Four Kubernetes Networking Challenges
  • Optimizing Kubernetes Costs With FinOps Best Practices
  • Microservices With Apache Camel and Quarkus (Part 3)
  • An Overview of Popular Open-Source Kubernetes Tools

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook