Class KubernetesEndpointGroup

All Implemented Interfaces:
EndpointGroup, EndpointSelector, AsyncCloseable, Listenable<List<Endpoint>>, ListenableAsyncCloseable, AutoCloseable

@UnstableApi public final class KubernetesEndpointGroup extends DynamicEndpointGroup
A DynamicEndpointGroup that fetches a node IP and a node port for each Pod from Kubernetes.

Note that the Kubernetes service must have a type of NodePort or 'LoadBalancer' to expose a node port for client side load balancing.

KubernetesEndpointGroup watches the nodes, services and pods in the Kubernetes cluster and updates the endpoints, so the credentials in the Config used to create KubernetesClient should have permission to watch services, nodes and pods. Otherwise, the KubernetesEndpointGroup will not be able to fetch the endpoints.

For instance, the following RBAC configuration is required:


 apiVersion: rbac.authorization.k8s.io/v1
 kind: ClusterRole
 metadata:
   name: my-cluster-role
 rules:
 - apiGroups: [""]
   resources: ["pods", "services", "nodes"]
   verbs: ["watch"]
 

Example:


 // Create a KubernetesEndpointGroup that fetches the endpoints of the 'my-service' service in the 'default'
 // namespace. The Kubernetes client will be created with the default configuration in the $HOME/.kube/config.
 KubernetesClient kubernetesClient = new KubernetesClientBuilder().build();
 KubernetesEndpointGroup
   .builder(kubernetesClient)
   .namespace("default")
   .serviceName("my-service")
   .build();

 // If you want to use a custom configuration, you can create a KubernetesEndpointGroup as follows:
 // The custom configuration would be useful when you want to access Kubernetes from outside the cluster.
 Config config =
   new ConfigBuilder()
     .withMasterUrl("https://my-k8s-master")
     .withOauthToken("my-token")
     .build();
 KubernetesEndpointGroup
   .builder(config)
   .namespace("my-namespace")
   .serviceName("my-service")
   .build();