Skip to main content
Version: 5.17

Tutorial: Replicate data between two Hazelcast clusters

Learn how to keep data in sync across two Hazelcast clusters.

Overview​

In this tutorial, you'll do the following:

  • Deploy two Hazelcast clusters.
  • Create two Hazelcast map configurations on one of the clusters.
  • Synchronize map data between the two Hazelcast clusters.

The tutorial should take approximately 10 minutes to complete.

Prerequisites​

Before you begin, make sure that you have:

Start the Hazelcast cluster​

  1. Create a secret with your Hazelcast Enterprise License.

    shell
    kubectl create secret generic hazelcast-license-key --from-literal=license-key=<hz-license-key>
  2. Create the Hazelcast clusters.

    1. Create the first cluster:

      shell
      kubectl apply -f - <<EOF
      apiVersion: hazelcast.com/v1alpha1
      kind: Hazelcast
      metadata:
      name: hazelcast-first
      spec:
      licenseKeySecretName: hazelcast-license-key
      exposeExternally:
      type: Unisocket
      discoveryServiceType: LoadBalancer
      EOF
    2. Create the second cluster:

      shell
      kubectl apply -f - <<EOF
      apiVersion: hazelcast.com/v1alpha1
      kind: Hazelcast
      metadata:
      name: hazelcast-second
      spec:
      licenseKeySecretName: hazelcast-license-key
      exposeExternally:
      type: Unisocket
      discoveryServiceType: LoadBalancer
      EOF
  3. Check the status of the clusters to make sure that both clusters are running.

    shell
    kubectl get hazelcast
    shell
    NAME STATUS MEMBERS
    hazelcast-first Running 3/3
    hazelcast-second Running 3/3
  4. Find the addresses of the clusters.

    shell
    kubectl get hazelcastendpoint --selector="app.kubernetes.io/instance in (hazelcast-first, hazelcast-second)"
    shell
    NAME TYPE ADDRESS
    hazelcast-first Discovery 34.123.9.149:5701
    hazelcast-first-wan WAN 34.123.9.149:5710
    hazelcast-second Discovery 34.16.0.16:5701
    hazelcast-second-wan WAN 34.16.0.16:5710

    The ADDRESS column displays the external addresses of the Hazelcast clusters.

Create a WAN replication configuration​

  1. Create two maps on the first cluster. In this example, the following maps are created:

    • map-1

    • map-2.

      shell
      kubectl apply -f - <<EOF
      apiVersion: hazelcast.com/v1alpha1
      kind: Map
      metadata:
      name: map-1
      spec:
      hazelcastResourceName: hazelcast-first
      ---
      apiVersion: hazelcast.com/v1alpha1
      kind: Map
      metadata:
      name: map-2
      spec:
      hazelcastResourceName: hazelcast-first
      EOF
  2. Create the configuration for WAN replication:

    • Use the first cluster as the source cluster by adding its name as a resource in the WAN replication configuration. Adding the cluster name as a resource starts WAN replication for both the maps that you created earlier.

    • Add the second cluster as the target cluster to receive the WAN replication events.

    • Apply the configuration:

      shell
      kubectl apply -f - <<EOF
      apiVersion: hazelcast.com/v1alpha1
      kind: WanReplication
      metadata:
      name: wan-replication
      spec:
      resources:
      - name: hazelcast-first
      kind: Hazelcast
      targetClusterName: dev
      endpoints: "<SECOND-CLUSTER-EXTERNAL-IP>"
      EOF

Put entries to the maps on the source cluster​

In this step, you'll fill the maps on the source cluster.

  1. Configure the Hazelcast client to connect to the source cluster, using its address.

    Open the examples folder to access all sample clients:

    shell
    cd hazelcast-platform-operator-wan-replication

    The sample code for this tutorial is in the docs/modules/ROOT/examples/operator-wan directory.

    note

    CLC must be installed in your system. See the CLC installation instructions.

    Add the first cluster config to the CLC:

    bash
    clc config add hz-1 cluster.name=dev cluster.address=<FIRST-CLUSTER-EXTERNAL-IP>
  2. Start to fill the maps.

    Run the following command for each map, using the map name as an argument to fill each map with entries. Use the map names map-1 and map-2:

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

    Again, for each map, check if the sizes are expected:

    bash
    clc -c hz-1 map size --name <MAP-NAME>

Verify the replication of map entries​

In this step, you'll check the sizes of the maps on the second, target cluster to make sure that WAN replication events have been received.

  1. Configure the Hazelcast client to connect to the second cluster, as you did in Configure the Hazelcast Client.

  2. Start the application for each map, using the map name as an argument to check the map size, and to check that WAN replication was successful. Use the map names map-1 and map-2.

    bash
    clc -c hz-2 map size --name <MAP-NAME>

Clean up​

To remove all custom resources, run the following commands:

shell
kubectl delete secret hazelcast-license-key
kubectl delete $(kubectl get wanreplications,map,hazelcast -o name)

Summary​

In this tutorial, you learned how to:

  • Deploy two Hazelcast clusters.
  • Create two Hazelcast map configurations on one of the clusters.
  • Synchronize map data between the two Hazelcast clusters.

Next steps​

See also: