Class K8sNodeDiscovery
java.lang.Object
com.scivicslab.pojoactor.core.distributed.discovery.K8sNodeDiscovery
- All Implemented Interfaces:
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 ServiceREPLICAS- 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 Summary
ConstructorsConstructorDescriptionConstructs a K8sNodeDiscovery with default port 8080 and default namespace.K8sNodeDiscovery(int defaultPort, String namespace) Constructs a K8sNodeDiscovery with specified port and namespace. -
Method Summary
Modifier and TypeMethodDescriptionReturns information about all nodes in the cluster.Returns the hostname or IP address for the current node.Returns the unique identifier for the current node.intReturns the default port number for actor communication.booleanChecks if this discovery implementation is applicable to the current environment.
-
Constructor Details
-
K8sNodeDiscovery
public K8sNodeDiscovery()Constructs a K8sNodeDiscovery with default port 8080 and default namespace. -
K8sNodeDiscovery
Constructs a K8sNodeDiscovery with specified port and namespace.- Parameters:
defaultPort- the default port for actor communicationnamespace- the Kubernetes namespace
-
-
Method Details
-
isApplicable
public boolean isApplicable()Description copied from interface:NodeDiscoveryChecks if this discovery implementation is applicable to the current environment.- Specified by:
isApplicablein interfaceNodeDiscovery- Returns:
- true if this discovery can be used in current environment
-
getMyNodeId
Description copied from interface:NodeDiscoveryReturns the unique identifier for the current node.- Specified by:
getMyNodeIdin interfaceNodeDiscovery- Returns:
- the node ID
-
getMyHost
Description copied from interface:NodeDiscoveryReturns the hostname or IP address for the current node.- Specified by:
getMyHostin interfaceNodeDiscovery- Returns:
- the host address
-
getMyPort
public int getMyPort()Description copied from interface:NodeDiscoveryReturns the default port number for actor communication.This can be overridden by the user if needed.
- Specified by:
getMyPortin interfaceNodeDiscovery- Returns:
- the port number
-
getAllNodes
Description copied from interface:NodeDiscoveryReturns information about all nodes in the cluster.This includes the current node and all remote nodes.
- Specified by:
getAllNodesin interfaceNodeDiscovery- Returns:
- list of all node information
-