docker搭建redis集群

1、配置文件

使用docker搭建redis集群必须要配置的内容,搭建几个集群,就需要几个配置文件。
配置文件中本节点
IP(cluster-announce-ip)、
端口(cluster-announce-port)、
总线端口(cluster-announce-bus-port)

需要单独配置,配置为实际可访问到服务的地址,即docker映射后的端口。

# 设置可访问IP
bind 0.0.0.0
# 设置启用集群模式
cluster-enabled yes
# 关闭保护模式(如果不是在同一台服务器上的redis服务,不关闭保护模式会导致访问不到该服务)
protected-mode no

# 设置密码
requirepass xxxxxx
# 设置主节点密码
masterauth xxxxxx

# 设置本节点IP
cluster-announce-ip xxx.x.x.x
# 设置本节点端口
cluster-announce-port 6387
#设置本节点总线端口
cluster-announce-bus-port 16387

2、启动redis服务

# docker 启动redis并设置映射地址
docker run -d --name=redis6381 -p 6381:6379 -p 16381:16379 -v /www/dk_project/projects/redis_6381/redis_config:/etc/redis -v /www/dk_project/projects/redis_6381/data:/data -v /www/dk_project/projects/redis_6381/logs:/logs redis:7.0.5 redis-server /etc/redis/redis.conf
docker run -d --name=redis6382 -p 6382:6379 -p 16382:16379 -v /www/dk_project/projects/redis_6382/redis_config:/etc/redis -v /www/dk_project/projects/redis_6382/data:/data -v /www/dk_project/projects/redis_6382/logs:/logs redis:7.0.5 redis-server /etc/redis/redis.conf
docker run -d --name=redis6383 -p 6383:6379 -p 16383:16379 -v /www/dk_project/projects/redis_6383/redis_config:/etc/redis -v /www/dk_project/projects/redis_6383/data:/data -v /www/dk_project/projects/redis_6383/logs:/logs redis:7.0.5 redis-server /etc/redis/redis.conf
docker run -d --name=redis6384 -p 6384:6379 -p 16384:16379 -v /www/dk_project/projects/redis_6384/redis_config:/etc/redis -v /www/dk_project/projects/redis_6384/data:/data -v /www/dk_project/projects/redis_6384/logs:/logs redis:7.0.5 redis-server /etc/redis/redis.conf
docker run -d --name=redis6385 -p 6385:6379 -p 16385:16379 -v /www/dk_project/projects/redis_6385/redis_config:/etc/redis -v /www/dk_project/projects/redis_6385/data:/data -v /www/dk_project/projects/redis_6385/logs:/logs redis:7.0.5 redis-server /etc/redis/redis.conf
docker run -d --name=redis6386 -p 6386:6379 -p 16386:16379 -v /www/dk_project/projects/redis_6386/redis_config:/etc/redis -v /www/dk_project/projects/redis_6386/data:/data -v /www/dk_project/projects/redis_6386/logs:/logs redis:7.0.5 redis-server /etc/redis/redis.conf
docker run -d --name=redis6387 -p 6387:6379 -p 16387:16379 -v /www/dk_project/projects/redis_6387/redis_config:/etc/redis -v /www/dk_project/projects/redis_6387/data:/data -v /www/dk_project/projects/redis_6387/logs:/logs redis:7.0.5 redis-server /etc/redis/redis.conf
docker run -d --name=redis6388 -p 6388:6379 -p 16388:16379 -v /www/dk_project/projects/redis_6388/redis_config:/etc/redis -v /www/dk_project/projects/redis_6388/data:/data -v /www/dk_project/projects/redis_6388/logs:/logs redis:7.0.5 redis-server /etc/redis/redis.conf

3、创建集群

确保redis服务都启动成功后,先进入到一台redis服务中(本机能使用redis-cli命令也可),使用命令进行集群创建。

redis-cli --cluster create xxx.xxx.xx.xx:6381 xxx.xxx.xx.xx:6382 xxx.xxx.xx.xx:6383 xxx.xxx.xx.xx:6384 xxx.xxx.xx.xx:6385 xxx.xxx.xx.xx:6386 -a xxxxxx --cluster-replicas 1
# -a 是配置redis密码
# --cluster-replicas 1 配置一个节点有几台从机
#这里是使用6台redis服务进行集群创建,设置每个节点至少1台从机,则会自动生成3个节点的集群,每个节点自动分配一个从机

该命令会自动创建集群并进行节点主从节点分配,如果失败自行研究。
其中如果redis一直找不到其他节点导致报错,可能是因为是docker单台进行部署,需要开启IP4转发(修改 /etc/sysctl.conf 配置文件:net.ipv4.ip_forward=1

4、验证集群

查询集群信息
CLUSTER INFO

127.0.0.1:6379> 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:9
cluster_my_epoch:1
cluster_stats_messages_ping_sent:7888307
cluster_stats_messages_pong_sent:7748755
cluster_stats_messages_sent:15637062
cluster_stats_messages_ping_received:7748755
cluster_stats_messages_pong_received:7888307
cluster_stats_messages_received:15637062
total_cluster_links_buffer_limit_exceeded:0

查询节点信息
CLUSTER NODES

aa21d77504e84708dfb3bbfac5245caf15454de1 xxx.xxx.xx.xx:6382@16382 myself,master - 0 1708240763000 9 connected 0-1364 5461-12287
87db139b877abef5260acad4abfc98a13d7c89dc xxx.xxx.xx.xx:6381@16381 master - 0 1708240763000 1 connected 1365-5460
64e08ec5999853e9872e2caa96a740ccc7419a56 xxx.xxx.xx.xx:6386@16386 slave 87db139b877abef5260acad4abfc98a13d7c89dc 0 1708240766231 1 connected
a879da074a4167c284ed5fd15277e02bf2d25626 xxx.xxx.xx.xx:6385@16385 slave b53ada569ade1032fcdea547a8244ee794dc7214 0 1708240764000 3 connected
b53ada569ade1032fcdea547a8244ee794dc7214 xxx.xxx.xx.xx:6383@16383 master - 0 1708240767000 3 connected 12288-16383
fe3e77c93c006a768b4151bbec630dedb42455c7 xxx.xxx.xx.xx:6384@16384 slave aa21d77504e84708dfb3bbfac5245caf15454de1 0 1708240765226 9 connected
cbb4d8289865fc39c093dc8c89d6e5a22f9e7612 xxx.xxx.xx.xx:6388@16388 slave aa21d77504e84708dfb3bbfac5245caf15454de1 0 1708240767234 9 connected
f9c3d83a04697d79522361a7dfe3c05783ed65d2 xxx.xxx.xx.xx:6387@16387 slave aa21d77504e84708dfb3bbfac5245caf15454de1 0 1708240764223 9 connected

集群的创建到这里就结束了,下面是关于集群的一些简单操作。

5、操作节点

检查集群槽点分配

redis-cli --cluster check xxx.xx.xx.xx:6381 -a xxxxxx

[root@VM-4-9-centos ~]# redis-cli  --cluster check xxx.xx.xx.xx:6381 -a xxxxxx
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
xxx.xx.xx.xx:6381 (87db139b...) -> 4096 keys | 4096 slots | 1 slaves.
xxx.xx.xx.xx:6383 (b53ada56...) -> 4097 keys | 4096 slots | 1 slaves.
xxx.xx.xx.xx:6382 (aa21d775...) -> 8192 keys | 8192 slots | 3 slaves.
[OK] 16385 keys in 3 masters.
1.00 keys per slot on average.
>>> Performing Cluster Check (using node xxx.xx.xx.xx:6381)
M: 87db139b877abef5260acad4abfc98a13d7c89dc xxx.xx.xx.xx:6381
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: cbb4d8289865fc39c093dc8c89d6e5a22f9e7612 xxx.xx.xx.xx:6388
   slots: (0 slots) slave
   replicates aa21d77504e84708dfb3bbfac5245caf15454de1
S: 64e08ec5999853e9872e2caa96a740ccc7419a56 xxx.xx.xx.xx:6386
   slots: (0 slots) slave
   replicates 87db139b877abef5260acad4abfc98a13d7c89dc
M: b53ada569ade1032fcdea547a8244ee794dc7214 xxx.xx.xx.xx:6383
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: aa21d77504e84708dfb3bbfac5245caf15454de1 xxx.xx.xx.xx:6382
   slots:[0-1364],[5461-12287] (8192 slots) master
   3 additional replica(s)
S: a879da074a4167c284ed5fd15277e02bf2d25626 xxx.xx.xx.xx:6385
   slots: (0 slots) slave
   replicates b53ada569ade1032fcdea547a8244ee794dc7214
S: f9c3d83a04697d79522361a7dfe3c05783ed65d2 xxx.xx.xx.xx:6387
   slots: (0 slots) slave
   replicates aa21d77504e84708dfb3bbfac5245caf15454de1
S: fe3e77c93c006a768b4151bbec630dedb42455c7 xxx.xx.xx.xx:6384
   slots: (0 slots) slave
   replicates aa21d77504e84708dfb3bbfac5245caf15454de1
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

添加节点(新节点->目标集群节点)

redis-cli -p 6381 --cluster add-node xxx.xx.xx.xx:6387 xxx.xx.xx.xx:6381 -a xxxxxx

[root@VM-4-9-centos ~]# redis-cli -p 6381 --cluster add-node xxx.xx.xx.xx:6387 xxx.xx.xx.xx:6381 -a xxxxxx
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node xxx.xx.xx.xx:6387 to cluster xxx.xx.xx.xx:6381
>>> Performing Cluster Check (using node xxx.xx.xx.xx:6381)
M: 87db139b877abef5260acad4abfc98a13d7c89dc xxx.xx.xx.xx:6381
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: 64e08ec5999853e9872e2caa96a740ccc7419a56 xxx.xx.xx.xx:6386
   slots: (0 slots) slave
   replicates 87db139b877abef5260acad4abfc98a13d7c89dc
M: b53ada569ade1032fcdea547a8244ee794dc7214 xxx.xx.xx.xx:6383
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: aa21d77504e84708dfb3bbfac5245caf15454de1 xxx.xx.xx.xx:6382
   slots:[0-1364],[5461-12287] (8192 slots) master
   1 additional replica(s)
S: a879da074a4167c284ed5fd15277e02bf2d25626 xxx.xx.xx.xx:6385
   slots: (0 slots) slave
   replicates b53ada569ade1032fcdea547a8244ee794dc7214
S: fe3e77c93c006a768b4151bbec630dedb42455c7 xxx.xx.xx.xx:6384
   slots: (0 slots) slave
   replicates aa21d77504e84708dfb3bbfac5245caf15454de1
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node xxx.xx.xx.xx:6387 to make it join the cluster.
[OK] New node added correctly.

添加从节点(新节点->目标集群节点)

redis-cli --cluster add-node xxx.xx.xx.xx:6388 xxx.xx.xx.xx:6381 -a xxxxxx --cluster-slave

[root@VM-4-9-centos ~]# redis-cli --cluster add-node xxx.xx.xx.xx:6388 xxx.xx.xx.xx:6381 -a xxxxxx --cluster-slave
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node xxx.xx.xx.xx:6388 to cluster xxx.xx.xx.xx:6381
>>> Performing Cluster Check (using node xxx.xx.xx.xx:6381)
M: 87db139b877abef5260acad4abfc98a13d7c89dc xxx.xx.xx.xx:6381
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: 64e08ec5999853e9872e2caa96a740ccc7419a56 xxx.xx.xx.xx:6386
   slots: (0 slots) slave
   replicates 87db139b877abef5260acad4abfc98a13d7c89dc
M: b53ada569ade1032fcdea547a8244ee794dc7214 xxx.xx.xx.xx:6383
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: aa21d77504e84708dfb3bbfac5245caf15454de1 xxx.xx.xx.xx:6382
   slots:[0-1364],[5461-12287] (8192 slots) master
   1 additional replica(s)
S: a879da074a4167c284ed5fd15277e02bf2d25626 xxx.xx.xx.xx:6385
   slots: (0 slots) slave
   replicates b53ada569ade1032fcdea547a8244ee794dc7214
S: fe3e77c93c006a768b4151bbec630dedb42455c7 xxx.xx.xx.xx:6384
   slots: (0 slots) slave
   replicates aa21d77504e84708dfb3bbfac5245caf15454de1
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Automatically selected master xxx.xx.xx.xx:6381
>>> Send CLUSTER MEET to node xxx.xx.xx.xx:6388 to make it join the cluster.
Waiting for the cluster to join

>>> Configure node as replica of xxx.xx.xx.xx:6381.
[OK] New node added correctly.
给新节点分配槽位

redis-cli -a xxxxxx --cluster reshard xxx.xx.xx.xx:6387

[root@VM-4-9-centos ~]# redis-cli -a xxxxxx --cluster reshard xxx.xx.xx.xx:6387
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node xxx.xx.xx.xx:6387)
M: f9c3d83a04697d79522361a7dfe3c05783ed65d2 xxx.xx.xx.xx:6387
   slots:[0-1364],[5461-8191] (4096 slots) master
   1 additional replica(s)
S: cbb4d8289865fc39c093dc8c89d6e5a22f9e7612 xxx.xx.xx.xx:6388
   slots: (0 slots) slave
   replicates 87db139b877abef5260acad4abfc98a13d7c89dc
M: aa21d77504e84708dfb3bbfac5245caf15454de1 xxx.xx.xx.xx:6382
   slots:[8192-12287] (4096 slots) master
   1 additional replica(s)
S: a879da074a4167c284ed5fd15277e02bf2d25626 xxx.xx.xx.xx:6385
   slots: (0 slots) slave
   replicates b53ada569ade1032fcdea547a8244ee794dc7214
M: b53ada569ade1032fcdea547a8244ee794dc7214 xxx.xx.xx.xx:6383
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: 87db139b877abef5260acad4abfc98a13d7c89dc xxx.xx.xx.xx:6381
   slots:[1365-5460] (4096 slots) master
   1 additional replica(s)
S: fe3e77c93c006a768b4151bbec630dedb42455c7 xxx.xx.xx.xx:6384
   slots: (0 slots) slave
   replicates aa21d77504e84708dfb3bbfac5245caf15454de1
S: 64e08ec5999853e9872e2caa96a740ccc7419a56 xxx.xx.xx.xx:6386
   slots: (0 slots) slave
   replicates f9c3d83a04697d79522361a7dfe3c05783ed65d2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)?

执行命令后会询问你想给这个节点分配多少个槽位,输入个数后回车即可

What is the receiving node ID?

然后会再询问你要给那个ID节点分配槽位,需要输入目标节点的ID

What is the receiving node ID? f9c3d83a04697d79522361a7dfe3c05783ed65d2
Please enter all the source node IDs.
  Type 'all' to use all the nodes as source nodes for the hash slots.
  Type 'done' once you entered all the source nodes IDs.
Source node #1:

继续询问你打算从哪个节点上将槽位让出来给目标节点,如果不想指定让出槽位的节点,可以输入all,如果只从一个节点上让出槽位,则输入对应节点ID后回车,会继续询问你第二个节点,输入done即可。

Do you want to proceed with the proposed reshard plan (yes/no)?

输入yes开始分配

手动移动槽点

redis-cli --cluster reshard 节点地址:端口 --cluster-from 源节点ID --cluster-to 目标节点ID --cluster-slots 槽点数量 --cluster-yes -a xxxxx

修复集群节点

如果分配节点时出现意外关闭,导致故障
[WARNING] Node xxx.xx.xx.xx:6387 has slots in importing state 886.
[WARNING] The following slots are open: 886.

[root@VM-4-9-centos ~]# redis-cli --cluster reshard xxx.xx.xx.xx:6381  --cluster-from f9c3d83a04697d79522361a7dfe3c05783ed65d2 --cluster-to 87db139b877abef5260acad4abfc98a13d7c89dc --cluster-slots 2048 --cluster-yes -a xxxxxx 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node xxx.xx.xx.xx:6381)
M: 87db139b877abef5260acad4abfc98a13d7c89dc xxx.xx.xx.xx:6381
   slots:[0-885],[2730-5460] (3617 slots) master
   1 additional replica(s)
S: cbb4d8289865fc39c093dc8c89d6e5a22f9e7612 xxx.xx.xx.xx:6388
   slots: (0 slots) slave
   replicates 87db139b877abef5260acad4abfc98a13d7c89dc
S: 64e08ec5999853e9872e2caa96a740ccc7419a56 xxx.xx.xx.xx:6386
   slots: (0 slots) slave
   replicates f9c3d83a04697d79522361a7dfe3c05783ed65d2
M: b53ada569ade1032fcdea547a8244ee794dc7214 xxx.xx.xx.xx:6383
   slots:[13653-16383] (2731 slots) master
   1 additional replica(s)
M: aa21d77504e84708dfb3bbfac5245caf15454de1 xxx.xx.xx.xx:6382
   slots:[9558-12287] (2730 slots) master
   1 additional replica(s)
S: a879da074a4167c284ed5fd15277e02bf2d25626 xxx.xx.xx.xx:6385
   slots: (0 slots) slave
   replicates b53ada569ade1032fcdea547a8244ee794dc7214
M: f9c3d83a04697d79522361a7dfe3c05783ed65d2 xxx.xx.xx.xx:6387
   slots:[886-2729],[5461-9557],[12288-13652] (7306 slots) master
   1 additional replica(s)
S: fe3e77c93c006a768b4151bbec630dedb42455c7 xxx.xx.xx.xx:6384
   slots: (0 slots) slave
   replicates aa21d77504e84708dfb3bbfac5245caf15454de1
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
[WARNING] Node xxx.xx.xx.xx:6381 has slots in importing state 886.
[WARNING] Node xxx.xx.xx.xx:6387 has slots in migrating state 886.
[WARNING] The following slots are open: 886.
>>> Check slots coverage...
[OK] All 16384 slots covered.
*** Please fix your cluster problems before resharding

redis-cli --cluster fix xxx.xx.xx.xx:6387
可使用上面命令进行自动修复

[root@VM-4-9-centos ~]# redis-cli --cluster fix xxx.xx.xx.xx:6387 -a xxxxxx 
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
xxx.xx.xx.xx:6387 (f9c3d83a...) -> 4071 keys | 4057 slots | 1 slaves.
xxx.xx.xx.xx:6382 (aa21d775...) -> 4100 keys | 4096 slots | 1 slaves.
xxx.xx.xx.xx:6383 (b53ada56...) -> 4088 keys | 4096 slots | 1 slaves.
xxx.xx.xx.xx:6381 (87db139b...) -> 4126 keys | 4135 slots | 1 slaves.
[OK] 16385 keys in 4 masters.
1.00 keys per slot on average.
>>> Performing Cluster Check (using node xxx.xx.xx.xx:6387)
M: f9c3d83a04697d79522361a7dfe3c05783ed65d2 xxx.xx.xx.xx:6387
   slots:[6866-9557],[12288-13652] (4057 slots) master
   1 additional replica(s)
S: cbb4d8289865fc39c093dc8c89d6e5a22f9e7612 xxx.xx.xx.xx:6388
   slots: (0 slots) slave
   replicates 87db139b877abef5260acad4abfc98a13d7c89dc
M: aa21d77504e84708dfb3bbfac5245caf15454de1 xxx.xx.xx.xx:6382
   slots:[886-2251],[9558-12287] (4096 slots) master
   1 additional replica(s)
S: a879da074a4167c284ed5fd15277e02bf2d25626 xxx.xx.xx.xx:6385
   slots: (0 slots) slave
   replicates b53ada569ade1032fcdea547a8244ee794dc7214
M: b53ada569ade1032fcdea547a8244ee794dc7214 xxx.xx.xx.xx:6383
   slots:[2252-2729],[5461-6347],[13653-16383] (4096 slots) master
   1 additional replica(s)
M: 87db139b877abef5260acad4abfc98a13d7c89dc xxx.xx.xx.xx:6381
   slots:[0-885],[2730-5460],[6348-6865] (4135 slots) master
   1 additional replica(s)
S: fe3e77c93c006a768b4151bbec630dedb42455c7 xxx.xx.xx.xx:6384
   slots: (0 slots) slave
   replicates aa21d77504e84708dfb3bbfac5245caf15454de1
S: 64e08ec5999853e9872e2caa96a740ccc7419a56 xxx.xx.xx.xx:6386
   slots: (0 slots) slave
   replicates f9c3d83a04697d79522361a7dfe3c05783ed65d2
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
[WARNING] Node xxx.xx.xx.xx:6387 has slots in migrating state 6866.
[WARNING] Node xxx.xx.xx.xx:6381 has slots in importing state 6866.
[WARNING] The following slots are open: 6866.
>>> Fixing open slot 6866
Set as migrating in: xxx.xx.xx.xx:6387
Set as importing in: xxx.xx.xx.xx:6381
>>> Case 1: Moving slot 6866 from xxx.xx.xx.xx:6387 to xxx.xx.xx.xx:6381
Moving slot 6866 from xxx.xx.xx.xx:6387 to xxx.xx.xx.xx:6381: ..
>>> Check slots coverage...
[OK] All 16384 slots covered.

如果修复失败,可以手动登录目标服务进行修复
cluster setslot 886 stable

127.0.0.1:6379> cluster setslot 886 stable
OK
平衡槽点覆盖率

redis-cli -a xxxxxx --cluster rebalance xxx.xx.xx.xx:6387

root@09b7395e0bdb:/data# redis-cli -a xxxxxx --cluster rebalance xxx.xx.xx.xx:6387
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node xxx.xx.xx.xx:6387)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Rebalancing across 4 nodes. Total weight = 4.00
Moving 1366 slots from xxx.xx.xx.xx:6387 to xxx.xx.xx.xx:6382
#############################################...
Moving 1365 slots from xxx.xx.xx.xx:6387 to xxx.xx.xx.xx:6383
#############################################...
Moving 479 slots from xxx.xx.xx.xx:6387 to xxx.xx.xx.xx:6381
#############################################...

自动均衡每个节点的槽位,持续输出#需要耐心等待。

删除节点(需要将槽点移空)

redis-cli --cluster del-node xxx.xx.xx.xx:6387 [node-id] -a xxxxxx

[root@VM-4-9-centos ~]# redis-cli --cluster del-node xxx.xxx.xx.xx:6388 cbb4d8289865fc39c093dc8c89d6e5a22f9e7612   -a xxxxxx
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node cbb4d8289865fc39c093dc8c89d6e5a22f9e7612 from cluster xxx.xxx.xx.xx:6388
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.

粗略解释一下槽位:
redis有16384个槽位,存储的每个数据经过哈希之后放入对应的槽,每个主节点认领一部分槽位,当进行查询时,根据数据在哪个槽上则去哪个主节点上获取数据(所以当使用redis-cli进入服务查询key时,如果没有加-c表名该服务是集群,则查询一个不在当前服务所拥有的槽的key时,会报错(error) MOVED 15059 xxx.xx.xx.xx:6383)。

  • 20
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要使用 Docker 搭建 Redis 集群,你可以按照以下步骤进行操作: 1. 确保已经安装并配置好了 Docker。 2. 创建一个用于 Redis 集群的自定义网络: ```bash docker network create redis-net ``` 3. 创建 Redis 主节点的容器。假设我们需要创建3个主节点,分别使用端口 7001、7002 和 7003: ```bash docker run -d --name redis1 --network redis-net -p 7001:6379 redis redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes docker run -d --name redis2 --network redis-net -p 7002:6379 redis redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes docker run -d --name redis3 --network redis-net -p 7003:6379 redis redis-server --port 6379 --cluster-enabled yes --cluster-config-file nodes.conf --cluster-node-timeout 5000 --appendonly yes ``` 这将创建三个 Redis 主节点容器,并将端口映射到主机的 7001、7002 和 7003 端口。 4. 创建 Redis 集群: ```bash docker run -it --rm --network redis-net redis redis-cli --cluster create <node-ip>:<node-port> <node-ip>:<node-port> <node-ip>:<node-port> ``` 将 `<node-ip>` 替换为你主机的 IP 地址, `<node-port>` 替换为容器中 Redis 主节点的端口(6379)。 例如: ```bash docker run -it --rm --network redis-net redis redis-cli --cluster create 172.18.0.2:6379 172.18.0.3:6379 172.18.0.4:6379 ``` 这将创建一个 Redis 集群,并将三个主节点添加到集群中。 5. 检查集群状态: ```bash docker run -it --rm --network redis-net redis redis-cli --cluster check <node-ip>:<node-port> ``` 例如: ```bash docker run -it --rm --network redis-net redis redis-cli --cluster check 172.18.0.2:6379 ``` 这将检查集群的状态,并输出每个节点的信息。 这样,你就成功使用 Docker 搭建了一个 Redis 集群。请根据你的实际需求进行相应的配置和调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值