Master slave replication of redis using docker

Time:2021-7-20

1、 Background

Recently, we are working on a zero trust security gateway, so we need to use itRedisAs an authentication cache server, because the gateway servers are distributed in multiple clusters, it is difficult to implement cross room authentication every time; So you need to useRedisMaster slave synchronization, the process will be recorded, hoping to give students a little reference.

2、 Operation steps

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

3、 Install docker

This article mainly asked about the process of recording master-slave configuration, so I used the simplest docker method to build and install redis servicedockerThe command is as follows

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

After the command is executed, you can see the interface as shown in the figure below
Master slave replication of redis using docker

You can see some information about docker in the figure above. We need to confirm whether the installation of docker is successful and can be useddocker infoCommand, execute the command as follows

docker info 

After the command is executed, the return information is shown in the figure below
Master slave replication of redis using docker
In the figure above, you can see that the version information of docker is20.10.3, which is the latest version at present. It has been confirmed that the installation is successful.

4、 Main service configuration

Next, I need to use docker to install redis service. In practice, I found that it was abnormal to use redis image directly, so I used CentOS image to install 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 figure below
Master slave replication of redis using docker
As you can see in the figure above, I 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 figure below
Master slave replication of redis using docker

As you can see from the above figure, redis has been installed. Next, you need to create a new redis main database configuration file. The command is as follows

vi  ~/master.conf

The configuration files are as follows. Copy and paste the following configuration files 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

After pasting in the VI edit window, as shown in the figure below
Master slave replication of redis using docker

After pasting and checking, use:wq!After saving the command, you can start the redis program. The starting 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 figure below
Master slave replication of redis 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 library server again. 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 in the same host, in order not to conflict with the main library port, I set the host port26379After the startup is completed, you can enter the slave container. The command to run is as follows

docker exec -it redis_slave  bash

After executing the command, you also need to create a new redis configuration file. The running command is as follows

vi ~/redis.conf

In the configuration file, you need to add the configuration code of the slave library. The configuration example is shown below

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 copying in the configuration sample, the window is as follows
Master slave replication of redis using docker

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

Then install the 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 figure below

Master slave replication of redis using docker
As you can see in the figure above, redis from the library has also been installed. Next, use theredis-serverCommand to start the slave library server, as shown in the figure below

redis-server redis.conf

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

6、 Validation of results

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

6.1 preliminary verification

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

cat /tmp/redis.log

After the command is executed, the redis log information will be returned, as shown in the figure below
Master slave replication of redis using docker

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

6.2 synchronization check

Although the log indicates success, 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, you can operate the redis command. Here I set onetestby123123The setting command is as follows

set  test  123123

After the command is executed, the returned information is shown in the figure below
Master slave replication of redis using docker

As you can see in the figure above, redis has prompted that the key value pair has been set successfully. Next, I will go back to the terminal window of the slave library, enter the redis console, and execute the following command

redis-cli -a 123123123

After the command is executed, it can be passedkeysCommand to view the key value pair of the current slave library. The command is as follows

keys *

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

Master slave replication of redis using docker

You can see it in the picture abovetestThis data has been successfully copied.


Author: Tang Qingsong

Wechat: songboy8888

Date: April 11, 2021
Master slave replication of redis using docker