【珍藏版】redis集群搭建詳細與卡槽遷移遇到的坑

  • 2020 年 3 月 13 日
  • 筆記

前言

redis5.0.0之前操作 redis集群用的是 ruby寫的腳本 redis-trib.rb,這樣帶來的弊端就是需要安裝 rubygems環境,官方拉取的 docker鏡像里是沒有 redis-trib.rbruby環境的,需要自己安裝,在中國網路環境下還是比較費時複雜的,也不容易成功,而且最後製作的docker鏡像也比較大。再者安裝的 ruby、redis擴展版本不對,也會導致操作集群時,報各種各樣的錯誤。

最重要的硬傷是 redis-trib.rb不能操作帶密碼的 redis集群,雖然 github上有人在 redis-trib.rb腳本基礎上已經加了密碼支援。但對於 move_slots的遷移卡槽相關的操作,我試了還是沒加。可以查看以下的提交記錄:

Redis-Cluster: Add support to auth in redis-trib.rb #4288

redis5.0.0之後的 redis-cli可以直接操作 redis集群,可以支援帶密碼操作,可以說帶來了很大的方便。但是在 redis4.0.7版本之前遷移卡槽本來就是不支援密碼 auth參數的,以下命令中的 [AUTH password]redis4.0.7版本才支援。

MIGRATE host port key|"" destination-db timeout [COPY] [REPLACE] [AUTH password] [KEYS key [key ...]]

具體可以查看官方文檔:MIGRATE說明文檔

Options COPY — Do not remove the key from the local instance. REPLACE — Replace existing key on the remote instance. KEYS — If the key argument is an empty string, the command will instead migrate all the keys that follow the KEYS option (see the above section for more info). AUTH — Authenticate with the given password to the remote instance. COPY and REPLACE are available only in 3.0 and above. KEYS is available starting with Redis 3.0.6. AUTH is available starting with Redis 4.0.7.

我們最近容器化時,用到的版本為 redis3.2.6,在 redis集群有數據(有hash數據時必現),且帶有密碼時,從 3主3從擴規模到 5主5從時,用 5.0.7的redis-cli操作 redis:3.2.6集群,執行遷移卡槽命令時,報 ERR syntax error錯誤。

試著用 github上加密碼的 redis-trib.rb操作 3.2.6集群,進行卡槽遷移時報錯 [ERR]CallingMIGRATE:ERRTargetinstance repliedwitherror:NOAUTHAuthenticationrequired

這個是因為 github上的 redis-trib.rb沒在 move_slots操作時,加密碼參數。於是乎,我試著自己在遷移卡槽的地方傳入密碼參數,像下面這樣:

source.r.client.call(["migrate",target.info[:host],target.info[:port],"",0,@timeout,:auth,target.info[:password],:keys,*keys])

加了之後執行卡槽遷移時,也報 ERR syntax error錯誤。從這可以看出, redis3.2.6集群確實在 migrate時不支援密碼參數。那麼下面在 redis4.0.7集群上進行驗證,是否可以正常遷移卡槽。

創建三主三從集群

創建configmaps

將以下內容保存到 redis-test.conf文件:

#設置為守護進程daemonize no#Redis運行的進程pid文件pidfile redis-6397.pid#Redis服務埠號port 6379#Redis服務綁定ip#bind 192.168.100.144bind {PODIP}#最大記憶體maxmemory 2g#開啟集群模式cluster-enabled yes   #節點配置文件cluster-config-file nodes-6397.conf#集群節點超時時間(單位:毫秒)cluster-node-timeout 15000#集群是否需要所有的slot都分配給在線節點,才能正常訪問cluster-require-full-coverage no#工作目錄(aof、rdb、日誌文件)dir /data#tcp-backlogtcp-backlog 511#客戶端閑置多少秒後關閉連接(單位:秒)timeout 300#檢測TCP連接活性的周期(單位:秒)tcp-keepalive 60#redis密碼requirepass test123masterauth test123#日誌級別loglevel notice#日誌記錄目錄logfile "redis-6397.log"#可用的資料庫數databases 16#RDB保存條件save 900 1save 300 10save 60 10000#bgsave執行錯誤,是否停止Redis接受請求stop-writes-on-bgsave-error no#RDB文件是否壓縮rdbcompression yes#RDB文件是否使用校驗和rdbchecksum yes#RDB文件名dbfilename dump-6397.rdb#當從節點與主節點連接中斷時,如果此參數值設置為「yes」,從節點可以繼續處理客戶端的#請求。否則除info和slaveof命令之外,拒絕的所有請求並統一回復"SYNC with master in #progress"slave-serve-stale-data yes#從節點是否開啟只讀模式,集群架構下從節點默認讀寫都不可用,需要調用readyonly命令#開啟只讀模式slave-read-only yes#是否開啟無盤複製repl-diskless-sync no#開啟無盤複製後,需要延遲多少秒後進行創建RDB操作,一般用於同時加入多個從節點時,#保證多個從節點可共享RDBrepl-diskless-sync-delay 5#是否開啟主從複製socket的NO_DELAY選項:yes:Redis會合併小的TCP包來節省頻寬,但##是這樣增加同步延遲,造成主#從數據不一致;no:主節點會立即發送同步數據,沒有延遲repl-disable-tcp-nodelay no#從節點的優先順序slave-priority 100#是否開啟AOF持久化模式appendonly no#Lua腳本「超時時間」(單位:毫秒)lua-time-limit 5000#慢查詢被記錄的閥值(單位微秒)slowlog-log-slower-than 10000#最多記錄慢查詢的條數slowlog-max-len 1000#Redis服務記憶體延遲監控latency-monitor-threshold 0notify-keyspace-events ""hash-max-ziplist-entries 512hash-max-ziplist-value 64list-max-ziplist-entries 512list-max-ziplist-value 64set-max-intset-entries 512zset-max-ziplist-entries 128zset-max-ziplist-value 64hll-sparse-max-bytes 3000#是否激活重置哈希activerehashing yes#客戶端輸出緩衝區限制client-output-buffer-limit normal 0 0 0client-output-buffer-limit slave 512mb 128mb 60client-output-buffer-limit pubsub 32mb 8mb 60#複製積壓快取區大小repl-backlog-size 256mb#redis server執行後台任務的頻率,默認為10hz 10#最大客戶端連接數maxclients 15000

使用以下命令創建 configmaps

kubectl create cm redis-test-conf --from-file=redis.conf=redis-test.conf

創建redis實例

將以下 yaml內容保存到redis-sts.yaml中:

apiVersion: apps/v1kind: StatefulSetmetadata:  name: redis-test  namespace: defaultspec:  podManagementPolicy: OrderedReady  replicas: 10  revisionHistoryLimit: 10  selector:    matchLabels:      name: redis-test  serviceName: redis-test  template:    metadata:      labels:        name: redis-test    spec:      containers:      - command:        - sh        - -c        - cp /config/redis.conf /data/; sed -i "s?{PODIP}?${PODIP}?g" /data/redis.conf ;redis-server /data/redis.conf        image: redis:4.0.7        env:        - name: PODIP          valueFrom:            fieldRef:              apiVersion: v1              fieldPath: status.podIP        imagePullPolicy: IfNotPresent        name: redis        ports:        - containerPort: 6379          name: redis          protocol: TCP        resources: {}        terminationMessagePath: /dev/termination-log        terminationMessagePolicy: File        volumeMounts:        - mountPath: /config          name: redis-config        - mountPath: /tmp          name: tmp-dir       dnsPolicy: ClusterFirst      restartPolicy: Always      schedulerName: default-scheduler      securityContext: {}      terminationGracePeriodSeconds: 30      volumes:      - configMap:          defaultMode: 420          name: redis-test-conf        name: redis-config      - emptyDir: {}        name: tmp-dir  updateStrategy:    rollingUpdate:      partition: 0    type: RollingUpdate

這裡使用 redis:4.0.7的docker鏡像,創建如下10個pod:

[root@liabio ~]# kubectl apply -f redis-sts.yaml[root@liabio ~]# kubectl get pod  -owideNAME                       READY   STATUS    RESTARTS   AGE     IP                NODE     NOMINATED NODE   READINESS GATESredis-test-0               1/1     Running   0          60s     192.168.155.117   liabio   <none>           <none>redis-test-1               1/1     Running   0          59s     192.168.155.91    liabio   <none>           <none>redis-test-2               1/1     Running   0          58s     192.168.155.112   liabio   <none>           <none>redis-test-3               1/1     Running   0          55s     192.168.155.103   liabio   <none>           <none>redis-test-4               1/1     Running   0          54s     192.168.155.102    liabio   <none>           <none>redis-test-5               1/1     Running   0          52s     192.168.155.78    liabio   <none>           <none>redis-test-6               1/1     Running   0          51s     192.168.155.108   liabio   <none>           <none>redis-test-7               1/1     Running   0          49s     192.168.155.111   liabio   <none>           <none>redis-test-8               1/1     Running   0          47s     192.168.155.114   liabio   <none>           <none>redis-test-9               1/1     Running   0          45s     192.168.155.73    liabio   <none>           <none>

使用redis-cli 5.0.7操作集群

使用 redis-cli5.0.7版本操作集群:

[root@liabio ~]# redis-cli -vredis-cli 5.0.7

創建3master集群

先選3個master出來,創建集群:

[root@liabio ~]# echo yes | redis-cli --cluster create  192.168.155.117:6379 192.168.155.91:6379 192.168.155.112:6379 -a test123Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.>>> Performing hash slots allocation on 3 nodes...Master[0] -> Slots 0 - 5460Master[1] -> Slots 5461 - 10922Master[2] -> Slots 10923 - 16383M: f070ddf68e39896af42dc08251199cda7c8b9b92 192.168.155.117:6379   slots:[0-5460] (5461 slots) masterM: c4ab027656d55b800cc54063493bca198a5e1385 192.168.155.91:6379   slots:[5461-10922] (5462 slots) masterM: 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650 192.168.155.112:6379   slots:[10923-16383] (5461 slots) masterCan I set the above configuration? (type 'yes' to accept): >>> Nodes configuration updated>>> Assign a different config epoch to each node>>> Sending CLUSTER MEET messages to join the clusterWaiting for the cluster to join.>>> Performing Cluster Check (using node 192.168.155.117:6379)M: f070ddf68e39896af42dc08251199cda7c8b9b92 192.168.155.117:6379   slots:[0-5460] (5461 slots) masterM: c4ab027656d55b800cc54063493bca198a5e1385 192.168.155.91:6379   slots:[5461-10922] (5462 slots) masterM: 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650 192.168.155.112:6379   slots:[10923-16383] (5461 slots) master[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.

添加slave1

[root@liabio ~]# redis-cli --cluster add-node 192.168.155.103:6379 192.168.155.117:6379 --cluster-slave --cluster-master-id f070ddf68e39896af42dc08251199cda7c8b9b92 -a test123Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.>>> Adding node 192.168.155.103:6379 to cluster 192.168.155.117:6379>>> Performing Cluster Check (using node 192.168.155.117:6379)M: f070ddf68e39896af42dc08251199cda7c8b9b92 192.168.155.117:6379   slots:[0-5460] (5461 slots) masterM: c4ab027656d55b800cc54063493bca198a5e1385 192.168.155.91:6379   slots:[5461-10922] (5462 slots) masterM: 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650 192.168.155.112:6379   slots:[10923-16383] (5461 slots) master[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.155.103:6379 to make it join the cluster.Waiting for the cluster to join  >>> Configure node as replica of 192.168.155.117:6379.[OK] New node added correctly.

添加slave2

[root@liabio ~]# redis-cli --cluster add-node 192.168.155.102:6379 192.168.155.117:6379 --cluster-slave --cluster-master-id c4ab027656d55b800cc54063493bca198a5e1385 -a test123Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.>>> Adding node 192.168.155.102:6379 to cluster 192.168.155.117:6379>>> Performing Cluster Check (using node 192.168.155.117:6379)M: f070ddf68e39896af42dc08251199cda7c8b9b92 192.168.155.117:6379   slots:[0-5460] (5461 slots) master   1 additional replica(s)S: 1121a45980f1d5964f0010b331aeb514ff9dedb8 192.168.155.103:6379   slots: (0 slots) slave   replicates f070ddf68e39896af42dc08251199cda7c8b9b92M: c4ab027656d55b800cc54063493bca198a5e1385 192.168.155.91:6379   slots:[5461-10922] (5462 slots) masterM: 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650 192.168.155.112:6379   slots:[10923-16383] (5461 slots) master[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.155.102:6379 to make it join the cluster.Waiting for the cluster to join  >>> Configure node as replica of 192.168.155.91:6379.[OK] New node added correctly.

添加slave3

[root@liabio ~]# redis-cli --cluster add-node 192.168.155.78:6379 192.168.155.117:6379 --cluster-slave --cluster-master-id 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650 -a test123Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.>>> Adding node 192.168.155.78:6379 to cluster 192.168.155.117:6379>>> Performing Cluster Check (using node 192.168.155.117:6379)M: f070ddf68e39896af42dc08251199cda7c8b9b92 192.168.155.117:6379   slots:[0-5460] (5461 slots) master   1 additional replica(s)S: 1121a45980f1d5964f0010b331aeb514ff9dedb8 192.168.155.103:6379   slots: (0 slots) slave   replicates f070ddf68e39896af42dc08251199cda7c8b9b92M: 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650 192.168.155.112:6379   slots:[10923-16383] (5461 slots) masterS: 941108cab0121fc840f909da080d92770dbbdee1 192.168.155.102:6379   slots: (0 slots) slave   replicates c4ab027656d55b800cc54063493bca198a5e1385M: c4ab027656d55b800cc54063493bca198a5e1385 192.168.155.91: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.155.78:6379 to make it join the cluster.Waiting for the cluster to join  >>> Configure node as replica of 192.168.155.112:6379.[OK] New node added correctly.[root@liabio ~]# 

給集群里新增各種數據類型

分別給集群里設置hash類型的hash1;list類型的list1;有序集合(sorted set)類型的在zset1;string類型的a;set類型的set1

[root@liabio ~]# redis-cli -c -h 192.168.155.112 -p 6379 -a test123Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.155.112:6379> 192.168.155.112:6379> HMSET hash1 name1 "redis sasa"-> Redirected to slot [4414] located at 192.168.155.117:6379OK192.168.155.117:6379> LPUSH list1 aa-> Redirected to slot [7141] located at 192.168.155.91:6379(integer) 1192.168.155.91:6379> zadd zset1 1 aa-> Redirected to slot [4341] located at 192.168.155.117:6379(integer) 1192.168.155.117:6379> set a b-> Redirected to slot [15495] located at 192.168.155.112:6379OK192.168.155.112:6379> SADD set1 redis-> Redirected to slot [3037] located at 192.168.155.117:6379(integer) 1192.168.155.117:6379> [root@liabio ~]# [root@liabio ~]# redis-cli -a test123 --cluster  check 192.168.155.117:6379Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.155.117:6379 (f070ddf6...) -> 3 keys | 5461 slots | 1 slaves.192.168.155.112:6379 (8d1e7150...) -> 1 keys | 5461 slots | 1 slaves.192.168.155.91:6379 (c4ab0276...) -> 1 keys | 5462 slots | 1 slaves.[OK] 5 keys in 3 masters.0.00 keys per slot on average.>>> Performing Cluster Check (using node 192.168.155.117:6379)M: f070ddf68e39896af42dc08251199cda7c8b9b92 192.168.155.117:6379   slots:[0-5460] (5461 slots) master   1 additional replica(s)S: 1121a45980f1d5964f0010b331aeb514ff9dedb8 192.168.155.103:6379   slots: (0 slots) slave   replicates f070ddf68e39896af42dc08251199cda7c8b9b92M: 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650 192.168.155.112:6379   slots:[10923-16383] (5461 slots) master   1 additional replica(s)S: 0a6188b6bf590c928a2f7b1b0de9aeb206977d72 192.168.155.78:6379   slots: (0 slots) slave   replicates 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650S: 941108cab0121fc840f909da080d92770dbbdee1 192.168.155.102:6379   slots: (0 slots) slave   replicates c4ab027656d55b800cc54063493bca198a5e1385M: c4ab027656d55b800cc54063493bca198a5e1385 192.168.155.91: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.

擴容為五主五從集群

添加新的master1

[root@liabio ~]# redis-cli --cluster add-node 192.168.155.108:6379 192.168.155.117:6379 -a test123Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.>>> Adding node 192.168.155.108:6379 to cluster 192.168.155.117:6379>>> Performing Cluster Check (using node 192.168.155.117:6379)M: f070ddf68e39896af42dc08251199cda7c8b9b92 192.168.155.117:6379   slots:[0-5460] (5461 slots) master   1 additional replica(s)S: 1121a45980f1d5964f0010b331aeb514ff9dedb8 192.168.155.103:6379   slots: (0 slots) slave   replicates f070ddf68e39896af42dc08251199cda7c8b9b92M: 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650 192.168.155.112:6379   slots:[10923-16383] (5461 slots) master   1 additional replica(s)S: 0a6188b6bf590c928a2f7b1b0de9aeb206977d72 192.168.155.78:6379   slots: (0 slots) slave   replicates 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650S: 941108cab0121fc840f909da080d92770dbbdee1 192.168.155.102:6379   slots: (0 slots) slave   replicates c4ab027656d55b800cc54063493bca198a5e1385M: c4ab027656d55b800cc54063493bca198a5e1385 192.168.155.91: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.155.108:6379 to make it join the cluster.[OK] New node added correctly.

添加新的master2

[root@liabio ~]# redis-cli --cluster add-node 192.168.155.111:6379 192.168.155.117:6379 -a test123Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.>>> Adding node 192.168.155.111:6379 to cluster 192.168.155.117:6379>>> Performing Cluster Check (using node 192.168.155.117:6379)M: f070ddf68e39896af42dc08251199cda7c8b9b92 192.168.155.117:6379   slots:[0-5460] (5461 slots) master   1 additional replica(s)S: 1121a45980f1d5964f0010b331aeb514ff9dedb8 192.168.155.103:6379   slots: (0 slots) slave   replicates f070ddf68e39896af42dc08251199cda7c8b9b92M: 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650 192.168.155.112:6379   slots:[10923-16383] (5461 slots) master   1 additional replica(s)S: 0a6188b6bf590c928a2f7b1b0de9aeb206977d72 192.168.155.78:6379   slots: (0 slots) slave   replicates 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650S: 941108cab0121fc840f909da080d92770dbbdee1 192.168.155.102:6379   slots: (0 slots) slave   replicates c4ab027656d55b800cc54063493bca198a5e1385M: 78ad24899c7a7b1c0cad99e2c1afab82e8c98ec2 192.168.155.108:6379   slots: (0 slots) masterM: c4ab027656d55b800cc54063493bca198a5e1385 192.168.155.91: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.155.111:6379 to make it join the cluster.[OK] New node added correctly.[root@liabio ~]# redis-cli -a test123 --cluster  check 192.168.155.117:6379Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.192.168.155.117:6379 (f070ddf6...) -> 3 keys | 5461 slots | 1 slaves.192.168.155.111:6379 (1b3b59ff...) -> 0 keys | 0 slots | 0 slaves.192.168.155.112:6379 (8d1e7150...) -> 1 keys | 5461 slots | 1 slaves.192.168.155.108:6379 (78ad2489...) -> 0 keys | 0 slots | 0 slaves.192.168.155.91:6379 (c4ab0276...) -> 1 keys | 5462 slots | 1 slaves.[OK] 5 keys in 5 masters.0.00 keys per slot on average.>>> Performing Cluster Check (using node 192.168.155.117:6379)M: f070ddf68e39896af42dc08251199cda7c8b9b92 192.168.155.117:6379   slots:[0-5460] (5461 slots) master   1 additional replica(s)M: 1b3b59ff2b7ecf0ae67568104501450b94aa9ac0 192.168.155.111:6379   slots: (0 slots) masterS: 1121a45980f1d5964f0010b331aeb514ff9dedb8 192.168.155.103:6379   slots: (0 slots) slave   replicates f070ddf68e39896af42dc08251199cda7c8b9b92M: 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650 192.168.155.112:6379   slots:[10923-16383] (5461 slots) master   1 additional replica(s)S: 0a6188b6bf590c928a2f7b1b0de9aeb206977d72 192.168.155.78:6379   slots: (0 slots) slave   replicates 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650S: 941108cab0121fc840f909da080d92770dbbdee1 192.168.155.102:6379   slots: (0 slots) slave   replicates c4ab027656d55b800cc54063493bca198a5e1385M: 78ad24899c7a7b1c0cad99e2c1afab82e8c98ec2 192.168.155.108:6379   slots: (0 slots) masterM: c4ab027656d55b800cc54063493bca198a5e1385 192.168.155.91: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.

添加新的slave1

[root@liabio ~]# redis-cli --cluster add-node 192.168.155.114:6379 192.168.155.117:6379 --cluster-slave --cluster-master-id 78ad24899c7a7b1c0cad99e2c1afab82e8c98ec2 -a test123Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.>>> Adding node 192.168.155.114:6379 to cluster 192.168.155.117:6379>>> Performing Cluster Check (using node 192.168.155.117:6379)M: f070ddf68e39896af42dc08251199cda7c8b9b92 192.168.155.117:6379   slots:[0-5460] (5461 slots) master   1 additional replica(s)M: 1b3b59ff2b7ecf0ae67568104501450b94aa9ac0 192.168.155.111:6379   slots: (0 slots) masterS: 1121a45980f1d5964f0010b331aeb514ff9dedb8 192.168.155.103:6379   slots: (0 slots) slave   replicates f070ddf68e39896af42dc08251199cda7c8b9b92M: 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650 192.168.155.112:6379   slots:[10923-16383] (5461 slots) master   1 additional replica(s)S: 0a6188b6bf590c928a2f7b1b0de9aeb206977d72 192.168.155.78:6379   slots: (0 slots) slave   replicates 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650S: 941108cab0121fc840f909da080d92770dbbdee1 192.168.155.102:6379   slots: (0 slots) slave   replicates c4ab027656d55b800cc54063493bca198a5e1385M: 78ad24899c7a7b1c0cad99e2c1afab82e8c98ec2 192.168.155.108:6379   slots: (0 slots) masterM: c4ab027656d55b800cc54063493bca198a5e1385 192.168.155.91: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.155.114:6379 to make it join the cluster.Waiting for the cluster to join  >>> Configure node as replica of 192.168.155.108:6379.[OK] New node added correctly.

添加新的slave2

[root@liabio ~]# redis-cli --cluster add-node 192.168.155.73:6379 192.168.155.117:6379 --cluster-slave --cluster-master-id 1b3b59ff2b7ecf0ae67568104501450b94aa9ac0 -a test123Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.>>> Adding node 192.168.155.73:6379 to cluster 192.168.155.117:6379>>> Performing Cluster Check (using node 192.168.155.117:6379)M: f070ddf68e39896af42dc08251199cda7c8b9b92 192.168.155.117:6379   slots:[0-5460] (5461 slots) master   1 additional replica(s)S: 941108cab0121fc840f909da080d92770dbbdee1 192.168.155.102:6379   slots: (0 slots) slave   replicates c4ab027656d55b800cc54063493bca198a5e1385M: c4ab027656d55b800cc54063493bca198a5e1385 192.168.155.91:6379   slots:[5461-10922] (5462 slots) master   1 additional replica(s)S: 1121a45980f1d5964f0010b331aeb514ff9dedb8 192.168.155.103:6379   slots: (0 slots) slave   replicates f070ddf68e39896af42dc08251199cda7c8b9b92M: 1b3b59ff2b7ecf0ae67568104501450b94aa9ac0 192.168.155.111:6379   slots: (0 slots) masterM: 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650 192.168.155.112:6379   slots:[10923-16383] (5461 slots) master   1 additional replica(s)S: 0a6188b6bf590c928a2f7b1b0de9aeb206977d72 192.168.155.78:6379   slots: (0 slots) slave   replicates 8d1e71502b9f1c29b3170eb9bf1391ae5aa50650S: 9dc374d48b730432f726147828cde001ea5a7dc4 192.168.155.114:6379   slots: (0 slots) slave   replicates 78ad24899c7a7b1c0cad99e2c1afab82e8c98ec2M: 78ad24899c7a7b1c0cad99e2c1afab82e8c98ec2 192.168.155.108:6379   slots: (0 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.155.73:6379 to make it join the cluster.Waiting for the cluster to join  >>> Configure node as replica of 192.168.155.111:6379.[OK] New node added correctly.

遷移卡槽

進行使用rebalance子命令進行卡槽分配:

[root@liabio ~]# redis-cli --cluster rebalance --cluster-use-empty-masters --cluster-pipeline 100 192.168.155.117:6379 -a test123Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.>>> Performing Cluster Check (using node 192.168.155.117:6379)[OK] All nodes agree about slots configuration.>>> Check for open slots...>>> Check slots coverage...[OK] All 16384 slots covered.>>> Rebalancing across 5 nodes. Total weight = 5.00Moving 2186 slots from 192.168.155.91:6379 to 192.168.155.111:6379####################################################################Moving 1092 slots from 192.168.155.112:6379 to 192.168.155.111:6379###################################################################Moving 1093 slots from 192.168.155.112:6379 to 192.168.155.108:6379#################################################################Moving 2185 slots from 192.168.155.117:6379 to 192.168.155.108:6379################################################################[root@liabio ~]# 

結語

從以上可以看出, redis-cli5.0.7版本的客戶端,可以操作 帶密碼4.0.7版本的redis集群,卡槽遷移不會報以下語法錯誤 ERR syntax error

– END –