惊呆了,竟然可以用这种方式秒建 Redis 集群?
作者 | 老王
来源 | Java 中文社区
秒建 Redis 集群
1.快速搭建 Redis Cluster
1$ ./create-cluster start # 创建集群
2Starting 30001
3Starting 30002
4Starting 30003
5Starting 30004
6Starting 30005
7Starting 30006
接下来我们需要把以上创建的 6 个节点节点通过 create 命令组成一个集群,执行如下:
1[@iZ2ze0nc5n41zomzyqtksmZ:create-cluster]$ ./create-cluster create # 组建集群
2>>> Performing hash slots allocation on 6 nodes...
3Master[0] -> Slots 0 - 5460
4Master[1] -> Slots 5461 - 10922
5Master[2] -> Slots 10923 - 16383
6Adding replica 127.0.0.1:30005 to 127.0.0.1:30001
7Adding replica 127.0.0.1:30006 to 127.0.0.1:30002
8Adding replica 127.0.0.1:30004 to 127.0.0.1:30003
9>>> Trying to optimize slaves allocation for anti-affinity
10[WARNING] Some slaves are in the same host as their master
11M: 445f2a86fe36d397613839d8cc1ae6702c976593 127.0.0.1:30001
12 slots:[0-5460] (5461 slots) master
13M: 63bb14023c0bf58926738cbf857ea304bff8eb50 127.0.0.1:30002
14 slots:[5461-10922] (5462 slots) master
15M: 864d4dfe32e3e0b81a64cec8b393bbd26a65cbcc 127.0.0.1:30003
16 slots:[10923-16383] (5461 slots) master
17S: 64828ab44566fc5ad656e831fd33de87be1387a0 127.0.0.1:30004
18 replicates 445f2a86fe36d397613839d8cc1ae6702c976593
19S: 0b17b00542706343583aa73149ec5ff63419f140 127.0.0.1:30005
20 replicates 63bb14023c0bf58926738cbf857ea304bff8eb50
21S: e35f06ca9b700073472d72001a39ea4dfcb541cd 127.0.0.1:30006
22 replicates 864d4dfe32e3e0b81a64cec8b393bbd26a65cbcc
23Can I set the above configuration? (type 'yes' to accept): yes
24>>> Nodes configuration updated
25>>> Assign a different config epoch to each node
26>>> Sending CLUSTER MEET messages to join the cluster
27Waiting for the cluster to join
28.
29>>> Performing Cluster Check (using node 127.0.0.1:30001)
30M: 445f2a86fe36d397613839d8cc1ae6702c976593 127.0.0.1:30001
31 slots:[0-5460] (5461 slots) master
32 1 additional replica(s)
33M: 864d4dfe32e3e0b81a64cec8b393bbd26a65cbcc 127.0.0.1:30003
34 slots:[10923-16383] (5461 slots) master
35 1 additional replica(s)
36S: e35f06ca9b700073472d72001a39ea4dfcb541cd 127.0.0.1:30006
37 slots: (0 slots) slave
38 replicates 864d4dfe32e3e0b81a64cec8b393bbd26a65cbcc
39S: 0b17b00542706343583aa73149ec5ff63419f140 127.0.0.1:30005
40 slots: (0 slots) slave
41 replicates 63bb14023c0bf58926738cbf857ea304bff8eb50
42M: 63bb14023c0bf58926738cbf857ea304bff8eb50 127.0.0.1:30002
43 slots:[5461-10922] (5462 slots) master
44 1 additional replica(s)
45S: 64828ab44566fc5ad656e831fd33de87be1387a0 127.0.0.1:30004
46 slots: (0 slots) slave
47 replicates 445f2a86fe36d397613839d8cc1ae6702c976593
48[OK] All nodes agree about slots configuration.
49>>> Check for open slots...
50>>> Check slots coverage...
51[OK] All 16384 slots covered.
1$ redis-cli -c -p 30001
1127.0.0.1:30001> cluster nodes
2864d4dfe32e3e0b81a64cec8b393bbd26a65cbcc 127.0.0.1:30003@40003 master - 0 1585125835078 3 connected 10923-16383
3e35f06ca9b700073472d72001a39ea4dfcb541cd 127.0.0.1:30006@40006 slave 864d4dfe32e3e0b81a64cec8b393bbd26a65cbcc 0 1585125835078 6 connected
40b17b00542706343583aa73149ec5ff63419f140 127.0.0.1:30005@40005 slave 63bb14023c0bf58926738cbf857ea304bff8eb50 0 1585125835078 5 connected
563bb14023c0bf58926738cbf857ea304bff8eb50 127.0.0.1:30002@40002 master - 0 1585125834175 2 connected 5461-10922
6445f2a86fe36d397613839d8cc1ae6702c976593 127.0.0.1:30001@40001 myself,master - 0 1585125835000 1 connected 0-5460
764828ab44566fc5ad656e831fd33de87be1387a0 127.0.0.1:30004@40004 slave 445f2a86fe36d397613839d8cc1ae6702c976593 0 1585125835000 4 connected
可以看出 30001、 30002、30003 都为主节点,30001 对应的槽位是 0-5460,30002 对应的槽位是 5461-10922,30003 对应的槽位是 10923-16383,总共有槽位 16384 个 (0 ~ 16383)。
1$ ./create-cluster stop # 关闭集群
2Stopping 30001
3Stopping 30002
4Stopping 30003
5Stopping 30004
6Stopping 30005
7Stopping 30006
8$ ./create-cluster clean # 清理集群
2.手动搭建 Redis Cluster
① 设置配置文件
② 启动各个节点
1cd /usr/local/soft/mycluster/node1
2./src/redis-server redis.conf
③ 创建集群并分配槽位
1redis-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
1>>> Performing hash slots allocation on 6 nodes...
2Master[0] -> Slots 0 - 5460
3Master[1] -> Slots 5461 - 10922
4Master[2] -> Slots 10923 - 16383
5Adding replica 127.0.0.1:30005 to 127.0.0.1:30001
6Adding replica 127.0.0.1:30006 to 127.0.0.1:30002
7Adding replica 127.0.0.1:30004 to 127.0.0.1:30003
8>>> Trying to optimize slaves allocation for anti-affinity
9[WARNING] Some slaves are in the same host as their master
10M: bdd1c913f87eacbdfeabc71befd0d06c913c891c 127.0.0.1:30001
11 slots:[0-5460] (5461 slots) master
12M: bdd1c913f87eacbdfeabc71befd0d06c913c891c 127.0.0.1:30002
13 slots:[5461-10922] (5462 slots) master
14M: bdd1c913f87eacbdfeabc71befd0d06c913c891c 127.0.0.1:30003
15 slots:[10923-16383] (5461 slots) master
16S: bdd1c913f87eacbdfeabc71befd0d06c913c891c 127.0.0.1:30004
17 replicates bdd1c913f87eacbdfeabc71befd0d06c913c891c
18S: bdd1c913f87eacbdfeabc71befd0d06c913c891c 127.0.0.1:30005
19 replicates bdd1c913f87eacbdfeabc71befd0d06c913c891c
20S: bdd1c913f87eacbdfeabc71befd0d06c913c891c 127.0.0.1:30006
21 replicates bdd1c913f87eacbdfeabc71befd0d06c913c891c
22Can I set the above configuration? (type 'yes' to accept):
1Can I set the above configuration? (type 'yes' to accept): yes
2>>> Nodes configuration updated
3>>> Assign a different config epoch to each node
4>>> Sending CLUSTER MEET messages to join the cluster
5Waiting for the cluster to join
6....
7>>> Performing Cluster Check (using node 127.0.0.1:30001)
8M: 887397e6fefe8ad19ea7569e99f5eb8a803e3785 127.0.0.1:30001
9 slots:[0-5460] (5461 slots) master
10 1 additional replica(s)
11S: abec9f98f9c01208ba77346959bc35e8e274b6a3 127.0.0.1:30005
12 slots: (0 slots) slave
13 replicates 887397e6fefe8ad19ea7569e99f5eb8a803e3785
14S: 1a324d828430f61be6eaca7eb2a90728dd5049de 127.0.0.1:30004
15 slots: (0 slots) slave
16 replicates f5958382af41d4e1f5b0217c1413fe19f390b55f
17S: dc0702625743c48c75ea935c87813c4060547cef 127.0.0.1:30006
18 slots: (0 slots) slave
19 replicates 3da35c40c43b457a113b539259f17e7ed616d13d
20M: 3da35c40c43b457a113b539259f17e7ed616d13d 127.0.0.1:30002
21 slots:[5461-10922] (5462 slots) master
22 1 additional replica(s)
23M: f5958382af41d4e1f5b0217c1413fe19f390b55f 127.0.0.1:30003
24 slots:[10923-16383] (5461 slots) master
25 1 additional replica(s)
26[OK] All nodes agree about slots configuration.
27>>> Check for open slots...
28>>> Check slots coverage...
29[OK] All 16384 slots covered.
1$ redis-cli -c -p 30001 # 连接到集群
2127.0.0.1:30001> cluster info # 查看集群信息
3cluster_state:ok # 状态正常
4cluster_slots_assigned:16384 # 槽位数
5cluster_slots_ok:16384 # 正常的槽位数
6cluster_slots_pfail:0
7cluster_slots_fail:0
8cluster_known_nodes:6 # 集群的节点数
9cluster_size:3 # 集群主节点数
10cluster_current_epoch:6
11cluster_my_epoch:1
12cluster_stats_messages_ping_sent:130
13cluster_stats_messages_pong_sent:127
14cluster_stats_messages_sent:257
15cluster_stats_messages_ping_received:122
16cluster_stats_messages_pong_received:130
17cluster_stats_messages_meet_received:5
18cluster_stats_messages_received:257
动态增删节点
1.增加主节点
添加方式一:cluster meet
1127.0.0.1:30001> cluster meet 127.0.0.1 30007
2OK
3127.0.0.1:30001> cluster nodes
4dc0702625743c48c75ea935c87813c4060547cef 127.0.0.1:30006@40006 slave 3da35c40c43b457a113b539259f17e7ed616d13d 0 1585142916000 6 connected
5df0190853a53d8e078205d0e2fa56046f20362a7 127.0.0.1:30007@40007 master - 0 1585142917740 0 connected
6f5958382af41d4e1f5b0217c1413fe19f390b55f 127.0.0.1:30003@40003 master - 0 1585142916738 3 connected 10923-16383
73da35c40c43b457a113b539259f17e7ed616d13d 127.0.0.1:30002@40002 master - 0 1585142913000 2 connected 5461-10922
8abec9f98f9c01208ba77346959bc35e8e274b6a3 127.0.0.1:30005@40005 slave 887397e6fefe8ad19ea7569e99f5eb8a803e3785 0 1585142917000 5 connected
9887397e6fefe8ad19ea7569e99f5eb8a803e3785 127.0.0.1:30001@40001 myself,master - 0 1585142915000 1 connected 0-5460
101a324d828430f61be6eaca7eb2a90728dd5049de 127.0.0.1:30004@40004 slave f5958382af41d4e1f5b0217c1413fe19f390b55f 0 1585142916000 4 connected
添加方式二:add-node
1$ redis-cli --cluster add-node 127.0.0.1:30008 127.0.0.1:30001
2>>> Adding node 127.0.0.1:30008 to cluster 127.0.0.1:30001
3>>> Performing Cluster Check (using node 127.0.0.1:30001)
4M: 887397e6fefe8ad19ea7569e99f5eb8a803e3785 127.0.0.1:30001
5 slots:[0-5460] (5461 slots) master
6 1 additional replica(s)
7S: dc0702625743c48c75ea935c87813c4060547cef 127.0.0.1:30006
8 slots: (0 slots) slave
9 replicates 3da35c40c43b457a113b539259f17e7ed616d13d
10M: df0190853a53d8e078205d0e2fa56046f20362a7 127.0.0.1:30007
11 slots: (0 slots) master
12M: f5958382af41d4e1f5b0217c1413fe19f390b55f 127.0.0.1:30003
13 slots:[10923-16383] (5461 slots) master
14 1 additional replica(s)
15M: 1d09d26fd755298709efe60278457eaa09cefc26 127.0.0.1:30008
16 slots: (0 slots) master
17M: 3da35c40c43b457a113b539259f17e7ed616d13d 127.0.0.1:30002
18 slots:[5461-10922] (5462 slots) master
19 1 additional replica(s)
20S: abec9f98f9c01208ba77346959bc35e8e274b6a3 127.0.0.1:30005
21 slots: (0 slots) slave
22 replicates 887397e6fefe8ad19ea7569e99f5eb8a803e3785
23S: 1a324d828430f61be6eaca7eb2a90728dd5049de 127.0.0.1:30004
24 slots: (0 slots) slave
25 replicates f5958382af41d4e1f5b0217c1413fe19f390b55f
26[OK] All nodes agree about slots configuration.
27>>> Check for open slots...
28>>> Check slots coverage...
29[OK] All 16384 slots covered.
30[ERR] Node 127.0.0.1:30008 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
2.添加从节点
1127.0.0.1:30008> cluster replicate df0190853a53d8e078205d0e2fa56046f20362a7
2OK
3127.0.0.1:30008> cluster nodes
4df0190853a53d8e078205d0e2fa56046f20362a7 127.0.0.1:30007@40007 master - 0 1585147827000 0 connected
5abec9f98f9c01208ba77346959bc35e8e274b6a3 127.0.0.1:30005@40005 slave 887397e6fefe8ad19ea7569e99f5eb8a803e3785 0 1585147827000 1 connected
61a324d828430f61be6eaca7eb2a90728dd5049de 127.0.0.1:30004@40004 slave f5958382af41d4e1f5b0217c1413fe19f390b55f 0 1585147823000 3 connected
7887397e6fefe8ad19ea7569e99f5eb8a803e3785 127.0.0.1:30001@40001 master - 0 1585147826000 1 connected 0-5460
8dc0702625743c48c75ea935c87813c4060547cef 127.0.0.1:30006@40006 slave 3da35c40c43b457a113b539259f17e7ed616d13d 0 1585147826930 2 connected
9f5958382af41d4e1f5b0217c1413fe19f390b55f 127.0.0.1:30003@40003 master - 0 1585147826000 3 connected 10923-16383
101d09d26fd755298709efe60278457eaa09cefc26 127.0.0.1:30008@40008 myself,slave df0190853a53d8e078205d0e2fa56046f20362a7 0 1585147823000 7 connected
113da35c40c43b457a113b539259f17e7ed616d13d 127.0.0.1:30002@40002 master - 0 1585147827933 2 connected 5461-10922
3.删除节点
1127.0.0.1:30001> cluster forget df0190853a53d8e078205d0e2fa56046f20362a7
2OK
1127.0.0.1:30001> cluster nodes
2dc0702625743c48c75ea935c87813c4060547cef 127.0.0.1:30006@40006 slave 3da35c40c43b457a113b539259f17e7ed616d13d 0 1585143789940 6 connected
3f5958382af41d4e1f5b0217c1413fe19f390b55f 127.0.0.1:30003@40003 master - 0 1585143791000 3 connected 10923-16383
43da35c40c43b457a113b539259f17e7ed616d13d 127.0.0.1:30002@40002 master - 0 1585143789000 2 connected 5461-10922
5abec9f98f9c01208ba77346959bc35e8e274b6a3 127.0.0.1:30005@40005 slave 887397e6fefe8ad19ea7569e99f5eb8a803e3785 0 1585143789000 5 connected
6887397e6fefe8ad19ea7569e99f5eb8a803e3785 127.0.0.1:30001@40001 myself,master - 0 1585143786000 1 connected 0-5460
71a324d828430f61be6eaca7eb2a90728dd5049de 127.0.0.1:30004@40004 slave f5958382af41d4e1f5b0217c1413fe19f390b55f 0 1585143791945 4 connected
小结
希望本文对你有帮助,动手实践起来吧~
防疫、复工如何并行?天云数据推出人工智能监测方案!到底如何做到事前预防,而不是事后诸葛亮?本周四晚8点,天云数据VP陈勇为各位揭晓答案!扫描下方二维码免费报名~
☞
☞
点击阅读原文,参与报名