共计 12401 个字符,预计需要花费 32 分钟才能阅读完成。
此处记录下一些redis-cluster中cli的一些指令
查看集群状态信息
127.0.0.1:7000> CLUSTER info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:9
cluster_size:3
cluster_current_epoch:13
cluster_my_epoch:11
cluster_stats_messages_sent:2219
cluster_stats_messages_received:820
查看集群实例状态
上面cluster_state:ok,但是博主这里slave有一个有问题,看来slave并不影响集群使用,只是高可用性降低了
127.0.0.1:7000> CLUSTER nodes
44bc79bddc52458fa7660d348a09fe96148f805e 127.0.0.1:7001 master - 0 1675169631335 12 connected 5461-6825 6827-10922
d6f1de5130c5150e1a544b3e292fc400f7cfd429 127.0.0.1:7003 slave 2b7b2a56a221d48fb3ff83c03de1afc5611916d3 0 1675169632343 11 connected
8f2f0e2e8ca7b431c93f4975a306853c3624937b 127.0.0.1:7006 slave 44bc79bddc52458fa7660d348a09fe96148f805e 0 1675169633350 12 connected
c10adb1919dbfc8b7aa3e74607d657a248677835 127.0.0.1:7004 slave 2b7b2a56a221d48fb3ff83c03de1afc5611916d3 0 1675169633350 11 connected
60d458a88ec083ffce11bce2196b36f5f9b12768 127.0.0.1:7007 slave f9bb669d59aff71c5d3477494b4ff88203d8ce63 0 1675169632343 13 connected
de39bf610b53bc382c09b2d85b8121841e8f1410 127.0.0.1:7008 slave,fail f9bb669d59aff71c5d3477494b4ff88203d8ce63 1675169496269 1675169496265 13 disconnected
f9bb669d59aff71c5d3477494b4ff88203d8ce63 127.0.0.1:7002 master - 0 1675169632846 13 connected 6826 10923-16383
2b7b2a56a221d48fb3ff83c03de1afc5611916d3 127.0.0.1:7000 myself,master - 0 0 11 connected 0-5460
d09f47b1dd2674f119f8b6406cf140f1704946a7 127.0.0.1:7005 slave 44bc79bddc52458fa7660d348a09fe96148f805e 0 1675169631335 12 connected
把那个disconnected 的实例节点起来即可,博主这里是漏启动了。。。
加入新实例节点
之前使用工具redis-trib.rb进行增加节点,此处可以通过cli的方式增加master
127.0.0.1:7000> CLUSTER MEET 127.0.0.1 7009
OK
127.0.0.1:7000> CLUSTER nodes
44bc79bddc52458fa7660d348a09fe96148f805e 127.0.0.1:7001 master - 0 1675170392351 12 connected 5461-6825 6827-10922
d6f1de5130c5150e1a544b3e292fc400f7cfd429 127.0.0.1:7003 slave 2b7b2a56a221d48fb3ff83c03de1afc5611916d3 0 1675170393360 11 connected
f190d9c102242cc5898fd4eaca4311c17b676858 127.0.0.1:7009 master - 0 1675170393360 10 connected
8f2f0e2e8ca7b431c93f4975a306853c3624937b 127.0.0.1:7006 slave 44bc79bddc52458fa7660d348a09fe96148f805e 0 1675170392857 12 connected
c10adb1919dbfc8b7aa3e74607d657a248677835 127.0.0.1:7004 slave 2b7b2a56a221d48fb3ff83c03de1afc5611916d3 0 1675170393864 11 connected
60d458a88ec083ffce11bce2196b36f5f9b12768 127.0.0.1:7007 slave f9bb669d59aff71c5d3477494b4ff88203d8ce63 0 1675170392351 13 connected
de39bf610b53bc382c09b2d85b8121841e8f1410 127.0.0.1:7008 slave f9bb669d59aff71c5d3477494b4ff88203d8ce63 0 1675170391848 13 connected
f9bb669d59aff71c5d3477494b4ff88203d8ce63 127.0.0.1:7002 master - 0 1675170391848 13 connected 6826 10923-16383
2b7b2a56a221d48fb3ff83c03de1afc5611916d3 127.0.0.1:7000 myself,master - 0 0 11 connected 0-5460
d09f47b1dd2674f119f8b6406cf140f1704946a7 127.0.0.1:7005 slave 44bc79bddc52458fa7660d348a09fe96148f805e 0 1675170393864 12 connected
手动方式迁移slots
之前使用redis-trib.rb的reshard均衡迁移,此处则演示在cli中迁移指定slots迁移
创建模拟数据
# 通过hash tag方式创建模拟数据,只要key中{}内数据相同,则key都在同一slots下
[root@blog ~]# redis-cli -c -p 7001
127.0.0.1:7001> set key:{name}:1 value:name:xadocker1
OK
127.0.0.1:7001> set key:{name}:2 value:name:xadocker2
OK
127.0.0.1:7001> set key:{name}:3 value:name:xadocker3
OK
# 查看key所在的slots
127.0.0.1:7001> CLUSTER KEYSLOT key:{name}:1
(integer) 5798
127.0.0.1:7001> CLUSTER KEYSLOT key:{name}:2
(integer) 5798
127.0.0.1:7001> CLUSTER KEYSLOT key:{name}:3
(integer) 5798
查看当前集群slots信息
127.0.0.1:7001> cluster nodes
f9bb669d59aff71c5d3477494b4ff88203d8ce63 127.0.0.1:7002 master - 0 1675173887698 13 connected 6826 6918 10923-16383
44bc79bddc52458fa7660d348a09fe96148f805e 127.0.0.1:7001 myself,master - 0 0 12 connected 5461-6825 6827-6917 6919-10922
2b7b2a56a221d48fb3ff83c03de1afc5611916d3 127.0.0.1:7000 master - 0 1675173887196 11 connected 0-5460
c10adb1919dbfc8b7aa3e74607d657a248677835 127.0.0.1:7004 slave 2b7b2a56a221d48fb3ff83c03de1afc5611916d3 0 1675173886691 11 connected
8f2f0e2e8ca7b431c93f4975a306853c3624937b 127.0.0.1:7006 slave 44bc79bddc52458fa7660d348a09fe96148f805e 0 1675173887196 12 connected
d6f1de5130c5150e1a544b3e292fc400f7cfd429 127.0.0.1:7003 slave 2b7b2a56a221d48fb3ff83c03de1afc5611916d3 0 1675173887700 11 connected
f190d9c102242cc5898fd4eaca4311c17b676858 127.0.0.1:7009 master - 0 1675173886691 10 connected
d09f47b1dd2674f119f8b6406cf140f1704946a7 127.0.0.1:7005 slave 44bc79bddc52458fa7660d348a09fe96148f805e 0 1675173886187 12 connected
60d458a88ec083ffce11bce2196b36f5f9b12768 127.0.0.1:7007 slave f9bb669d59aff71c5d3477494b4ff88203d8ce63 0 1675173886187 13 connected
de39bf610b53bc382c09b2d85b8121841e8f1410 127.0.0.1:7008 slave f9bb669d59aff71c5d3477494b4ff88203d8ce63 0 1675173887196 13 connected
可以发现此时slots 5798 坐落于:44bc79bddc52458fa7660d348a09fe96148f805e 127.0.0.1:7001
我们将这个slot迁移到:f9bb669d59aff71c5d3477494b4ff88203d8ce63 127.0.0.1:7002
在目标实例7002上将slots 5789设置为导入状态
[root@blog ~]# redis-cli -c -p 7002
127.0.0.1:7002> CLUSTER SETSLOT 5798 importing 44bc79bddc52458fa7660d348a09fe96148f805e
OK
127.0.0.1:7002> cluster nodes
f9bb669d59aff71c5d3477494b4ff88203d8ce63 127.0.0.1:7002 myself,master - 0 0 13 connected 6826 6918 10923-16383 [5798-<-44bc79bddc52458fa7660d348a09fe96148f805e]
44bc79bddc52458fa7660d348a09fe96148f805e 127.0.0.1:7001 master - 0 1675174451061 12 connected 5461-6825 6827-6917 6919-10922
c10adb1919dbfc8b7aa3e74607d657a248677835 127.0.0.1:7004 slave 2b7b2a56a221d48fb3ff83c03de1afc5611916d3 0 1675174453081 11 connected
60d458a88ec083ffce11bce2196b36f5f9b12768 127.0.0.1:7007 slave f9bb669d59aff71c5d3477494b4ff88203d8ce63 0 1675174452072 13 connected
d09f47b1dd2674f119f8b6406cf140f1704946a7 127.0.0.1:7005 slave 44bc79bddc52458fa7660d348a09fe96148f805e 0 1675174452573 12 connected
8f2f0e2e8ca7b431c93f4975a306853c3624937b 127.0.0.1:7006 slave 44bc79bddc52458fa7660d348a09fe96148f805e 0 1675174452576 12 connected
de39bf610b53bc382c09b2d85b8121841e8f1410 127.0.0.1:7008 slave f9bb669d59aff71c5d3477494b4ff88203d8ce63 0 1675174451060 13 connected
f190d9c102242cc5898fd4eaca4311c17b676858 127.0.0.1:7009 master - 0 1675174451566 10 connected
d6f1de5130c5150e1a544b3e292fc400f7cfd429 127.0.0.1:7003 slave 2b7b2a56a221d48fb3ff83c03de1afc5611916d3 0 1675174452573 11 connected
2b7b2a56a221d48fb3ff83c03de1afc5611916d3 127.0.0.1:7000 master - 0 1675174451566 11 connected 0-5460
在源实例7001上将slots 5798设置为迁移状态
127.0.0.1:7001> cluster nodes
f9bb669d59aff71c5d3477494b4ff88203d8ce63 127.0.0.1:7002 master - 0 1675174535878 13 connected 6826 6918 10923-16383
44bc79bddc52458fa7660d348a09fe96148f805e 127.0.0.1:7001 myself,master - 0 0 12 connected 5461-6825 6827-6917 6919-10922
2b7b2a56a221d48fb3ff83c03de1afc5611916d3 127.0.0.1:7000 master - 0 1675174536381 11 connected 0-5460
c10adb1919dbfc8b7aa3e74607d657a248677835 127.0.0.1:7004 slave 2b7b2a56a221d48fb3ff83c03de1afc5611916d3 0 1675174536381 11 connected
8f2f0e2e8ca7b431c93f4975a306853c3624937b 127.0.0.1:7006 slave 44bc79bddc52458fa7660d348a09fe96148f805e 0 1675174536885 12 connected
d6f1de5130c5150e1a544b3e292fc400f7cfd429 127.0.0.1:7003 slave 2b7b2a56a221d48fb3ff83c03de1afc5611916d3 0 1675174536885 11 connected
f190d9c102242cc5898fd4eaca4311c17b676858 127.0.0.1:7009 master - 0 1675174535374 10 connected
d09f47b1dd2674f119f8b6406cf140f1704946a7 127.0.0.1:7005 slave 44bc79bddc52458fa7660d348a09fe96148f805e 0 1675174535374 12 connected
60d458a88ec083ffce11bce2196b36f5f9b12768 127.0.0.1:7007 slave f9bb669d59aff71c5d3477494b4ff88203d8ce63 0 1675174534870 13 connected
de39bf610b53bc382c09b2d85b8121841e8f1410 127.0.0.1:7008 slave f9bb669d59aff71c5d3477494b4ff88203d8ce63 0 1675174535374 13 connected
127.0.0.1:7001>
127.0.0.1:7001> CLUSTER SETSLOT 5798 migrating f9bb669d59aff71c5d3477494b4ff88203d8ce63
OK
127.0.0.1:7001> cluster nodes
f9bb669d59aff71c5d3477494b4ff88203d8ce63 127.0.0.1:7002 master - 0 1675174566093 13 connected 6826 6918 10923-16383
44bc79bddc52458fa7660d348a09fe96148f805e 127.0.0.1:7001 myself,master - 0 0 12 connected 5461-6825 6827-6917 6919-10922 [5798->-f9bb669d59aff71c5d3477494b4ff88203d8ce63]
2b7b2a56a221d48fb3ff83c03de1afc5611916d3 127.0.0.1:7000 master - 0 1675174566093 11 connected 0-5460
c10adb1919dbfc8b7aa3e74607d657a248677835 127.0.0.1:7004 slave 2b7b2a56a221d48fb3ff83c03de1afc5611916d3 0 1675174566093 11 connected
8f2f0e2e8ca7b431c93f4975a306853c3624937b 127.0.0.1:7006 slave 44bc79bddc52458fa7660d348a09fe96148f805e 0 1675174566596 12 connected
d6f1de5130c5150e1a544b3e292fc400f7cfd429 127.0.0.1:7003 slave 2b7b2a56a221d48fb3ff83c03de1afc5611916d3 0 1675174567605 11 connected
f190d9c102242cc5898fd4eaca4311c17b676858 127.0.0.1:7009 master - 0 1675174565589 10 connected
d09f47b1dd2674f119f8b6406cf140f1704946a7 127.0.0.1:7005 slave 44bc79bddc52458fa7660d348a09fe96148f805e 0 1675174566596 12 connected
60d458a88ec083ffce11bce2196b36f5f9b12768 127.0.0.1:7007 slave f9bb669d59aff71c5d3477494b4ff88203d8ce63 0 1675174565589 13 connected
de39bf610b53bc382c09b2d85b8121841e8f1410 127.0.0.1:7008 slave f9bb669d59aff71c5d3477494b4ff88203d8ce63 0 1675174567605 13 connected
迁移slots内得数据
MIGRATE host port key dbid timeout [COPY | REPLACE]
MIGRATE host port "" dbid timeout [COPY | REPLACE] KEYS key1 key2 ... keyN
注意:
- 集群模式只有db0,单例模式才有db[0-15]
- 如果指定了 COPY 选项,表示不删除源节点上的key
- 如果指定了 REPLACE 选项,替换目标节点上已存在的key(如果存在)
迁移源7001实例的slots 5798内的数据到目标7002实例的slots 5798上
# 此时数据还在7001实例内
127.0.0.1:7001> MGET key:{name}:1 key:{name}:2 key:{name}:3
1) "value:name:xadocker1"
2) "value:name:xadocker2"
3) "value:name:xadocker3"
# 开始迁移数据
127.0.0.1:7001> MIGRATE 127.0.0.1 7002 "" 0 1000 keys key:{name}:1 key:{name}:2 key:{name}:3
OK
# 此时在7001和7002发起查询
127.0.0.1:7001> MGET key:{name}:1 key:{name}:2 key:{name}:3
(error) ASK 5798 127.0.0.1:7002
127.0.0.1:7002> MGET key:{name}:1 key:{name}:2 key:{name}:3
-> Redirected to slot [5798] located at 127.0.0.1:7001
(error) ASK 5798 127.0.0.1:7002
将slots指派给目标node
上面都收到ask异常响应,说明此时slots处于迁移状态,数据其实已经迁移完成的,只是需要更新下每个节点的slots的node映射关系后才能查询到该slots数据
# 不能再源7001实例上先运行slots指派node,会提示
127.0.0.1:7001> CLUSTER SETSLOT 5798 node f9bb669d59aff71c5d3477494b4ff88203d8ce63
(error) ERR Can't assign hashslot 5798 to a different node while I still hold keys for this hash slot.
# 得在非源实例得任意一个实例上执行,比如7002实例上执行
[root@blog ~]# redis-cli -c -p 7002
127.0.0.1:7002> CLUSTER SETSLOT 5798 node f9bb669d59aff71c5d3477494b4ff88203d8ce63
OK
127.0.0.1:7002> cluster nodes
f9bb669d59aff71c5d3477494b4ff88203d8ce63 127.0.0.1:7002 myself,master - 0 0 13 connected 5798 6826 6918 10923-16383
44bc79bddc52458fa7660d348a09fe96148f805e 127.0.0.1:7001 master - 0 1675175285526 12 connected 5461-5797 5799-6825 6827-6917 6919-10922
c10adb1919dbfc8b7aa3e74607d657a248677835 127.0.0.1:7004 slave 2b7b2a56a221d48fb3ff83c03de1afc5611916d3 0 1675175285526 11 connected
60d458a88ec083ffce11bce2196b36f5f9b12768 127.0.0.1:7007 slave f9bb669d59aff71c5d3477494b4ff88203d8ce63 0 1675175284518 13 connected
d09f47b1dd2674f119f8b6406cf140f1704946a7 127.0.0.1:7005 slave 44bc79bddc52458fa7660d348a09fe96148f805e 0 1675175285021 12 connected
8f2f0e2e8ca7b431c93f4975a306853c3624937b 127.0.0.1:7006 slave 44bc79bddc52458fa7660d348a09fe96148f805e 0 1675175284016 12 connected
de39bf610b53bc382c09b2d85b8121841e8f1410 127.0.0.1:7008 slave f9bb669d59aff71c5d3477494b4ff88203d8ce63 0 1675175284518 13 connected
f190d9c102242cc5898fd4eaca4311c17b676858 127.0.0.1:7009 master - 0 1675175284518 10 connected
d6f1de5130c5150e1a544b3e292fc400f7cfd429 127.0.0.1:7003 slave 2b7b2a56a221d48fb3ff83c03de1afc5611916d3 0 1675175284518 11 connected
2b7b2a56a221d48fb3ff83c03de1afc5611916d3 127.0.0.1:7000 master - 0 1675175285021 11 connected 0-5460
# 执行完后即可查询到数据,否则一直处于ask异常应答
127.0.0.1:7002> MGET key:{name}:1 key:{name}:2 key:{name}:3
1) "value:name:xadocker1"
2) "value:name:xadocker2"
3) "value:name:xadocker3"
其他实例上不需要执行,7002实例会自动同步slots信息给其他实例
为slots设置为正常状态
# 如果错误将slots设置为导入或迁移状态,可以通过以下命令设置为正常状态
127.0.0.1:7001> CLUSTER SETSLOT 5798 stable
为master添加slave实例
登录新得slave实例:7010,设置为实例7009得slave:
127.0.0.1:7010> CLUSTER REPLICATE f190d9c102242cc5898fd4eaca4311c17b676858
OK
127.0.0.1:7010> cluster nodes
2b7b2a56a221d48fb3ff83c03de1afc5611916d3 127.0.0.1:7000 master - 0 1675176238476 11 connected 0-5460
f190d9c102242cc5898fd4eaca4311c17b676858 127.0.0.1:7009 master - 0 1675176237469 10 connected
f9bb669d59aff71c5d3477494b4ff88203d8ce63 127.0.0.1:7002 master - 0 1675176238476 13 connected 5798 6826 6918 10923-16383
60d458a88ec083ffce11bce2196b36f5f9b12768 127.0.0.1:7007 slave f9bb669d59aff71c5d3477494b4ff88203d8ce63 0 1675176238476 13 connected
8f2f0e2e8ca7b431c93f4975a306853c3624937b 127.0.0.1:7006 slave 44bc79bddc52458fa7660d348a09fe96148f805e 0 1675176237972 12 connected
d09f47b1dd2674f119f8b6406cf140f1704946a7 127.0.0.1:7005 slave 44bc79bddc52458fa7660d348a09fe96148f805e 0 1675176237469 12 connected
c10adb1919dbfc8b7aa3e74607d657a248677835 127.0.0.1:7004 slave 2b7b2a56a221d48fb3ff83c03de1afc5611916d3 0 1675176238979 11 connected
0128a53ffe38b5d60a3a7e402423911f66697e38 127.0.0.1:7010 myself,slave f190d9c102242cc5898fd4eaca4311c17b676858 0 0 0 connected
d6f1de5130c5150e1a544b3e292fc400f7cfd429 127.0.0.1:7003 slave 2b7b2a56a221d48fb3ff83c03de1afc5611916d3 0 1675176237972 11 connected
de39bf610b53bc382c09b2d85b8121841e8f1410 127.0.0.1:7008 slave f9bb669d59aff71c5d3477494b4ff88203d8ce63 0 1675176239483 13 connected
44bc79bddc52458fa7660d348a09fe96148f805e 127.0.0.1:7001 master - 0 1675176239483 12 connected 5461-5797 5799-6825 6827-6917 6919-10922
正文完