Java - Kubernetes find services by shortcut

I am trying to develop a sample application using spring cloud and minicube which consists of 3 spring boot applications.

The first two are two different applications (servers) that have the same endpoint but have different functionality, and the third is a client used to integrate two other applications into one exposed api.

I was able to deploy all three applications in a mini-cube and managed to create a full stack and get them to talk to each other, but now I want to go further and make the two servers open automatically without hardcoding the service names.

I have deployed two servers to minikube using the same shortcut and would like to find something so that the client can automatically find the services associated with the two server applications. This will make it easy to extend the application, so when I add a new server to the stack, the client will find it and expose it without any changes.

Using Netflix Eureka this can be easily achieved using something like

discoveryClient.getInstances("service-name").forEach((ServiceInstance s)

      

But I don't want to add an additional eureka server to the list of microservices, since we will be using Kubernetes.

Is there a library that gives this function for Kubernetes?

+3


source to share


2 answers


You can use:

The CLI: kubectl get services --selector=YOUR-LABEL-NAME

.



API: GET /api/v1/namespaces/{namespace}/services

with parameter labelSelector

see API docs .

+1


source


I found fabric8 library that helped me achieve this. Still don't know if this is the correct answer, but it works: D

https://github.com/fabric8io/kubernetes-client/tree/master/kubernetes-client



@RequestMapping("/")
private String getResponse() {
    String ret = "hello from Client L0L!!!\n";

    //Config config = new ConfigBuilder().withMasterUrl("https://mymaster.com").build();
    //KubernetesClient client = new DefaultKubernetesClient(config);
    KubernetesClient client = new DefaultKubernetesClient();


    ServiceList services = client.services().withLabel("APIService").list();
    Service server = null;
    log.warn("---------------------------------------------->");
    for (Service s : services.getItems()) {
        log.warn(s.getMetadata().getName());
        log.warn(s.toString());
        if (s.getMetadata().getLabels().containsKey("ServiceType") && s.getMetadata().getLabels().get("ServiceType").equals("server"))
            server = s;
    }

    log.warn("---------------------------------------------->");

    String s = "";
    if (server != null) {
        RestTemplate t = new RestTemplate();
        String url = "http://" + server.getMetadata().getName() + ":" + server.getSpec().getPorts().get(0).getPort() + "/";
        log.warn("Contacting server service on: " + url);
        s = t.getForObject(url, String.class);
        log.warn("Response: " + s);
    } else {
        log.warn("Didn't find service with label ServiceType=server!!!");
    }

    return ret + " - " + s;
}

      

I am creating two services and adding two labels used in the code.

+1


source







All Articles