Implementation of master-slave replication in docker compose deployment

Implementation of master-slave replication in docker compose deployment

Due to the limitations of Redis' single-point performance and our inherent need for data backup, Redis provides master-slave replication services.

This article records the construction of a master-dual slave Redis service through docker compose.

Configuration parsing

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

# [Slave] Configuration of connecting to Master # slaveof 172.25.0.101 6379

# 【Slave】Read-only mode# slave-read-only yes

# 【Slave】Password# masterauth <master-password>

# [Slave] Is it allowed to respond to queries during replication? Dirty data may be returned. # slave-serve-stale-data yes

# [Slave] The priority of a slave to be promoted to a master. This is only effective in sentinel mode. # slave-priority 100

# 【Slave】Slave's own IP reported to the Master
# slave-announce-ip 5.5.5.5

# 【Slave】Slave's own port reported to the Master# slave-announce-port 1234

# 【Slave】The interval between Slave pinging Master# repl-ping-slave-period 10

# 【Master/Slave】Timeout# repl-timeout 60

# [Master] Diskless means directly writing the RDB file to be copied to the Socket without storing it on the disk first repl-diskless-sync no

# [Master] If Diskless is enabled, it will wait for the specified number of seconds before replicating, so that more clients can connect within the window period and transmit in parallel. # repl-diskless-sync-delay 5

# [Master] Whether to enable the Nagle algorithm, which can reduce traffic usage but will make synchronization slower repl-disable-tcp-nodelay no

# [Master] The size of the ring buffer log, which is used to reconnect after the Slave is disconnected to avoid full replication. The default value is 1mb
# repl-backlog-size 1mb

# 【Master】When the Master disconnects all Slaves for a specified period of time, the Master will clear the backlog
# repl-backlog-ttl 3600

# [Master] When the number of slave connections is less than the specified number, the Master rejects all write operations # min-slaves-to-write 3

# [Master] When the delay is higher than the specified number of seconds, the Master rejects all write operations # min-slaves-max-lag 10

Service Construction

Directory Structure

replication/
├── docker-compose.yml
├── master
│ ├── data
│ └── redis.conf
├── slave1
│ ├── data
│ └── redis.conf
└── slave2
    ├── data
    └── redis.conf

Compose File

A subnet is defined for easy operation, and ports 6371 (Master), 6372, and 6373 are exposed.

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 is required, the simplest thing is to specify a port.

port 6379
protected-mode no

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

Slave:

The instance configurations just need to remain consistent, because the 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

Start the 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

Checking the Master log, you can see that the replication requests from two Slaves 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 '0 ...
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

Check the Slave log to see the entire 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 the Master and try to write a new key.

127.0.0.1:6371> set hello world
OK

Log in to the slave and check whether it can read:

127.0.0.1:6372> get hello
"world"

The slave attempts a write operation:

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

This is the end of this article about the implementation of master-slave replication in docker compose deployment. For more relevant docker compose master-slave replication content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Detailed example of Docker Compose's rapid deployment of multi-container services
  • Implementation of Docker Compose multi-container deployment
  • Using Docker Compose to build and deploy ElasticSearch configuration process

<<:  A brief discussion on how to use slots in Vue

>>:  Mysql auto-increment primary key id is not processed in this way

Recommend

Coexistence of python2 and python3 under centos7 system

The first step is to check the version number and...

Nginx anti-crawler strategy to prevent UA from crawling websites

Added anti-crawler policy file: vim /usr/www/serv...

Summary of JavaScript Timer Types

Table of contents 1.setInterval() 2.setTimeout() ...

A brief analysis of mysql index

A database index is a data structure whose purpos...

Vue uses WebSocket to simulate the chat function

The effect shows that two browsers simulate each ...

js implements a simple countdown

This article example shares the specific code of ...

Rules for using mysql joint indexes

A joint index is also called a composite index. F...

How to modify the location of data files in CentOS6.7 mysql5.6.33

Problem: The partition where MySQL stores data fi...

About the problem of vertical centering of img and span in div

As shown below: XML/HTML CodeCopy content to clip...

A brief understanding of the differences between MySQL InnoDB and MyISAM

Preface MySQL supports many types of tables (i.e....

JavaScript to dynamically load and delete tables

This article shares the specific code of JavaScri...

Implementation of Docker Compose multi-container deployment

Table of contents 1. WordPress deployment 1. Prep...