Redis集群搭建和扩缩容

注意:

每个redis节点使用相同硬件的配置,相同的版本,相同的密码。

redis节点必须没有任何数据,否则分配槽位会失败。

集群规划:

Redis集群搭建和扩缩容

原生命令手动部署集群

部署过程

  • 安装redis并配置开启cluster功能
  • 各个节点执行meet,实现所有节点互相通信
  • 为每个master节点分配槽位
  • 为每个master节点分配从节点

安装redis并修改配置:

# 安装redis
[root@centos8 ~]#yum -y install redis


# 手动修改相关配置
[root@centos8 ~]#vim /etc/redis.conf
bind 0.0.0.0
masterauth 744123								# masterauth必须和requirepass保持一致
requirepass 744123
cluster-enabled yes								# 启用redis的集群功能
cluster-config-file nodes-6379.conf				# 集群节点自动维护的文件,主要用于记录集群中节点的状态,信息和参数等
cluster-require-full-coverage no				# 如果当前redis发现有failed的槽位,默认会将cluster_state从ok转变为fail, 写入命令会失败。改为no则不会有此限制。

# 或者使用sed修改
[root@centos8 ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/c masterauth 744123' -e '/# requirepass/c requirepass 744123' -e '/cluster-enabled yes/c cluster-enabled yes' -e '/cluster-config-file/c cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf


# 修改完后将配置文件复制到其他各个节点
[root@centos8 ~]#scp /etc/redis.conf 10.0.0.82:/etc/redis.conf
[root@centos8 ~]#scp /etc/redis.conf 10.0.0.83:/etc/redis.conf
[root@centos8 ~]#scp /etc/redis.conf 10.0.0.84:/etc/redis.conf
[root@centos8 ~]#scp /etc/redis.conf 10.0.0.85:/etc/redis.conf
[root@centos8 ~]#scp /etc/redis.conf 10.0.0.86:/etc/redis.conf


# 启动redis
[root@centos8 ~]#systemctl enable --now redis

查看集群状态:

# 查看进程会发现redis已经转为cluster状态
[root@centos8 ~]#ps aux | grep redis
redis       6162  0.0  0.5  53520 10188 ?        Ssl  09:47   0:00 /usr/bin/redis-server 0.0.0.0:6379 [cluster]
root        6181  0.0  0.0  12108   968 pts/0    S+   09:48   0:00 grep --color=auto redis


# 查看端口号会多出一个16379的集群通信端口
[root@centos8 ~]#ss -nlt
State      Recv-Q     Send-Q         Local Address:Port            Peer Address:Port     
LISTEN     0          128                  0.0.0.0:16379                0.0.0.0:*        
LISTEN     0          128                  0.0.0.0:6379                 0.0.0.0:*        
LISTEN     0          128                  0.0.0.0:80                   0.0.0.0:*        
LISTEN     0          128                  0.0.0.0:22                   0.0.0.0:*        
LISTEN     0          128                     [::]:80                      [::]:*        
LISTEN     0          128                     [::]:22                      [::]:*        


# 在每个节点上查看集群状态,都认为自己就是一个集群,只有自己一个节点。
[root@centos8 ~]#redis-cli -a 744123 --no-auth-warning cluster nodes
e04073997fccfb6fb7589e4979c2214174462545 :6379@16379 myself,master - 0 0 0 connected

执行meet操作,实现集群互相通信:

[root@centos8 ~]#redis-cli -a 744123 --no-auth-warning cluster meet 10.0.0.82 6379
OK
[root@centos8 ~]#redis-cli -a 744123 --no-auth-warning cluster meet 10.0.0.83 6379
OK
[root@centos8 ~]#redis-cli -a 744123 --no-auth-warning cluster meet 10.0.0.84 6379
OK
[root@centos8 ~]#redis-cli -a 744123 --no-auth-warning cluster meet 10.0.0.85 6379
OK
[root@centos8 ~]#redis-cli -a 744123 --no-auth-warning cluster meet 10.0.0.86 6379
OK


# 再次在每个节点上查看集群状态
[root@centos8 ~]#redis-cli -a 744123 --no-auth-warning cluster nodes
c77ba9378c30faa74b4250b1f2ed4cf9c30e7c47 10.0.0.82:6379@16379 myself,master - 0 1609552558000 1 connected
318479e04162e0eba6bd85e7548686a4d5fb1c60 10.0.0.83:6379@16379 master - 0 1609552560000 2 connected
1036067707c22095ac3652b1f691b5531ccfa4b5 10.0.0.85:6379@16379 master - 0 1609552560805 4 connected
4bf57cb52ecc2f1ee3e95e95ad85c1173ae26669 10.0.0.86:6379@16379 master - 0 1609552559801 5 connected
e04073997fccfb6fb7589e4979c2214174462545 10.0.0.81:6379@16379 master - 0 1609552560000 0 connected
ce8226db5314fccb50bae448bbf6252ccd417766 10.0.0.84:6379@16379 master - 0 1609552559000 3 connected


# 查看集群信息
[root@centos8 ~]#redis-cli -a 744123 --no-auth-warning cluster info
cluster_state:fail							# 集群状态
cluster_slots_assigned:0					# 已分配槽位数量 0
cluster_slots_ok:0							# 状态为OK槽位数量 0
cluster_slots_pfail:0						# 可能失效槽位数量
cluster_slots_fail:0						# 已经失效槽位数量
cluster_known_nodes:6						# 集群节点数量 6
cluster_size:0								# 已分配槽位的节点数量 0
cluster_current_epoch:5
cluster_my_epoch:1
cluster_stats_messages_ping_sent:245		# 这里往下均为message统计信息
cluster_stats_messages_pong_sent:239
cluster_stats_messages_meet_sent:3
cluster_stats_messages_sent:487
cluster_stats_messages_ping_received:237
cluster_stats_messages_pong_received:248
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:487

为master节点分配槽位:

# 一共16384个槽位,需要一个一个手动分配,因此使用脚本进行分配
[root@centos8 ~]#cat addslot.sh 
#!/bin/bash
#
#********************************************************************
#Author:            Wuvikr
#QQ:                744123155
#Date:              2021-01-02
#FileName           addslot.sh
#URL:               http://www.wuvikr.com
#Description        The test script
#Copyright (C):     2021 All rights reserved
#********************************************************************
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

HOST=$1
PORT=$2
START=$3
END=$4
PASS=744123

for slot in `seq ${START} ${END}`;do
    echo "slot: ${slot}"
    redis-cli -h ${HOST} -p ${PORT} -a ${PASS} --no-auth-warning cluster addslots ${slot}
done


# 选择10.0.0.81,10.0.0.82,10.0.0.83为master节点进行槽位分配
[root@centos8 ~]#bash addslot.sh 10.0.0.81 6379 0 5461
[root@centos8 ~]#bash addslot.sh 10.0.0.82 6379 5462 10922
[root@centos8 ~]#bash addslot.sh 10.0.0.83 6379 10923 16383


# 分配完槽位后,查看节点信息可以看到槽位分配状况
[root@centos8 ~]#redis-cli -a 744123 --no-auth-warning cluster nodes
ce8226db5314fccb50bae448bbf6252ccd417766 10.0.0.84:6379@16379 master - 0 1609556408129 3 connected
4bf57cb52ecc2f1ee3e95e95ad85c1173ae26669 10.0.0.86:6379@16379 master - 0 1609556410139 5 connected
1036067707c22095ac3652b1f691b5531ccfa4b5 10.0.0.85:6379@16379 master - 0 1609556408000 4 connected
318479e04162e0eba6bd85e7548686a4d5fb1c60 10.0.0.83:6379@16379 master - 0 1609556409135 2 connected 10923-16383
e04073997fccfb6fb7589e4979c2214174462545 10.0.0.81:6379@16379 myself,master - 0 1609556409000 0 connected 0-5461
c77ba9378c30faa74b4250b1f2ed4cf9c30e7c47 10.0.0.82:6379@16379 master - 0 1609556410000 1 connected 5462-10922

# 查看集群信息
[root@centos8 ~]#redis-cli -a 744123 --no-auth-warning 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:0
cluster_stats_messages_ping_sent:4256
cluster_stats_messages_pong_sent:4358
cluster_stats_messages_meet_sent:5
cluster_stats_messages_sent:8619
cluster_stats_messages_ping_received:4358
cluster_stats_messages_pong_received:4261
cluster_stats_messages_received:8619

为master节点指定slave节点:

[root@centos8 ~]#redis-cli -h 10.0.0.84 -a 744123 --no-auth-warning cluster replicate e04073997fccfb6fb7589e4979c2214174462545
OK
[root@centos8 ~]#redis-cli -h 10.0.0.85 -a 744123 --no-auth-warning cluster replicate c77ba9378c30faa74b4250b1f2ed4cf9c30e7c47
OK
[root@centos8 ~]#redis-cli -h 10.0.0.86 -a 744123 --no-auth-warning cluster replicate 318479e04162e0eba6bd85e7548686a4d5fb1c60
OK


# 查看节点信息,可以发现10.0.0.83,10.0.0.84,10.0.0.85已经成为slave节点了
[root@centos8 ~]#redis-cli -a 744123 --no-auth-warning cluster nodes
ce8226db5314fccb50bae448bbf6252ccd417766 10.0.0.84:6379@16379 slave e04073997fccfb6fb7589e4979c2214174462545 0 1609557506286 3 connected
4bf57cb52ecc2f1ee3e95e95ad85c1173ae26669 10.0.0.86:6379@16379 slave 318479e04162e0eba6bd85e7548686a4d5fb1c60 0 1609557505281 5 connected
1036067707c22095ac3652b1f691b5531ccfa4b5 10.0.0.85:6379@16379 slave c77ba9378c30faa74b4250b1f2ed4cf9c30e7c47 0 1609557504277 4 connected
318479e04162e0eba6bd85e7548686a4d5fb1c60 10.0.0.83:6379@16379 master - 0 1609557507291 2 connected 10923-16383
e04073997fccfb6fb7589e4979c2214174462545 10.0.0.81:6379@16379 myself,master - 0 1609557505000 0 connected 0-5461
c77ba9378c30faa74b4250b1f2ed4cf9c30e7c47 10.0.0.82:6379@16379 master - 0 1609557506000 1 connected 5462-10922


# 查看其中一组redis主从复制
[root@centos8 ~]#redis-cli -a 744123 --no-auth-warning info replication
# Replication
role:master
connected_slaves:1
slave0:ip=10.0.0.84,port=6379,state=online,offset=392,lag=1
master_replid:a9f5fed2d0ddb1e2898f271db4a0de8105d57e63
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:392
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:392

[root@centos8 ~]#redis-cli -a 744123 -h 10.0.0.84 --no-auth-warning info replication
# Replication
role:slave
master_host:10.0.0.81
master_port:6379
master_link_status:up
master_last_io_seconds_ago:4
master_sync_in_progress:0
slave_repl_offset:420
slave_priority:100
slave_read_only:1
connected_slaves:0
master_replid:a9f5fed2d0ddb1e2898f271db4a0de8105d57e63
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:420
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:420


# 查看槽位及主从关系
[root@centos82 ~]#redis-cli -a 744123 --no-auth-warning cluster slots
1) 1) (integer) 5462
   2) (integer) 10922
   3) 1) "10.0.0.82"
      2) (integer) 6379
      3) "c77ba9378c30faa74b4250b1f2ed4cf9c30e7c47"
   4) 1) "10.0.0.85"
      2) (integer) 6379
      3) "1036067707c22095ac3652b1f691b5531ccfa4b5"
2) 1) (integer) 0
   2) (integer) 5461
   3) 1) "10.0.0.81"
      2) (integer) 6379
      3) "e04073997fccfb6fb7589e4979c2214174462545"
   4) 1) "10.0.0.84"
      2) (integer) 6379
      3) "ce8226db5314fccb50bae448bbf6252ccd417766"
3) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "10.0.0.83"
      2) (integer) 6379
      3) "318479e04162e0eba6bd85e7548686a4d5fb1c60"
   4) 1) "10.0.0.86"
      2) (integer) 6379
      3) "4bf57cb52ecc2f1ee3e95e95ad85c1173ae26669"

一条命令部署cluster

此方法只适用于5及以上版本

[root@centos8 ~]#redis-cli --cluster  help
Cluster Manager Commands:
  create         host1:port1 ... hostN:portN
                 --cluster-replicas <arg>
  check          host:port
                 --cluster-search-multiple-owners
  info           host:port
  fix            host:port
                 --cluster-search-multiple-owners
  reshard        host:port
                 --cluster-from <arg>
                 --cluster-to <arg>
                 --cluster-slots <arg>
                 --cluster-yes
                 --cluster-timeout <arg>
                 --cluster-pipeline <arg>
                 --cluster-replace
  rebalance      host:port
                 --cluster-weight <node1=w1...nodeN=wN>
                 --cluster-use-empty-masters
                 --cluster-timeout <arg>
                 --cluster-simulate
                 --cluster-pipeline <arg>
                 --cluster-threshold <arg>
                 --cluster-replace
  add-node       new_host:new_port existing_host:existing_port
                 --cluster-slave
                 --cluster-master-id <arg>
  del-node       host:port node_id
  call           host:port command arg arg .. arg
  set-timeout    host:port milliseconds
  import         host:port
                 --cluster-from <arg>
                 --cluster-copy
                 --cluster-replace
  help           

For check, fix, reshard, del-node, set-timeout you can specify the host and port of any working node in the cluster.

使用--cluster create命令一键部署cluster:

# 默认前一半节点为主,后一半节点为从,按顺序匹配
[root@centos8 ~]#redis-cli -a 744123 --cluster create 10.0.0.81:6379 10.0.0.82:6379 10.0.0.83:6379 10.0.0.84:6379 10.0.0.85:6379 10.0.0.86:6379 --cluster-replicas 1
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 10.0.0.84:6379 to 10.0.0.81:6379
Adding replica 10.0.0.85:6379 to 10.0.0.82:6379
Adding replica 10.0.0.86:6379 to 10.0.0.83:6379
M: 0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0 10.0.0.81:6379
   slots:[0-5460] (5461 slots) master
M: bdccb2d59cd85950cf0e507bb4e29f64f7e23636 10.0.0.82:6379
   slots:[5461-10922] (5462 slots) master
M: 8679f81ab720ee66925adee602b620abb89f1bc6 10.0.0.83:6379
   slots:[10923-16383] (5461 slots) master
S: 1df4fd3501abfffa7a81a9bfb1061720089b5414 10.0.0.84:6379
   replicates 0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0
S: dccf11f7d55f74a3d5bf774449b2604334d2f0b1 10.0.0.85:6379
   replicates bdccb2d59cd85950cf0e507bb4e29f64f7e23636
S: a82f0f41c14d6ff43a4fbb4c1a3e34269d1b6eef 10.0.0.86:6379
   replicates 8679f81ab720ee66925adee602b620abb89f1bc6
Can I set the above configuration? (type 'yes' to accept): yes
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.


# 查看集群状况
[root@centos8 ~]#redis-cli -a 744123 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0 10.0.0.81:6379@16379 myself,master - 0 1609578057000 1 connected 0-5460
1df4fd3501abfffa7a81a9bfb1061720089b5414 10.0.0.84:6379@16379 slave 0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0 0 1609578059296 4 connected
bdccb2d59cd85950cf0e507bb4e29f64f7e23636 10.0.0.82:6379@16379 master - 0 1609578058292 2 connected 5461-10922
8679f81ab720ee66925adee602b620abb89f1bc6 10.0.0.83:6379@16379 master - 0 1609578057287 3 connected 10923-16383
a82f0f41c14d6ff43a4fbb4c1a3e34269d1b6eef 10.0.0.86:6379@16379 slave 8679f81ab720ee66925adee602b620abb89f1bc6 0 1609578057000 6 connected
dccf11f7d55f74a3d5bf774449b2604334d2f0b1 10.0.0.85:6379@16379 slave bdccb2d59cd85950cf0e507bb4e29f64f7e23636 0 1609578057000 5 connected

[root@centos8 ~]#redis-cli -a 744123 cluster slots
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
1) 1) (integer) 0
   2) (integer) 5460
   3) 1) "10.0.0.81"
      2) (integer) 6379
      3) "0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0"
   4) 1) "10.0.0.84"
      2) (integer) 6379
      3) "1df4fd3501abfffa7a81a9bfb1061720089b5414"
2) 1) (integer) 5461
   2) (integer) 10922
   3) 1) "10.0.0.82"
      2) (integer) 6379
      3) "bdccb2d59cd85950cf0e507bb4e29f64f7e23636"
   4) 1) "10.0.0.85"
      2) (integer) 6379
      3) "dccf11f7d55f74a3d5bf774449b2604334d2f0b1"
3) 1) (integer) 10923
   2) (integer) 16383
   3) 1) "10.0.0.83"
      2) (integer) 6379
      3) "8679f81ab720ee66925adee602b620abb89f1bc6"
   4) 1) "10.0.0.86"
      2) (integer) 6379
      3) "a82f0f41c14d6ff43a4fbb4c1a3e34269d1b6eef"

集群扩容

当现有的三主三从redis cluster无法满足生产需要,需要进行集群扩容,临时新加入一组redis主(10.0.0.87)从(10.0.0.88)。(生产环境master节点最好为奇数个)。

扩容可能导致数据丢失,最好提前做好数据备份,然后清空集群数据,完成扩容后,再将数据导回。

首先在新机器上安装redis并修改配置:

[root@centos8 ~]#dnf -y install redis
[root@centos8 ~]#sed -i.bak -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e '/masterauth/c masterauth 744123' -e '/# requirepass/c requirepass 744123' -e '/cluster-enabled yes/c cluster-enabled yes' -e '/cluster-config-file/c cluster-config-file nodes-6379.conf' -e '/cluster-require-full-coverage yes/c cluster-require-full-coverage no' /etc/redis.conf
[root@centos8 ~]#systemctl enable --now redis

添加新的master节点到集群中:

# 将IP为10.0.0.87的新机器加入到现有集群中
# 最后指定加入到集群中任意一台机器的IP+Port都可以
[root@centos8 ~]#redis-cli -a 744123 --cluster add-node 10.0.0.87:6379 10.0.0.81:6379

# 查看集群信息可以看到新加入的master节点10.0.0.87
[root@centos8 ~]#redis-cli -a 744123 --no-auth-warning cluster nodes
44f0f1daf3850712ff3aaa2b761235fefce7e79e 10.0.0.87:6379@16379 myself,master - 0 1609633265000 0 connected
bdccb2d59cd85950cf0e507bb4e29f64f7e23636 10.0.0.82:6379@16379 master - 0 1609633268680 2 connected 5461-10922
1df4fd3501abfffa7a81a9bfb1061720089b5414 10.0.0.84:6379@16379 slave 0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0 0 1609633266669 1 connected
8679f81ab720ee66925adee602b620abb89f1bc6 10.0.0.83:6379@16379 master - 0 1609633267000 3 connected 10923-16383
dccf11f7d55f74a3d5bf774449b2604334d2f0b1 10.0.0.85:6379@16379 slave bdccb2d59cd85950cf0e507bb4e29f64f7e23636 0 1609633267675 2 connected
a82f0f41c14d6ff43a4fbb4c1a3e34269d1b6eef 10.0.0.86:6379@16379 slave 8679f81ab720ee66925adee602b620abb89f1bc6 0 1609633265664 3 connected
0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0 10.0.0.81:6379@16379 master - 0 1609633266000 1 connected 0-5460

因为新添加了节点,所有整个集群的槽位需要重新分配:

# 指定任意集群IP+Port都可以
[root@centos8 ~]#redis-cli -a 744123 --cluster reshard 10.0.0.81:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 10.0.0.81:6379)
M: 0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0 10.0.0.81:6379
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
M: 44f0f1daf3850712ff3aaa2b761235fefce7e79e 10.0.0.87:6379
   slots: (0 slots) master
S: 1df4fd3501abfffa7a81a9bfb1061720089b5414 10.0.0.84:6379
   slots: (0 slots) slave
   replicates 0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0
M: bdccb2d59cd85950cf0e507bb4e29f64f7e23636 10.0.0.82:6379
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
M: 8679f81ab720ee66925adee602b620abb89f1bc6 10.0.0.83:6379
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
S: a82f0f41c14d6ff43a4fbb4c1a3e34269d1b6eef 10.0.0.86:6379
   slots: (0 slots) slave
   replicates 8679f81ab720ee66925adee602b620abb89f1bc6
S: dccf11f7d55f74a3d5bf774449b2604334d2f0b1 10.0.0.85:6379
   slots: (0 slots) slave
   replicates bdccb2d59cd85950cf0e507bb4e29f64f7e23636
[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? 44f0f1daf3850712ff3aaa2b761235fefce7e79e		# 要移动到那个机器上
Please enter all the source node IDs.										# 怎么移动?
  Type 'all' to use all the nodes as source nodes for the hash slots.		# all:从其他master上均匀分配过去
  Type 'done' once you entered all the source nodes IDs.					# done:指定从某台master上移动过去
Source node #1: all

将新的slave加入到集群中,并为其指定master:

[root@centos8 ~]#redis-cli -a 744123 --cluster add-node 10.0.0.88:6379 10.0.0.81:6379 --cluster-slave --cluster-master-id 44f0f1daf3850712ff3aaa2b761235fefce7e79e

# 查看集群信息
[root@centos8 ~]#redis-cli -a 744123 --no-auth-warning cluster nodes
0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0 10.0.0.81:6379@16379 myself,master - 0 1609634922000 1 connected 1365-5460
44f0f1daf3850712ff3aaa2b761235fefce7e79e 10.0.0.87:6379@16379 master - 0 1609634918000 7 connected 0-1364 5461-6826 10923-12287
1df4fd3501abfffa7a81a9bfb1061720089b5414 10.0.0.84:6379@16379 slave 0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0 0 1609634921746 4 connected
a4513e13c34b0a9b1ec88b6c9296ca6183249723 10.0.0.88:6379@16379 slave 44f0f1daf3850712ff3aaa2b761235fefce7e79e 0 1609634921000 7 connected
bdccb2d59cd85950cf0e507bb4e29f64f7e23636 10.0.0.82:6379@16379 master - 0 1609634921000 2 connected 6827-10922
8679f81ab720ee66925adee602b620abb89f1bc6 10.0.0.83:6379@16379 master - 0 1609634923755 3 connected 12288-16383
a82f0f41c14d6ff43a4fbb4c1a3e34269d1b6eef 10.0.0.86:6379@16379 slave 8679f81ab720ee66925adee602b620abb89f1bc6 0 1609634922751 6 connected
dccf11f7d55f74a3d5bf774449b2604334d2f0b1 10.0.0.85:6379@16379 slave bdccb2d59cd85950cf0e507bb4e29f64f7e23636 0 1609634921000 5 connected

集群缩容

首先要将需要进行缩容操作的机器(10.0.0.81 & 10.0.0.84)上的槽位移动到其他master上去。

重新进行集群分片:

[root@centos8 ~]#redis-cli -a 744123 --cluster reshard 10.0.0.85:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 10.0.0.85:6379)
S: dccf11f7d55f74a3d5bf774449b2604334d2f0b1 10.0.0.85:6379
   slots: (0 slots) slave
   replicates bdccb2d59cd85950cf0e507bb4e29f64f7e23636
S: a4513e13c34b0a9b1ec88b6c9296ca6183249723 10.0.0.88:6379
   slots: (0 slots) slave
   replicates 44f0f1daf3850712ff3aaa2b761235fefce7e79e
M: bdccb2d59cd85950cf0e507bb4e29f64f7e23636 10.0.0.82:6379
   slots:[6827-10922] (4096 slots) master
   1 additional replica(s)
S: a82f0f41c14d6ff43a4fbb4c1a3e34269d1b6eef 10.0.0.86:6379
   slots: (0 slots) slave
   replicates 8679f81ab720ee66925adee602b620abb89f1bc6
M: 8679f81ab720ee66925adee602b620abb89f1bc6 10.0.0.83:6379
   slots:[12288-16383] (4096 slots) master
   1 additional replica(s)
M: 44f0f1daf3850712ff3aaa2b761235fefce7e79e 10.0.0.87:6379
   slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
   1 additional replica(s)
S: 1df4fd3501abfffa7a81a9bfb1061720089b5414 10.0.0.84:6379
   slots: (0 slots) slave
   replicates 0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0
M: 0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0 10.0.0.81:6379
   slots:[1365-5460] (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)? 1200
What is the receiving node ID? bdccb2d59cd85950cf0e507bb4e29f64f7e23636
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: 0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0
Source node #2: done

# 非交互式操作
[root@centos8 ~]#redis-cli -a 744123 --cluster reshard 10.0.0.85:6379 --cluster-slots 1555 --cluster-from 0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0 --cluster-to 44f0f1daf3850712ff3aaa2b761235fefce7e79e --cluster-yes


# 重复上述操作多次,直到将节点的槽位全部分完,最好平均分配到其他master节点
[root@centos8 ~]#redis-cli -a 744123 --cluster check 10.0.0.85:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
10.0.0.82:6379 (bdccb2d5...) -> 0 keys | 5296 slots | 1 slaves.
10.0.0.83:6379 (8679f81a...) -> 0 keys | 5537 slots | 2 slaves.
10.0.0.87:6379 (44f0f1da...) -> 0 keys | 5551 slots | 1 slaves.
10.0.0.81:6379 (0cc72ea4...) -> 0 keys | 0 slots | 0 slaves.
[OK] 0 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 10.0.0.85:6379)
S: dccf11f7d55f74a3d5bf774449b2604334d2f0b1 10.0.0.85:6379
   slots: (0 slots) slave
   replicates bdccb2d59cd85950cf0e507bb4e29f64f7e23636
S: a4513e13c34b0a9b1ec88b6c9296ca6183249723 10.0.0.88:6379
   slots: (0 slots) slave
   replicates 44f0f1daf3850712ff3aaa2b761235fefce7e79e
M: bdccb2d59cd85950cf0e507bb4e29f64f7e23636 10.0.0.82:6379
   slots:[1365-2564],[6827-10922] (5296 slots) master
   1 additional replica(s)
S: a82f0f41c14d6ff43a4fbb4c1a3e34269d1b6eef 10.0.0.86:6379
   slots: (0 slots) slave
   replicates 8679f81ab720ee66925adee602b620abb89f1bc6
M: 8679f81ab720ee66925adee602b620abb89f1bc6 10.0.0.83:6379
   slots:[4020-5460],[12288-16383] (5537 slots) master
   2 additional replica(s)
M: 44f0f1daf3850712ff3aaa2b761235fefce7e79e 10.0.0.87:6379
   slots:[0-1364],[2565-4019],[5461-6826],[10923-12287] (5551 slots) master
   1 additional replica(s)
S: 1df4fd3501abfffa7a81a9bfb1061720089b5414 10.0.0.84:6379
   slots: (0 slots) slave
   replicates 8679f81ab720ee66925adee602b620abb89f1bc6
M: 0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0 10.0.0.81:6379
   slots: (0 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

删除多余的节点:

# 删除master节点
[root@centos8 ~]#redis-cli -a 744123 --cluster del-node 10.0.0.85:6379 0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 0cc72ea4e0278c47a6a42c352de649e9ffd2bfb0 from cluster 10.0.0.85:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
# 节点从集群中删除后会自动停止redis进程


# 删除集群信息文件
[root@centos8 ~]#rm -f /var/lib/redis/nodes-6379.conf


# 删除slave节点
[root@centos8 ~]#redis-cli -a 744123 --cluster del-node 10.0.0.85:6379 1df4fd3501abfffa7a81a9bfb1061720089b5414
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 1df4fd3501abfffa7a81a9bfb1061720089b5414 from cluster 10.0.0.85:6379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.


# 查看集群信息,验证缩容结果
[root@centos8 ~]#redis-cli -a 744123 -h 10.0.0.87 cluster info
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
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:10
cluster_my_epoch:9
cluster_stats_messages_ping_sent:4760
cluster_stats_messages_pong_sent:4700
cluster_stats_messages_meet_sent:6
cluster_stats_messages_update_sent:22
cluster_stats_messages_sent:9488
cluster_stats_messages_ping_received:4699
cluster_stats_messages_pong_received:4764
cluster_stats_messages_meet_received:1
cluster_stats_messages_received:9464

[root@centos8 ~]#redis-cli -a 744123 -h 10.0.0.87 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
44f0f1daf3850712ff3aaa2b761235fefce7e79e 10.0.0.87:6379@16379 myself,master - 0 1609676439000 9 connected 0-1364 2565-4019 5461-6826 10923-12287
bdccb2d59cd85950cf0e507bb4e29f64f7e23636 10.0.0.82:6379@16379 master - 0 1609676441735 8 connected 1365-2564 6827-10922
a4513e13c34b0a9b1ec88b6c9296ca6183249723 10.0.0.88:6379@16379 slave 44f0f1daf3850712ff3aaa2b761235fefce7e79e 0 1609676443744 9 connected
8679f81ab720ee66925adee602b620abb89f1bc6 10.0.0.83:6379@16379 master - 0 1609676442740 10 connected 4020-5460 12288-16383
dccf11f7d55f74a3d5bf774449b2604334d2f0b1 10.0.0.85:6379@16379 slave bdccb2d59cd85950cf0e507bb4e29f64f7e23636 0 1609676442000 8 connected
a82f0f41c14d6ff43a4fbb4c1a3e34269d1b6eef 10.0.0.86:6379@16379 slave 8679f81ab720ee66925adee602b620abb89f1bc6 0 1609676442000 10 connected
上一篇:详解.Redis Cluster 工作原理和集群创建和使用


下一篇:Redis相关知识