Kubernetes Discovery with Embedded Infinispan

What You Will Learn

How to configure embedded Infinispan to form a cluster on Kubernetes using JGroups DNS_PING for node discovery, and how to build and deploy the application using JKube.

Prerequisites

  • Java 17+

  • A Kubernetes cluster (e.g., Minikube, OpenShift)

  • kubectl configured to access the cluster

Step 1: Add the Embedded Dependency

Add Infinispan core to your pom.xml:

<dependency>
   <groupId>org.infinispan</groupId>
   <artifactId>infinispan-core</artifactId>
</dependency>

Step 2: Configure JGroups Kubernetes Transport

Use the built-in Kubernetes JGroups configuration and pass the DNS query via a system property:

      //Configure Infinispan to use default transport and Kubernetes configuration
      GlobalConfiguration globalConfig = new GlobalConfigurationBuilder()
       .transport()
            .defaultTransport()
            .addProperty("configurationFile", "default-configs/default-jgroups-kubernetes.xml")
            .build();

The DNS query is set through the jgroups.dns.query system property, which points to the headless Kubernetes service:

<options.dnsPing>
   -Djgroups.dns.query=${options.service}.${options.namespace}.svc.cluster.local
</options.dnsPing>

Step 3: Create a Replicated Cache

Create a replicated cache so every pod in the deployment holds all entries:

         // We need a distributed cache for the purpose of this demo
         Configuration cacheConfiguration = new ConfigurationBuilder()
               .clustering()
               .cacheMode(CacheMode.REPL_SYNC)
               .build();

         Cache<String, String> cache = cacheManager.createCache("kubernetes", cacheConfiguration);

Step 4: Populate the Cache Periodically

Each pod updates its own entry on a schedule, demonstrating cluster-wide data replication:

         // Each cluster member will update its own entry in the cache
         String hostname = Inet4Address.getLocalHost().getHostName();
         ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
         scheduler.scheduleAtFixedRate(() -> {
                  String time = Instant.now().toString();
                  cache.put(hostname, time);
                  System.out.println("[" + time + "][" + hostname + "] Values from the cache: ");
                  System.out.println(cache.entrySet());
               },
               0, 2, TimeUnit.SECONDS);

Step 5: Build and Deploy to Kubernetes

Build the Docker image and deploy to Kubernetes using JKube:

mvn package k8s:build k8s:resource k8s:apply -Pdocker

This creates a deployment with 2 replicas and a headless service for DNS-based cluster discovery.

What’s Next