$ docker --versionDocker version 28.0.4, build b8034c0$ docker pull redisStatus: Downloaded newer image for redis:latestdocker.io/library/redis:latest摘要:Create directories and configuration files for Redis nodesfor port in $(seq 6379 6384); do mkdir -p ~/redis-cluster/node-${port}/c
让我们为 6 个 Redis 节点创建必要的目录和配置文件。脚本设置这些配置文件:
mkdir -p ~/redis-cluster为每个 Redis 节点创建配置文件。脚本生成这些文件:
#!/bin/bash# Create directories and configuration files for Redis nodesfor port in $(seq 6379 6384); do mkdir -p ~/redis-cluster/node-${port}/confcat ~/redis-cluster/node-${port}/conf/redis.confport ${port}requirepass 1234bind 0.0.0.0protected-mode nodaemonize noappendonly yescluster-enabled yes cluster-config-file nodes.confcluster-node-timeout 5000cluster-announce-ip 127.0.0.1cluster-announce-port ${port}cluster-announce-bus-port 1${port}EOFecho "Created configuration for Redis node ${port}"doneecho "All Redis node configurations created successfully!"使脚本可执行并运行它以创建 Redis 配置文件:
chmod +x ~/redis-cluster/setup-redis-cluster.sh脚本创建 Redis 配置文件:
~/redis-cluster/setup-redis-cluster.shCreated configuration for Redis node 6379Created configuration for Redis node 6380Created configuration for Redis node 6381Created configuration for Redis node 6382Created configuration for Redis node 6383Created configuration for Redis node 6384All Redis node configurations created successfully!脚本启动所有 6 个 Redis 容器
#!/bin/bash# Start Redis containersfor port in $(seq 6379 6384); dodocker 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.confecho "Started Redis container for port ${port}"doneecho "All Redis containers started successfully!"echo "Wait a few seconds before creating the cluster..."sleep 5echo "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.shc1fde3cd17390d4733b6a3309671fe8dfaf67c9af69fe1c4e285e332a55dccc8docker: 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 allocatedRun 'docker run --help' for more informationStarted Redis container for port 63798feb0850d3cc433291fdc53ef2b1bb7d209427a20ac641f69295fc2d06dcada4Started Redis container for port 6380fd928918270dfa2837dc4bdd547b8dc210e8773804d80721c10dce7a46744319Started Redis container for port 63818842810ed383bd2495e33cd4235f45167d3345fbdd97e33980b85da92a726888Started Redis container for port 6382d3d6a114e4ad18f2ed7e009abb65487643ecf8f0192aa0a1c4bcf41f25766e79Started Redis container for port 638371f4fb5f4ece6ae05537f77d58f56ab6d6ca272eb44974dc71166edd8152a316Started Redis container for port 6384All 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 redis71f4fb5f4ece 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-6384d3d6a114e4ad 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-63838842810ed383 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-6382fd928918270d 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-63818feb0850d3cc 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-6380c1fde3cd1739 redis "docker-entrypoint.s…" 46 seconds ago Created redis-6379a991b12dca33 redis:6-alpine "docker-entrypoint.s…" 3 days ago Up 2 days 0.0.0.0:6379->6379/tcp zealous_pascalb9064b85660f redis:6-alpine "docker-entrypoint.s…" 13 days ago Up 3 days (healthy) 6379/tcp docker-redis-11a9ca3f16c39 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; doneredis-6379redis-6379redis-6380redis-6380redis-6381redis-6381redis-6382redis-6382redis-6383redis-6383redis-6384redis-6384现在检查系统上是否有其他 Redis 实例正在运行,可能会占用我们需要的端口。让我们停止使用端口 6379 的现有 Redis 容器:
docker stop zealous_pascal现在我们已经停止了使用端口 6379 的 Redis 容器,让我们再次运行脚本来启动集群所需的 Redis 容器:
~/redis-cluster/start-redis-containers.shd0569057eda29eba27df2fcf15fe1db7b00366dbfc554d033e3a7309dc44e6a9Started Redis container for port 637935ab657be7866cf6e3594275d78eb597466e4597af896eafb5bceb0aa0a0e6eaStarted Redis container for port 6380b27290b595cb787db4d888c539427b68887daf7c6096200f87de6852b484f640Started Redis container for port 6381d41fa09f89692105ac3e40a564e0762d804499d9360444c31a2514245a50d96eStarted Redis container for port 6382cebdf814c885ad97b3f5f3c6e7ae2ca54245ffe9a616d2dea5732afdcbc121b3Started Redis container for port 638317ddba5baba745f8f50ea8a24520f8dc96014dd098ac209d132214fa9d97dba1Started Redis container for port 6384All 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 集群。
现在我将执行命令来创建包含 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-yesWarning: 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 addressescat ~/redis-cluster/node-6379/conf/redis.confport 6379requirepass 1234bind 0.0.0.0protected-mode nodaemonize noappendonly yescluster-enabled yes cluster-config-file nodes.confcluster-node-timeout 5000cluster-announce-ip 172.20.0.2cluster-announce-port 6379cluster-announce-bus-port 16379EOFcat ~/redis-cluster/node-6380/conf/redis.confport 6380requirepass 1234bind 0.0.0.0protected-mode nodaemonize noappendonly yescluster-enabled yes cluster-config-file nodes.confcluster-node-timeout 5000cluster-announce-ip 172.20.0.3cluster-announce-port 6380cluster-announce-bus-port 16380EOFcat ~/redis-cluster/node-6381/conf/redis.confport 6381requirepass 1234bind 0.0.0.0protected-mode nodaemonize noappendonly yescluster-enabled yes cluster-config-file nodes.confcluster-node-timeout 5000cluster-announce-ip 172.20.0.4cluster-announce-port 6381cluster-announce-bus-port 16381EOFcat ~/redis-cluster/node-6382/conf/redis.confport 6382requirepass 1234bind 0.0.0.0protected-mode nodaemonize noappendonly yescluster-enabled yes cluster-config-file nodes.confcluster-node-timeout 5000cluster-announce-ip 172.20.0.5cluster-announce-port 6382cluster-announce-bus-port 16382EOFcat ~/redis-cluster/node-6383/conf/redis.confport 6383requirepass 1234bind 0.0.0.0protected-mode nodaemonize noappendonly yescluster-enabled yes cluster-config-file nodes.confcluster-node-timeout 5000cluster-announce-ip 172.20.0.6cluster-announce-port 6383cluster-announce-bus-port 16383EOFcat ~/redis-cluster/node-6384/conf/redis.confport 6384requirepass 1234bind 0.0.0.0protected-mode nodaemonize noappendonly yescluster-enabled yes cluster-config-file nodes.confcluster-node-timeout 5000cluster-announce-ip 172.20.0.7cluster-announce-port 6384cluster-announce-bus-port 16384EOFecho "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 容器以应用这些更改。让我们停止并删除现有的容器,然后再次启动它们,使用更新后的配置:
现在让我们再次启动 Redis 容器,使用更新后的配置:
~/redis-cluster/start-redis-containers.sh835029f336169760eae4c63f4b9042452a26954d3009fccbccefbb765b93b968Started Redis container for port 6379d08225a94f579975e7af6b8c2c877ca79c7dab7e24724e9ce938b114faf52255Started Redis container for port 638037c6dd8369886900c9fe7180141986c21e5d7c0e9c4f348424a039ce8e37f3a8Started Redis container for port 6381367eaf1f52420c9adddafd7067fb7b0bae4495f689f79827872ad559f3b0f4b9Started Redis container for port 63822468ebf310f923fff78d1df1e2597fe97ac2151c53d9dd0f2b6b255c723e0e06Started Redis container for port 6383f0f7b161e7638f762e927de895eac64ddef0cc12fe4fa2d4c1025e79e3cd8b7dStarted Redis container for port 6384All 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/bashecho "Testing connection to Redis Cluster..."# Connect to the first node and check cluster infoecho "Checking cluster info:"docker exec -i redis-6379 redis-cli -a 1234 -c cluster infoecho -e "\nChecking cluster nodes:"docker exec -i redis-6379 redis-cli -a 1234 -c cluster nodesecho -e "\nTesting basic operations:"# Set a key-value pairecho "Setting test key..."docker exec -i redis-6379 redis-cli -a 1234 -c set testkey "Hello Redis Cluster"# Get the valueecho "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 slotsecho -e "\nTesting slot redirection with different keys..."for i in {1..5}; dodocker 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"donechmod +x ~/redis-cluster/test-cluster.sh现在让我们运行测试脚本,连接到 Redis 集群并验证其是否正常工作:
~/redis-cluster/test-cluster.shTesting 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:failcluster_slots_assigned:0cluster_slots_ok:0cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:1cluster_size:0cluster_current_epoch:0cluster_my_epoch:0cluster_stats_messages_sent:0cluster_stats_messages_received:0total_cluster_links_buffer_limit_exceeded:0Checking 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 connectedTesting 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 servedGetting test key...Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.CLUSTERDOWN Hash slot not servedTesting 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 servedCLUSTERDOWN Hash slot not servedWarning: 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 servedWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.CLUSTERDOWN Hash slot not servedWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.CLUSTERDOWN Hash slot not servedWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.CLUSTERDOWN Hash slot not servedWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.CLUSTERDOWN Hash slot not servedWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.CLUSTERDOWN Hash slot not servedWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.CLUSTERDOWN Hash slot not servedWarning: 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-yesWarning: 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 - 5460Master[1] -> Slots 5461 - 10922Master[2] -> Slots 10923 - 16383Adding replica 172.20.0.6:6383 to 172.20.0.2:6379Adding replica 172.20.0.7:6384 to 172.20.0.3:6380Adding replica 172.20.0.5:6382 to 172.20.0.4:6381M: 6edf0eb190d33dd3168cc27d1b5a15debf4edb2f 172.20.0.2:6379slots:[0-5460] (5461 slots) masterM: 882d5e02369a0a2dcb944124e4d5d3a794bffcbd 172.20.0.3:6380slots:[5461-10922] (5462 slots) masterM: 2f6549eb92d48a21dc71f96e3805c788676963c5 172.20.0.4:6381slots:[10923-16383] (5461 slots) masterS: 082e1a23e60546119a550dbf2de90605e35efe4e 172.20.0.5:6382replicates 2f6549eb92d48a21dc71f96e3805c788676963c5S: a19dbeed445a5c8f68fd1a54abf76e221ea7dcc3 172.20.0.6:6383replicates 6edf0eb190d33dd3168cc27d1b5a15debf4edb2fS: 17c9cf456f3f6efbd40bdd824a8eedffd92b0f96 172.20.0.7:6384replicates 882d5e02369a0a2dcb944124e4d5d3a794bffcbd>>> Nodes configuration updated>>> Assign a different config epoch to each node>>> Sending CLUSTER MEET messages to join the clusterWaiting for the cluster to join...>>> Performing Cluster Check (using node 172.20.0.2:6379)M: 6edf0eb190d33dd3168cc27d1b5a15debf4edb2f 172.20.0.2:6379slots:[0-5460] (5461 slots) master1 additional replica(s)M: 882d5e02369a0a2dcb944124e4d5d3a794bffcbd 172.20.0.3:6380slots:[5461-10922] (5462 slots) master1 additional replica(s)M: 2f6549eb92d48a21dc71f96e3805c788676963c5 172.20.0.4:6381slots:[10923-16383] (5461 slots) master1 additional replica(s)S: 17c9cf456f3f6efbd40bdd824a8eedffd92b0f96 172.20.0.7:6384slots: (0 slots) slavereplicates 882d5e02369a0a2dcb944124e4d5d3a794bffcbdS: a19dbeed445a5c8f68fd1a54abf76e221ea7dcc3 172.20.0.6:6383slots: (0 slots) slavereplicates 6edf0eb190d33dd3168cc27d1b5a15debf4edb2fS: 082e1a23e60546119a550dbf2de90605e35efe4e 172.20.0.5:6382slots: (0 slots) slavereplicates 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.shTesting 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:okcluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0cluster_known_nodes:6cluster_size:3cluster_current_epoch:6cluster_my_epoch:1cluster_stats_messages_ping_sent:42cluster_stats_messages_pong_sent:39cluster_stats_messages_sent:81cluster_stats_messages_ping_received:34cluster_stats_messages_pong_received:42cluster_stats_messages_meet_received:5cluster_stats_messages_received:81total_cluster_links_buffer_limit_exceeded:0Checking 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-109222f6549eb92d48a21dc71f96e3805c788676963c5 172.20.0.4:6381@16381 master - 0 1747301775159 3 connected 10923-163836edf0eb190d33dd3168cc27d1b5a15debf4edb2f 172.20.0.2:6379@16379 myself,master - 0 0 1 connected 0-546017c9cf456f3f6efbd40bdd824a8eedffd92b0f96 172.20.0.7:6384@16384 slave 882d5e02369a0a2dcb944124e4d5d3a794bffcbd 0 1747301774157 2 connecteda19dbeed445a5c8f68fd1a54abf76e221ea7dcc3 172.20.0.6:6383@16383 slave 6edf0eb190d33dd3168cc27d1b5a15debf4edb2f 0 1747301774558 1 connected082e1a23e60546119a550dbf2de90605e35efe4e 172.20.0.5:6382@16382 slave 2f6549eb92d48a21dc71f96e3805c788676963c5 0 1747301774658 3 connectedTesting basic operations:Setting test key...Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.OKGetting test key...Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.Hello Redis ClusterTesting slot redirection with different keys...Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.OKWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.value1Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.OKWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.value2Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.OKvalue3Warning: 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.OKWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.value4Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.OKWarning: 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 outputRED='\033[0;31m'GREEN='\033[0;32m'YELLOW='\033[0;33m'BLUE='\033[0;34m'NC='\033[0m' # No Colorecho -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 commandsexecute_redis_command {docker exec -i redis-6379 redis-cli -a 1234 -c "$@"}# Display cluster informationecho -e "${GREEN}Cluster Information:${NC}"execute_redis_command cluster info | grep cluster_stateexecute_redis_command cluster info | grep cluster_slotsexecute_redis_command cluster info | grep cluster_known_nodesexecute_redis_command cluster info | grep cluster_sizeecho -e "\n${GREEN}Cluster Nodes:${NC}"execute_redis_command cluster nodes | awk '{print $1, $2, $3, $8, $9}'# Interactive menuwhile true; doecho -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): " choicecase $choice in1)read -p "Enter key: " keyread -p "Enter value: " valueecho -e "\n${YELLOW}Setting $key = $value${NC}"execute_redis_command set "$key" "$value";;2)read -p "Enter key: " keyecho -e "\n${YELLOW}Getting value for $key${NC}"execute_redis_command get "$key";;3)read -p "Enter key to delete: " keyecho -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}";;esacdone让我们使交互式测试脚本可执行,以便你可轻松测试 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 ClusterCluster Information:Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.cluster_state:okWarning: Using a password with '-a' or '-u' option on the command line interface may not be safe.cluster_slots_assigned:16384cluster_slots_ok:16384cluster_slots_pfail:0cluster_slots_fail:0Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.cluster_known_nodes:6Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.cluster_size:3Cluster 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-109222f6549eb92d48a21dc71f96e3805c788676963c5 172.20.0.4:6381@16381 master connected 10923-163836edf0eb190d33dd3168cc27d1b5a15debf4edb2f 172.20.0.2:6379@16379 myself,master connected 0-546017c9cf456f3f6efbd40bdd824a8eedffd92b0f96 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 pair2. Get a value by key3. Delete a key4. List all keys (warning: use with caution on large datasets)5. Check cluster info6. Check slot distribution7. ExitEnter your choice (1-7):你可:
设置键值对获取键的值删除键列出所有键查看集群信息检查槽位分布退出Enter your choice (1-7): 4Listing keys (limited to 100)Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.key3testkeykey2Choose an operation:1. Set a key-value pair2. Get a value by key3. Delete a key4. List all keys (warning: use with caution on large datasets)5. Check cluster info6. Check slot distribution7. ExitEnter your choice (1-7):如想直连到集群中的某节点进行操作,可用:
docker exec -it redis-6379 redis-cli -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本文已收录在Github,关注我,紧跟本系列专栏文章,咱们下篇再续!
魔都架构师 | 全网30W技术追随者 大厂分布式系统/数据中台实战专家 主导交易系统百万级流量调优 & 车联网平台架构 AIGC应用开发先行者 | 区块链落地实践者 以技术驱动创新,我们的征途是改变世界! 实战干货:编程严选网来源:JavaEdge