Skip to main content

Connect to Hazelcast from Outside Kubernetes

What You'll Learn​

In this tutorial, you'll connect to a Hazelcast cluster running in Kubernetes from outside of the Kubernetes environment.

Before you Begin​

note

Starting with version 5.13, Hazelcast Platform Operator only supports Hazelcast Enterprise cluster creation. Even the feature can be used in Hazelcast Open Source clusters, Hazelcast Platform Operator requires license key to run a cluster.

warning

This tutorial uses LoadBalancer services to connect to Hazelcast from outside of the Kubernetes cluster. Therefore, it is essential to ensure that your Kubernetes cluster can assign public IPs to LoadBalancer services. This is particularly important if you are using a local Kubernetes cluster such as Minikube or Kind.

Introduction​

There are two available options for Expose Externally feature of Hazelcast Platform Operator:

  • Unisocket - client requests are load balanced between Hazelcast members.
  • Smart - client connects to all members and sends requests directly to the members owning the data.

Let's see both approaches.

Unisocket​

The first option is to use the Unisocket type. This option will use the standard Kubernetes mechanism that automatically load balances the traffic to Hazelcast members.

Hazelcast Unisocket Client

Start the Hazelcast Cluster​

Run the following command to create the Hazelcast cluster with Expose Externally feature enabled using Unisocket type.

shell
kubectl apply -f - <<EOF
apiVersion: hazelcast.com/v1alpha1
kind: Hazelcast
metadata:
name: my-hazelcast-unisocket
spec:
licenseKeySecretName: hazelcast-license-key
exposeExternally:
type: Unisocket
discoveryServiceType: LoadBalancer
EOF

For discoveryServiceType you can use:

  • LoadBalancer - will create an external LoadBalancer for discovery service;
  • NodePort - will expose the discovery service via NodePort.

Verify the Hazelcast Cluster​

Check the cluster status by running the following command.

shell
$ kubectl get hazelcast my-hazelcast-unisocket
NAME STATUS MEMBERS
my-hazelcast Running 3/3

After verifying that the cluster is Running and all the members are ready (3/3), run the following command to find the discovery address.

shell
$ kubectl get hazelcastendpoint my-hazelcast-unisocket
NAME TYPE ADDRESS
my-hazelcast-unisocket Discovery 34.135.7.75:5701

The ADDRESS column displays the external address of your Hazelcast cluster.

Connect Hazelcast Clients to the Cluster​

To access all sample clients, clone the following repository:

shell
git clone https://github.com/hazelcast-guides/hazelcast-platform-operator-expose-externally.git
cd hazelcast-platform-operator-expose-externally/docs/modules/ROOT/examples/operator-expose-externally
  1. The sample code(excluding CLC) for this tutorial is in the docs/modules/ROOT/examples/operator-expose-externally directory.

Configure the Hazelcast client with the external address and disable smart routing to use the unisocket connection.

note

Before using CLC, it should be installed in your system. Check the installation instructions for CLC: Installing the Hazelcast CLC.

Run the following command for adding the cluster config to the CLC.

bash
clc config add hz cluster.name=dev cluster.address=<EXTERNAL-IP>

Now you can start the application.

Run the following command to fill a map.

bash
for i in {1..10};
do
clc -c hz map set --name map1 key-$i value-$i;
done

Run the following command to check the map size.

bash
clc -c hz map size --name map1

Smart Client​

The second option is to use the Smart type. With this option, each Hazelcast member will be exposed with its own service (it can be either LoadBalancer or NodePort). Hazelcast smart client is capable of mapping the given key with its owner member, which means that it sends the data directly to the member which contains the right data partition.

Hazelcast Smart Client

Start the Hazelcast Cluster​

Run the following command to create the Hazelcast cluster with Expose Externally feature enabled using Smart type.

shell
kubectl apply -f - <<EOF
apiVersion: hazelcast.com/v1alpha1
kind: Hazelcast
metadata:
name: my-hazelcast-smart
spec:
licenseKeySecretName: hazelcast-license-key
exposeExternally:
type: Smart
discoveryServiceType: LoadBalancer
memberAccess: LoadBalancer
EOF

This will create the Hazelcast cluster and one LoadBalancer service for discovery and one LoadBalancer service for each pod.

For discoveryServiceType you can use:

  • LoadBalancer - will create an external LoadBalancer for discovery service;
  • NodePort - will expose the discovery service via NodePort.

For memberAccess you can use the following options:

  • LoadBalancer - lets the client access Hazelcast member with the LoadBalancer service;
  • NodePortNodeName - lets the client access Hazelcast member with the NodePort service and the node name;
  • NodePortExternalIP - lets the client access Hazelcast member with the NodePort service and the node external IP/hostname.

Verify the Hazelcast Cluster​

Check the cluster status by running the following command.

shell
$ kubectl get hazelcast my-hazelcast-smart
NAME STATUS MEMBERS
my-hazelcast Running 3/3

After verifying that the cluster is Running and all the members are ready (3/3), run the following command to find the discovery and member addresses.

shell
$ kubectl get hazelcastendpoint --selector="app.kubernetes.io/instance=my-hazelcast-smart"
NAME TYPE ADDRESS
my-hazelcast Discovery 35.184.21.18:5701
my-hazelcast-0 Member 34.121.197.134:5701
my-hazelcast-1 Member 35.238.20.134:5701
my-hazelcast-2 Member 34.30.60.128:5701
my-hazelcast-wan WAN 35.184.21.18:5701

The ADDRESS column displays the external addresses of your Hazelcast cluster and members.

Connect Hazelcast Clients to the Cluster​

Configure the Hazelcast client to connect to the cluster external address.

note

Before using CLC, it should be installed in your system. Check the installation instructions for CLC: Installing the Hazelcast CLC.

Run the following command for adding the cluster config to the CLC.

bash
clc config add hz cluster.name=dev cluster.address=<EXTERNAL-IP>

Now you can start the application.

Run the following command to fill a map.

bash
for i in {1..10};
do
clc -c hz map set --name map1 key-$i value-$i;
done

Run the following command to check the map size.

bash
clc -c hz map size --name map1

Clean Up​

To clean up the created resources remove the Hazelcast Custom Resources.

shell
kubectl delete hazelcast my-hazelcast-unisocket
kubectl delete hazelcast my-hazelcast-smart

See Also​