Two separate hazelcast clusters in kubernetes Two separate hazelcast clusters in kubernetes kubernetes kubernetes

Two separate hazelcast clusters in kubernetes


It's possible to embed multiple Hazelcast instances into an application deployed in a single POD. Then, you can control how the cluster are formed. It requires an additional configuration, but you don't have to modify HazelcastKubernetesDiscoveryStrategy.

Sample application

I've created a sample application to present how it works. Please check it here: https://github.com/leszko/hazelcast-code-samples/tree/kubernetes-embedded-multiple/hazelcast-integration/kubernetes/samples/embedded.

Configuration steps

Hazelcast Configuration

You have two Hazelcast instances in your application, so you need to specify the ports they use. Also, with the parameters of the hazelcast-kubernetes plugin, you can configure which Hazelcast instances form the cluster together.

For example, assuming the first Hazelcast instance should form a cluster with all other Hazelcast instances in the current Kubernetes namespace, its configuration can look as follows.

Config config = new Config();config.getNetworkConfig().setPort(5701);config.getProperties().setProperty("hazelcast.discovery.enabled", "true");JoinConfig joinConfig = config.getNetworkConfig().getJoin();joinConfig.getMulticastConfig().setEnabled(false);HazelcastKubernetesDiscoveryStrategyFactory discoveryStrategyFactory = new HazelcastKubernetesDiscoveryStrategyFactory();Map<String, Comparable> properties = new HashMap<>();joinConfig.getDiscoveryConfig().addDiscoveryStrategyConfig(new DiscoveryStrategyConfig(discoveryStrategyFactory, properties));

Then, the second Hazelcast instance could form the cluster only with the same application. We could make this separation by giving it a service-name parameter with the value from environment variables.

Config config = new Config();config.getNetworkConfig().setPort(5702);config.getProperties().setProperty("hazelcast.discovery.enabled", "true");JoinConfig joinConfig = config.getNetworkConfig().getJoin();joinConfig.getMulticastConfig().setEnabled(false);HazelcastKubernetesDiscoveryStrategyFactory discoveryStrategyFactory = new HazelcastKubernetesDiscoveryStrategyFactory();Map<String, Comparable> properties = new HashMap<>();String serviceName = System.getenv("KUBERNETES_SERVICE_NAME");properties.put("service-name", serviceName);properties.put("service-port", "5702");joinConfig.getDiscoveryConfig().addDiscoveryStrategyConfig(new DiscoveryStrategyConfig(discoveryStrategyFactory, properties));GroupConfig groupConfig = new GroupConfig("separate");

Kubernetes Template

Then, in the Kubernetes Deployment template, you need to configure both ports: 5701 and 5702.

ports:- containerPort: 5701- containerPort: 5702

And the environment variable with the service name:

env:- name: KUBERNETES_SERVICE_NAME  value: hazelcast-separate-1

Plus, you need to create two services, for each Hazelcast instance.

kind: Servicemetadata:  name: hazelcast-shared-1spec:  type: ClusterIP  selector:    app: hazelcast-app  ports:  - name: hazelcast-shared    port: 5701kind: Servicemetadata:  name: hazelcast-separate-1spec:  type: ClusterIP  selector:    app: hazelcast-app  ports:  - name: hazelcast-separate  port: 5702

Obviously, in the same manner, you can separate Hazelcast clusters using service labels instead of service names.