Redis Cluster集群的原生创建

2022-06-20 19:55:43 浏览数 (1)

Redis Cluster是Redis的分布式解决方案,在Redis 3.0版本正式推出的,有效解决了Redis分布式方面的需求.当遇到单机内存,并发,流量等瓶颈时,可以采用Cluster架构达到负载均衡的目的.

redis提供了ruby脚本,快速创建集群并配置节点主从关系和,hash槽等信息

代码语言:javascript复制
./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006

但这种方式不利于我们理解系统的创建过程,下面我们使用原生方式是如何创建集群的.

原生方式配置集群

一. 集群配置

添加最简配置项,打开集群模式

点击阅读全文查看各节点配置

代码语言:javascript复制
port 6379
cluster-enabled yes
cluster-config-file nodes-6379.conf

修改下其他节点配置,并放入配置目录中

代码语言:javascript复制
sed 's/6379/9000/g' redis.conf > redis-9001.conf
sed 's/6379/9000/g' redis.conf > redis-9000.conf
sed 's/6379/9001/g' redis.conf > redis-9001.conf
sed 's/6379/9002/g' redis.conf > redis-9002.conf
sed 's/6379/9003/g' redis.conf > redis-9003.conf
sed 's/6379/9004/g' redis.conf > redis-9004.conf
sed 's/6379/9005/g' redis.conf > redis-9005.conf

二. 节点启动

这里为了方便测试,使用docker方式启动,并使用host网络模式.

代码语言:javascript复制
docker run -d --name redis1 -v /xxx/cluster/redis-9000.conf:/etc/redis/redis.conf --network host redis:4.0-alpine redis-server /etc/redis/redis.conf
docker run -d --name redis2 -v /xxx/cluster/redis-9001.conf:/etc/redis/redis.conf --network host redis:4.0-alpine redis-server /etc/redis/redis.conf
docker run -d --name redis3 -v /xxx/cluster/redis-9002.conf:/etc/redis/redis.conf --network host redis:4.0-alpine redis-server /etc/redis/redis.conf
docker run -d --name redis4 -v /xxx/cluster/redis-9003.conf:/etc/redis/redis.conf --network host redis:4.0-alpine redis-server /etc/redis/redis.conf
docker run -d --name redis5 -v /xxx/cluster/redis-9004.conf:/etc/redis/redis.conf --network host redis:4.0-alpine redis-server /etc/redis/redis.conf
docker run -d --name redis6 -v /xxx/cluster/redis-9005.conf:/etc/redis/redis.conf --network host redis:4.0-alpine redis-server /etc/redis/redis.conf

查看节点状态

按集群方式访问9000节点

代码语言:javascript复制
docker exec -it redis1 sh
redis-cli -c  -p 9000

查看集群信息

cluster_state:表示集群状态

cluster_known_nodes:表示已知集群的节点数量

代码语言:javascript复制
127.0.0.1:9000> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:1
cluster_size:0
cluster_current_epoch:0
cluster_my_epoch:0
cluster_stats_messages_sent:0
cluster_stats_messages_received:0

查看集群节点状态

当前也只发现自己一个节点,并没有找到其他节点

代码语言:javascript复制
127.0.0.1:9000> cluster nodes
1cb4d5c65294ea54102e039d7d74371d41fa0a3f :9000@19000 myself,master - 0 0 0 connected

发现其他节点

发现9001节点

代码语言:javascript复制
127.0.0.1:9000> cluster meet 127.0.0.1 9001

查看集群节点已经发现新节点

代码语言:javascript复制
127.0.0.1:9000> cluster nodes
59676d3978149d243e90f00e477687269870b061 127.0.0.1:9001@19001 master - 0 1590842420401 0 connected
1cb4d5c65294ea54102e039d7d74371d41fa0a3f 127.0.0.1:9000@19000 myself,master - 0 0 1 connected

同理发现其他节点

代码语言:javascript复制
127.0.0.1:9000> cluster meet 127.0.0.1 9002
OK
127.0.0.1:9000> cluster meet 127.0.0.1 9003
OK
127.0.0.1:9000> cluster meet 127.0.0.1 9004
OK
127.0.0.1:9000> cluster meet 127.0.0.1 9005
OK

查看集群节点信息

能够发现其他节点了,但所有节点都是master节点,不符合集群的高可用要求,后续还需要配置节点的主从信息

代码语言:javascript复制
127.0.0.1:9000> cluster nodes
78fed7854b210a9b4470431821c5a574a7cf689e 127.0.0.1:9005@19005 master - 0 1590842498000 0 connected
75962ed329741f3699c5058b865bd1262b82c3cb 127.0.0.1:9003@19003 master - 0 1590842498688 5 connected
59676d3978149d243e90f00e477687269870b061 127.0.0.1:9001@19001 master - 0 1590842500716 3 connected
50edf24a4b9b43e40c2dbe4a40b168463af6285c 127.0.0.1:9004@19004 master - 0 1590842499698 4 connected
05be0926609d27d50f0d83b629d4bc0d4e647fd5 127.0.0.1:9002@19002 master - 0 1590842497670 2 connected
1cb4d5c65294ea54102e039d7d74371d41fa0a3f 127.0.0.1:9000@19000 myself,master - 0 1590842499000 1 connected

查看集群信息

集群并没有可用hash槽,需后续继续配置

代码语言:javascript复制
127.0.0.1:9000> cluster info
cluster_state:fail
cluster_slots_assigned:0
cluster_slots_ok:0
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:0
cluster_current_epoch:5
cluster_my_epoch:1
cluster_stats_messages_ping_sent:90
cluster_stats_messages_pong_sent:98
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:193
cluster_stats_messages_ping_received:98
cluster_stats_messages_pong_received:95
cluster_stats_messages_received:193

三. 添加hash槽

redis原生批量添加槽命令并不友好,这里使用lua脚本完成此功能

退出集群模式,直接执行lua脚本

代码语言:javascript复制
/data # redis-cli -p 9000 eval "for i=0,5000,1 do redis.call('cluster','addslots',i) end" 0
(nil)
/data # redis-cli -p 9001 eval "for i=5001,10000,1 do redis.call('cluster','addslots',i) end" 0
(nil)
/data # redis-cli -p 9002 eval "for i=10001,16383,1 do redis.call('cluster','addslots',i) end" 0
(nil)

进入集群模式查看hash槽分配情况

代码语言:javascript复制
/data # redis-cli -c -p 9000
127.0.0.1:9000>
127.0.0.1:9000> cluster slots
1) 1) (integer) 5001
   2) (integer) 10000
   3) 1) "127.0.0.1"
      2) (integer) 9001
      3) "59676d3978149d243e90f00e477687269870b061"
2) 1) (integer) 10001
   2) (integer) 16383
   3) 1) "127.0.0.1"
      2) (integer) 9002
      3) "05be0926609d27d50f0d83b629d4bc0d4e647fd5"
3) 1) (integer) 0
   2) (integer) 5000
   3) 1) "127.0.0.1"
      2) (integer) 9000
      3) "1cb4d5c65294ea54102e039d7d74371d41fa0a3f"

查看集群节点信息

代码语言:javascript复制
127.0.0.1:9000> cluster nodes
78fed7854b210a9b4470431821c5a574a7cf689e 127.0.0.1:9005@19005 master - 0 1590844218000 0 connected
75962ed329741f3699c5058b865bd1262b82c3cb 127.0.0.1:9003@19003 master - 0 1590844220115 5 connected
59676d3978149d243e90f00e477687269870b061 127.0.0.1:9001@19001 master - 0 1590844218083 3 connected 5001-10000
50edf24a4b9b43e40c2dbe4a40b168463af6285c 127.0.0.1:9004@19004 master - 0 1590844219095 4 connected
05be0926609d27d50f0d83b629d4bc0d4e647fd5 127.0.0.1:9002@19002 master - 0 1590844215000 2 connected 10001-16383
1cb4d5c65294ea54102e039d7d74371d41fa0a3f 127.0.0.1:9000@19000 myself,master - 0 1590844218000 1 connected 0-5000

查看集群信息

代码语言:javascript复制
127.0.0.1:9000> 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:5
cluster_my_epoch:1
cluster_stats_messages_ping_sent:1789
cluster_stats_messages_pong_sent:1761
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:3555
cluster_stats_messages_ping_received:1761
cluster_stats_messages_pong_received:1794
cluster_stats_messages_received:3555

四. 配置集群从节点信息

利用cluster replicate命令配置集群主从节点关系

代码语言:javascript复制
/data # redis-cli -h 127.0.0.1 -p 9003 cluster replicate 1cb4d5c65294ea54102e039d7d74371d41fa0a3f
OK
/data # redis-cli -h 127.0.0.1 -p 9004 cluster replicate 59676d3978149d243e90f00e477687269870b061
OK
/data # redis-cli -h 127.0.0.1 -p 9005 cluster replicate 05be0926609d27d50f0d83b629d4bc0d4e647fd5
OK

查看节点信息,可以发现各节点间的主从关系

代码语言:javascript复制
/data # redis-cli -c -p 9000
127.0.0.1:9000> cluster nodes
78fed7854b210a9b4470431821c5a574a7cf689e 127.0.0.1:9005@19005 slave 05be0926609d27d50f0d83b629d4bc0d4e647fd5 0 1590844603431 2 connected
75962ed329741f3699c5058b865bd1262b82c3cb 127.0.0.1:9003@19003 slave 1cb4d5c65294ea54102e039d7d74371d41fa0a3f 0 1590844602000 5 connected
59676d3978149d243e90f00e477687269870b061 127.0.0.1:9001@19001 master - 0 1590844603000 3 connected 5001-10000
50edf24a4b9b43e40c2dbe4a40b168463af6285c 127.0.0.1:9004@19004 slave 59676d3978149d243e90f00e477687269870b061 0 1590844604452 4 connected
05be0926609d27d50f0d83b629d4bc0d4e647fd5 127.0.0.1:9002@19002 master - 0 1590844602410 2 connected 10001-16383
1cb4d5c65294ea54102e039d7d74371d41fa0a3f 127.0.0.1:9000@19000 myself,master - 0 1590844604000 1 connected 0-5000

五. 集群测试

系统配置成功后,添加数据测试

代码语言:javascript复制
/data # redis-cli -c -p 9000
127.0.0.1:9000> set key v
-> Redirected to slot [12539] located at 127.0.0.1:9002
OK
127.0.0.1:9002> get key
"v"
127.0.0.1:9002> exit
/data # redis-cli  -p 9002
127.0.0.1:9002> get key
"v"
127.0.0.1:9002>

综上cluster集群的创建主要有3个步骤:

1.发现集群其他各节点

2.配置hash槽

3.配置集群中各节点主从关系

0 人点赞