Skip to main content
Version: 0.13.0

Mirroring Two Local Clusters

This tutorial is a demonstration of mirroring between two clusters running on Docker.

Home and Remote

To understand mirroring, we need to understand what is a Home and a Remote cluster:

  • Home cluster is the target cluster that will receive and consume data.
  • Remote cluster is the source cluster that will send data.

Dockerfile and Docker-compose

Copy the docker-compose.yaml and Dockerfile to a directory.

docker-compose.yaml:

version: "3"
services:
  ## Home Cluster
  sc-home:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: sc-home
    environment:
      - RUST_LOG=info
    entrypoint: >
      /bin/sh -c "
      fluvio profile add docker 0.0.0.0:9003 docker;
      bash -c 'sleep 5 ; fluvio cluster spu register --id 5001 -p spu-home:9010 --private-server spu-home:9011' &
      ./fluvio-run sc --local /fluvio/metadata
      "
    volumes:
      - ./home/fluvio-metadata:/fluvio/metadata
      - ./shared:/shared
  spu-home:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: spu-home
    hostname: spu-home
    volumes:
      - ./home/fluvio-data:/fluvio/data
    environment:
      - RUST_LOG=info
    command: "./fluvio-run spu -i 5001 -p spu-home:9010 -v spu-home:9011 --sc-addr sc-home:9004 --log-base-dir /fluvio/data"
    depends_on:
      - sc-home
  ## Remote Cluster        
  sc-remote:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: sc-remote
    hostname: sc-remote
    environment:
      - RUST_LOG=info
    entrypoint: >
      /bin/sh -c "
      fluvio profile add docker 0.0.0.0:9003 docker;
      bash -c 'sleep 5 ; fluvio cluster spu register --id 5001 -p spu-remote:9010 --private-server spu-remote:9011' &
      ./fluvio-run sc --local /fluvio/metadata
      "
    volumes:
      - ./remote/fluvio-metadata:/fluvio/metadata
      - ./shared:/shared
  spu-remote:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: spu-remote
    hostname: spu-remote
    volumes:
      - ./remote/fluvio-data:/fluvio/data
    environment:
      - RUST_LOG=info
    command: "./fluvio-run spu -i 5001 -p spu-remote:9010 -v spu-remote:9011 --sc-addr sc-remote:9004 --log-base-dir /fluvio/data"
    depends_on:
      - sc-remote

Dockerfile:

FROM infinyon/fluvio:latest

RUN apk update
RUN apk add curl unzip bash
RUN curl -fsS https://hub.infinyon.cloud/install/install.sh?ctx=dc | VERSION='latest' bash

RUN mv /root/.fluvio/bin/fluvio /usr/local/bin/fluvio

Start Docker Containers with:

docker-compose up -d

Register Remote clusters on the Home

First, enter into the home cluster shell:

docker exec -it sc-home /bin/sh

Use the remote CLI to register the remote clusters with the home cluster:

fluvio remote register docker-remote

List remote clusters to check their status:

fluvio remote list

It should show the following:

  REMOTE           SC STATUS  SPU STATUS  LAST SEEN  ERRORS
  docker-remote    Waiting    Waiting     -          -

Create the mirror topic

Mirror topics on the home clusters has multiple partitions, where each partition has a 1-to-1 relationship with the remote cluster.

Create the mirror topic, then add the remote to it:

fluvio topic create mirror-topic  --mirror
fluvio topic add-mirror mirror-topic docker-remote

or apply a json file with an array of remotes that you want to assign when creating a topic:

echo '["docker-remote"]' > assignment_file.json
fluvio topic create mirror-topic --mirror-apply assignment_file.json

List partitions to check the assignment:

fluvio partition list

It should display the partition that we created:

  TOPIC         PARTITION  LEADER  MIRROR        REPLICAS  RESOLUTION  SIZE  HW  LEO  LRS  FOLLOWER OFFSETS
  mirror-topic  0          5001    docker-remote []        Online      0 B   0   0    0    0                 []

Generate Metadata for Remote Clusters

Each remote cluster requires a unique metadata file that gives the remote cluster the information to connect to the home cluster and the topic/mirror where the data is synchronized.

Generate a metadata file for the remote:

fluvio remote export docker-remote --file /shared/docker-remote.json --public-endpoint sc-home:9003

Connect to the Home Cluster from the Remote

Now, connect to the remote shell in another terminal:

docker exec -it sc-remote /bin/sh

Then, we'll use the metadata file to connect to home:

fluvio home connect --file /shared/docker-remote.json

Let's check the partitions:

fluvio partition list

The remote device should show the following partition::

  TOPIC       PARTITION  LEADER  MIRROR                     REPLICAS  RESOLUTION  SIZE  HW  LEO  LRS  FOLLOWER OFFSETS
  mirror-topic  0          5001  home:5001:spu-home:9010    []        Online      0 B   0   0    0    0                 []

Also, check the home status with:

fluvio home status

It should show the following:

  REMOTE  SC STATUS  SPU STATUS  LAST SEEN  ERRORS
  home    Connected  Connected   1s         -

Producing and Consuming on Mirroring

Let's produce on the remote and consume from the home cluster.

Produce to remote cluster

On remote shell, produce with:

fluvio produce mirror-topic
> A
Ok!
> B
Ok!

Consume from Home cluster

On home shell, consume with:

fluvio consume mirror-topic --mirror docker-remote -B
A
B

🎉 Congratulations! You have successfully tested mirroring.