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
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

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

  • The Role of AI in Identity and Access Management for Organizations
  • Monolith: The Good, The Bad and The Ugly
  • AI-Driven Test Automation Techniques for Multimodal Systems
  • Performing and Managing Incremental Backups Using pg_basebackup in PostgreSQL 17
  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 user avatar
Jan-Rudolph Bührmann
·
Jan. 17, 22 · Code Snippet
Likes (4)
Comment
Save
Tweet
Share
6.5K Views

Join the DZone community and get the full member experience.

Join For Free

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
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!