One, foreword
How to use Docker to achieve the master/slave replication function of Redis. Here is a step by step start using Docker to build, which is mainly divided into several steps:
- Install Docker(I have already installed Docker environment, skip this step)
- Configure redis master
- Configure redis slave
- test
Second, basic Settings
2.1 Check whether Docker has been successfully installed
Because I have installed the Docker environment, now I just need to check whether Docker has been successfully installed and can provide services. If not, I can install it according to the method of the official website
docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., V0.9.1 -beta3)
buildx: Build with BuildKit (Docker Inc., V0.5.1 - docker)
Server:
Containers: 17
Running: 2
Paused: 0
Stopped: 15
Images: 6
Server Version: 20.103.
Storage Driver: overlay2
Backing Filesystem: xfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b
runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 4.18. 0240.101..el8_3.x86_64
Operating System: CentOS Linux 8
OSType: linux
Architecture: x86_64
CPUs: 8
Total Memory: 3.566GiB
Name: localhost.localdomain
ID: 6MAT:I5R3:3XEI:BS5X:LBYU:UXDO:MZBD:BCKD:CB6Q:RCHB:S3C4:7JMH
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0. 0. 0/ 8
Live Restore Enabled: false
Copy the code
2.2 Creating the Redis Runtime Environment
According to the introduction of up, if you directly use redis mirror, there will be some problems, so I follow his method to configure, his core idea is: create centos mirror, install Redis in centos
Create redis_master
Install an image of Centos7
docker pull centos:7
Copy the code
Create a Redis master
We need to create redis master, need to install redis in centos, then configure redis
Start the centos container
docker run -d -it -p 16379:6379 --name redis_master centos:7
After the startup is complete, enter the container
docker exec -it redis_master bash
# installed redis
yum install -y epel-release && yum install -y redis
Create redis_master configuration file
vi ~/master.conf
Copy the code
Add the following configuration files to the master.conf file
# 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 mic123
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
Copy the code
Start the Redis service
# start service
redis-server ~/master.conf
Start the client
redis-cli
Copy the code
Create redis slave
We have created the Redis master, so we need to create the Redis slave here
Create centos 7
docker pull centos:7
Start the centos container
docker run -d -it -p 26379:6379 --name redis_slave centos:7
After the startup is complete, enter the container
docker exec -it redis_slave bash
# installed redis
yum install -y epel-release && yum install -y redis
Create redis_master configuration file
vi ~/slave.conf
Copy the code
Configure the following statement into slave.conf
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 192.168.2.128 16379
masterauth mic123
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
Copy the code
Note that we need to configure the address of the master
Slaveof 192.168.2.128 16379Copy the code
Five, the test
5.1 Starting Redis Master
# Enter container
docker exec -it redis_master bash
Requirepass mic123 was configured in master.conf. This is the redis password
redis-cli -a mic123
# we add the age field
set age 12
Copy the code
5.2 Starting Redis Slave
# Enter container
docker exec -it redis_master bash
Requirepass mic123 was configured in master.conf. This is the redis password
redis-cli -a mic123
Get the age field
get age
Copy the code
conclusion
We added the age property to the master, and when we check the slave, we also have this property, so we have configured the master and slave