redis集群

redis集群

redis cluster 的特点

redis cluster 拥有主从复制的特点,也拥有了sentinel哨兵机制的高可用,最主要是拥有了横向扩展的特点,解决单个节点瓶颈的问题。作为无中心架构的redis cluster集群,其每个节点保存当前节点数据和整个集群状态,每个节点都和其他所有节点连接,特点如下:

  1. 所有节点通过ping机智互联,保证节点心跳。
  2. 集群中的所有节点都做为哨兵,当集群中的某个节点失效时,需要半数节点都检测失效时才算真正的失效
  3. 客户端不需要proxy即可直接连接redis,应用程序需要写全部的redis服务器IP。
  4. redis cluster把所有的redis node映射到 0-16383个槽位(slot)上,读写需要到指定的redis node上进行
    操作,因此有多少个reids node相当于redis 并发扩展了多少倍。
  5. Redis cluster预先分配16384个(slot)槽位,当需要在redis集群中写入一个key -value的时候,会使用
    CRC16(key) 对16384取模之后的值,决定将key写入值哪一个槽位从而决定写入哪一个Redis节点上,从而有效解决单机瓶颈。

redis cluster 构建

redis版本:4.0.14
环境:6台主机
192.168.7.10:6379
192.168.7.11:6379
192.168.7.12:6379
192.168.7.13:6379
192.168.7.14:6379
192.168.7.15:6379

主从对应关系是随机分配的,如果要设置服务器密码,则需要所有节点的密码都一致,所以在个节点上最好都不设置requirepass

集群创建在redis的3、4版本需要使用redis-trib.rb这个命令,但是需要依赖ruby环境,yum安装的ruby版本太低,导致redis-trib.rb命令无法使用,所以需要手动编译安装,或者从官网下载最新的rpm包。

yum remove ruby rubygems -y 
wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.5.tar.gz 
tar xf ruby-2.5.5.tar.gz 
cd ruby-2.5.5 
./configure 
make -j 2 
make install 
gem install redis -v 3.3.5
#如果无法在线安装,可以下载reids模块安装包离线安装
#https://rubygems.org/gems/redis #先下[ERR] Calling MIGRATE ERR Syntax error, try CLIENT (LIST | KILL | GETNAME | SETNAME | PAUSE | REPLY)载redis模块安装包
#gem install -l redis-3.3.0.gem #安装redis模块

redis如果版本为4的时候,gem 安装的redis版本不能为4,不指定的话就是4版本,此时如果使用redis-trib.rb迁移槽位(reshard)的话,会报错
[ERR] Calling MIGRATE ERR Syntax error, try CLIENT (LIST | KILL | GETNAME | SETNAME | PAUSE | REPLY)
需要gem uninstall redis ,重新gem install redis -v 3.3.5

创建redis-trib.rb软连接

 ln -sv  /usr/local/src/redis-4.0.14/src/redis-trib.rb /usr/bin/

redis-trib.rb命令

[root@localhost 11:03:38 etc]#redis-trib.rb 
Usage: redis-trib <command> <options> <arguments ...>

  create          host1:port1 ... hostN:portN #创建集群
                  --replicas <arg> #每个节点有多少个slave
  check           host:port	#检查集群状况
  info            host:port #查看集群信息
  fix             host:port #修复集群
                  --timeout <arg> #修复超时时长
  reshard         host:port  #在线迁移槽位
                  --from <arg>
                  --to <arg>
                  --slots <arg>
                  --yes
                  --timeout <arg>
                  --pipeline <arg>
  rebalance       host:port  #重新分配槽位
                  --weight <arg>
                  --auto-weights
                  --use-empty-masters
                  --timeout <arg>
                  --simulate
                  --pipeline <arg>
                  --threshold <arg>
  add-node        new_host:new_port existing_host:existing_port #增加集群节点
                  --slave	
                  --master-id <arg>
  del-node        host:port node_id  #删除集群几点
  set-timeout     host:port milliseconds	#设置节点超时时长
  call            host:port command arg arg .. arg  #在集群所有节点上执行命令
  import          host:port  #从集群外导入数据
                  --from <arg>
                  --copy
                  --replace
  help            (show this help)

创建集群
前提:需要保证所有节点没有任何数据,最好使用flushallcluster reset命令,否则可能会出现以下错误

client.rb:126:in `call': ERR Slot 5798 is already busy (Redis::CommandError)
[root@localhost 16:03:32 redis]#redis-trib.rb create --replicas 1 \
> 192.168.7.10:6379 \
> 192.168.7.11:6379 \
> 192.168.7.12:6379 \
> 192.168.7.13:6379 \
> 192.168.7.14:6379 \
> 192.168.7.15:6379
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
192.168.7.10:6379
192.168.7.11:6379
192.168.7.12:6379
Adding replica 192.168.7.14:6379 to 192.168.7.10:6379
Adding replica 192.168.7.15:6379 to 192.168.7.11:6379
Adding replica 192.168.7.13:6379 to 192.168.7.12:6379
M: 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 192.168.7.10:6379
   slots:0-5460 (5461 slots) master
M: 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 192.168.7.11:6379
   slots:5461-10922 (5462 slots) master
M: 1e51d10dea6fa1da24f44df63edad2e65b55141f 192.168.7.12:6379
   slots:10923-16383 (5461 slots) master
S: 336691c7e5a71f97a72c66529c31c1068980a42d 192.168.7.13:6379
   replicates 1e51d10dea6fa1da24f44df63edad2e65b55141f
S: c5c38ae80c1784b09f101aac0f7bd492c37c5d9a 192.168.7.14:6379
   replicates 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83
S: 3f8994ac67095bf97ee79213ca34f9bf5edfc291 192.168.7.15:6379
   replicates 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join....
>>> Performing Cluster Check (using node 192.168.7.10:6379)
M: 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 192.168.7.10:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: c5c38ae80c1784b09f101aac0f7bd492c37c5d9a 192.168.7.14:6379
   slots: (0 slots) slave
   replicates 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83
S: 336691c7e5a71f97a72c66529c31c1068980a42d 192.168.7.13:6379
   slots: (0 slots) slave
   replicates 1e51d10dea6fa1da24f44df63edad2e65b55141f
M: 1e51d10dea6fa1da24f44df63edad2e65b55141f 192.168.7.12:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 3f8994ac67095bf97ee79213ca34f9bf5edfc291 192.168.7.15:6379
   slots: (0 slots) slave
   replicates 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4
M: 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 192.168.7.11:6379
   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.

如上表示redis cluster创建成功

在任意节点上都可以查看集群的信息和状态

**检查集群信息**
127.0.0.1:6379> 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_ping_sent:55
cluster_stats_messages_pong_sent:56
cluster_stats_messages_sent:111
cluster_stats_messages_ping_received:51
cluster_stats_messages_pong_received:55
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:111

**检查集群节点对应主从关系**
127.0.0.1:6379> CLUSTER NODES
c5c38ae80c1784b09f101aac0f7bd492c37c5d9a 192.168.7.14:6379@16379 slave 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 0 1581581184734 5 connected
336691c7e5a71f97a72c66529c31c1068980a42d 192.168.7.13:6379@16379 slave 1e51d10dea6fa1da24f44df63edad2e65b55141f 0 1581581184644 4 connected
29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 192.168.7.10:6379@16379 myself,master - 0 1581581183000 1 connected 0-5460
1e51d10dea6fa1da24f44df63edad2e65b55141f 192.168.7.12:6379@16379 master - 0 1581581185813 3 connected 10923-16383
3f8994ac67095bf97ee79213ca34f9bf5edfc291 192.168.7.15:6379@16379 slave 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 0 1581581182586 6 connected
04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 192.168.7.11:6379@16379 master - 0 1581581184000 2 connected 5461-10922


动态添加节点

首先新增一个与其他节点相通配置的节点:192.168.7.16
添加节点
添加节点的命令:
redis-trib.rb add-node new_host:new_port(要添加的节点) existing_host:existing_port(已存在的节点)

[root@localhost 16:06:50 redis]#redis-trib.rb add-node 192.168.7.16:6379 192.168.7.10:6379
>>> Adding node 192.168.7.16:6379 to cluster 192.168.7.10:6379
>>> Performing Cluster Check (using node 192.168.7.10:6379)
M: 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 192.168.7.10:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: c5c38ae80c1784b09f101aac0f7bd492c37c5d9a 192.168.7.14:6379
   slots: (0 slots) slave
   replicates 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83
S: 336691c7e5a71f97a72c66529c31c1068980a42d 192.168.7.13:6379
   slots: (0 slots) slave
   replicates 1e51d10dea6fa1da24f44df63edad2e65b55141f
M: 1e51d10dea6fa1da24f44df63edad2e65b55141f 192.168.7.12:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
S: 3f8994ac67095bf97ee79213ca34f9bf5edfc291 192.168.7.15:6379
   slots: (0 slots) slave
   replicates 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4
M: 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 192.168.7.11:6379
   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.
>>> Send CLUSTER MEET to node 192.168.7.16:6379 to make it join the cluster.
[OK] New node added correctly.


通过命令检查集群信息,可以发现新增的节点(192.168.7.16:6379)是没有分配槽位的,也就是任何数据都不可能写入此节点。

[root@localhost 16:07:00 redis]#redis-trib.rb check 192.168.7.10:6379
>>> Performing Cluster Check (using node 192.168.7.10:6379)
M: 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 192.168.7.10:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: c5c38ae80c1784b09f101aac0f7bd492c37c5d9a 192.168.7.14:6379
   slots: (0 slots) slave
   replicates 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83
S: 336691c7e5a71f97a72c66529c31c1068980a42d 192.168.7.13:6379
   slots: (0 slots) slave
   replicates 1e51d10dea6fa1da24f44df63edad2e65b55141f
M: 1e51d10dea6fa1da24f44df63edad2e65b55141f 192.168.7.12:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: c17387f64fba23f09cc750a11c3ce45f3c167e5f 192.168.7.16:6379
   slots: (0 slots) master
   0 additional replica(s)
S: 3f8994ac67095bf97ee79213ca34f9bf5edfc291 192.168.7.15:6379
   slots: (0 slots) slave
   replicates 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4
M: 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 192.168.7.11:6379
   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.


重新分配槽位
使用 redis-trib.rb reshard new_ip:new_port(新的节点)

[root@localhost 16:09:08 redis]#redis-trib.rb reshard 192.168.7.16:6379
>>> Performing Cluster Check (using node 192.168.7.16:6379)
M: c17387f64fba23f09cc750a11c3ce45f3c167e5f 192.168.7.16:6379
   slots: (0 slots) master
   0 additional replica(s)
S: c5c38ae80c1784b09f101aac0f7bd492c37c5d9a 192.168.7.14:6379
   slots: (0 slots) slave
   replicates 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83
M: 1e51d10dea6fa1da24f44df63edad2e65b55141f 192.168.7.12:6379
   slots:10923-16383 (5461 slots) master
   1 additional replica(s)
M: 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 192.168.7.11:6379
   slots:5461-10922 (5462 slots) master
   1 additional replica(s)
M: 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 192.168.7.10:6379
   slots:0-5460 (5461 slots) master
   1 additional replica(s)
S: 336691c7e5a71f97a72c66529c31c1068980a42d 192.168.7.13:6379
   slots: (0 slots) slave
   replicates 1e51d10dea6fa1da24f44df63edad2e65b55141f
S: 3f8994ac67095bf97ee79213ca34f9bf5edfc291 192.168.7.15:6379
   slots: (0 slots) slave
   replicates 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4
[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? c17387f64fba23f09cc750a11c3ce45f3c167e5f
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:all

检查集群中的节点信息,可以看到槽位的变化,每个主节点的槽位都为4096个


[root@localhost 16:10:13 redis]#redis-trib.rb check 192.168.7.16:6379
>>> Performing Cluster Check (using node 192.168.7.16:6379)
M: c17387f64fba23f09cc750a11c3ce45f3c167e5f 192.168.7.16:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   0 additional replica(s)
S: c5c38ae80c1784b09f101aac0f7bd492c37c5d9a 192.168.7.14:6379
   slots: (0 slots) slave
   replicates 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83
M: 1e51d10dea6fa1da24f44df63edad2e65b55141f 192.168.7.12:6379
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
M: 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 192.168.7.11:6379
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
M: 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 192.168.7.10:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
S: 336691c7e5a71f97a72c66529c31c1068980a42d 192.168.7.13:6379
   slots: (0 slots) slave
   replicates 1e51d10dea6fa1da24f44df63edad2e65b55141f
S: 3f8994ac67095bf97ee79213ca34f9bf5edfc291 192.168.7.15:6379
   slots: (0 slots) slave
   replicates 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

再添加新的节点做为16的从节点,以防单点失败

[root@localhost 16:24:33 redis]#redis-trib.rb add-node 192.168.7.17:6379 192.168.7.10:6379
>>> Adding node 192.168.7.17:6379 to cluster 192.168.7.10:6379
>>> Performing Cluster Check (using node 192.168.7.10:6379)
M: 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 192.168.7.10:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
S: c5c38ae80c1784b09f101aac0f7bd492c37c5d9a 192.168.7.14:6379
   slots: (0 slots) slave
   replicates 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83
S: 336691c7e5a71f97a72c66529c31c1068980a42d 192.168.7.13:6379
   slots: (0 slots) slave
   replicates 1e51d10dea6fa1da24f44df63edad2e65b55141f
M: 1e51d10dea6fa1da24f44df63edad2e65b55141f 192.168.7.12:6379
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
M: c17387f64fba23f09cc750a11c3ce45f3c167e5f 192.168.7.16:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   0 additional replica(s)
S: 3f8994ac67095bf97ee79213ca34f9bf5edfc291 192.168.7.15:6379
   slots: (0 slots) slave
   replicates 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4
M: 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 192.168.7.11:6379
   slots:6827-10922 (4096 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.
>>> Send CLUSTER MEET to node 192.168.7.17:6379 to make it join the cluster.
[OK] New node added correctly.

检查集群中的节点信息,新添加的节点为主节点

[root@localhost 16:26:32 redis]#redis-trib.rb check 192.168.7.16:6379
>>> Performing Cluster Check (using node 192.168.7.16:6379)
M: c17387f64fba23f09cc750a11c3ce45f3c167e5f 192.168.7.16:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   0 additional replica(s)
S: c5c38ae80c1784b09f101aac0f7bd492c37c5d9a 192.168.7.14:6379
   slots: (0 slots) slave
   replicates 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83
M: f80bf2c1364546bb5dec348ce7b01fe6f19e3aea 192.168.7.17:6379
   slots: (0 slots) master
   0 additional replica(s)
M: 1e51d10dea6fa1da24f44df63edad2e65b55141f 192.168.7.12:6379
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
M: 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 192.168.7.11:6379
   slots:6827-10922 (4096 slots) master
   1 additional replica(s)
M: 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 192.168.7.10:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
S: 336691c7e5a71f97a72c66529c31c1068980a42d 192.168.7.13:6379
   slots: (0 slots) slave
   replicates 1e51d10dea6fa1da24f44df63edad2e65b55141f
S: 3f8994ac67095bf97ee79213ca34f9bf5edfc291 192.168.7.15:6379
   slots: (0 slots) slave
   replicates 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

为新增的节点指定master

[root@localhost 16:26:37 redis]#redis-cli  -h 192.168.7.17 
192.168.7.17:6379> CLUSTER NODES
c17387f64fba23f09cc750a11c3ce45f3c167e5f 192.168.7.16:6379@16379 master - 0 1581583121495 7 connected 0-1364 5461-6826 10923-12287
04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 192.168.7.11:6379@16379 master - 0 1581583122500 2 connected 6827-10922
29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 192.168.7.10:6379@16379 master - 0 1581583120000 1 connected 1365-5460
f80bf2c1364546bb5dec348ce7b01fe6f19e3aea 192.168.7.17:6379@16379 myself,master - 0 1581583121000 0 connected
3f8994ac67095bf97ee79213ca34f9bf5edfc291 192.168.7.15:6379@16379 slave 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 0 1581583118472 2 connected
1e51d10dea6fa1da24f44df63edad2e65b55141f 192.168.7.12:6379@16379 master - 0 1581583122000 3 connected 12288-16383
c5c38ae80c1784b09f101aac0f7bd492c37c5d9a 192.168.7.14:6379@16379 slave 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 0 1581583123509 1 connected
336691c7e5a71f97a72c66529c31c1068980a42d 192.168.7.13:6379@16379 slave 1e51d10dea6fa1da24f44df63edad2e65b55141f 0 1581583120487 3 connected

192.168.7.17:6379> CLUSTER REPLICATE c17387f64fba23f09cc750a11c3ce45f3c167e5f #指定新节点的ID号即可
OK
192.168.7.17:6379> CLUSTER NODES
c17387f64fba23f09cc750a11c3ce45f3c167e5f 192.168.7.16:6379@16379 master - 0 1581583162841 7 connected 0-1364 5461-6826 10923-12287
04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 192.168.7.11:6379@16379 master - 0 1581583161000 2 connected 6827-10922
29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 192.168.7.10:6379@16379 master - 0 1581583163000 1 connected 1365-5460
f80bf2c1364546bb5dec348ce7b01fe6f19e3aea 192.168.7.17:6379@16379 myself,slave c17387f64fba23f09cc750a11c3ce45f3c167e5f 0 1581583162000 0 connected
3f8994ac67095bf97ee79213ca34f9bf5edfc291 192.168.7.15:6379@16379 slave 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 0 1581583162000 2 connected
1e51d10dea6fa1da24f44df63edad2e65b55141f 192.168.7.12:6379@16379 master - 0 1581583163848 3 connected 12288-16383
c5c38ae80c1784b09f101aac0f7bd492c37c5d9a 192.168.7.14:6379@16379 slave 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 0 1581583162000 1 connected
336691c7e5a71f97a72c66529c31c1068980a42d 192.168.7.13:6379@16379 slave 1e51d10dea6fa1da24f44df63edad2e65b55141f 0 1581583164857 3 connected

删除集群节点

删除节点的步骤与新增节点相反,需要先减少槽位并移动到新的节点上,再删除节点,否则会删除失败

移动10节点的槽位到11节点

[root@localhost 16:47:52 redis]#redis-trib.rb reshard 192.168.7.10:6379
>>> Performing Cluster Check (using node 192.168.7.10:6379)
M: 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 192.168.7.10:6379
   slots:1365-5460 (4096 slots) master
   1 additional replica(s)
S: c5c38ae80c1784b09f101aac0f7bd492c37c5d9a 192.168.7.14:6379
   slots: (0 slots) slave
   replicates 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83
S: 336691c7e5a71f97a72c66529c31c1068980a42d 192.168.7.13:6379
   slots: (0 slots) slave
   replicates 1e51d10dea6fa1da24f44df63edad2e65b55141f
M: 1e51d10dea6fa1da24f44df63edad2e65b55141f 192.168.7.12:6379
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: f80bf2c1364546bb5dec348ce7b01fe6f19e3aea 192.168.7.17:6379
   slots: (0 slots) slave
   replicates c17387f64fba23f09cc750a11c3ce45f3c167e5f
M: c17387f64fba23f09cc750a11c3ce45f3c167e5f 192.168.7.16:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   1 additional replica(s)
S: 3f8994ac67095bf97ee79213ca34f9bf5edfc291 192.168.7.15:6379
   slots: (0 slots) slave
   replicates 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4
M: 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 192.168.7.11:6379
   slots:6827-10922 (4096 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? 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 #接收槽位的节点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:29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 #从那个节点ID移动
Source node #2:done #done表示结束,将从上面的所有节点ID移动4096个槽位到接收的节点

检查集群节点信息
10主机已经没有槽位了

[root@localhost 16:49:08 redis]#redis-trib.rb check 192.168.7.10:6379
>>> Performing Cluster Check (using node 192.168.7.10:6379)
M: 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 192.168.7.10:6379
   slots: (0 slots) master
   0 additional replica(s)
S: c5c38ae80c1784b09f101aac0f7bd492c37c5d9a 192.168.7.14:6379
   slots: (0 slots) slave
   replicates 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4
S: 336691c7e5a71f97a72c66529c31c1068980a42d 192.168.7.13:6379
   slots: (0 slots) slave
   replicates 1e51d10dea6fa1da24f44df63edad2e65b55141f
M: 1e51d10dea6fa1da24f44df63edad2e65b55141f 192.168.7.12:6379
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: f80bf2c1364546bb5dec348ce7b01fe6f19e3aea 192.168.7.17:6379
   slots: (0 slots) slave
   replicates c17387f64fba23f09cc750a11c3ce45f3c167e5f
M: c17387f64fba23f09cc750a11c3ce45f3c167e5f 192.168.7.16:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   1 additional replica(s)
S: 3f8994ac67095bf97ee79213ca34f9bf5edfc291 192.168.7.15:6379
   slots: (0 slots) slave
   replicates 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4
M: 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 192.168.7.11:6379
   slots:1365-5460,6827-10922 (8192 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.

删除节点
用法:redis-trib.rb del-node host_IP:host_port(集群中任意主机) ID(删除的节点的ID)

[root@localhost 16:57:51 redis]#redis-trib.rb del-node 192.168.7.10:6379 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83
>>> Removing node 29ed27a90e9388a3a4c7c8e8255db0b669c3ec83 from cluster 192.168.7.10:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.

查看集群信息
可以发现11主机继承了10主机的slave,个数变成2.

[root@localhost 16:59:52 redis]#redis-trib.rb info 192.168.7.11:6379 
192.168.7.11:6379 (04e009c4...) -> 0 keys | 8192 slots | 2 slaves.
192.168.7.16:6379 (c17387f6...) -> 0 keys | 4096 slots | 1 slaves.
192.168.7.12:6379 (1e51d10d...) -> 0 keys | 4096 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.

集群导入数据

在其他未在集群的节点上通过脚本创建keys,然后导入到集群中
语法:redis-trib.rb import --from server_ip:server_port --replace cluster_ip:cluster_port

[root@localhost 17:15:39 ~]#redis-trib.rb import  --from 192.168.7.10:6379 --replace 192.168.7.11:6379
>>> Importing data from 192.168.7.10:6379 to cluster 
>>> Performing Cluster Check (using node 192.168.7.11:6379)
M: 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4 192.168.7.11:6379
   slots:1365-5460,6827-10922 (8192 slots) master
   2 additional replica(s)
M: c17387f64fba23f09cc750a11c3ce45f3c167e5f 192.168.7.16:6379
   slots:0-1364,5461-6826,10923-12287 (4096 slots) master
   1 additional replica(s)
S: f80bf2c1364546bb5dec348ce7b01fe6f19e3aea 192.168.7.17:6379
   slots: (0 slots) slave
   replicates c17387f64fba23f09cc750a11c3ce45f3c167e5f
S: 3f8994ac67095bf97ee79213ca34f9bf5edfc291 192.168.7.15:6379
   slots: (0 slots) slave
   replicates 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4
M: 1e51d10dea6fa1da24f44df63edad2e65b55141f 192.168.7.12:6379
   slots:12288-16383 (4096 slots) master
   1 additional replica(s)
S: c5c38ae80c1784b09f101aac0f7bd492c37c5d9a 192.168.7.14:6379
   slots: (0 slots) slave
   replicates 04e009c49ed9bbf5dbe2417c7d3089d659dfc9c4
S: 336691c7e5a71f97a72c66529c31c1068980a42d 192.168.7.13:6379
   slots: (0 slots) slave
   replicates 1e51d10dea6fa1da24f44df63edad2e65b55141f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Connecting to the source Redis instance
*** Importing 100 keys from DB 0
Migrating k58 to 192.168.7.11:6379: OK
Migrating k79 to 192.168.7.11:6379: OK
...

删除集群

备份所有集群节点的rdb和aof文件后,删除所有的rdb、aof和cluster配置文件后,重启redis即可

总结

  1. 如果redis是4版本的话,gem 安装redis时需保证版本不能为4,否则迁移槽点时报错
  2. 增删集群节点时,需确保数据的安全性,做好备份
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值