redis 集群讲解 (二 )动态扩容

1、加入新master

在上篇基础上增加
7007.conf,和一个redis_7007启动脚本

redis-trib.rb add-node 127.0.0.1:7007 127.0.0.1:7001

redis-trib.rb check 127.0.0.1:7001

out:

[root@1b0c5aafc62b init.d]# redis-trib.rb check 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 5f7f1ff4582dcd1a6cc66a9e636fe18bcacc7d4c 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 0e2a60a544a2d4441beb03224a07ad6c437363c6 127.0.0.1:7007
   slots: (0 slots) master
   0 additional replica(s)
M: be557259c0ea02517557931251df7120c00cd4c2 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 65e578ed3fe7d5947a4f9c621c8157ab6c1251b5 127.0.0.1:7006
   slots: (0 slots) slave
   replicates be557259c0ea02517557931251df7120c00cd4c2
S: 3cb4215cb614950f9e68be6a995bf98ea81d52a6 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 63035068bbbc1ee1df40a050d5d47b7779f86043
S: 210ae237bb73aa3c876eb9933a9926c575fba0cd 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 5f7f1ff4582dcd1a6cc66a9e636fe18bcacc7d4c
M: 63035068bbbc1ee1df40a050d5d47b7779f86043 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

发现 新加入的 master 没有槽位

2、reshard一些数据过去

resharding的意思就是把一部分hash slot从一些node上迁移到另外一些node上

之前三个 master 每个 平均大概 5461 槽位

变成四个 每个master 4096 reshard 后输入 4096

redis-trib.rb reshard 127.0.0.1:7001


[root@1b0c5aafc62b init.d]# redis-trib.rb reshard 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 5f7f1ff4582dcd1a6cc66a9e636fe18bcacc7d4c 127.0.0.1:7001
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
M: 0e2a60a544a2d4441beb03224a07ad6c437363c6 127.0.0.1:7007
   slots: (0 slots) master
   0 additional replica(s)
M: be557259c0ea02517557931251df7120c00cd4c2 127.0.0.1:7003
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 65e578ed3fe7d5947a4f9c621c8157ab6c1251b5 127.0.0.1:7006
   slots: (0 slots) slave
   replicates be557259c0ea02517557931251df7120c00cd4c2
S: 3cb4215cb614950f9e68be6a995bf98ea81d52a6 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 63035068bbbc1ee1df40a050d5d47b7779f86043
S: 210ae237bb73aa3c876eb9933a9926c575fba0cd 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 5f7f1ff4582dcd1a6cc66a9e636fe18bcacc7d4c
M: 63035068bbbc1ee1df40a050d5d47b7779f86043 127.0.0.1:7002
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID? 0e2a60a544a2d4441beb03224a07ad6c437363c6
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:5f7f1ff4582dcd1a6cc66a9e636fe18bcacc7d4c
Source node #2:be557259c0ea02517557931251df7120c00cd4c2
Source node #3:63035068bbbc1ee1df40a050d5d47b7779f86043

输入4096 然后输入目标 的master ID 然后输入 原始的 master Id (源头)
输入 done 结尾

3、添加node作为slave

增加 7008.conf,和一个redis_7008启动脚本

mkdir -p /var/redis/7008

port 7008
cluster-enabled yes
cluster-config-file /etc/redis-cluster/node-7008.conf
cluster-node-timeout 15000
daemonize   yes                         
pidfile     /var/run/redis_7008.pid                         
dir         /var/redis/7008     
logfile /var/log/redis/7008.log
bind 127.0.0.1      
appendonly yes

增加slave

redis-trib.rb add-node --slave --master-id XXXXXXXX 127.0.0.1:7008 127.0.0.1:7001

我挂载到 新加入的master 上

redis-trib.rb add-node --slave --master-id 0e2a60a544a2d4441beb03224a07ad6c437363c6 127.0.0.1:7008 127.0.0.1:7001

4、删除node

redis-trib.rb reshard  127.0.0.1:7001

先用resharding将数据都移除到其他节点,确保node为空之后,才能执行remove操作

2个是1365,1个是1366

当你清空了一个master的hashslot时,redis cluster就会自动将其slave挂载到其他master上去

这个时候就只要删除掉master就可以了

redis-trib.rb del-node 127.0.0.1:7001 be557259c0ea02517557931251df7120c00cd4c2

check 发现 slove 自动转移了

[root@1b0c5aafc62b /]# redis-trib.rb check 127.0.0.1:7001
>>> Performing Cluster Check (using node 127.0.0.1:7001)
M: 5f7f1ff4582dcd1a6cc66a9e636fe18bcacc7d4c 127.0.0.1:7001
   slots:1365-5460,13653-15017 (5461 slots) master
   1 additional replica(s)
M: 0e2a60a544a2d4441beb03224a07ad6c437363c6 127.0.0.1:7007
   slots:0-1364,5461-6826,10923-13652 (5461 slots) master
   1 additional replica(s)
S: 65e578ed3fe7d5947a4f9c621c8157ab6c1251b5 127.0.0.1:7006
   slots: (0 slots) slave
   replicates 63035068bbbc1ee1df40a050d5d47b7779f86043
S: 3cb4215cb614950f9e68be6a995bf98ea81d52a6 127.0.0.1:7005
   slots: (0 slots) slave
   replicates 63035068bbbc1ee1df40a050d5d47b7779f86043
S: f3f6a32fb58b4b734f2c90cfca64ad71c6acb83f 127.0.0.1:7008
   slots: (0 slots) slave
   replicates 0e2a60a544a2d4441beb03224a07ad6c437363c6
S: 210ae237bb73aa3c876eb9933a9926c575fba0cd 127.0.0.1:7004
   slots: (0 slots) slave
   replicates 5f7f1ff4582dcd1a6cc66a9e636fe18bcacc7d4c
M: 63035068bbbc1ee1df40a050d5d47b7779f86043 127.0.0.1:7002
   slots:6827-10922,15018-16383 (5462 slots) master
   2 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

slave的自动迁移

比如现在有10个master,每个有1个slave,然后新增了3个slave作为冗余,有的master就有2个slave了,有的master出现了salve冗余

如果某个master的slave挂了,那么redis cluster会自动迁移一个冗余的slave给那个master

只要多加一些冗余的slave就可以了

为了避免的场景,就是说,如果你每个master只有一个slave,万一说一个slave死了,然后很快,master也死了,那可用性还是降低了

但是如果你给整个集群挂载了一些冗余slave,那么某个master的slave死了,冗余的slave会被自动迁移过去,作为master的新slave,此时即使那个master也死了

还是有一个slave会切换成master的

之前有一个master是有冗余slave的,直接让其他master其中的一个slave死掉,然后看有冗余slave会不会自动挂载到那个master

你可能感兴趣的:(redis 集群讲解 (二 )动态扩容)