Practice of redis master-slave replication using docker

Time:2021-10-14

1、 Background

Recently, we are working on a zero trust security gateway, which needs to be usedRedisAs an authentication cache server, because the gateway server is distributed in multiple clusters, cross machine room authentication is not realized every time; So you need to useRedisMaster slave synchronization, record the process, and hope to give some reference to the students who need it.

2、 Operation steps

  1. Install docker
  2. Master service configuration
  3. From service configuration
  4. Verify synchronization effect

3、 Install docker

This article mainly asks about the process of recording master-slave configuration, so I use the simplest docker method to build and install redis servicesdockerThe commands for are as follows

curl -sSL https://get.daocloud.io/docker | sh

After the command is executed, you can see the interface shown in the following figure
Practice of redis master-slave replication using docker

You can see some information about docker in the figure above. We need to confirm whether the docker is installed successfully and can be useddocker infoCommand to view and execute the command as shown below

docker info 

After the command is executed, the returned information is shown in the following figure
Practice of redis master-slave replication using docker
In the figure above, you can see that the version information of docker is20.10.3, this is the latest version at present. It has been confirmed that the installation is successful.

4、 Master service configuration

Next, I need to use docker to install redis service. In practice, I found some exceptions in using redis image directly, so I used CentOS image and installed redis in the container. The command to run the container is as follows

docker run -d -it -p 16379:6379  --name  redis_master  centos:7

Enter the container after the command is executed. The command to enter the container is as follows:

docker exec -it redis_master  bash

After the command is executed, the returned information is shown in the following figure
Practice of redis master-slave replication using docker
In the figure above, you can see that you have successfully entered the container. Next, I need to install redis in the container. The command to install redis is as follows

yum install -y epel-release  && yum install -y redis

After the command is executed, the returned information is shown in the following figure
Practice of redis master-slave replication using docker

As can be seen from the above figure, redis has been installed. Next, you need to create a new redis master database configuration file. The execution commands are as follows

vi  ~/master.conf

The configuration file is as follows. Copy and paste the following configuration file into the VI edit window.

#bind 0.0.0.0
protected-mode yes
port 6379
tcp-backlog 511
unixsocket /tmp/redis_auth.sock
unixsocketperm 777
timeout 0
tcp-keepalive 300
daemonize yes
supervised auto
pidfile /var/run/redis_auth.pid
loglevel debug
logfile /tmp/redis_auth.log
databases 16
save ""
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
requirepass 123123123
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly yes
appendfilename "funfe.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 512mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
hz 10
aof-rewrite-incremental-fsync yes

Paste in the VI edit window, as shown in the following figure
Practice of redis master-slave replication using docker

After pasting and checking, use:wq!Save the command, and then you can start the redis program. The started command is as follows

redis-server `/redis.conf

In the above startup command, you need to specify the configuration file path, as shown in the following figure
Practice of redis master-slave replication using docker

In the figure above, you can see that the redis service has been started.

5、 From service configuration

Next, I need to start a redis slave server again, and the command to run the container is as follows

docker run -d -it -p 26379:6379  --name  redis_slave  centos:7

In the above command, because I am on the same host, in order not to conflict with the primary library port, I will use the host port26379, after startup, you can enter the slave library container, and the running commands are as follows

docker exec -it redis_slave  bash

After the command is executed, you also need to create a redis configuration file. The running commands are as follows

vi ~/redis.conf

In the configuration file, you need to add the configuration code from the library. The configuration example is as follows

bind 127.0.0.1
protected-mode yes
port 6379
tcp-backlog 511
unixsocket /tmp/redis_auth.sock
unixsocketperm 777
timeout 0
tcp-keepalive 300
daemonize yes
supervised auto
pidfile /var/run/redis_6379.pid
loglevel notice
logfile /tmp/redis.log
databases 16
save ""
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /var/lib/redis
slaveof 172.23.193.148 16379
masterauth 123123123
slave-serve-stale-data yes
slave-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly yes
appendfilename "funfe.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 512mb
aof-load-truncated yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
activerehashing yes
hz 10
aof-rewrite-incremental-fsync yes

After the configuration sample is copied in, the window is as follows
Practice of redis master-slave replication using docker

After pasting and checking, use:wq!Save the command, and then you can start the redis program. The started command is as follows

Then install redis service. The installation command is as follows:

yum install -y epel-release  && yum install -y redis

After the command is executed, the returned information is shown in the following figure

Practice of redis master-slave replication using docker
In the figure above, you can see that redis from the library has also been installed. Next, useredis-serverThe command starts the slave library server, as shown in the following figure

redis-server redis.conf

After the command is executed, the returned information is shown in the following figure
Practice of redis master-slave replication using docker
In the figure above, you can see that redis has been started, and then you can verify the effect

6、 Result verification

The verification method is mainly to set data in the master database and observe whether the slave database will also be updated synchronously;

6.1 preliminary verification

However, this operation is a little troublesome. We’d better check the startup log from the library first. The command to view the startup log is as follows

cat /tmp/redis.log

After the command is executed, redis log information will be returned, as shown in the following figure
Practice of redis master-slave replication using docker

From the log information in the figure above, you can see that the slave database has successfully copied the master database information to the local database.

6.2 synchronization check

Although the log indicates that the synchronization is successful, whether the master-slave synchronization is successful or not depends on the actual effect. Here, I go back to the terminal window of the master server, and then enter the redis command console. The command to enter the console is as follows

redis-cli -a 123123123

After the command is executed, the redis command can be operated. Here I set onetestby123123The setting command is as follows

set  test  123123

After the command is executed, the returned information is shown in the following figure
Practice of redis master-slave replication using docker

In the figure above, you can see that redis has prompted that the key value pair has been set successfully. Next, I continue to return to the terminal window of the slave library, and then enter redis console to execute the command as shown below

redis-cli -a 123123123

After the command is executed, you can use thekeysCommand to view the key value pairs of the current slave library. The command is as follows

keys *

After the command is executed, the returned information is shown in the following figure

Practice of redis master-slave replication using docker

You can see in the above figuretestThis data has been successfully copied.


Author: Tang Qingsong

Wechat: songboy8888

Date: April 11, 2021

This work adoptsCC agreement, reprint must indicate the author and the link to this article