[Microservice Deployment] 10. Use Docker Compose to build a highly available Redis cluster

  Nowadays, business systems' dependence on caching Redis seems to be indispensable. We can see Redis in a variety of systems. Considering the stability of system operation, Redis applications need to be deployed with high availability like MySQL database.

1. Various high-availability solutions for Redis
Common Redis high availability solutions include the following:
  1. Redis Replication (master-slave replication): Redis's master-slave replication can realize data backup and read-write separation. By configuring the master node and the slave node, the master node asynchronously copies data to the slave node. When the master node fails, a slave node can be promoted to the new master node to achieve failover. Master-slave replication is suitable for scenarios with many read operations and high availability requirements.

  2. Redis Sentinel (Sentinel mode): Sentinel mode is one of the officially recommended solutions for Redis to achieve high availability. Monitor the status of the Redis master node by running one or more Sentinel processes. When the master node fails, Sentinel will automatically perform failover and promote one of the slave nodes to the new master node. Sentinel can also monitor slave nodes and perform failure recovery. Sentry mode is suitable for scenarios where high availability requirements are not particularly high.

  3. Redis Cluster (cluster mode): Redis Cluster is a high-availability and distributed solution officially provided by Redis. By combining multiple Redis instances into a cluster, Redis Cluster provides automatic data sharding and high availability. Data is distributed to different nodes, and the gossip protocol is used for communication between nodes. When a node fails, Redis Cluster can automatically migrate data to other normal nodes. Redis Cluster is suitable for scenarios with high requirements on availability and scalability. Cluster mode can only be stored in db0.

  4. Third-party middleware/solutions: In addition to the high-availability solutions officially provided by Redis, there are also some third-party middleware or solutions that can be used to achieve high availability of Redis, such as Codis, Twemproxy, etc. These middleware provide more functions and scalability, such as proxy, load balancing, fault recovery, etc.

  When choosing a high-availability solution, you need to consider the system's availability requirements, data consistency requirements, network topology and other factors. At the same time, attention should also be paid to appropriate testing and monitoring to ensure the stability and high availability of the Redis cluster.

2. Use Docker Compose to install Redis and configure Sentinel mode (Redis Sentinel)
1. Environment preparation

  The cluster architecture generally has an odd number of servers. Therefore, if the cluster mode is used, at least 3 Linux servers are prepared. Due to the production environment, we only have two Linux servers, but we can use Docker to build multiple Redis services (Redis Master service 1, Redis slave service 2, Redis slave service 3):

  • 192.168.0.210 (Redis master service 1, Redis slave service 2)
  • 192.168.0.195 (Redis slave service 3)
2. Prepare the Redis file storage directory
  • Prepare the Redis storage directory and execute the commands on the two master and slave servers respectively
    Execute the directory and permission commands required for Redis master service 1 on the 192.168.0.210 server
mkdir -p /opt/container/redis/master/data /opt/container/redis/master/conf /opt/container/redis/master/logs /opt/container/redis/sentinel/data /opt/container/redis/sentinel/conf /opt/container/redis/sentinel/logs

chmod -R 777 /opt/container/redis/master/data /opt/container/redis/master/conf /opt/container/redis/master/logs /opt/container/redis/sentinel/data /opt/container/redis/sentinel/conf /opt/container/redis/sentinel/logs

Execute the directory and permission commands required by Redis slave service 2 on the 192.168.0.210 server.

mkdir -p /opt/container/redis/slave1/data /opt/container/redis/slave1/conf /opt/container/redis/slave1/logs /opt/container/redis/sentinel1/data /opt/container/redis/sentinel1/conf /opt/container/redis/sentinel1/logs

chmod -R 777 /opt/container/redis/slave1/data /opt/container/redis/slave1/conf /opt/container/redis/slave1/logs /opt/container/redis/sentinel1/data /opt/container/redis/sentinel1/conf /opt/container/redis/sentinel1/logs

Execute the directory and permission commands required by Redis slave service 3 on the 192.168.0.195 server.

mkdir -p /opt/container/redis/slave2/data /opt/container/redis/slave2/conf /opt/container/redis/slave2/logs /opt/container/redis/sentinel2/data /opt/container/redis/sentinel2/conf /opt/container/redis/sentinel2/logs

chmod -R 777 /opt/container/redis/slave2/data /opt/container/redis/slave2/conf /opt/container/redis/slave2/logs /opt/container/redis/sentinel2/data /opt/container/redis/sentinel2/conf /opt/container/redis/sentinel2/logs

/opt/container/redis/ ** /data is used to store Redis data files
/opt/container/redis/ ** /conf is used to store Redis configuration files a>
/opt/container/redis/ ** /logs is used to store Redis log files

/opt/container/sentinel/ ** /data is used to store Redis Sentinel data files
/opt/container/sentinel/ ** /conf is used to store Redis Sentinel configuration files
/opt/container/sentinel/ ** /logs is used to store Redis sentinel log files

3. Redis server docker-compose.yml file
  • The docker-compose-redis.yml file of two Redis services (Redis master service 1, Redis slave service 2) is deployed on the 192.168.0.210 server.
version: '3'
services:
    ##redis主配置
    redisMaster:
      image: redis:latest
      restart: always
      container_name: redis-master
      command: redis-server /usr/local/etc/redis/redis.conf
      ##将26381映射到26381上
      ports:
        - "26381:26381"
      volumes:
        ##数据目录,要确保先创建好
        - /opt/container/redis/master/data:/data
        - /opt/container/redis/master/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/master/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
    ##redis从配置
    redisSlave1:
      image: redis:latest
      restart: always
      container_name: redis-slave-1
      command: redis-server /usr/local/etc/redis/redis.conf
      ##将26382映射到26382上
      ports:
        - "26382:26382"
      volumes:
        ##数据目录,要确保先创建好
        - /opt/container/redis/slave1/data:/data
        - /opt/container/redis/slave1/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/slave1/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
    sentinel:
      image: redis:latest
      restart: always
      container_name: redis-sentinel
      ports:
        - 36381:36381
      command: redis-sentinel /opt/redis/sentinel/sentinel.conf
      volumes:
        - /opt/container/redis/sentinel/data:/data
        - /opt/container/redis/sentinel/conf/sentinel.conf:/opt/redis/sentinel/sentinel.conf
        - /opt/container/redis/sentinel/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
    sentinel1:
      image: redis:latest
      restart: always
      container_name: redis-sentinel-1
      ports:
        - 36382:36382
      command: redis-sentinel /opt/redis/sentinel/sentinel1.conf
      volumes:
        - /opt/container/redis/sentinel1/data:/data
        - /opt/container/redis/sentinel1/conf/sentinel1.conf:/opt/redis/sentinel/sentinel1.conf
        - /opt/container/redis/sentinel1/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
  • The docker-compose-redis-slave.yml file that deploys the Redis service (Redis slave service 3) on the 192.168.0.195 server
version: '3'
services:
    ##redis从2配置
    redisSlave2:
      image: redis:latest
      restart: always
      container_name: redis-slave-2
      command: redis-server /usr/local/etc/redis/redis.conf
      ##将26383映射到26383上
      ports:
        - "26383:26383"
      volumes:
        ##数据目录,要确保先创建好
        - /opt/container/redis/slave2/data:/data
        - /opt/container/redis/slave2/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/slave2/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
    sentinel2:
      image: redis:latest
      restart: always
      container_name: redis-sentinel-2
      ports:
        - 36383:36383
      command: redis-sentinel /opt/redis/sentinel/sentinel2.conf
      volumes:
        - /opt/container/redis/sentinel2/data:/data
        - /opt/container/redis/sentinel2/conf/sentinel2.conf:/opt/redis/sentinel/sentinel2.conf
        - /opt/container/redis/sentinel2/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
4. Prepare the configuration file for the Redis service
  • 192.168.0.210 main Redis configuration file, configure port 26381, Redis password, and upload the configuration file redis.conf to the /opt/container/redis/master/conf directory
appendonly yes
port 26381
appendfilename appendonly.aof
appendfsync everysec
auto-aof-rewrite-min-size 10M
auto-aof-rewrite-percentage 100
requirepass "设置密码"
 
masterauth "设置密码" 
replica-read-only no
  • 192.168.0.210 From the Redis configuration file, configure port 26382, password, and upload the configuration file redis.conf to the /opt/container/redis/slave1/conf directory
appendonly yes
port 26382
appendfilename appendonly.aof
appendfsync everysec
auto-aof-rewrite-min-size 10M
auto-aof-rewrite-percentage 100
requirepass "设置密码"
 
replicaof 192.168.0.210 26381
masterauth "设置密码" 
replica-read-only no
  • 192.168.0.195 From the Redis configuration file, configure port 26383, password, and upload the configuration file redis.conf to the /opt/container/redis/slave2/conf directory
appendonly yes
port 26383
appendfilename appendonly.aof
appendfsync everysec
auto-aof-rewrite-min-size 10M
auto-aof-rewrite-percentage 100
requirepass "设置密码"
 
replicaof 192.168.0.210 26381
masterauth "设置密码" 
replica-read-only no
4. Prepare the Redis Sentinel configuration file. Each Redis service corresponds to a Sentinel configuration.
  • 192.168.0.210 main Redis sentinel configuration file, configure the port 36381 where the sentinel instance runs, the Redis password, and upload the configuration file sentinel.conf to the /opt/container/redis/sentinel/conf directory
# 哨兵sentinel实例运行的端口
port 36381
daemonize no
pidfile /var/run/redis-sentinel.pid
dir /tmp
sentinel monitor mymaster 192.168.0.210 26381 2
sentinel auth-pass mymaster "Redis密码"
# 指定多少毫秒之后 主节点没有应答哨兵sentinel 此时 哨兵主观上认为主节点下线 默认30秒
sentinel down-after-milliseconds mymaster 30000
# 指定了在发生failover主备切换时最多可以有多少个slave同时对新的master进行同步,这个数字越小,完成failover所需的时间就越长
sentinel parallel-syncs mymaster 1
# 故障转移的超时时间
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
  • 192.168.0.210 From the Redis sentinel configuration file, configure the port 36382 where the sentinel1 instance runs, the Redis password, and upload the configuration file sentinel1.conf to the /opt/container/redis/sentinel1/conf directory
# 哨兵sentinel实例运行的端口
port 36382
daemonize no
pidfile /var/run/redis-sentinel1.pid
dir /tmp
sentinel monitor mymaster 192.168.0.210 26381 2
sentinel auth-pass mymaster "Redis密码"
# 指定多少毫秒之后 主节点没有应答哨兵sentinel 此时 哨兵主观上认为主节点下线 默认30秒
sentinel down-after-milliseconds mymaster 30000
# 指定了在发生failover主备切换时最多可以有多少个slave同时对新的master进行同步,这个数字越小,完成failover所需的时间就越长
sentinel parallel-syncs mymaster 1
# 故障转移的超时时间
sentinel failover-timeout mymaster 180000
sentinel deny-scripts-reconfig yes
  • 192.168.0.195 From the Redis sentinel configuration file, configure the port 36383 where the sentinel2 instance runs, the Redis password, and upload the configuration file sentinel2.conf to the /opt/container/redis/sentinel2/conf directory
appendonly yes
port 26383
appendfilename appendonly.aof
appendfsync everysec
auto-aof-rewrite-min-size 10M
auto-aof-rewrite-percentage 100
requirepass "设置密码"
 
replicaof 192.168.0.210 26381
masterauth "设置密码" 
replica-read-only no
5. Execute the docker-compose installation and startup commands on the two servers respectively.

Upload docker-compose-redis.yml to the /opt/software directory. You can choose this directory yourself, and then execute the installation startup command in the directory.

docker-compose -f docker-compose-redis.yml up -d
[root@localhost software]# docker-compose -f docker-compose-redis.yml up -d
[+] Running 10/10
 ⠿ sentinel1 Pulled                                                                                                                                                                                                                        15.7s
 ⠿ redisSlave1 Pulled                                                                                                                                                                                                                      15.7s
 ⠿ sentinel Pulled                                                                                                                                                                                                                          3.4s
   ⠿ a2abf6c4d29d Already exists                                                                                                                                                                                                            0.0s
   ⠿ c7a4e4382001 Pull complete                                                                                                                                                                                                             0.4s
   ⠿ 4044b9ba67c9 Pull complete                                                                                                                                                                                                             1.0s
   ⠿ c8388a79482f Pull complete                                                                                                                                                                                                             2.3s
   ⠿ 413c8bb60be2 Pull complete                                                                                                                                                                                                             2.3s
   ⠿ 1abfd3011519 Pull complete                                                                                                                                                                                                             2.4s
 ⠿ redisMaster Pulled                                                                                                                                                                                                                      15.7s
WARN[0015] Found orphan containers ([mysql nginx]) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up. 
[+] Running 4/4
 ⠿ Container redis-sentinel-1  Started                                                                                                                                                                                                      0.5s
 ⠿ Container redis-slave-1     Started                                                                                                                                                                                                      0.6s
 ⠿ Container redis-master      Started                                                                                                                                                                                                      0.6s
 ⠿ Container redis-sentinel    Started                                                                                                                                                                                                      0.5s

Through the docker ps command, you can see that redis and redis sentinel have been installed and started successfully.

[root@localhost software]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                                                            NAMES
f609322cabaa   redis:latest   "docker-entrypoint.s…"   59 seconds ago   Up 58 seconds   6379/tcp, 0.0.0.0:26381->26381/tcp, :::26381->26381/tcp                          redis-master
18b75828b5b7   redis:latest   "docker-entrypoint.s…"   59 seconds ago   Up 58 seconds   6379/tcp, 0.0.0.0:36381->36381/tcp, :::36381->36381/tcp                          redis-sentinel
f0f9a037c7ae   redis:latest   "docker-entrypoint.s…"   59 seconds ago   Up 58 seconds   6379/tcp, 0.0.0.0:26382->26382/tcp, :::26382->26382/tcp                          redis-slave-1
e51d3b0bc696   redis:latest   "docker-entrypoint.s…"   59 seconds ago   Up 58 seconds   6379/tcp, 0.0.0.0:36382->36382/tcp, :::36382->36382/tcp                          redis-sentinel-1

6. Check the redis sentinel status through commands
  • Enter the docker container
docker exec -it f609322cabaa bash
  • Enter the Redis directory
cd /usr/local/bin
  • Run the info Replication command to view the master node information. We can see that there are 2 connected_slaves, among which the ip of slave0 is 172.18.0.1. This is because it obtains the docker ip address.
./redis-cli  -h 127.0.0.1 -p 26381 -a "密码" info Replication

# Replication
role:master
connected_slaves:2
slave0:ip=172.18.0.1,port=26382,state=online,offset=700123,lag=0
slave1:ip=192.168.0.195,port=26383,state=online,offset=700123,lag=0
master_failover_state:no-failover
master_replid:9ee56f68d25b71158544f6cfafc677822c401ec3
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:700123
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:700123
  • Enter the Sentinel container using the same operation method and view the sentinel information through the INFO Sentinel command. We can see that there are two redis slave services and three sentinels.
root@fba6d91e10f6:/usr/local/bin# ./redis-cli -h 127.0.0.1 -p 36381 INFO Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.0.210:26381,slaves=2,sentinels=3
7. Test redis sentinel active and backup switching
  • Manually shut down the main Redis service
docker stop 5541698b65a1
  • Enter the Sentinel container and check the sentinel information through the INFO Sentinel command. You can see that the main Redis service address has been switched to 192.168.0.195:26383.
root@fba6d91e10f6:/usr/local/bin# ./redis-cli -h 127.0.0.1 -p 36381 INFO Sentinel
# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=192.168.0.195:26383,slaves=3,sentinels=3
  • Enter the Redis container at 192.168.0.195 and use the info Replication command to view the node information. We can see that this Redis node has become the main service and has a slave service slave0:ip=192.168.0.210,port=26382
 ./redis-cli  -h 127.0.0.1 -p 26383 -a "密码" info Replication

Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.0.210,port=26382,state=online,offset=894509,lag=0
master_failover_state:no-failover
master_replid:e9ebebdff0a5f7b7622c6c5fbfed7b1e44d84ae2
master_replid2:9ee56f68d25b71158544f6cfafc677822c401ec3
master_repl_offset:894509
second_repl_offset:884279
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:846
repl_backlog_histlen:893664
3. Configure and test Redis Cluster mode (Redis Cluster)

  The essence of Redis sentry mode is still master-slave replication, but it adds sentinels for automatic master-slave switching. Redis cluster mode (Redis Cluster) is the true cluster deployment. It can store data in distributed shards, but its Can only be stored in db0.

1. Environment preparation

  We still use the above Linux server environment to install the redis cluster: install three Redis services, redisCluster1, redisCluster2, and redisCluster3 on 192.168.0.210; install three Redis services, redisCluster4, redisCluster5, and redisCluster6 on 192.168.0.195.

2. Prepare the Redis file storage directory
  • Prepare the Redis storage directory and execute the commands on the two master and slave servers respectively
    Execute the directory and permission commands required for the Redis cluster on the 192.168.0.210 server
mkdir -p /opt/container/redis/cluster1/data /opt/container/redis/cluster1/conf /opt/container/redis/cluster1/logs /opt/container/redis/cluster2/data /opt/container/redis/cluster2/conf /opt/container/redis/cluster2/logs /opt/container/redis/cluster3/data /opt/container/redis/cluster3/conf /opt/container/redis/cluster3/logs

chmod -R 777 /opt/container/redis/cluster1/data /opt/container/redis/cluster1/conf /opt/container/redis/cluster1/logs /opt/container/redis/cluster2/data /opt/container/redis/cluster2/conf /opt/container/redis/cluster2/logs /opt/container/redis/cluster3/data /opt/container/redis/cluster3/conf /opt/container/redis/cluster3/logs

Execute the directory and permission commands required by the Redis cluster on the 192.168.0.195 server.

mkdir -p /opt/container/redis/cluster4/data /opt/container/redis/cluster4/conf /opt/container/redis/cluster4/logs /opt/container/redis/cluster5/data /opt/container/redis/cluster5/conf /opt/container/redis/cluster5/logs  /opt/container/redis/cluster6/data /opt/container/redis/cluster6/conf /opt/container/redis/cluster6/logs 

chmod -R 777 /opt/container/redis/cluster4/data /opt/container/redis/cluster4/conf /opt/container/redis/cluster4/logs /opt/container/redis/cluster5/data /opt/container/redis/cluster5/conf /opt/container/redis/cluster5/logs /opt/container/redis/cluster6/data /opt/container/redis/cluster6/conf /opt/container/redis/cluster6/logs 
  • 192.168.0.210 /192.168.0.195Redis configuration file, port (12381, 12382, 12383, 12384, 12385, 12386), password, and upload the configuration file redis.conf to the corresponding directory, only the ports need to be different
port 12381
cluster-enabled yes #启动集群模式
cluster-config-file nodes-1.conf
cluster-node-timeout 5000
cluster-announce-ip 192.168.0.210
cluster-announce-port 12381
cluster-announce-bus-port 22381
bind 0.0.0.0
protected-mode no
appendonly yes
#如果要设置密码需要增加如下配置:
 #(设置redis访问密码)
requirepass 密码
 #(设置集群节点间访问密码,跟上面一致)
masterauth 密码
3. Redis server docker-compose.yml file
  • The docker-compose-redis-cluster.yml file deploying two Redis services on the 192.168.0.210 server
version: '3'
services:
    ##redis节点1配置
    redisCluster1:
      image: redis:latest
      restart: always
      network_mode: "host"
      container_name: redis-cluster1
      command: redis-server /usr/local/etc/redis/redis.conf
      volumes:
        ##数据目录,要确保先创建好
        - /opt/container/redis/cluster1/data:/data
        - /opt/container/redis/cluster1/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/cluster1/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
    ##redis节点2配置
    redisCluster2:
      image: redis:latest
      restart: always
      network_mode: "host"
      container_name: redis-cluster2
      command: redis-server /usr/local/etc/redis/redis.conf
      volumes:
        ##数据目录,要确保先创建好
        - /opt/container/redis/cluster2/data:/data
        - /opt/container/redis/cluster2/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/cluster2/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
    ##redis节点3配置
    redisCluster3:
      image: redis:latest
      restart: always
      network_mode: "host"
      container_name: redis-cluster3
      command: redis-server /usr/local/etc/redis/redis.conf
      volumes:
        ##数据目录,要确保先创建好
        - /opt/container/redis/cluster3/data:/data
        - /opt/container/redis/cluster3/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/cluster3/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
  • The docker-compose-redis-cluster.yml file that deploys three Redis services on the 192.168.0.195 server
version: '3'
services:
    ##redis节点4配置
    redisCluster4:
      image: redis:latest
      restart: always
      network_mode: "host"
      container_name: redis-cluster4
      command: redis-server /usr/local/etc/redis/redis.conf
      volumes:
        ##数据目录,要确保先创建好
        - /opt/container/redis/cluster4/data:/data
        - /opt/container/redis/cluster4/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/cluster4/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
    ##redis节点5配置
    redisCluster5:
      image: redis:latest
      restart: always
      network_mode: "host"
      container_name: redis-cluster5
      command: redis-server /usr/local/etc/redis/redis.conf
      volumes:
        ##数据目录,要确保先创建好
        - /opt/container/redis/cluster5/data:/data
        - /opt/container/redis/cluster5/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/cluster5/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
    ##redis节点6配置
    redisCluster6:
      image: redis:latest
      restart: always
      network_mode: "host"
      container_name: redis-cluster6
      command: redis-server /usr/local/etc/redis/redis.conf
      volumes:
        ##数据目录,要确保先创建好
        - /opt/container/redis/cluster6/data:/data
        - /opt/container/redis/cluster6/conf/redis.conf:/usr/local/etc/redis/redis.conf
        - /opt/container/redis/cluster6/logs:/logs
        - "/etc/localtime:/etc/localtime"
        - "/usr/share/zoneinfo/Asia/Shanghai:/etc/timezone"
4. Execute the docker-compose installation and startup commands on the two servers respectively.

Upload docker-compose-redis-cluster.yml to the /opt/software directory. You can choose this directory yourself, and then execute the installation startup command in the directory.

docker-compose -f docker-compose-redis-cluster.yml up -d
[+] Running 3/3
 ⠿ Container redis-cluster3  Started                                                                                                                                                                                                        0.5s
 ⠿ Container redis-cluster2  Started                                                                                                                                                                                                        0.4s
 ⠿ Container redis-cluster1  Started                                                                                                                                                                                                        0.5s

Through the docker ps command, you can see that redis and redis sentinel have been installed and started successfully.

[root@localhost software]# docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS             PORTS                                                                            NAMES
67a48962160d   redis:latest   "docker-entrypoint.s…"   10 seconds ago   Up 9 seconds       6379/tcp, 0.0.0.0:52381->52381/tcp, :::52381->52381/tcp                          redis-cluster1
b10f669691b3   redis:latest   "docker-entrypoint.s…"   10 seconds ago   Up 9 seconds       6379/tcp, 0.0.0.0:52383->52383/tcp, :::52383->52383/tcp                          redis-cluster3
d3899c9c01f6   redis:latest   "docker-entrypoint.s…"   10 seconds ago   Up 9 seconds       6379/tcp, 0.0.0.0:52382->52382/tcp, :::52382->52382/tcp                          redis-cluster2
5. Execute the docker-compose installation and startup commands on the two servers respectively.
  • Log in to the docker container
docker exec -it 67a48962160d bash
  • Create the entire redis cluster with redis-cli
cd /usr/local/bin

root@localhost:/usr/local/bin# ./redis-cli --cluster create 192.168.0.210:12381 192.168.0.210:12382 192.168.0.210:12383 192.168.0.195:12384 192.168.0.195:12385 192.168.0.195:12386 --cluster-replicas 1 -a "密码"
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 192.168.0.195:12386 to 192.168.0.210:12381
Adding replica 192.168.0.210:12383 to 192.168.0.195:12384
Adding replica 192.168.0.195:12385 to 192.168.0.210:12382
M: 061d7b1bf2f93df7cbf261e47a7981800d636e63 192.168.0.210:12381
   slots:[0-5460] (5461 slots) master
M: 6cfbf9677ab802483ddc7cbb715fe770c8de884a 192.168.0.210:12382
   slots:[10923-16383] (5461 slots) master
S: 5afc2e7d2da8f9d7f7ad6e99d5ad04ffbf5bdfe5 192.168.0.210:12383
   replicates d54562615048044b43e368db71789829d76fa263
M: d54562615048044b43e368db71789829d76fa263 192.168.0.195:12384
   slots:[5461-10922] (5462 slots) master
S: 9137f05da40ce173a975fa4a5e86e65b9d3fe4e3 192.168.0.195:12385
   replicates 6cfbf9677ab802483ddc7cbb715fe770c8de884a
S: de04b0b6d207bd8653f2cb738cb47443c427810e 192.168.0.195:12386
   replicates 061d7b1bf2f93df7cbf261e47a7981800d636e63
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join

>>> Performing Cluster Check (using node 192.168.0.210:12381)
M: 061d7b1bf2f93df7cbf261e47a7981800d636e63 192.168.0.210:12381
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 5afc2e7d2da8f9d7f7ad6e99d5ad04ffbf5bdfe5 192.168.0.210:12383
   slots: (0 slots) slave
   replicates d54562615048044b43e368db71789829d76fa263
S: 9137f05da40ce173a975fa4a5e86e65b9d3fe4e3 192.168.0.195:12385
   slots: (0 slots) slave
   replicates 6cfbf9677ab802483ddc7cbb715fe770c8de884a
M: d54562615048044b43e368db71789829d76fa263 192.168.0.195:12384
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: de04b0b6d207bd8653f2cb738cb47443c427810e 192.168.0.195:12386
   slots: (0 slots) slave
   replicates 061d7b1bf2f93df7cbf261e47a7981800d636e63
M: 6cfbf9677ab802483ddc7cbb715fe770c8de884a 192.168.0.210:12382
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

  • Log in to redis and view cluster information
# 查看集群信息
cluster info
# 查看节点列表
cluster nodes
[root@localhost software]# docker exec -it 407e3847371a bash
root@localhost:/data# cd /usr/local/bin
root@localhost:/usr/local/bin# redis-cli -c -h 127.0.0.1 -p 12383 -a "密码"
127.0.0.1:12383> cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:4
cluster_stats_messages_ping_sent:173
cluster_stats_messages_pong_sent:163
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:337
cluster_stats_messages_ping_received:163
cluster_stats_messages_pong_received:174
cluster_stats_messages_received:337
127.0.0.1:12383> cluster nodes
061d7b1bf2f93df7cbf261e47a7981800d636e63 192.168.0.210:12381@22381 master - 0 1696952366541 1 connected 0-5460
de04b0b6d207bd8653f2cb738cb47443c427810e 192.168.0.195:12386@22386 slave 061d7b1bf2f93df7cbf261e47a7981800d636e63 0 1696952366541 1 connected
5afc2e7d2da8f9d7f7ad6e99d5ad04ffbf5bdfe5 192.168.0.210:12383@22383 slave d54562615048044b43e368db71789829d76fa263 0 1696952366039 4 connected
9137f05da40ce173a975fa4a5e86e65b9d3fe4e3 192.168.0.195:12385@22385 slave 6cfbf9677ab802483ddc7cbb715fe770c8de884a 0 1696952367043 2 connected
6cfbf9677ab802483ddc7cbb715fe770c8de884a 192.168.0.210:12382@22382 master - 0 1696952365537 2 connected 10923-16383
d54562615048044b43e368db71789829d76fa263 192.168.0.195:12383@22384 myself,master - 0 1696952366000 4 connected 5461-10922

  Be sure to pay attention when building Redis cluster mode. Docker-compose configuration must set network_mode to "host" mode, and port mapping is not required. If when you create a cluster, it always shows that it is waiting for a connection, you need to configure the firewall and open the Redis cluster port.

Guess you like

Origin blog.csdn.net/wmz1932/article/details/133761948