如果您对Redis的了解不够深入请关注本栏目,本栏目包括Redis安装,Redis配置文件说明,Redis命令和数据类型说明,Redis持久化配置,Redis主从复制和哨兵机制。
cd utils/create-cluster/
vi create-cluster
# 测试节点总数
NODES=6
# 从机数,这里相当于三主三从(master=total/slave+1)
REPLICAS=1
# 如果不是本机测试,则如下配置十分重要(具体可参考本栏目Redis配置文件说明)
# 配置集群主机信息,192.168.8.60 根据需要改您主机的IP。
CLUSTER_HOST=192.168.8.60
# 是否开启保护模式
PROTECTED_MODE=no
./create-cluster start
[root@yiqifu-redis create-cluster]# ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
./create-cluster create
[root@yiqifu-redis create-cluster]# ./create-cluster create
>>> Performing hash slots allocation on 6 nodes…
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:30005 to 127.0.0.1:30001
Adding replica 127.0.0.1:30006 to 127.0.0.1:30002
Adding replica 127.0.0.1:30004 to 127.0.0.1:30003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: cf12868ca9b7049ba7b73757aed83cf56e3e6b09 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
M: f8521587351d126ee6618fee70bdc994c598fab0 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
M: 764d51f5a7a986f424cb218794a3fcbe493913b1 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
S: 46233ddb3462efcbf37b2d1e7229f9a54e36d9e8 127.0.0.1:30004
replicates f8521587351d126ee6618fee70bdc994c598fab0
S: 31d9d8aa6927509ec33b7534a3ddeb3e921ce171 127.0.0.1:30005
replicates 764d51f5a7a986f424cb218794a3fcbe493913b1
S: 89415c9c43b4515441f49ac285d2e0e1a80b4525 127.0.0.1:30006
replicates cf12868ca9b7049ba7b73757aed83cf56e3e6b09Can 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 joinPerforming Cluster Check (using node 127.0.0.1:30001)
M: cf12868ca9b7049ba7b73757aed83cf56e3e6b09 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 31d9d8aa6927509ec33b7534a3ddeb3e921ce171 127.0.0.1:30005
slots: (0 slots) slave
replicates 764d51f5a7a986f424cb218794a3fcbe493913b1
M: 764d51f5a7a986f424cb218794a3fcbe493913b1 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: f8521587351d126ee6618fee70bdc994c598fab0 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 46233ddb3462efcbf37b2d1e7229f9a54e36d9e8 127.0.0.1:30004
slots: (0 slots) slave
replicates f8521587351d126ee6618fee70bdc994c598fab0
S: 89415c9c43b4515441f49ac285d2e0e1a80b4525 127.0.0.1:30006
slots: (0 slots) slave
replicates cf12868ca9b7049ba7b73757aed83cf56e3e6b09
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.
# -c 表示以集群的方式连接
# -p 指定端口
redis-cli -c -p 30001
[root@yiqifu-redis create-cluster]# redis-cli -c -p 30001
127.0.0.1:30001> set aaa 111
-> Redirected to slot [10439] located at 127.0.0.1:30002
OK
127.0.0.1:30002> set bbb 222
-> Redirected to slot [5287] located at 127.0.0.1:30001
OK
127.0.0.1:30001> set ccc 333
OK
127.0.0.1:30001> set {g1}aaa 111
-> Redirected to slot [13519] located at 127.0.0.1:30003
OK
127.0.0.1:30003> set {g1}bbb 222
OK
127.0.0.1:30003> set {g1}ccc 333
OK
127.0.0.1:30003> set {g1}ddd 444
OK
127.0.0.1:30003> set {g1}eee 555
OK127.0.0.1:30003> multi
OK
127.0.0.1:30003> set {g1}bbb 222222
QUEUED
127.0.0.1:30003> set {g1}ccc 333333
QUEUED
127.0.0.1:30003> exec1) OK
2) OK127.0.0.1:30003>
./create-cluster stop
[root@yiqifu-redis create-cluster]# ./create-cluster stop
Stopping 30001
Stopping 30002
Stopping 30003
Stopping 30004
Stopping 30005
Stopping 30006
./create-cluster clean
./create-cluster start
[root@yiqifu-redis create-cluster]# ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
如果是自己配置 redis.conf 文件,我后通过 redis-server redis.conf 来启动实例,那么以下配置要正确修改才能创建集群。
# port 6391
cluster-enabled yes
# 配置集群配置文件为 nodes.conf
# nodes.conf 无需手动创建,集群启动时会自动创建。
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
根据现有Redis服务实例(这些实例是您分布在不同主机的Redis服务),使用命令手动创建集群。
redis-cli --cluster create 127.0.0.1:30001 127.0.0.1:30002 127.0.0.1:30003 127.0.0.1:30004 127.0.0.1:30005 127.0.0.1:30006 --cluster-replicas 1
参数说明
# -c 表示以集群的方式连接
redis-cli -c -p 30001
为什么要移动槽位?我想到以下几中情况:
在 Redis 中,槽(Slots)是用于分片数据的概念。Redis Cluster使用槽位来将数据分布到多个节点,以实现水平扩展和提高系统的可伸缩性和可用性。每个 Redis Cluster 包含16384个槽,这些槽可以用于分配数据。
在创建集群的时候有这样一些信息,他说明了每个主从节点所分配的槽位信息。这里如Master[0]的槽位信息是 0 ~ 5460。和每个主从节点的ID和所属关系。如下,留意这些信息,后面会用到。
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383M: fc253a3c9f2c301f7c5f6e72b47a338237b3447d 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
M: ab3db8a2ed5fc4066c3257344618d02059e88648 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
M: 5d4d3f51cf968ca13718e378b5ffd68452d70479 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
S: a82a0b7c77addee864259f02a6c38f47745c5da0 127.0.0.1:30004
replicates 5d4d3f51cf968ca13718e378b5ffd68452d70479
S: b2944e78407304948150003bc395de9165354010 127.0.0.1:30005
replicates fc253a3c9f2c301f7c5f6e72b47a338237b3447d
S: 934fc2185bd12ff361fd40ae1b26bd0355bdf6e3 127.0.0.1:30006
replicates ab3db8a2ed5fc4066c3257344618d02059e88648
移动槽位命令语法:redis-cli --cluster reshard 集群任何一个主节点
如下,我们将主机( 127.0.0.1:30001) 的2000个槽位移动到主机( 127.0.0.1:30002) 的过程。
过程要查看集群中主从的ID,如果前没有记录,可以先看5.3节看命令。
# 进入集群移动槽位命令(在我的配置环境下,以下任何一条命令都可以)
redis-cli --cluster reshard 127.0.0.1:30001
# redis-cli --cluster reshard 127.0.0.1:30002
# redis-cli --cluster reshard 127.0.0.1:30003
redis-cli --cluster info 127.0.0.1:30001
[root@yiqifu-redis create-cluster]# redis-cli --cluster info 127.0.0.1:30001
127.0.0.1:30001 (fc253a3c…) -> 0 keys | 3461 slots | 1 slaves.
127.0.0.1:30003 (5d4d3f51…) -> 0 keys | 7461 slots | 1 slaves.
127.0.0.1:30002 (ab3db8a2…) -> 0 keys | 5462 slots | 1 slaves.
redis-cli --cluster check 127.0.0.1:30001
[root@yiqifu-redis create-cluster]# redis-cli --cluster check 127.0.0.1:30001
127.0.0.1:30001 (fc253a3c…) -> 0 keys | 3461 slots | 1 slaves.
127.0.0.1:30003 (5d4d3f51…) -> 0 keys | 7461 slots | 1 slaves.
127.0.0.1:30002 (ab3db8a2…) -> 0 keys | 5462 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: fc253a3c9f2c301f7c5f6e72b47a338237b3447d 127.0.0.1:30001
slots:[2000-5460] (3461 slots) master
1 additional replica(s)
M: 5d4d3f51cf968ca13718e378b5ffd68452d70479 127.0.0.1:30003
slots:[0-1999],[10923-16383] (7461 slots) master
1 additional replica(s)
S: 934fc2185bd12ff361fd40ae1b26bd0355bdf6e3 127.0.0.1:30006
slots: (0 slots) slave
replicates ab3db8a2ed5fc4066c3257344618d02059e88648
M: ab3db8a2ed5fc4066c3257344618d02059e88648 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: a82a0b7c77addee864259f02a6c38f47745c5da0 127.0.0.1:30004
slots: (0 slots) slave
replicates 5d4d3f51cf968ca13718e378b5ffd68452d70479
S: b2944e78407304948150003bc395de9165354010 127.0.0.1:30005
slots: (0 slots) slave
replicates fc253a3c9f2c301f7c5f6e72b47a338237b3447d
[OK] All nodes agree about slots configuration.
>>> Check for open slots…
>>> Check slots coverage…
[OK] All 16384 slots covered.