Class K8sNodeDiscovery

java.lang.Object
com.scivicslab.pojoactor.core.distributed.discovery.K8sNodeDiscovery
All Implemented Interfaces:
NodeDiscovery

public class K8sNodeDiscovery extends Object implements NodeDiscovery
Node discovery implementation for Kubernetes StatefulSets.

This implementation uses Kubernetes Headless Service DNS to discover pods in a StatefulSet. It requires the following environment variables:

  • POD_NAME - Name of the current pod (e.g., "pojo-actor-0")
  • SERVICE_NAME - Name of the Headless Service
  • REPLICAS - Number of replicas in the StatefulSet

Kubernetes Configuration Example


 apiVersion: v1
 kind: Service
 metadata:
   name: actor-nodes
 spec:
   clusterIP: None  # Headless Service
   selector:
     app: pojo-actor
   ports:
   - port: 8080
 ---
 apiVersion: apps/v1
 kind: StatefulSet
 metadata:
   name: pojo-actor
 spec:
   serviceName: actor-nodes
   replicas: 10
   template:
     spec:
       containers:
       - name: actor-node
         image: myapp:v2
         env:
         - name: POD_NAME
           valueFrom:
             fieldRef:
               fieldPath: metadata.name
         - name: SERVICE_NAME
           value: "actor-nodes"
         - name: REPLICAS
           value: "10"
         ports:
         - containerPort: 8080
 

Usage Example


 public static void main(String[] args) {
     NodeDiscovery discovery = new K8sNodeDiscovery();

     if (!discovery.isApplicable()) {
         System.err.println("Not running in Kubernetes environment");
         System.exit(1);
     }

     DistributedActorSystem system = new DistributedActorSystem(
         discovery.getMyNodeId(),
         discovery.getMyHost(),
         discovery.getMyPort()
     );

     // Register all remote nodes
     for (NodeInfo node : discovery.getAllNodes()) {
         if (!node.getNodeId().equals(discovery.getMyNodeId())) {
             system.registerRemoteNode(
                 node.getNodeId(),
                 node.getHost(),
                 node.getPort()
             );
         }
     }
 }
 
Since:
2.5.0
Author:
devteam@scivicslab.com
  • Constructor Details

    • K8sNodeDiscovery

      public K8sNodeDiscovery()
      Constructs a K8sNodeDiscovery with default port 8080 and default namespace.
    • K8sNodeDiscovery

      public K8sNodeDiscovery(int defaultPort, String namespace)
      Constructs a K8sNodeDiscovery with specified port and namespace.
      Parameters:
      defaultPort - the default port for actor communication
      namespace - the Kubernetes namespace
  • Method Details

    • isApplicable

      public boolean isApplicable()
      Description copied from interface: NodeDiscovery
      Checks if this discovery implementation is applicable to the current environment.
      Specified by:
      isApplicable in interface NodeDiscovery
      Returns:
      true if this discovery can be used in current environment
    • getMyNodeId

      public String getMyNodeId()
      Description copied from interface: NodeDiscovery
      Returns the unique identifier for the current node.
      Specified by:
      getMyNodeId in interface NodeDiscovery
      Returns:
      the node ID
    • getMyHost

      public String getMyHost()
      Description copied from interface: NodeDiscovery
      Returns the hostname or IP address for the current node.
      Specified by:
      getMyHost in interface NodeDiscovery
      Returns:
      the host address
    • getMyPort

      public int getMyPort()
      Description copied from interface: NodeDiscovery
      Returns the default port number for actor communication.

      This can be overridden by the user if needed.

      Specified by:
      getMyPort in interface NodeDiscovery
      Returns:
      the port number
    • getAllNodes

      public List<NodeInfo> getAllNodes()
      Description copied from interface: NodeDiscovery
      Returns information about all nodes in the cluster.

      This includes the current node and all remote nodes.

      Specified by:
      getAllNodes in interface NodeDiscovery
      Returns:
      list of all node information