In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
Based on how to build redis master-slave cluster in docker environment, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
1 preface
The author introduces how to build a Redis master-slave replication cluster based on docker, how to make the redis master-slave cluster run well on the docker container, and how to switch between the redis master and slave in case of failure under the container.
2 Redis master-slave replication characteristics
L uses asynchronous replication.
L supports one master and multiple slaves. A master can have more than one slave.
L Slave can accept connections from other slaves. In addition to being able to connect multiple slaves to the same master, slaves can also connect to other slaves in a similar cascading manner.
L Redis replication is non-blocking on the master side. This means that master can continue to process requests (from the client) when slave is performing the first synchronization.
L Redis replication can be used for capacity expansion, such as using multiple slaves for read-only queries, or just for data redundancy.
L Redis replication may also be used to prevent master from writing the entire dataset to the hard disk. This technique requires configuring master's redis.conf file and then connecting to a slave, which must be configured to save from time to time, or AOF enabled.
3 advantages of Redis containerization
1. Save the installation and configuration of redis master / slave
2. The redis container can be started in seconds
3. It can quickly expand redis slave nodes.
4. It can quickly switch between redis master and slave.
5. Strong transferability.
4 Redis master-slave replication architecture
5 Redis Master-Slave Cluster Building 5.1 redis Node Information
Here, one master and two slaves are used to test.
Serial number
Node name
IP address
Use
one
Redis-master
192.168.56.108
Redis master node, node data can be written and read
two
Redis-slave01
192.168.56.109
Redis slave node 1, node data is readable and unwritable
three
Redis-slave02
192.168.56.110
Redis slave node 2, node data is readable and unwritable
5.2 Redis basic image encapsulation
1. Redis image dockerfile script
FROM jaymarco/centos:7.3
MAINTAINER jaymarco
ENV VERSION=3.2.5
ENV DOWN_URL= http://download.redis.io/releases/redis-${VERSION}.tar.gz\
TEMP_DIR=/tmp/redis\
DATA_DIR=/data/redis
RUN mkdir-p ${TEMP_DIR} ${DATA_DIR} &
Yum install-y gcc gcc-c++ make cmake tar & &\
Groupadd redis & & useradd-g redis-d ${DATA_DIR}-s / sbin/nologin redis & &\
Curl-Lk ${DOWN_URL} | tar xz-C ${TEMP_DIR}-strip-components=1 & &\
Cd ${TEMP_DIR} & &\
Make-C ${TEMP_DIR}-j $(awk'/ processor/ {ionization +} END {print I}'/ proc/cpuinfo) & &\
Make-C ${TEMP_DIR} install & &\
Rm-rf ${TEMP_DIR} & &\
/ bin/cp / usr/share/zoneinfo/Asia/Shanghai / etc/localtime & &\
Echo 'Asia/Shanghai' > / etc/timezone
COPY entrypoint.sh / entrypoint.sh
RUN chmod + x / entrypoint.sh
VOLUME ${DATA_DIR}
WORKDIR ${DATA_DIR}
EXPOSE 6379/tcp
ENTRYPOINT ["/ entrypoint.sh"]
2. Redis initialization startup script
The operating system parameters are optimized when building dockerfile.
#! / bin/bash
If! Which redis-server > / dev/null 2 > & 1; then
Source / etc/profile.d/redis.sh
Fi
Set-e
Sysctl-w net.core.somaxconn=1024 > / dev/null 2 > & 1
Sysctl-w vm.overcommit_memory=1 > / dev/null 2 > & 1
Echo never > / sys/kernel/mm/transparent_hugepage/enabled
Echo never > / sys/kernel/mm/transparent_hugepage/defrag
# first arg is `- f` or`-some- option`
# or first arg is `something.conf`
If ["${1clients -}"! = "$1"] | | ["${1%.conf}"! = "$1"]; then
Set-redis-server "$@"
Fi
# allow the container to be started with `--user`
If ["$1" = 'redis-server'-a "$(id-u)" =' 0']; then
Chown-R redis.
# exec gosu redis "$0"$@"
Fi
If ["$1" = 'redis-server']; then
DoProtectedMode=1
ConfigFile=
If [- f "$2"]; then
ConfigFile= "$2"
If grep-Q'^ protected-mode' "$configFile"; then
DoProtectedMode=
Fi
Fi
If ["$doProtectedMode"]; then
Shift # "redis-server"
If ["$configFile"]; then
Shift
Fi
Set-protected-mode no "$@"
If ["$configFile"]; then
Set-- "$configFile"$@"
Fi
Set-redis-server "$@"
Fi
Fi
Exec "$@"
3. Build a basic image of redis
Encapsulate the redis image in the same directory of the dockerfile file, and finally generate an image file of jaymarco/redis:3.2.5. You can refer to the following command to build a redis image:
Docker build-t jaymarco/redis: 3.2.5.
4. Import redis image to other nodes
When the docker image of Redis has been encapsulated on one of the nodes, you need to synchronize the docker image of redisr to the other two redis host nodes.
1. Check the redis image and get the image ID: 04fd033441e2.
2. Use the docker save command to save the redis image to the host disk as a file.
3. Copy the redis325.tar file remotely to the other two redis nodes.
Scp redis325.tar 192.168.56.109:/home
Scp redis325.tar 192.168.56.110:/home
4. Import redis325.tar into docker using the docker load command.
Through the above operations, the encapsulation of redis image has been completed, and the mirror has been synchronized to other nodes, indicating that redis docker image exists on all three nodes of redis.
5.3 Redis master-slave cluster configuration
The key to the Redis master-slave cluster is the redis.conf configuration file between the master and slave, which makes the parameter setting and performance optimization configuration of the master-slave. Previously, we have installed the redis software in a mirror image on the three nodes. Next, we set up the redis master-slave cluster environment through configuration. At the same time, we start the redis container and use the host network mode to run the redis container on three virtual hosts.
5.3.1 master Primary Node (192.168.56.108)
Next, configure the redis-master master node.
1. Redis configuration file
Bind 0.0.0.0
Protected-mode yes
Port 6379
Tcp-backlog 511
Timeout 0
Tcp-keepalive 300
Daemonize no
Supervised no
Pidfile / var/run/redis_6379.pid
Loglevel notice
Logfile ""
Databases 8
Save 900 1
Save 300 10
Save 60 10000
Stop-writes-on-bgsave-error yes
Rdbcompression yes
Rdbchecksum yes
Dbfilename dump.rdb
Dir / data/redis
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 no
Appendfilename "appendonly.aof"
Appendfsync everysec
No-appendfsync-on-rewrite no
Auto-aof-rewrite-percentage 100
Auto-aof-rewrite-min-size 64mb
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
Client-output-buffer-limit normal 0 0 0
Client-output-buffer-limit slave 256mb 64mb 60
Client-output-buffer-limit pubsub 32mb 8mb 60
Hz 10
Aof-rewrite-incremental-fsync yes
Masterauth JayRedisHaZi
Requirepass JayRedisHaZi
2. Start the master redis container
Docker run-d\
-- privileged-- name redis-master\
-- network host\
-v / etc/redis.conf:/etc/redis.conf\
-v / etc/localtime:/etc/localtime\
Jaymarco/redis:3.2.5
See below: the redis container of the primary node starts successfully
5.3.2 Slave slave node 1 (192.168.56.109)
Next, configure the redis-slave01 slave node.
1. Redis configuration file
Bind 0.0.0.0
Protected-mode yes
Port 6379
Tcp-backlog 511
Timeout 0
Tcp-keepalive 300
Daemonize no
Supervised no
Pidfile / var/run/redis_6379.pid
Loglevel notice
Logfile ""
Databases 8
Save 900 1
Save 300 10
Save 60 10000
Stop-writes-on-bgsave-error yes
Rdbcompression yes
Rdbchecksum yes
Dbfilename dump.rdb
Dir / data/redis
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 no
Appendfilename "appendonly.aof"
Appendfsync everysec
No-appendfsync-on-rewrite no
Auto-aof-rewrite-percentage 100
Auto-aof-rewrite-min-size 64mb
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
Client-output-buffer-limit normal 0 0 0
Client-output-buffer-limit slave 256mb 64mb 60
Client-output-buffer-limit pubsub 32mb 8mb 60
Hz 10
Aof-rewrite-incremental-fsync yes
Slaveof 192.168.56.108 6379
Masterauth JayRedisHaZi
Requirepass JayRedisHaZi
2 start the master redis container
Docker run-d\
-- privileged-- name redis-slave01\
-- network host\
-v / etc/redis.conf:/etc/redis.conf\
-v / etc/localtime:/etc/localtime\
Jaymarco/redis:3.2.5
See below: the redis container of the primary node starts successfully
5.3.3 Slave Slave Node 2 (192.168.56.110)
Next, configure the redis-slave02 slave node.
1. Redis configuration file
Bind 0.0.0.0
Protected-mode yes
Port 6379
Tcp-backlog 511
Timeout 0
Tcp-keepalive 300
Daemonize no
Supervised no
Pidfile / var/run/redis_6379.pid
Loglevel notice
Logfile ""
Databases 8
Save 900 1
Save 300 10
Save 60 10000
Stop-writes-on-bgsave-error yes
Rdbcompression yes
Rdbchecksum yes
Dbfilename dump.rdb
Dir / data/redis
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 no
Appendfilename "appendonly.aof"
Appendfsync everysec
No-appendfsync-on-rewrite no
Auto-aof-rewrite-percentage 100
Auto-aof-rewrite-min-size 64mb
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
Client-output-buffer-limit normal 0 0 0
Client-output-buffer-limit slave 256mb 64mb 60
Client-output-buffer-limit pubsub 32mb 8mb 60
Hz 10
Aof-rewrite-incremental-fsync yes
Slaveof 192.168.56.108 6379
Masterauth JayRedisHaZi
Requirepass JayRedisHaZi
2 start the master redis container
Docker run-d\
-- privileged-- name redis-slave02\
-- network host\
-v / etc/redis.conf:/etc/redis.conf\
-v / etc/localtime:/etc/localtime\
Jaymarco/redis:3.2.5
See below: the redis container of the primary node starts successfully
5.4 Redis Master-Slave check
Check the master-slave synchronization status of master and slave with the following command
1. Redis-master status log
Docker exec-it redis-master redis-cli-h 192.168.56.108-a JayRedisHaZi info replication
# Replication
Role:master
Connected_slaves:2
Slave0:ip=192.168.56.110,port=6379,state=online,offset=99,lag=0
Slave1:ip=192.168.56.109,port=6379,state=online,offset=99,lag=1
Master_repl_offset:113
Repl_backlog_active:1
Repl_backlog_size:1048576
Repl_backlog_first_byte_offset:2
Repl_backlog_histlen:112
2. Redis-slave01 status log
Docker exec-it redis-slave01 redis-cli-h 192.168.56.109-a JayRedisHaZi info replication
# Replication
Role:slave
Master_host:192.168.56.108
Master_port:6379
Master_link_status:up
Master_last_io_seconds_ago:8
Master_sync_in_progress:0
Slave_repl_offset:253
Slave_priority:100
Slave_read_only:1
Connected_slaves:0
Master_repl_offset:0
Repl_backlog_active:0
Repl_backlog_size:1048576
Repl_backlog_first_byte_offset:0
Repl_backlog_histlen:0
3. Redis-slave02 status log
Docker exec-it redis-slave02 redis-cli-h 192.168.56.110-a JayRedisHaZi info replication
# Replication
Role:slave
Master_host:192.168.56.108
Master_port:6379
Master_link_status:up
Master_last_io_seconds_ago:9
Master_sync_in_progress:0
Slave_repl_offset:281
Slave_priority:100
Slave_read_only:1
Connected_slaves:0
Master_repl_offset:0
Repl_backlog_active:0
Repl_backlog_size:1048576
Repl_backlog_first_byte_offset:0
Repl_backlog_histlen:0
6 Redis master-slave synchronization test 6.1 verifies master-slave data synchronization
1. The redis master master node inserts two values
2. Redis slave01 can view the data normally from node 1.
3. Redis slave01 can view the data normally from node 1.
6.2 verify that slave nodes are read-only and cannot write
Two slave of redis failed to validate write data from the node, indicating read-only mode.
7 Redis Master-Slave switch
Next, we do such a master-slave switching simulation test, stop the redis service of the redis master master node (192.168.56.108), and switch the redis slave from node 1 (192.168.56.109) to the redis master master node. Also switch the original master master node to slave slave node, and then verify the master-slave node.
7.1 Master-Slave switch
The following are the steps for master-slave switching and slave switching:
1. Simulated primary node outage (192.168.56.108 operation)
Docker stop redis-master
2. Switch slave Node 1 to master Master Node (192.168.56.109 operation)
Docker exec-it redis-slave01 redis-cli-h 192.168.56.109-a JayRedisHaZi CONFIG SET slave-read-only no
Docker exec-it redis-slave01 redis-cli-h 192.168.56.109-a JayRedisHaZi SLAVEOF NO ONE
3. Add slave Node 2 to the new master node (192.168.56.110 operation)
Docker exec-it redis-slave02 redis-cli-h 192.168.56.110-a JayRedisHaZi SLAVEOF 192.168.56.109 6379
4. Switch the original master master node to slave slave node (192.168.56.108 operation)
Docker exec-it redis redis-cli-h 192.168.56.108-a JayRedisHaZi CONFIG SET slave-read-only no
Docker exec-it redis redis-cli-h 192.168.56.108-a JayRedisHaZi SLAVEOF NO ONE
7.2 Master-Slave switch check
From the following status data, we can see that the redis container on host 192.168.56.109 has changed from the original slave node to master node, while the redis container on 192.168.56.108 on the host has changed from the original master to slave. The master-slave switch has occurred, and finally the switch is successful.
1. Host 192.168.56.109
[root@dcos-redis01 redis] # docker exec-it redis-slave01 redis-cli-h 192.168.56.109-a JayRedisHaZi info replication
# Replication
Role:master
Connected_slaves:2
Slave0:ip=192.168.56.110,port=6379,state=online,offset=1601,lag=1
Slave1:ip=192.168.56.108,port=6379,state=online,offset=1601,lag=1
Master_repl_offset:1601
Repl_backlog_active:1
Repl_backlog_size:1048576
Repl_backlog_first_byte_offset:2
Repl_backlog_histlen:1600
2. Host 192.168.56.108
[root@docker-build-env etc] # docker exec-it redis-master redis-cli-h 192.168.56.108-a JayRedisHaZi info replication
# Replication
Role:slave
Master_host:192.168.56.109
Master_port:6379
Master_link_status:up
Master_last_io_seconds_ago:0
Master_sync_in_progress:0
Slave_repl_offset:1713
Slave_priority:100
Slave_read_only:1
Connected_slaves:0
Master_repl_offset:0
Repl_backlog_active:0
Repl_backlog_size:1048576
Repl_backlog_first_byte_offset:0
Repl_backlog_histlen:0
3. Host 192.168.56.110
[root@dcos-redis02 redis] # docker exec-it redis-slave02 redis-cli-h 192.168.56.110-a JayRedisHaZi info replication
# Replication
Role:slave
Master_host:192.168.56.109
Master_port:6379
Master_link_status:up
Master_last_io_seconds_ago:6
Master_sync_in_progress:0
Slave_repl_offset:1755
Slave_priority:100
Slave_read_only:1
Connected_slaves:0
Master_repl_offset:0
Repl_backlog_active:0
Repl_backlog_size:1048576
Repl_backlog_first_byte_offset:0
Repl_backlog_histlen:0
7.3 verify master-slave data synchronization
1. Check that the two slave nodes 192.168.56.108 and 192.168.56.110 are in read-only mode.
2. Master node 192.168.56.109 writes data and synchronizes to two slave nodes normally.
This is the answer to the question about how to build a redis master-slave cluster based on docker. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.