1. 程式人生 > 實用技巧 >CSS之定位

CSS之定位

注意:

每個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