Friday, October 22, 2021

Run Redis Cluster on Docker

Introduction 

I was recently asked to implement Redis-Cluster  This cluster will spawn 3 master and 3 slave nodes.

Cluster

I have used the docker-compose.yml from Bitnami:
version: '2'
services:
  r01:
    image: docker.io/bitnami/redis-cluster:6.2
    hostname: r01
    container_name: r01
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
      - REDIS_NODES=r01 r02 r03 r04 r05 r06
    networks:
      - redis_net

  r02:
    image: docker.io/bitnami/redis-cluster:6.2
    hostname: r02
    container_name: r02
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
      - REDIS_NODES=r01 r02 r03 r04 r05 r06
    networks:
      - redis_net

  r03:
    image: docker.io/bitnami/redis-cluster:6.2
    hostname: r03
    container_name: r03
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
      - REDIS_NODES=r01 r02 r03 r04 r05 r06
    networks:
      - redis_net

  r04:
    image: docker.io/bitnami/redis-cluster:6.2
    hostname: r04
    container_name: r04
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
      - REDIS_NODES=r01 r02 r03 r04 r05 r06
    networks:
      - redis_net

  r05:
    image: docker.io/bitnami/redis-cluster:6.2
    hostname: r05
    container_name: r05
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
      - REDIS_NODES=r01 r02 r03 r04 r05 r06
    networks:
      - redis_net

  r06:
    image: docker.io/bitnami/redis-cluster:6.2
    hostname: r06
    container_name: r06
    depends_on:
      - r01
      - r02
      - r03
      - r04
      - r05
    environment:
      - ALLOW_EMPTY_PASSWORD=yes
      - REDIS_NODES=r01 r02 r03 r04 r05 r06
      - REDIS_CLUSTER_REPLICAS=1
      - REDIS_CLUSTER_CREATOR=yes
    networks:
      - redis_net


networks:
  redis_net:
    driver: bridge
This will launch 6 containers:

$ docker ps
CONTAINER ID   IMAGE                       COMMAND                  CREATED          STATUS          PORTS      NAMES
4a2d11bd1275   bitnami/redis-cluster:6.2   "/opt/bitnami/script…"   19 seconds ago   Up 18 seconds   6379/tcp   r06
702d098a2c9c   bitnami/redis-cluster:6.2   "/opt/bitnami/script…"   20 seconds ago   Up 19 seconds   6379/tcp   r03
878870c4e8e6   bitnami/redis-cluster:6.2   "/opt/bitnami/script…"   20 seconds ago   Up 18 seconds   6379/tcp   r02
a8d0d5302e1c   bitnami/redis-cluster:6.2   "/opt/bitnami/script…"   20 seconds ago   Up 19 seconds   6379/tcp   r01
9dd772da1aee   bitnami/redis-cluster:6.2   "/opt/bitnami/script…"   20 seconds ago   Up 18 seconds   6379/tcp   r04
263a280f180a   bitnami/redis-cluster:6.2   "/opt/bitnami/script…"   20 seconds ago   Up 18 seconds   6379/tcp   r05
We can now login to one of the containers:

$ redis-cluster % docker exec -it r01 /bin/bash
I have no name!@r01:/$
You can test the cluster settings:

$ redis-cli
127.0.0.1:6379> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:52
cluster_stats_messages_pong_sent:55
cluster_stats_messages_sent:107
cluster_stats_messages_ping_received:50
cluster_stats_messages_pong_received:52
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:107
You can check the cluster nodes status:
 
$ cluster nodes
535a218f52e42503bfe306c82dc6308b1bdb0cd5 172.21.0.3:6379@16379 myself,master - 0 1634935181000 3 connected 10923-16383
3dec2af9999f386b0212104f08badd3124a43535 172.21.0.4:6379@16379 slave 535a218f52e42503bfe306c82dc6308b1bdb0cd5 0 1634935181702 3 connected
1b554ca155d2008b319b0f3117e970a446169977 172.21.0.5:6379@16379 slave b02f0d075b12ac25c16230b0ce14f9482ec251e1 0 1634935180692 1 connected
145398bae4b7347c382fb8417fb67d35f0e77715 172.21.0.7:6379@16379 slave 9932d062c2f355aab0565758b0d686b92a20c8f9 0 1634935180000 2 connected
b02f0d075b12ac25c16230b0ce14f9482ec251e1 172.21.0.2:6379@16379 master - 0 1634935179682 1 connected 0-5460
9932d062c2f355aab0565758b0d686b92a20c8f9 172.21.0.6:6379@16379 master - 0 1634935182677 2 connected 5461-10922
You can run some set and get:

172.21.0.3:6379> set hi "hello"
-> Redirected to slot [16140] located at 172.21.0.3:6379
OK
172.21.0.3:6379> get hi
"hello"
172.21.0.3:6379>
We can test the same on other nodes as well to test if the cluster is working by  et hi  on nodes r2-r6.

Happy Coding !!!

No comments:

Post a Comment