Implementation of docker compose Deployment Master Slave Replication

  • 2021-11-02 03:45:35
  • OfStack

Directory configuration resolution
Service building
Directory structure
Compose File
Instance configuration
Startup service
Test

Limited by Redis's single-point performance and our inherent need for data backup, Redis provides master-slave replication services.

This paper records the Redis service with one master and two slaves built through docker compose.

Configuration resolution


################################# REPLICATION #################################

#  " Slave "Connection  Master  Configuration of 
# slaveof 172.25.0.101 6379

#  " Slave "Read-only mode 
# slave-read-only yes

#  " Slave "Password 
# masterauth <master-password>

#  " Slave "Whether response to queries is allowed during replication, which may return dirty data 
# slave-serve-stale-data yes

#  " Slave " Slave  To be promoted to  Master  Priority of, effective only in Sentinel mode 
# slave-priority 100

#  " Slave " Slave  Toward  Master  Report your own  IP
# slave-announce-ip 5.5.5.5

#  " Slave " Slave  Toward  Master  Report on your own port 
# slave-announce-port 1234

#  " Slave " Slave ping Master  Time interval of 
# repl-ping-slave-period 10

#  " Master/Slave "Timeout 
# repl-timeout 60

#  " Master " Diskless  Is what will be copied directly  RDB  File writes to  Socket  Instead of storing it to disk first 
repl-diskless-sync no

#  " Master "If it is turned on  Diskless Waits for the specified second before replicating so that more clients can connect and transfer in parallel during the window period 
# repl-diskless-sync-delay 5

#  " Master "Is it turned on  Nagle  Algorithm, which can reduce traffic occupation, but will synchronize slowly 
repl-disable-tcp-nodelay no

#  " Master "The size of the ring buffer log, given  Slave  Reconnect after disconnection to avoid full replication, default  1mb
# repl-backlog-size 1mb

#  " Master "When  Master  Disconnected all  Slave  After the specified time, Master  Will be emptied  backlog
# repl-backlog-ttl 3600

#  " Master "When less than a specified number  Slave  When connected, Master  Reject all writes 
# min-slaves-to-write 3

#  " Master "When the delay is greater than the specified number of seconds, Master  Reject all writes 
# min-slaves-max-lag 10

Service building

Directory structure


replication/
 --  docker-compose.yml
 --  master
 The     --  data
 The     Off-  redis.conf
 --  slave1
 The     --  data
 The     Off-  redis.conf
 Off-  slave2
     --  data
     Off-  redis.conf

Compose File

A subnet is defined, which is easy to operate and exposes ports 6371 (Master), 6372 and 6373.


version: "3"

networks:
  redis-replication:
    driver: bridge
    ipam:
      config:
        - subnet: 172.25.0.0/24

services:
  master:
    image: redis
    container_name: redis-master
    ports:
      - "6371:6379"
    volumes:
      - "./master/redis.conf:/etc/redis.conf"
      - "./master/data:/data"
    command: ["redis-server", "/etc/redis.conf"]
    restart: always
    networks:
      redis-replication:
        ipv4_address: 172.25.0.101

  slave1:
    image: redis
    container_name: redis-slave-1
    ports:
      - "6372:6379"
    volumes:
      - "./slave1/redis.conf:/etc/redis.conf"
      - "./slave1/data:/data"
    command: ["redis-server", "/etc/redis.conf"]
    restart: always
    networks:
      redis-replication:
        ipv4_address: 172.25.0.102

  slave2:
    image: redis
    container_name: redis-slave-2
    ports:
      - "6373:6379"
    volumes:
      - "./slave2/redis.conf:/etc/redis.conf"
      - "./slave2/data:/data"
    command: ["redis-server", "/etc/redis.conf"]
    restart: always
    networks:
      redis-replication:
        ipv4_address: 172.25.0.103

Instance configuration

Master:

Basically no configuration, the simplest is to specify a port.


port 6379
protected-mode no

repl-diskless-sync no
repl-disable-tcp-nodelay no

Slave:

It is enough to keep the configuration of the instance 1, because subnets are defined and there are no port conflicts.


port 6379
protected-mode no

slaveof 172.25.0.101 6379
slave-read-only yes
slave-serve-stale-data yes

Startup service


ocker-compose up -d
Creating network "replication_redis-replication" with driver "bridge"
Creating redis-slave-1 ... done
Creating redis-master  ... done
Creating redis-slave-2 ... done

Looking at the Master log, you can see that two replication requests for Slave were accepted:

1:M 18 Aug 2021 15:50:31.772 * Replica 172.25.0.102:6379 asks for synchronization
1:M 18 Aug 2021 15:50:31.772 * Full resync requested by replica 172.25.0.102:6379
1:M 18 Aug 2021 15:50:31.772 * Replication backlog created, my new replication IDs are '5d27746f14ee9be9694d794f96de6ba14a669dd1' and '0000000000000000000000000000000000000000'
1:M 18 Aug 2021 15:50:31.772 * Starting BGSAVE for SYNC with target: disk
1:M 18 Aug 2021 15:50:31.773 * Background saving started by pid 19
19:C 18 Aug 2021 15:50:31.777 * DB saved on disk
19:C 18 Aug 2021 15:50:31.777 * RDB: 0 MB of memory used by copy-on-write
1:M 18 Aug 2021 15:50:31.822 * Background saving terminated with success
1:M 18 Aug 2021 15:50:31.823 * Synchronization with replica 172.25.0.102:6379 succeeded
1:M 18 Aug 2021 15:50:32.170 * Replica 172.25.0.103:6379 asks for synchronization
1:M 18 Aug 2021 15:50:32.170 * Full resync requested by replica 172.25.0.103:6379
1:M 18 Aug 2021 15:50:32.170 * Starting BGSAVE for SYNC with target: disk
1:M 18 Aug 2021 15:50:32.171 * Background saving started by pid 20
20:C 18 Aug 2021 15:50:32.175 * DB saved on disk
20:C 18 Aug 2021 15:50:32.175 * RDB: 0 MB of memory used by copy-on-write
1:M 18 Aug 2021 15:50:32.225 * Background saving terminated with success
1:M 18 Aug 2021 15:50:32.226 * Synchronization with replica 172.25.0.103:6379 succeeded

Looking at the Slave log, you can see the whole process of connection establishment:

1:S 18 Aug 2021 15:50:31.771 * Connecting to MASTER 172.25.0.101:6379
1:S 18 Aug 2021 15:50:31.771 * MASTER < - > REPLICA sync started
1:S 18 Aug 2021 15:50:31.771 * Non blocking connect for SYNC fired the event.
1:S 18 Aug 2021 15:50:31.771 * Master replied to PING, replication can continue...
1:S 18 Aug 2021 15:50:31.772 * Partial resynchronization not possible (no cached master)
1:S 18 Aug 2021 15:50:31.773 * Full resync from master: 5d27746f14ee9be9694d794f96de6ba14a669dd1:0
1:S 18 Aug 2021 15:50:31.823 * MASTER < - > REPLICA sync: receiving 175 bytes from master to disk
1:S 18 Aug 2021 15:50:31.823 * MASTER < - > REPLICA sync: Flushing old data
1:S 18 Aug 2021 15:50:31.823 * MASTER < - > REPLICA sync: Loading DB in memory
1:S 18 Aug 2021 15:50:31.828 * Loading RDB produced by version 6.2.5
1:S 18 Aug 2021 15:50:31.828 * RDB age 0 seconds
1:S 18 Aug 2021 15:50:31.828 * RDB memory usage when created 1.83 Mb
1:S 18 Aug 2021 15:50:31.829 * MASTER < - > REPLICA sync: Finished with success

Test

Log in to Master and try to write to the new Key.


127.0.0.1:6371> set hello world
OK

Log in to Slave to see if you can read:


127.0.0.1:6372> get hello
"world"

Slave Attempt to write:


127.0.0.1:6372> set hello redis
(error) READONLY You can't write against a read only replica.

Related articles: