在上篇文章,我们介绍了Redis集群-哨兵模式,总结了哨兵模式的缺陷,因此Redis集群之Cluster应运而生
Redis Cluster是一个由多个主从节点组成的分布式服务器群,它具有复制、高可用和分片特性。
Redis Cluster将存储区域划分为16384的slot,每个节点负责一部分slot。
Redis Cluster要将每个节点设置成cluster模式,其没有中心节点,可水平伸缩
Redis Cluster环境由多个主节点和多个从节点组成。
我们这里采用3主3从来搭建集群环境
redis-node6379.conf
、redis-node6380.conf
、redis-node6381.conf
、redis-node6382.conf
、redis-node6383.conf
、redis-node6384.conf
# 端口号与配置文件对应
port 6379
bind 192.168.110.97
# 关闭保护模式,允许其他host客户端访问
protected-mode no
daemonize yes
# 设置redis的连接密码
requirepass 123456
# 设置集群节点间的连接密码
masterauth 123456
# 存放数据文件的文件夹,需要提前创建好,如果没有创建,在启动时会报找不到该文件夹
dir "/usr/local/redis/node6379"
# 开启cluster集群模式
cluster-enabled yes
# cluster节点的配置文件
cluster-config-file nodes-6379.conf
# 集群节点的超时时限,单位为毫秒
cluster-node-timeout 15000
redis-server redis-node6379.conf
redis-server redis-node6380.conf
redis-server redis-node6381.conf
redis-server redis-node6382.conf
redis-server redis-node6383.conf
redis-server redis-node6384.conf
启动结果:可以看到redis节点以cluster方式运行
ps -ef|grep redis
root 111490 1 0 13:39 ? 00:00:00 redis-server 192.168.110.97:6379 [cluster]
root 111654 1 0 13:40 ? 00:00:00 redis-server 192.168.110.97:6380 [cluster]
root 111684 1 0 13:40 ? 00:00:00 redis-server 192.168.110.97:6381 [cluster]
root 111708 1 0 13:40 ? 00:00:00 redis-server 192.168.110.97:6382 [cluster]
root 111732 1 0 13:40 ? 00:00:00 redis-server 192.168.110.97:6383 [cluster]
root 111762 1 0 13:40 ? 00:00:00 redis-server 192.168.110.97:6384 [cluster]
# --cluster-replicas表示主节点与从节点的比例,为1时表示一个主节点对应一个从节点
# -a 表示连接密码
redis-cli --cluster create --cluster-replicas 1 192.168.110.97:6379 192.168.110.97:6380 192.168.110.97:6381 192.168.110.97:6382 192.168.110.97:6383 192.168.110.97:6384 -a 123456
当执行上面命令的时候,会提示Can I set the above configuration?
,输入yes即可。
M: 101882bbeed0125e7c34aa86e52770c9c2c80c9a 192.168.110.97:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: a4b1ce29816f56ea7774ffb38a97ae872dc775de 192.168.110.97:6382
slots: (0 slots) slave
replicates 58381c987d61434940662e171f76f2772d6cdcb9
M: 58381c987d61434940662e171f76f2772d6cdcb9 192.168.110.97:6380
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: fa3b50ffadd07e045ca9a95881c826b6f325883b 192.168.110.97:6383
slots: (0 slots) slave
replicates f5d5ce35f0678a85b0a399721e799706b39b404b
S: 4d1601c53455a3af0b3c8f751382c1ebf513bb4f 192.168.110.97:6384
slots: (0 slots) slave
replicates 101882bbeed0125e7c34aa86e52770c9c2c80c9a
M: f5d5ce35f0678a85b0a399721e799706b39b404b 192.168.110.97:6381
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
[OK] All nodes agree about slots configuration.
# 查看集群信息
cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
# 表示集群中有6个节点
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:3
cluster_stats_messages_ping_sent:2843
cluster_stats_messages_pong_sent:3120
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:5964
cluster_stats_messages_ping_received:3120
cluster_stats_messages_pong_received:2844
cluster_stats_messages_received:5964
total_cluster_links_buffer_limit_exceeded:0
CLUSTER NODES
4d1601c53455a3af0b3c8f751382c1ebf513bb4f 192.168.110.97:6384@16384 slave 101882bbeed0125e7c34aa86e52770c9c2c80c9a 0 1666831722000 1 connected
a4b1ce29816f56ea7774ffb38a97ae872dc775de 192.168.110.97:6382@16382 slave 58381c987d61434940662e171f76f2772d6cdcb9 0 1666831724223 2 connected
f5d5ce35f0678a85b0a399721e799706b39b404b 192.168.110.97:6381@16381 myself,master - 0 1666831722000 3 connected 10923-16383
101882bbeed0125e7c34aa86e52770c9c2c80c9a 192.168.110.97:6379@16379 master - 0 1666831723000 1 connected 0-5460
fa3b50ffadd07e045ca9a95881c826b6f325883b 192.168.110.97:6383@16383 slave f5d5ce35f0678a85b0a399721e799706b39b404b 0 1666831722000 3 connected
58381c987d61434940662e171f76f2772d6cdcb9 192.168.110.97:6380@16380 master - 0 1666831721101 2 connected 5461-10922
set k2 v2
Redirected to slot [449] located at 192.168.110.97:6379
"OK"
结果表明,向redis中存储值,redis会将key分配到合适的节点的slot中去。
创建redis-node6385.conf
、redis-node6386.conf配置文件,node6385、node6386文件夹,并启动
redis-server redis-node6385.conf
redis-server redis-node6386.conf
将节点加入集群
# 192.168.110.97:6385(代表要加入集群的节点),192.168.110.97:6379(代表集群中的任意系欸但)
redis-cli --cluster add-node 192.168.110.97:6385 192.168.110.97:6379 -a 123456
>>> Adding node 192.168.110.97:6385 to cluster 192.168.110.97:6379
>>> Performing Cluster Check (using node 192.168.110.97:6379)
M: 101882bbeed0125e7c34aa86e52770c9c2c80c9a 192.168.110.97:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: a4b1ce29816f56ea7774ffb38a97ae872dc775de 192.168.110.97:6382
slots: (0 slots) slave
replicates 58381c987d61434940662e171f76f2772d6cdcb9
M: 58381c987d61434940662e171f76f2772d6cdcb9 192.168.110.97:6380
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: fa3b50ffadd07e045ca9a95881c826b6f325883b 192.168.110.97:6383
slots: (0 slots) slave
replicates f5d5ce35f0678a85b0a399721e799706b39b404b
S: 4d1601c53455a3af0b3c8f751382c1ebf513bb4f 192.168.110.97:6384
slots: (0 slots) slave
replicates 101882bbeed0125e7c34aa86e52770c9c2c80c9a
M: f5d5ce35f0678a85b0a399721e799706b39b404b 192.168.110.97:6381
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.
>>> Getting functions from cluster
>>> Send FUNCTION LIST to 192.168.110.97:6385 to verify there is no functions in it
>>> Send FUNCTION RESTORE to 192.168.110.97:6385
>>> Send CLUSTER MEET to node 192.168.110.97:6385 to make it join the cluster.
[OK] New node added correctly.
查看集群节点信息
cluster nodes
4d1601c53455a3af0b3c8f751382c1ebf513bb4f 192.168.110.97:6384@16384 slave 101882bbeed0125e7c34aa86e52770c9c2c80c9a 0 1666832855052 1 connected
fe26f4ca043e6de3433d384826b60f4bef645328 192.168.110.97:6385@16385 master - 0 1666832857170 0 connected
a4b1ce29816f56ea7774ffb38a97ae872dc775de 192.168.110.97:6382@16382 slave 58381c987d61434940662e171f76f2772d6cdcb9 0 1666832858188 2 connected
f5d5ce35f0678a85b0a399721e799706b39b404b 192.168.110.97:6381@16381 myself,master - 0 1666832853000 3 connected 10923-16383
101882bbeed0125e7c34aa86e52770c9c2c80c9a 192.168.110.97:6379@16379 master - 0 1666832856101 1 connected 0-5460
fa3b50ffadd07e045ca9a95881c826b6f325883b 192.168.110.97:6383@16383 slave f5d5ce35f0678a85b0a399721e799706b39b404b 0 1666832857000 3 connected
58381c987d61434940662e171f76f2772d6cdcb9 192.168.110.97:6380@16380 master - 0 1666832854000 2 connected 5461-10922
可以看到192.168.110.97:6385节点已经加入集群中,并作为主节点,但是没有分配slot
添加从节点
redis-cli --cluster add-node 192.168.110.97:6386 192.168.110.97:6379 --cluster-slave -a 123456
>>> Adding node 192.168.110.97:6386 to cluster 192.168.110.97:6379
>>> Performing Cluster Check (using node 192.168.110.97:6379)
M: 101882bbeed0125e7c34aa86e52770c9c2c80c9a 192.168.110.97:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: a4b1ce29816f56ea7774ffb38a97ae872dc775de 192.168.110.97:6382
slots: (0 slots) slave
replicates 58381c987d61434940662e171f76f2772d6cdcb9
M: 58381c987d61434940662e171f76f2772d6cdcb9 192.168.110.97:6380
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: fa3b50ffadd07e045ca9a95881c826b6f325883b 192.168.110.97:6383
slots: (0 slots) slave
replicates f5d5ce35f0678a85b0a399721e799706b39b404b
S: 4d1601c53455a3af0b3c8f751382c1ebf513bb4f 192.168.110.97:6384
slots: (0 slots) slave
replicates 101882bbeed0125e7c34aa86e52770c9c2c80c9a
M: fe26f4ca043e6de3433d384826b60f4bef645328 192.168.110.97:6385
slots: (0 slots) master
M: f5d5ce35f0678a85b0a399721e799706b39b404b 192.168.110.97:6381
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.
Automatically selected master 192.168.110.97:6385
>>> Send CLUSTER MEET to node 192.168.110.97:6386 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.110.97:6385.
[OK] New node added correctly.
4d1601c53455a3af0b3c8f751382c1ebf513bb4f 192.168.110.97:6384@16384 slave 101882bbeed0125e7c34aa86e52770c9c2c80c9a 0 1666833525154 1 connected
fe26f4ca043e6de3433d384826b60f4bef645328 192.168.110.97:6385@16385 master - 0 1666833526200 0 connected
a4b1ce29816f56ea7774ffb38a97ae872dc775de 192.168.110.97:6382@16382 slave 58381c987d61434940662e171f76f2772d6cdcb9 0 1666833522000 2 connected
703554496306d31820a00a4b2725251bf8c793bc 192.168.110.97:6386@16386 slave fe26f4ca043e6de3433d384826b60f4bef645328 0 1666833523000 0 connected
f5d5ce35f0678a85b0a399721e799706b39b404b 192.168.110.97:6381@16381 myself,master - 0 1666833524000 3 connected 10923-16383
101882bbeed0125e7c34aa86e52770c9c2c80c9a 192.168.110.97:6379@16379 master - 0 1666833526000 1 connected 0-5460
fa3b50ffadd07e045ca9a95881c826b6f325883b 192.168.110.97:6383@16383 slave f5d5ce35f0678a85b0a399721e799706b39b404b 0 1666833523812 3 connected
58381c987d61434940662e171f76f2772d6cdcb9 192.168.110.97:6380@16380 master - 0 1666833523000 2 connected 5461-10922
可以看到节点192.168.110.97:6386
已经加入集群,并主动选择未分配slot的主节点192.168.110.97:6385
作为其主节点
为新添加的主节点分配slot
redis-cli --cluster reshard 192.168.110.97:6385 -a 123456
>>> Performing Cluster Check (using node 192.168.110.97:6385)
M: fe26f4ca043e6de3433d384826b60f4bef645328 192.168.110.97:6385
slots: (0 slots) master
1 additional replica(s)
S: 4d1601c53455a3af0b3c8f751382c1ebf513bb4f 192.168.110.97:6384
slots: (0 slots) slave
replicates 101882bbeed0125e7c34aa86e52770c9c2c80c9a
S: a4b1ce29816f56ea7774ffb38a97ae872dc775de 192.168.110.97:6382
slots: (0 slots) slave
replicates 58381c987d61434940662e171f76f2772d6cdcb9
M: f5d5ce35f0678a85b0a399721e799706b39b404b 192.168.110.97:6381
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 101882bbeed0125e7c34aa86e52770c9c2c80c9a 192.168.110.97:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 703554496306d31820a00a4b2725251bf8c793bc 192.168.110.97:6386
slots: (0 slots) slave
replicates fe26f4ca043e6de3433d384826b60f4bef645328
S: fa3b50ffadd07e045ca9a95881c826b6f325883b 192.168.110.97:6383
slots: (0 slots) slave
replicates f5d5ce35f0678a85b0a399721e799706b39b404b
M: 58381c987d61434940662e171f76f2772d6cdcb9 192.168.110.97:6380
slots:[5461-10922] (5462 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.
# 输入想要分配的slot数
How many slots do you want to move (from 1 to 16384)? 4096
# 输入要接收slot的节点id
What is the receiving node ID? fe26f4ca043e6de3433d384826b60f4bef645328
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.
# 输入all,即表示从所有已经存在slot的节点中分配一部分slot到新节点中,也可以输入节点id,然后输入done
Source node #1: all
会询问想要分配多少个slot给新加入的主节点,并选择要接收slot的节点,已经要从哪些主节点移除slot到新主节点
删除从节点
先删除slave节点,因为slave节点没有分配slot,比较容易
# redis-cli --cluster del-node 192.168.110.97:6379 `<node-id>` 192.168.110.97:6379表示集群中的任意节点 <node-id>表示要删除的节点id
redis-cli --cluster del-node 192.168.110.97:6379 703554496306d31820a00a4b2725251bf8c793bc -a 123456
>>> Removing node 703554496306d31820a00a4b2725251bf8c793bc from cluster 192.168.110.97:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
删除主节点
删除主节点比较繁琐,因为其分配了slot,因此需要将这些slot转移到其他master节点。
# 移除要删除的主节点的slot到其他主节点
redis-cli --cluster reshard 192.168.110.97:6385 -a 123456
>>> Performing Cluster Check (using node 192.168.110.97:6385)
M: fe26f4ca043e6de3433d384826b60f4bef645328 192.168.110.97:6385
slots:[0-1364],[5461-6926],[10923-12287] (4196 slots) master
S: 4d1601c53455a3af0b3c8f751382c1ebf513bb4f 192.168.110.97:6384
slots: (0 slots) slave
replicates 101882bbeed0125e7c34aa86e52770c9c2c80c9a
S: a4b1ce29816f56ea7774ffb38a97ae872dc775de 192.168.110.97:6382
slots: (0 slots) slave
replicates 58381c987d61434940662e171f76f2772d6cdcb9
M: f5d5ce35f0678a85b0a399721e799706b39b404b 192.168.110.97:6381
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
M: 101882bbeed0125e7c34aa86e52770c9c2c80c9a 192.168.110.97:6379
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
S: fa3b50ffadd07e045ca9a95881c826b6f325883b 192.168.110.97:6383
slots: (0 slots) slave
replicates f5d5ce35f0678a85b0a399721e799706b39b404b
M: 58381c987d61434940662e171f76f2772d6cdcb9 192.168.110.97:6380
slots:[6927-10922] (3996 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.
How many slots do you want to move (from 1 to 16384)? 4196
What is the receiving node ID? 58381c987d61434940662e171f76f2772d6cdcb9
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: fe26f4ca043e6de3433d384826b60f4bef645328
Source node #2: done
Ready to move 4196 slots.
Source nodes:
M: fe26f4ca043e6de3433d384826b60f4bef645328 192.168.110.97:6385
slots:[0-1364],[5461-6926],[10923-12287] (4196 slots) master
Destination node:
M: 58381c987d61434940662e171f76f2772d6cdcb9 192.168.110.97:6380
slots:[6927-10922] (3996 slots) master
1 additional replica(s)
# 删除节点
redis-cli --cluster del-node 192.168.110.97:6379 fe26f4ca043e6de3433d384826b60f4bef645328 -a 123456
查看节点信息
可以看到,我们之前添加的主节点192.168.110.97:6381
和从节点192.168.110.97:6381
已经从集群中移除
CLUSTER NODES
4d1601c53455a3af0b3c8f751382c1ebf513bb4f 192.168.110.97:6384@16384 slave 101882bbeed0125e7c34aa86e52770c9c2c80c9a 0 1666836876655 1 connected
a4b1ce29816f56ea7774ffb38a97ae872dc775de 192.168.110.97:6382@16382 slave 58381c987d61434940662e171f76f2772d6cdcb9 0 1666836876000 9 connected
f5d5ce35f0678a85b0a399721e799706b39b404b 192.168.110.97:6381@16381 myself,master - 0 1666836874000 3 connected 12288-16383
101882bbeed0125e7c34aa86e52770c9c2c80c9a 192.168.110.97:6379@16379 master - 0 1666836875000 1 connected 1365-5460
fa3b50ffadd07e045ca9a95881c826b6f325883b 192.168.110.97:6383@16383 slave f5d5ce35f0678a85b0a399721e799706b39b404b 0 1666836875000 3 connected
58381c987d61434940662e171f76f2772d6cdcb9 192.168.110.97:6380@16380 master - 0 1666836877730 9 connected 0-1364 5461-12287
到此,我们就完成了redis集群的伸缩。
spring.redis.cluster.nodes=192.168.110.97:6379,192.168.110.97:6380,192.168.110.97:6381,192.168.110.97:6382,192.168.110.97:6383,192.168.110.97:6384
spring.redis.password=123456