Setup master slave in Redis by using Docker

Setup master slave in Redis by using Docker

File docker-compose

version: '3'

services:
  redis-master:
    image: redis
    container_name: redis-master
    ports:
      - "6279:6379"

  redis-slave-1:
    image: redis
    container_name: redis-slave-1
    ports:
      - "6179:6379"

  redis-slave-2:
    image: redis
    container_name: redis-slave-2
    ports:
      - "6079:6379"

We will create three containers master and two slave with ports: 6279, 6179 and 6079. All containers will be exposed port by gate 6379.

-> Running the command "docker-compose -f docker-compose.yml up" to pull images and start all containers.

docker-compose

-> We will exec into redis-slave-1 and redis-slave-2 and run the command "slaveof redis-master 6379". And continue, we enter the command "info replication" to check the status of Redis slave that connect to Redis-master.

# redis-cli
127.0.0.1:6379> slaveof redis-master 6379
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:redis-master
master_port:6379
master_link_status:up
master_last_io_seconds_ago:7
master_sync_in_progress:0
slave_read_repl_offset:14
slave_repl_offset:14
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:76199e2ac4b98caee734d87f52db6844b60ed264
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:14
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:14
127.0.0.1:6379>
Last login: Mon Feb  5 23:02:51 on ttys008
➜  ~ docker exec -it 36b5113c9fff /bin/sh
# redis-cli
127.0.0.1:6379> slaveof redis-master 6379
OK
127.0.0.1:6379> info replication
# Replication
role:slave
master_host:redis-master
master_port:6379
master_link_status:up
master_last_io_seconds_ago:5
master_sync_in_progress:0
slave_read_repl_offset:28
slave_repl_offset:28
slave_priority:100
slave_read_only:1
replica_announced:1
connected_slaves:0
master_failover_state:no-failover
master_replid:76199e2ac4b98caee734d87f52db6844b60ed264
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:28
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:29
repl_backlog_histlen:0
127.0.0.1:6379>

docker-compose

This is status of Redis master that be received the configuration by Redis slave one and Redis slave two.

➜  ~ docker exec -it 4c14c8f23a39 /bin/sh
# redis-cli
127.0.0.1:6379> info replication
# Replication
role:master
connected_slaves:2
slave0:ip=172.21.0.2,port=6379,state=online,offset=308,lag=1
slave1:ip=172.21.0.3,port=6379,state=online,offset=308,lag=1
master_failover_state:no-failover
master_replid:76199e2ac4b98caee734d87f52db6844b60ed264
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:308
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:308
127.0.0.1:6379>

docker-compose

-> In the Redis master, we will run the command "set test 123" and check the key "test" in two Redis slaves. This is the result, and all Redis slaves are synced successfully.

docker-compose