Redis cluster construction – master-slave replication mode

*Note: In the fourth specific operation, it is recommended to allocate IP independently, so that the redis IP will not change after the server is restarted, otherwise the host IP in your slave conf will become invalid

1. Create a custom network type

docker network create --subnet=172.10.0.0/16 haveyb-network

2. Create redis-master container

docker run -itd --name redis-master --net haveyb-network -p 6380:6379 --ip 172.10.0.2
Parameter explanation:
-i: Run the container in interactive mode, usually used together with -t
-t: Reassign a pseudo input terminal to the container, usually used together with -i
-d: Run the container in the background and return the container ID;
--name: Name the created container
--net: Specify the network mode (here specify the custom network mode just created)
-p: port mapping, the format is: host (host) port: container port
--ip: Set a fixed IP for the container
1. The concept of redis master-slave synchronization

Reds master-slave synchronization is also called master-slave replication. After the host data is updated, it is automatically synchronized to the master/slaver mechanism of the standby machine according to the configuration and policy. The Master is mainly for writing, and the Slave is mainly for reading. Data replication is one-way, only from the master server to the slave server.

2. Redis master-slave synchronization principle

1: If you configure a slave for the master, regardless of whether the slave connects to the master for the first time, it will send a SYNC command (command before redis version 2.8) to the master to request data replication.
2: After the master receives the SYNC command, it will perform data persistence in the background and generate the latest RDB snapshot file through bgsave. During the persistence period, the master will continue to receive requests from the client, and it will cache these requests that may modify the data set in memory. middle. When persistence is completed, the master will send the RDB file data set to the slave.
3: The slave will persist the received data to generate rdb, and then load it into the memory.
4: Then, the master sends the command previously cached in the memory to the slave.
5: When the connection between the master and the slave is disconnected for some reason, the slave can automatically reconnect to the master. If the master receives multiple concurrent connection requests from the slave, it will only persist once, not once for each connection. , and then send this persistent data to multiple concurrently connected slaves.
6: When the master and slave are disconnected and reconnected, the entire data will generally be copied. However, starting from redis version 2.8, partial replication is supported after the master and slave are disconnected and reconnected.

Partial copy of data
Starting from version 2.8, slave and master can only perform partial data replication after the network connection is disconnected and reconnected. The master will create a cache queue for copying data in its memory to cache the data for the most recent period. The master and all its slaves maintain the copied data subscript offset and the master’s process id. Therefore, when the network connection is disconnected, Afterwards, the slave will request the master to continue the unfinished replication, starting from the recorded data index. If the master process ID changes, or the slave node data offset is too old and is no longer in the master’s cache queue, then a full data copy will be performed. Starting from version 2.8, redis uses the command PSYNC that can support partial data replication to synchronize data with the master.

3. Redis master-slave synchronization flow chart

Master-slave replication (full replication) flow chart:

Master-slave replication (partial replication) flow chart:

4. Detailed steps to build redis master-slave:

Pull redis image

sudo docker pull redis

Create the required folders to map the corresponding file paths of the container

mkdir -p /mydata/redis/master/data
touch /mydata/redis/master/redis.conf

mkdir -p /mydata/redis/slavefirst/data
touch /mydata/redis/slavefirst/redis.conf

mkdir -p /mydata/redis/slavesecond/data
touch /mydata/redis/slavesecond/redis.conf

Run the container and specify the mounting path

docker run -p 16379:6379 --name master --restart=always \
-v /mydata/redis/master/conf/redis.conf:/etc/redis/redis.conf \
-v /mydata/redis/master/data:/data \
-d redis redis-server /etc/redis/redis.conf

docker run -p 26379:6379 --name firstslave --restart=always \
-v /mydata/redis/slavefirst/conf/redis.conf:/etc/redis/redis.conf \
-v /mydata/redis/slavefirst/data:/data \
-d redis redis-server /etc/redis/redis.conf

docker run -p 36379:6379 --name secondslave --restart=always \
-v /mydata/redis/slavesecond/conf/redis.conf:/etc/redis/redis.conf \
-v /mydata/redis/slavesecond/data:/data \
-d redis redis-server /etc/redis/redis.conf 

Implement master-slave synchronization related configuration modifications – permanent
This construction mode will not be reset after restarting the container. Consider setting it up based on personal circumstances.

View node information:

#Enter the slave node service
docker exec -it [CONTAINER ID] redis-cli

#Set the node as a slave node of the master node,
Note: The IP and PORT used here are internal to the container and can be viewed through the docker inspect [CONTAINER ID] command.
172.17.0.2: IP exposed by the master node internally 6379: Port exposed by the master node
slaveof 172.17.0.2 6379

If you want to reset it later, you can use the following command
Reset slave node to master node: slaveof no one

Modify redis-master configuration file

#Add the following content to redis.conf

# Allow access from all ip addresses
bind 0.0.0.0

# Run as a daemon, that is, the remote connection window is closed, and redis is still running. You must comment when using the container, otherwise starting the container will fail.
# daemonize yes

# Setup requires a password to access
requirepass root

#Set redis persistence, the default is no
appendonly yes

# Set the password of the master node (you need to configure this item when the master node has set the requirepass configuration or when you need to set up the sentinel mode, because if the master node goes down and restarts, it will # # be replaced by the slave node, this Sometimes you need to connect to the new master node elected by sentinel)
# masterauth root

Modify the redis-slave configuration file (the configuration files in the slave nodes are consistent, if there are several slave nodes, modify the configuration files of several slave nodes)

# Allow access from all ip addresses
bind 0.0.0.0

# Run as a daemon, that is, the remote connection window is closed, and redis is still running. You must comment when using the container, otherwise starting the container will fail.
# daemonize yes

# Setup requires a password to access
requirepass root

#Set redis persistence, the default is no
appendonly yes

#Set the password of the master node (this item needs to be configured when the master node has requirepass configuration set)
masterauth root

# IP and PORT of the master node in master-slave mode (this should be determined according to the actual situation, I use the IP and port inside the container)
#The internal ip and port of the container can be viewed using docker inspect image id
# You can also use the name of the container here. When using the container name, you need to use the --link parameter when building the container.
# You can also use physical addresses here. When using physical addresses, you need to use the --net parameter when building the container.
replicaof 172.17.0.3 6379

Restart the redis master node and slave node containers

sudo docker restart redisname

Verify whether the establishment is successful. Here we take a look at the master node.

#Enter the redis-master master node
docker exec -it redis-master redis-cli
#Enter password to verify root
auth root
#View the role to which the instance belongs
role
View master-slave replication information
info replication

Detailed tutorial on setting up Redis master-slave synchronization with Docker_docker redis master-slave-CSDN blog

Docker implements Redis master-slave configuration and sentinel mode_yygr’s blog-CSDN blog