本文已收录在Github,关注我,紧跟本系列专栏文章,咱们下篇再续!
- 🚀 魔都架构师 | 全网30W技术追随者
- 🔧 大厂分布式系统/数据中台实战专家
- 🏆 主导交易系统百万级流量调优 & 车联网平台架构
- 🧠 AIGC应用开发先行者 | 区块链落地实践者
- 🌍 以技术驱动创新,我们的征途是改变世界!
- 👉 实战干货:编程严选网
1 拉取Redis镜像
$ docker --version
Docker version 28.0.4, build b8034c0
$ docker pull redis
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
2 创建 Docker 网络用于 Redis 集群
$ docker network create redis-cluster
3 创建 Redis 节点的目录和配置文件
让我们为 6 个 Redis 节点创建必要的目录和配置文件。脚本设置这些配置文件:
mkdir -p ~/redis-cluster
为每个 Redis 节点创建配置文件。脚本生成这些文件:
#!/bin/bash
# Create directories and configuration files for Redis nodes
for port in $(seq 6379 6384); do
mkdir -p ~/redis-cluster/node-${port}/conf
cat << EOF > ~/redis-cluster/node-${port}/conf/redis.conf
port ${port}
requirepass 1234
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 127.0.0.1
cluster-announce-port ${port}
cluster-announce-bus-port 1${port}
EOF
echo "Created configuration for Redis node ${port}"
done
echo "All Redis node configurations created successfully!"
使脚本可执行并运行它以创建 Redis 配置文件:
chmod +x ~/redis-cluster/setup-redis-cluster.sh
脚本创建 Redis 配置文件:
~/redis-cluster/setup-redis-cluster.sh
Created configuration for Redis node 6379
Created configuration for Redis node 6380
Created configuration for Redis node 6381
Created configuration for Redis node 6382
Created configuration for Redis node 6383
Created configuration for Redis node 6384
All Redis node configurations created successfully!
4 创建启动 Redis 容器的脚本
脚本启动所有 6 个 Redis 容器
#!/bin/bash
# Start Redis containers
for port in $(seq 6379 6384); do
docker run -d -p ${port}:${port} -p 1${port}:1${port} \
--privileged=true \
-v ~/redis-cluster/node-${port}/conf/redis.conf:/usr/local/etc/redis/redis.conf \
-v ~/redis-cluster/node-${port}/data:/data \
--restart always \
--name redis-${port} \
--net redis-cluster \
--sysctl net.core.somaxconn=1024 \
redis redis-server /usr/local/etc/redis/redis.conf
echo "Started Redis container for port ${port}"
done
echo "All Redis containers started successfully!"
echo "Wait a few seconds before creating the cluster..."
sleep 5
echo "Now you can create the Redis Cluster with the following command:"
echo "docker exec -it redis-6379 redis-cli -a 1234 --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1"
现在让我们使脚本可执行并运行它以启动 Redis 容器:
chmod +x ~/redis-cluster/start-redis-containers.sh
现在让我们运行脚本来启动所有 Redis 容器:
~/redis-cluster/start-redis-containers.sh
c1fde3cd17390d4733b6a3309671fe8dfaf67c9af69fe1c4e285e332a55dccc8
docker: Error response from daemon: failed to set up container networking: driver failed programming external connectivity on endpoint redis-6379 (2d6ac937f8cd5452cedebf18ad6a2c51d0d5c727c49247c20be984182f77c172): Bind for 0.0.0.0:6379 failed: port is already allocated
Run 'docker run --help' for more information
Started Redis container for port 6379
8feb0850d3cc433291fdc53ef2b1bb7d209427a20ac641f69295fc2d06dcada4
Started Redis container for port 6380
fd928918270dfa2837dc4bdd547b8dc210e8773804d80721c10dce7a46744319
Started Redis container for port 6381
8842810ed383bd2495e33cd4235f45167d3345fbdd97e33980b85da92a726888
Started Redis container for port 6382
d3d6a114e4ad18f2ed7e009abb65487643ecf8f0192aa0a1c4bcf41f25766e79
Started Redis container for port 6383
71f4fb5f4ece6ae05537f77d58f56ab6d6ca272eb44974dc71166edd8152a316
Started Redis container for port 6384
All Redis containers started successfully!
Wait a few seconds before creating the cluster...
Now you can create the Redis Cluster with the following command:
docker exec -it redis-6379 redis-cli -a 1234 --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1
发现其中一个 Redis 容器出现了错误。似乎系统已用端口 6379。让我们检查正在运行的容器并解决这个问题:
docker ps -a | grep redis
71f4fb5f4ece redis "docker-entrypoint.s…" 45 seconds ago Up 45 seconds 0.0.0.0:6384->6384/tcp, 6379/tcp, 0.0.0.0:16384->16384/tcp redis-6384
d3d6a114e4ad redis "docker-entrypoint.s…" 45 seconds ago Up 45 seconds 0.0.0.0:6383->6383/tcp, 6379/tcp, 0.0.0.0:16383->16383/tcp redis-6383
8842810ed383 redis "docker-entrypoint.s…" 46 seconds ago Up 45 seconds 0.0.0.0:6382->6382/tcp, 6379/tcp, 0.0.0.0:16382->16382/tcp redis-6382
fd928918270d redis "docker-entrypoint.s…" 46 seconds ago Up 45 seconds 0.0.0.0:6381->6381/tcp, 6379/tcp, 0.0.0.0:16381->16381/tcp redis-6381
8feb0850d3cc redis "docker-entrypoint.s…" 46 seconds ago Up 45 seconds 0.0.0.0:6380->6380/tcp, 6379/tcp, 0.0.0.0:16380->16380/tcp redis-6380
c1fde3cd1739 redis "docker-entrypoint.s…" 46 seconds ago Created redis-6379
a991b12dca33 redis:6-alpine "docker-entrypoint.s…" 3 days ago Up 2 days 0.0.0.0:6379->6379/tcp zealous_pascal
b9064b85660f redis:6-alpine "docker-entrypoint.s…" 13 days ago Up 3 days (healthy) 6379/tcp docker-redis-1
1a9ca3f16c39 redis:6.2.7 "docker-entrypoint.s…" 2 weeks ago Exited (0) 8 days ago redis-6.2.7
我发现问题所在。系统已运行 Redis 实例,包括一个占用了端口 6379 的实例。让我们清理一下,重新开始设置 Redis 集群。
首先,让我们停止并删除我们刚刚创建的 Redis 容器:
for port in $(seq 6379 6384); do docker stop redis-${port} || true; docker rm redis-${port} || true; done
redis-6379
redis-6379
redis-6380
redis-6380
redis-6381
redis-6381
redis-6382
redis-6382
redis-6383
redis-6383
redis-6384
redis-6384
现在检查系统上是否有其他 Redis 实例正在运行,可能会占用我们需要的端口。让我们停止使用端口 6379 的现有 Redis 容器:
docker stop zealous_pascal
现在我们已经停止了使用端口 6379 的 Redis 容器,让我们再次运行脚本来启动集群所需的 Redis 容器:
~/redis-cluster/start-redis-containers.sh
d0569057eda29eba27df2fcf15fe1db7b00366dbfc554d033e3a7309dc44e6a9
Started Redis container for port 6379
35ab657be7866cf6e3594275d78eb597466e4597af896eafb5bceb0aa0a0e6ea
Started Redis container for port 6380
b27290b595cb787db4d888c539427b68887daf7c6096200f87de6852b484f640
Started Redis container for port 6381
d41fa09f89692105ac3e40a564e0762d804499d9360444c31a2514245a50d96e
Started Redis container for port 6382
cebdf814c885ad97b3f5f3c6e7ae2ca54245ffe9a616d2dea5732afdcbc121b3
Started Redis container for port 6383
17ddba5baba745f8f50ea8a24520f8dc96014dd098ac209d132214fa9d97dba1
Started Redis container for port 6384
All Redis containers started successfully!
Wait a few seconds before creating the cluster...
Now you can create the Redis Cluster with the following command:
docker exec -it redis-6379 redis-cli -a 1234 --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1
太好了!你的所有 Redis 容器现在都已成功运行。让我们使用我们刚刚启动的容器来创建 Redis 集群。
5 创建 Redis 集群
现在我将执行命令来创建包含 3 个主节点和 3 个副本节点的 Redis 集群:
docker exec -it redis-6379 redis-cli -a 1234 --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1
我发现我们需要修改创建 Redis 集群的方法,因为交互式 TTY 命令没有成功。让我们创建一个非交互式的集群创建命令:
docker exec redis-6379 redis-cli -a 1234 --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1 --cluster-yes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 127.0.0.1:6380: Connection refused
我发现问题所在。我们需要使用 Docker 网络内部的 Redis 节点地址,而不是本地主机地址。让我们检查 Docker 网络中 Redis 容器的 IP 地址:
docker network inspect redis-cluster
[
{
"Name": "redis-cluster",
"Id": "63a3da8656c4bf9fdb204db49bf0e2b9f0507cdd69df1ce51e2dd6c5bf4c6775",
"Created": "2025-05-15T07:59:47.670413219Z",
"Scope": "local",
"Driver": "bridge",
"EnableIPv4": true,
"EnableIPv6": false,
"IPAM": {
"Driver": "default",
"Options": {},
"Config": [
{
"Subnet": "172.20.0.0/16",
"Gateway": "172.20.0.1"
}
]
},
"Internal": false,
"Attachable": false,
"Ingress": false,
"ConfigFrom": {
"Network": ""
},
"ConfigOnly": false,
"Containers": {
"17ddba5baba745f8f50ea8a24520f8dc96014dd098ac209d132214fa9d97dba1": {
"Name": "redis-6384",
"EndpointID": "44d7ed25f4c03c63c2f192ca88e78e436718bf9dd153a94495b5cebebe4d9251",
"MacAddress": "c2:68:b9:4b:b4:1a",
"IPv4Address": "172.20.0.7/16",
"IPv6Address": ""
},
"35ab657be7866cf6e3594275d78eb597466e4597af896eafb5bceb0aa0a0e6ea": {
"Name": "redis-6380",
"EndpointID": "4ec35e1e16a08bc2f2b1c8f7a4c76aa7fb580641821eb1a286467675a7a2fc30",
"MacAddress": "b2:bd:c0:c4:39:8b",
"IPv4Address": "172.20.0.3/16",
"IPv6Address": ""
},
"b27290b595cb787db4d888c539427b68887daf7c6096200f87de6852b484f640": {
"Name": "redis-6381",
"EndpointID": "05975791e957e77ab7d4a022f37614f73c1b05d891e59f58a45ebec981c7e287",
"MacAddress": "72:05:ac:df:70:92",
"IPv4Address": "172.20.0.4/16",
"IPv6Address": ""
},
"cebdf814c885ad97b3f5f3c6e7ae2ca54245ffe9a616d2dea5732afdcbc121b3": {
"Name": "redis-6383",
"EndpointID": "a4de946cfa894d85c21ceecce74edb12f5467e2f23db221924d8865fa367eae4",
"MacAddress": "f2:25:c6:3c:55:56",
"IPv4Address": "172.20.0.6/16",
"IPv6Address": ""
},
"d0569057eda29eba27df2fcf15fe1db7b00366dbfc554d033e3a7309dc44e6a9": {
"Name": "redis-6379",
"EndpointID": "d7b16373921b61e94bd51fa248dcde572d8567d31d5a8cdb06458423c149b337",
"MacAddress": "ce:25:8a:f1:ed:3e",
"IPv4Address": "172.20.0.2/16",
"IPv6Address": ""
},
"d41fa09f89692105ac3e40a564e0762d804499d9360444c31a2514245a50d96e": {
"Name": "redis-6382",
"EndpointID": "57bc2e7829888c118ab767477017f45bf5cb5a367f6bd280f25ecfcfe184babc",
"MacAddress": "06:ee:de:17:76:a4",
"IPv4Address": "172.20.0.5/16",
"IPv6Address": ""
}
},
"Options": {},
"Labels": {}
}
]
现在我有了 Docker 网络中所有 Redis 容器的 IP 地址。让我们更新 Redis 配置文件,使用这些 IP 地址作为 cluster-announce-ip
参数。这将确保 Redis 节点能够在 Docker 网络内正确通信。
让我们修改 Redis 配置文件:
#!/bin/bash
# Update Redis configuration files with the correct IP addresses
cat << EOF > ~/redis-cluster/node-6379/conf/redis.conf
port 6379
requirepass 1234
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.20.0.2
cluster-announce-port 6379
cluster-announce-bus-port 16379
EOF
cat << EOF > ~/redis-cluster/node-6380/conf/redis.conf
port 6380
requirepass 1234
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.20.0.3
cluster-announce-port 6380
cluster-announce-bus-port 16380
EOF
cat << EOF > ~/redis-cluster/node-6381/conf/redis.conf
port 6381
requirepass 1234
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.20.0.4
cluster-announce-port 6381
cluster-announce-bus-port 16381
EOF
cat << EOF > ~/redis-cluster/node-6382/conf/redis.conf
port 6382
requirepass 1234
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.20.0.5
cluster-announce-port 6382
cluster-announce-bus-port 16382
EOF
cat << EOF > ~/redis-cluster/node-6383/conf/redis.conf
port 6383
requirepass 1234
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.20.0.6
cluster-announce-port 6383
cluster-announce-bus-port 16383
EOF
cat << EOF > ~/redis-cluster/node-6384/conf/redis.conf
port 6384
requirepass 1234
bind 0.0.0.0
protected-mode no
daemonize no
appendonly yes
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
cluster-announce-ip 172.20.0.7
cluster-announce-port 6384
cluster-announce-bus-port 16384
EOF
echo "Updated all Redis configuration files with correct IP addresses"
chmod +x ~/redis-cluster/update-redis-configs.sh
让我们运行脚本来更新 Redis 配置文件:
~/redis-cluster/update-redis-configs.sh
现在我们已经使用正确的 IP 地址更新了 Redis 配置文件,我们需要重新启动 Redis 容器以应用这些更改。让我们停止并删除现有的容器,然后再次启动它们,使用更新后的配置:
for port in $(seq 6379 6384); do docker stop redis-${port} || true; docker rm redis-${port} || true; done
redis-6379
redis-6379
redis-6380
redis-6380
redis-6381
redis-6381
redis-6382
redis-6382
redis-6383
redis-6383
redis-6384
redis-6384
现在让我们再次启动 Redis 容器,使用更新后的配置:
~/redis-cluster/start-redis-containers.sh
835029f336169760eae4c63f4b9042452a26954d3009fccbccefbb765b93b968
Started Redis container for port 6379
d08225a94f579975e7af6b8c2c877ca79c7dab7e24724e9ce938b114faf52255
Started Redis container for port 6380
37c6dd8369886900c9fe7180141986c21e5d7c0e9c4f348424a039ce8e37f3a8
Started Redis container for port 6381
367eaf1f52420c9adddafd7067fb7b0bae4495f689f79827872ad559f3b0f4b9
Started Redis container for port 6382
2468ebf310f923fff78d1df1e2597fe97ac2151c53d9dd0f2b6b255c723e0e06
Started Redis container for port 6383
f0f7b161e7638f762e927de895eac64ddef0cc12fe4fa2d4c1025e79e3cd8b7d
Started Redis container for port 6384
All Redis containers started successfully!
Wait a few seconds before creating the cluster...
Now you can create the Redis Cluster with the following command:
docker exec -it redis-6379 redis-cli -a 1234 --cluster create 127.0.0.1:6379 127.0.0.1:6380 127.0.0.1:6381 127.0.0.1:6382 127.0.0.1:6383 127.0.0.1:6384 --cluster-replicas 1
可见 Redis 集群已经设置好了。让我们测试一下与 Redis 集群的连接。我将创建一个脚本来测试连接并执行一些基本操作:
#!/bin/bash
echo "Testing connection to Redis Cluster..."
# Connect to the first node and check cluster info
echo "Checking cluster info:"
docker exec -i redis-6379 redis-cli -a 1234 -c cluster info
echo -e "\nChecking cluster nodes:"
docker exec -i redis-6379 redis-cli -a 1234 -c cluster nodes
echo -e "\nTesting basic operations:"
# Set a key-value pair
echo "Setting test key..."
docker exec -i redis-6379 redis-cli -a 1234 -c set testkey "Hello Redis Cluster"
# Get the value
echo "Getting test key..."
docker exec -i redis-6379 redis-cli -a 1234 -c get testkey
# Test slot redirection by setting keys that hash to different slots
echo -e "\nTesting slot redirection with different keys..."
for i in {1..5}; do
docker exec -i redis-6379 redis-cli -a 1234 -c set "key$i" "value$i"
docker exec -i redis-6379 redis-cli -a 1234 -c get "key$i"
done
chmod +x ~/redis-cluster/test-cluster.sh
现在让我们运行测试脚本,连接到 Redis 集群并验证其是否正常工作:
~/redis-cluster/test-cluster.sh
Testing connection to Redis Cluster...
Checking cluster info:
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0
total_cluster_links_buffer_limit_exceeded:0
Checking cluster nodes:
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
6edf0eb190d33dd3168cc27d1b5a15debf4edb2f 172.20.0.2:6379@16379 myself,master - 0 0 0 connected
Testing basic operations:
Setting test key...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
CLUSTERDOWN Hash slot not served
Getting test key...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
CLUSTERDOWN Hash slot not served
Testing slot redirection with different keys...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
CLUSTERDOWN Hash slot not served
CLUSTERDOWN Hash slot not served
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
CLUSTERDOWN Hash slot not served
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
CLUSTERDOWN Hash slot not served
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
CLUSTERDOWN Hash slot not served
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
CLUSTERDOWN Hash slot not served
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
CLUSTERDOWN Hash slot not served
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
CLUSTERDOWN Hash slot not served
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
CLUSTERDOWN Hash slot not served
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
CLUSTERDOWN Hash slot not served
我发现问题所在。Redis 集群尚未正确配置。cluster_state:fail
和 CLUSTERDOWN Hash slot not served
错误表明集群尚未正确初始化。我们需要先创建集群,然后才能测试它。
让我们运行集群创建命令以正确设置 Redis 集群:
docker exec -i redis-6379 redis-cli -a 1234 --cluster create 172.20.0.2:6379 172.20.0.3:6380 172.20.0.4:6381 172.20.0.5:6382 172.20.0.6:6383 172.20.0.7:6384 --cluster-replicas 1 --cluster-yes
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 172.20.0.6:6383 to 172.20.0.2:6379
Adding replica 172.20.0.7:6384 to 172.20.0.3:6380
Adding replica 172.20.0.5:6382 to 172.20.0.4:6381
M: 6edf0eb190d33dd3168cc27d1b5a15debf4edb2f 172.20.0.2:6379
slots:[0-5460] (5461 slots) master
M: 882d5e02369a0a2dcb944124e4d5d3a794bffcbd 172.20.0.3:6380
slots:[5461-10922] (5462 slots) master
M: 2f6549eb92d48a21dc71f96e3805c788676963c5 172.20.0.4:6381
slots:[10923-16383] (5461 slots) master
S: 082e1a23e60546119a550dbf2de90605e35efe4e 172.20.0.5:6382
replicates 2f6549eb92d48a21dc71f96e3805c788676963c5
S: a19dbeed445a5c8f68fd1a54abf76e221ea7dcc3 172.20.0.6:6383
replicates 6edf0eb190d33dd3168cc27d1b5a15debf4edb2f
S: 17c9cf456f3f6efbd40bdd824a8eedffd92b0f96 172.20.0.7:6384
replicates 882d5e02369a0a2dcb944124e4d5d3a794bffcbd
>>> 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 172.20.0.2:6379)
M: 6edf0eb190d33dd3168cc27d1b5a15debf4edb2f 172.20.0.2:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 882d5e02369a0a2dcb944124e4d5d3a794bffcbd 172.20.0.3:6380
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 2f6549eb92d48a21dc71f96e3805c788676963c5 172.20.0.4:6381
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 17c9cf456f3f6efbd40bdd824a8eedffd92b0f96 172.20.0.7:6384
slots: (0 slots) slave
replicates 882d5e02369a0a2dcb944124e4d5d3a794bffcbd
S: a19dbeed445a5c8f68fd1a54abf76e221ea7dcc3 172.20.0.6:6383
slots: (0 slots) slave
replicates 6edf0eb190d33dd3168cc27d1b5a15debf4edb2f
S: 082e1a23e60546119a550dbf2de90605e35efe4e 172.20.0.5:6382
slots: (0 slots) slave
replicates 2f6549eb92d48a21dc71f96e3805c788676963c5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Great! The Redis Cluster has been successfully created. Now we have a 6-node Redis Cluster with 3 master nodes and 3 replica nodes. Let's test the connection and verify that it's working properly:
~/redis-cluster/test-cluster.sh
Testing connection to Redis Cluster...
Checking cluster info:
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
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:1
cluster_stats_messages_ping_sent:42
cluster_stats_messages_pong_sent:39
cluster_stats_messages_sent:81
cluster_stats_messages_ping_received:34
cluster_stats_messages_pong_received:42
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:81
total_cluster_links_buffer_limit_exceeded:0
Checking cluster nodes:
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
882d5e02369a0a2dcb944124e4d5d3a794bffcbd 172.20.0.3:6380@16380 master - 0 1747301774056 2 connected 5461-10922
2f6549eb92d48a21dc71f96e3805c788676963c5 172.20.0.4:6381@16381 master - 0 1747301775159 3 connected 10923-16383
6edf0eb190d33dd3168cc27d1b5a15debf4edb2f 172.20.0.2:6379@16379 myself,master - 0 0 1 connected 0-5460
17c9cf456f3f6efbd40bdd824a8eedffd92b0f96 172.20.0.7:6384@16384 slave 882d5e02369a0a2dcb944124e4d5d3a794bffcbd 0 1747301774157 2 connected
a19dbeed445a5c8f68fd1a54abf76e221ea7dcc3 172.20.0.6:6383@16383 slave 6edf0eb190d33dd3168cc27d1b5a15debf4edb2f 0 1747301774558 1 connected
082e1a23e60546119a550dbf2de90605e35efe4e 172.20.0.5:6382@16382 slave 2f6549eb92d48a21dc71f96e3805c788676963c5 0 1747301774658 3 connected
Testing basic operations:
Setting test key...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
Getting test key...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Hello Redis Cluster
Testing slot redirection with different keys...
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
value1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
value2
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
value3
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
value4
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
OK
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
value5
太好了!Redis 集群已成功创建。现在我们有了一个包含 6 个节点的 Redis 集群,其中包含 3 个主节点和 3 个副本节点。
6 测试
让我们测试连接并验证其是否正常工作:
#!/bin/bash
# Colors for better output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
echo -e "${BLUE}=== Redis Cluster Interactive Test Tool ===${NC}"
echo -e "${YELLOW}This tool allows you to interact with your Redis Cluster${NC}\n"
# Function to execute Redis CLI commands
execute_redis_command() {
docker exec -i redis-6379 redis-cli -a 1234 -c "$@"
}
# Display cluster information
echo -e "${GREEN}Cluster Information:${NC}"
execute_redis_command cluster info | grep cluster_state
execute_redis_command cluster info | grep cluster_slots
execute_redis_command cluster info | grep cluster_known_nodes
execute_redis_command cluster info | grep cluster_size
echo -e "\n${GREEN}Cluster Nodes:${NC}"
execute_redis_command cluster nodes | awk '{print $1, $2, $3, $8, $9}'
# Interactive menu
while true; do
echo -e "\n${BLUE}Choose an operation:${NC}"
echo "1. Set a key-value pair"
echo "2. Get a value by key"
echo "3. Delete a key"
echo "4. List all keys (warning: use with caution on large datasets)"
echo "5. Check cluster info"
echo "6. Check slot distribution"
echo "7. Exit"
read -p "Enter your choice (1-7): " choice
case $choice in
1)
read -p "Enter key: " key
read -p "Enter value: " value
echo -e "\n${YELLOW}Setting $key = $value${NC}"
execute_redis_command set "$key" "$value"
;;
2)
read -p "Enter key: " key
echo -e "\n${YELLOW}Getting value for $key${NC}"
execute_redis_command get "$key"
;;
3)
read -p "Enter key to delete: " key
echo -e "\n${YELLOW}Deleting key $key${NC}"
execute_redis_command del "$key"
;;
4)
echo -e "\n${YELLOW}Listing keys (limited to 100)${NC}"
execute_redis_command --scan --count 100
;;
5)
echo -e "\n${YELLOW}Cluster Info:${NC}"
execute_redis_command cluster info
;;
6)
echo -e "\n${YELLOW}Checking slot distribution:${NC}"
execute_redis_command cluster slots
;;
7)
echo -e "\n${GREEN}Exiting. Goodbye!${NC}"
exit 0
;;
*)
echo -e "\n${RED}Invalid choice. Please try again.${NC}"
;;
esac
done
让我们使交互式测试脚本可执行,以便你可轻松测试 Redis 集群。
chmod +x ~/redis-cluster/interactive-test.sh
现在你已经成功地在本地搭建了一个 Redis Cluster 集群!这个集群包含 6 个节点:3 个主节点和 3 个从节点。
可通过如下交互式测试脚本来测试和使用该 Redis Cluster:
~/redis-cluster/interactive-test.sh
=== Redis Cluster Interactive Test Tool ===
This tool allows you to interact with your Redis Cluster
Cluster Information:
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_state:ok
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_known_nodes:6
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
cluster_size:3
Cluster Nodes:
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
882d5e02369a0a2dcb944124e4d5d3a794bffcbd 172.20.0.3:6380@16380 master connected 5461-10922
2f6549eb92d48a21dc71f96e3805c788676963c5 172.20.0.4:6381@16381 master connected 10923-16383
6edf0eb190d33dd3168cc27d1b5a15debf4edb2f 172.20.0.2:6379@16379 myself,master connected 0-5460
17c9cf456f3f6efbd40bdd824a8eedffd92b0f96 172.20.0.7:6384@16384 slave connected
a19dbeed445a5c8f68fd1a54abf76e221ea7dcc3 172.20.0.6:6383@16383 slave connected
082e1a23e60546119a550dbf2de90605e35efe4e 172.20.0.5:6382@16382 slave connected
Choose an operation:
1. Set a key-value pair
2. Get a value by key
3. Delete a key
4. List all keys (warning: use with caution on large datasets)
5. Check cluster info
6. Check slot distribution
7. Exit
Enter your choice (1-7):
你可:
- 设置键值对
- 获取键的值
- 删除键
- 列出所有键
- 查看集群信息
- 检查槽位分布
- 退出
Enter your choice (1-7): 4
Listing keys (limited to 100)
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
key3
testkey
key2
Choose an operation:
1. Set a key-value pair
2. Get a value by key
3. Delete a key
4. List all keys (warning: use with caution on large datasets)
5. Check cluster info
6. Check slot distribution
7. Exit
Enter your choice (1-7):
如想直连到集群中的某节点进行操作,可用:
docker exec -it redis-6379 redis-cli -a 1234 -c
参数说明:
- -a 1234:我们设置的密码
- -c:表示启用集群模式,这样当你操作的键不在当前节点时,会自动重定向到正确的节点
说明 Redis Cluster 已经完全可用,所有 16384 个槽位都已分配给三个主节点,并且每个主节点都有一个从节点作为副本。这个设置提供了高可用性和负载均衡功能。
如想在应用程序中连接到这个集群,可以使用主机 IP 和端口 6379-6384。
注意当不再需要这集群时,可用以下命令停止并删除所有容器:
for port in $(seq 6379 6384); do docker stop redis-${port}; docker rm redis-${port}; done
本文由博客一文多发平台 OpenWrite 发布!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。