Skip to main content
Version: 6.0-SNAPSHOT

Deploy Hazelcast using Kubernetes

note

Hazelcast now offers the Hazelcast Platform Operator. We recommend using this operator when working with Hazelcast in Kubernetes. For more info, see Connect External Hazelcast Client to Kubernetes.

What you'll learn​

In this tutorial, you will deploy a Hazelcast cluster to Kubernetes and connect it to a Kubernetes application.

Before you begin​

Deploy Hazelcast cluster​

There are different ways of deploying Hazelcast to Kubernetes. For production environments, we recommend using Hazelcast Platform Operator or Helm because these methods provide the complete Hazelcast experience, including the following aspects:

  • Automated deployment including the Hazelcast Management Center tool
  • Automated lifecycle operations (scaling, upgrades) to avoid data loss
  • Role-based access control
bash
helm repo add hazelcast https://hazelcast-charts.s3.amazonaws.com/
helm repo update
helm install hz-hazelcast hazelcast/hazelcast

You can check that the Hazelcast cluster is up and running.

bash
kubectl logs hz-hazelcast-0
...
Members {size:3, ver:3} [
Member [10.216.6.7]:5701 - 6d2100e0-8dcf-4e7c-ab40-8e98e23475e3 this
Member [10.216.5.6]:5701 - 5ab4d554-fd7d-4929-8475-0ddf79a21076
Member [10.216.8.6]:5701 - 7f7dd5f4-e732-4575-89d6-a6e823da38da
]

At this point you have started a Hazelcast cluster with 3 members. It is exposed with a service called hz-hazelcast.

Configure Hazelcast client​

Use Hazelcast client to connect to the running Hazelcast cluster from your applications.

To use Hazelcast client, add Hazelcast client dependency to your application.

pom.xml
xml
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>${hazelcast.version}</version>
</dependency>

Configure the Hazelcast client in your application to connect to the hz-hazelcast service.

java
ClientConfig config = new ClientConfig();
config.getNetworkConfig().addAddress("hz-hazelcast");

Your application is now configured to automatically connect to the Hazelcast cluster once it's deployed to Kubernetes.

Deploy client application​

To deploy your application to Kubernetes cluster, you need first to dockerize it.

bash
docker build -t hazelcastguides/hazelcast-client java
note

If you use a remote Kubernetes cluster and you want to build your own Docker image then make sure that you also push your Docker image into the Docker registry.

bash
kubectl run hazelcast-client --image=hazelcastguides/hazelcast-client

After a moment, check application logs to see it running in Kubernetes.

bash
kubectl logs hazelcast-client
...
Members {size:3, ver:3} [
Member [10.216.6.7]:5701 - 6d2100e0-8dcf-4e7c-ab40-8e98e23475e3 this
Member [10.216.5.6]:5701 - 5ab4d554-fd7d-4929-8475-0ddf79a21076
Member [10.216.8.6]:5701 - 7f7dd5f4-e732-4575-89d6-a6e823da38da
]
...
Successful connection!
Starting to fill the map with random entries.
Current map size: 71754
Current map size: 71758
Current map size: 71782
Current map size: 71792
...

To remove the client application, execute the following command.

bash
kubectl delete pod hazelcast-client

Tear down Hazelcast cluster​

To delete Hazelcast cluster, run the following commands.

bash
helm uninstall hz-hazelcast

See Also​