rediscluster节点挂掉后_Redis的集搭建
redis-cluster架构图
(1)所有的redis节点彼此互联(PING-PONG机制),内部使⽤⼆进制协议优化传输速度和带宽.
(2)节点的fail是通过集中超过半数的节点检测失效时才⽣效.
(3)客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集所有节点,连接集中任何⼀个可⽤节点即可
(4)redis-cluster把所有的物理节点映射到[0-16383]slot上,cluster 负责维护node<->slot<->value
Redis 集中内置了 16384 个哈希槽,当需要在 Redis 集中放置⼀个 key-value 时,redis 先对 key 使⽤ crc16 算法算出⼀个结果,然后把结果对 16384 求余数,这样每个 key 都会对应⼀个编号在 0-16383 之间的哈希槽,redis 会根据节点数量⼤致均等的将哈希槽映射到不同的节点
⽰例如下
redis-cluster投票容错
⾸先你需要连接上redis
[root@localhost src]# ./redis-cli -p 6384 --第⼀步从客户端命令⼯具连接redis
127.0.0.1:6384> auth 123456 --输⼊登录密码,登录
[root@localhost src]# info replication --使⽤命令 info replication查看状态信息
⽰例图⽚
(1)集中所有master参与投票,如果半数以上master节点与其中⼀个master节点通信超过(cluster-node-timeout),认为该master节点挂掉.
(2):什么时候整个集不可⽤(cluster_state:fail)?
如果集任意master挂掉,且当前master没有slave,则集进⼊fail状态。也可以理解成集的[0-16383]slot映射不完全时进⼊fail状态。
如果集超过半数以上master挂掉,⽆论是否有slave,集进⼊fail状态。
搭建Ruby环境
redis集需要使⽤技巧管理⼯具redis-trib.rb,它依赖ruby环境,⾸先需要安装ruby环境。
第⼀步:安装ruby
[root@ERAYT-01 erayt]# yum install ruby
[root@ERAYT-01 erayt]# yum install rubygems
第⼆步:上传redis-3.到Linux系统
第三步:安装ruby和redis的接⼝程序redis-3.
[root@ERAYT-01 erayt]# gem install redis-3.
第四步:redis-3.0.0/src⽬录,将redis-trib.rb⽂件复制到/usr/local/redis-cluster⽬录下
[root@ERAYT-01 src]# cp redis-trib.rb /usr/local/redis-cluster/
Redis集搭建
Redis集最少需要三台主服务器,三台从服务器。
端⼝号分别为:7001~7006
第⼀步:创建7001实例,并编辑f⽂件,修改port为7001。
注意:创建实例,即拷贝单机版安装时,⽣成的bin⽬录,为7001⽬录。
[erayt@ERAYT-01 redis]$ cd /usr/local/
[erayt@ERAYT-01 local]$ ls
bin etc games include lib libexec redis redis-cluster sbin share src
[erayt@ERAYT-01 local]$ cp redis/bin/ redis-cluster/7001 -r
进⼊7001⽬录,修改端⼝,打开Cluster-enable yes
# Accept connections on the specified port, default is 6379.
# If port 0 is specified Redis will not listen on a TCP socket.
port 7001
cluster-enabled yes
第三步:复制7001,创建7002~7006实例,注意端⼝修改。
[pcts@ERAYT-01 redis-cluster]$ ls
7001 redis-trib.rb
[erayt@ERAYT-01 redis-cluster]$ cp 7001/ 7002 -r
[erayt@ERAYT-01 redis-cluster]$ cp 7001/ 7003 -r
[erayt@ERAYT-01 redis-cluster]$ cp 7001/ 7004 -r
[erayt@ERAYT-01 redis-cluster]$ cp 7001/ 7005 -r
[erayt@ERAYT-01 redis-cluster]$ cp 7001/ 7006 -r
第四步:启动所有的实例
[erayt@ERAYT-01 7001]$ ./f
[erayt@ERAYT-01 7001]$ cd ../7002
[erayt@ERAYT-01 7002]$ ./f
[erayt@ERAYT-01 7002]$ cd ../7003
[erayt@ERAYT-01 7003]$ ./f
linux安装redis报错[erayt@ERAYT-01 7003]$ cd ../7004
[erayt@ERAYT-01 7004]$ ./f
[erayt@ERAYT-01 7004]$ cd ../7005
[erayt@ERAYT-01 7005]$ ./f
[erayt@ERAYT-01 7005]$ cd ../7006
[erayt@ERAYT-01 7006]$ ./f
启动后,查看redis
[erayt@ERAYT-01 7006]$ ps aux | grep redis
erayt 3265 0.1 0.1 35548 1884 ? Ssl 02:38 0:00 ./redis-server *:7001 [cluster]
erayt 3269 0.1 0.1 35548 1884 ? Ssl 02:38 0:00 ./redis-server *:7002 [cluster]
erayt 3273 0.1 0.1 35548 1884 ? Ssl 02:38 0:00 ./redis-server *:7003 [cluster]
erayt 3277 0.1 0.1 35548 1884 ? Ssl 02:38 0:00 ./redis-server *:7004 [cluster]
erayt 3281 0.1 0.1 35548 1884 ? Ssl 02:38 0:00 ./redis-server *:7005 [cluster]
erayt 3285 0.1 0.1 35548 1888 ? Ssl 02:38 0:00 ./redis-server *:7006 [cluster]
erayt 3320 0.0 0.0 6052 788 pts/1 S+ 02:44 0:00 grep redis
第五步:创建集
[erayt@ERAYT-01 redis-cluster]$ ls
7001 7002 7003 7004 7005 7006 redis-trib.rb
[erayt@ERAYT-01 redis-cluster]$ ./redis-trib.rb create --replicas 1 192.168.232.128:7001 192.168.232.128:7002 192.168.232.128:7003 192.168.232.128:7004 192.168.232.128:7005 192.168.232.128:7006
replicas 后⾯的1表⽰的是每个主机都带有1个从机
>>> Creating cluster
Connecting to node 192.168.232.128:7001: OK
Connecting to node 192.168.232.128:7002: OK
Connecting to node 192.168.232.128:7003: OK
Connecting to node 192.168.232.128:7004: OK
Connecting to node 192.168.232.128:7005: OK
Connecting to node 192.168.232.128:7006: OK
>>> Performing hash slots allocation on
Using 3 masters:
192.168.232.128:7001
192.168.232.128:7002
192.168.232.128:7003
Adding replica 192.168.232.128:7004 to 192.168.232.128:7001
Adding replica 192.168.232.128:7005 to 192.168.232.128:7002
Adding replica 192.168.232.128:7006 to 192.168.232.128:7003
M: 1455dfd4f6ba95c94fb9b78219afe477eb449897 192.168.232.128:7001
slots:0-5460 (5461 slots) master
M: 78912c846586d65c43a243eb1780426bf2e363f2 192.168.232.128:7002
slots:5461-10922 (5462 slots) master
M: b9f0452e588df5eeec3ce9bc461f8327727b99c8 192.168.232.128:7003
slots:10923-16383 (5461 slots) master
S: a88a707d4c8a2db36365c78d69b8450c1b5f201b 192.168.232.128:7004
replicates 1455dfd4f6ba95c94fb9b78219afe477eb449897
S: 1e88baa5c85261c7803f6b7e8dec086612bd3d23 192.168.232.128:7005
replicates 78912c846586d65c43a243eb1780426bf2e363f2
S: 8a130089dc3158b55a110b8957fac85d5d0917cb 192.168.232.128:7006
replicates b9f0452e588df5eeec3ce9bc461f8327727b99c8
⽇志可以看到7001 7002 7003是主节点 7004 7005 7006是从节点,还可以看到redis 根据节点数量⼤致均等的将哈希槽映射到不同的节点。Redis集连接
命令:./redis-cli –h 127.0.0.1 –p 7001 –c
注意:-c 连接redis集
cluster info 查看集状态
[erayt@ERAYT-01 7001]$ ./redis-cli -p 7001 -c
127.0.0.1:7001> 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:6
cluster_my_epoch:1
cluster_stats_messages_sent:267
cluster_stats_messages_received:267
127.0.0.1:7001>
查看集中的节点
127.0.0.1:7001> cluster nodes
8a130089dc3158b55a110b8957fac85d5d0917cb 192.168.232.128:7006 slave b9f0452e588df5eeec3ce9bc461f8327727b99c8 0 1563823021952 6 connected
1e88baa5c85261c7803f6b7e8dec086612bd3d23 192.168.232.128:7005 slave 78912c846586d65c43a243eb1780426bf2e363f2 0 1563823027004 5 connected
1455dfd4f6ba95c94fb9b78219afe477eb449897 192.168.232.128:7001 myself,master - 0 0 1 connected 0-5460
a88a707d4c8a2db36365c78d69b8450c1b5f201b 192.168.232.128:7004 slave 1455dfd4f6ba95c94fb9b78219afe477eb449897 0 1563823023969 4 connected
78912c846586d65c43a243eb1780426bf2e363f2 192.168.232.128:7002 master - 0 1563823025989 2 connected 5461-10922
b9f0452e588df5eeec3ce9bc461f8327727b99c8 192.168.232.128:7003 master - 0 1563823024979 3 connected 10923-16383
127.0.0.1:7001>
添加主节点
集创建成功后可以向集中添加节点,下⾯是添加⼀个master主节点
添加7007结点作为新节点
1.先依照上⾯的复制⼀个redis实例,修改端⼝为7007,启动该服务
[erayt@ERAYT-01 7007]$ ./f
2.添加7007结点作为新节点
执⾏命令:./redis-trib.rb add-node 127.0.0.1:7007 127.0.0.1:7001
[erayt@ERAYT-01 redis-cluster]$ ./redis-trib.rb add-node 192.168.232.128:7007 192.168.232.128:7001
3.查看集结点发现7007已添加到集中
127.0.0.1:7001> cluster nodes
d1c9dbe277c82b36831b01fe29a0cac1f45f0dd3 192.168.232.128:7007 master - 0 1563824280943 0 connected
8a130089dc3158b55a110b8957fac85d5d0917cb 192.168.232.128:7006 slave b9f0452e588df5eeec3ce9bc461f8327727b99c8 0 1563824284990 6 connected
1e88baa5c85261c7803f6b7e8dec086612bd3d23 192.168.232.128:7005 slave 78912c846586d65c43a243eb1780426bf2e363f2 0 1563824280340 5 connected
1455dfd4f6ba95c94fb9b78219afe477eb449897 192.168.232.128:7001 myself,master - 0 0 1 connected 0-5460
a88a707d4c8a2db36365c78d69b8450c1b5f201b 192.168.232.128:7004 slave 1455dfd4f6ba95c94fb9b78219afe477eb449897 0 1563824283975 4 connected
78912c846586d65c43a243eb1780426bf2e363f2 192.168.232.128:7002 master - 0 1563824279930 2 connected 5461-10922
b9f0452e588df5eeec3ce9bc461f8327727b99c8 192.168.232.128:7003 master - 0 1563824282964 3 connected 10923-16383
127.0.0.1:7001>
hash槽重新分配
添加完主节点需要对主节点进⾏hash槽分配,这样该主节才可以存储数据。
1.查看集中槽占⽤情况
127.0.0.1:7001> cluster nodes
d1c9dbe277c82b36831b01fe29a0cac1f45f0dd3 192.168.232.128:7007 master - 0 1563824280943 0 connected
8a130089dc3158b55a110b8957fac85d5d0917cb 192.168.232.128:7006 slave b9f0452e588df5eeec3ce9bc461f8327727b99c8 0 1563824284990 6 connected
1e88baa5c85261c7803f6b7e8dec086612bd3d23 192.168.232.128:7005 slave 78912c846586d65c43a243eb1780426bf2e363f2 0 1563824280340 5 connected
1455dfd4f6ba95c94fb9b78219afe477eb449897 192.168.232.128:7001 myself,master - 0 0 1 connected 0-5460
a88a707d4c8a2db36365c78d69b8450c1b5f201b 192.168.232.128:7004 slave 1455dfd4f6ba95c94fb9b78219afe477eb449897 0 1563824283975 4 connected
78912c846586d65c43a243eb1780426bf2e363f2 192.168.232.128:7002 master - 0 1563824279930 2 connected 5461-10922
b9f0452e588df5eeec3ce9bc461f8327727b99c8 192.168.232.128:7003 master - 0 1563824282964 3 connected 10923-16383
127.0.0.1:7001>
redis集有16384个槽,集中的每个结点分配⾃已槽,通过查看集结点可以看到槽占⽤情况。
可以看到7001分配的0-5460、7002分配的 5461-10922、7003分配的10923-16383
2.连接上集(连接集中任意⼀个可⽤结点都⾏)
[erayt@ERAYT-01 redis-cluster]$ ./redis-trib.rb reshard 192.168.232.128:7001
2.1 输⼊要分配的槽数量
>>> Check for
>>> Check
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)?
2.2 输⼊:1000,表⽰要分配1000个槽点击回车根据具体情况定
How many slots do you want to move (from 1 to 16384)? 1000
What is the receiving node ID?
2.3 输⼊接收槽的结点id 输⼊7007对应的编号(cluster nodes命令查看)
What is the receiving node ID? d1c9dbe277c82b36831b01fe29a0cac1f45f0dd3
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:
2.4 输⼊源结点id,表⽰槽从哪⾥分配来
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:
槽的总量是⼀定的,之前都已经全部分配给了7001/7002/7003,现在要重新从分好的的节点拿出来⼀些分给7007节点。
可以输⼊多个节点的编号,每次输完⼀个点击回车,输完所以的输⼊done表⽰输⼊完成。表⽰7007的槽来源这⼏个节点
可以输⼊all,表⽰所有具有槽的节点都需要去分配⼀些给7007,我这⾥输⼊的是all
2.5 输⼊yes开始移动槽到⽬标结点id
Do you want to proceed with the proposed reshard plan (yes/no)? yes
2.6 查看7007节点槽的分配
d1c9dbe277c82b36831b01fe29a0cac1f45f0dd3 192.168.232.128:7007 master - 0 1563826629735 7 connected 0-332 5461-5794 10923-11255
添加从节点
集创建成功后也可以向集中添加从节点。
添加7008从结点,将7008作为7007的从结点
命令:./redis-trib.rb add-node --slave --master-id 主节点id 新节点的ip和端⼝旧节点ip和端⼝
[erayt@ERAYT-01 redis-cluster]$ ./redis-trib.rb add-node --slave --master-id d1c9dbe277c82b36831b01fe29a0cac1f45f0dd3
192.168.232.128:7008 192.168.101.3:7007
d1c9dbe277c82b36831b01fe29a0cac1f45f0dd3 是刚刚7007的id
删除节点
[erayt@ERAYT-01 redis-cluster]$ ./redis-trib.rb del-node 192.168.232.128:7007 d1c9dbe277c82b36831b01fe29a0cac1f45f0dd3
删除已经占有hash槽的结点会失败,报错如下:
[ERR] Node 192.168.232.128:7007 is not empty! Reshard data away and try again.
需要将该结点占⽤的hash槽分配出去
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论