創建 redis cluster集群的環境准備
系統 | 內存 | redis版本 | 主機名 | 主機IP |
CentOS8 | 2G | 5.0.3 | Master1 | 192.168.18.80 |
CentOS8 | 2G | 5.0.3 | Master2 | 192.168.18.81 |
CentOS8 | 2G | 5.0.3 | Master3 | 192.168.18.82 |
CentOS8 | 2G | 5.0.3 | Slave1 | 192.168.18.83 |
CentOS8 | 2G | 5.0.3 | Slave2 | 192.168.18.84 |
CentOS8 | 2G | 5.0.3 | Slave3 | 192.168.18.85 |
所有主機安裝redis
[root@Master1 ~]#dnf -y install redis
[root@Master2 ~]#dnf -y install redis
[root@Master3 ~]#dnf -y install redis
[root@Slave1 ~]#dnf -y install redis
[root@Slave2 ~]#dnf -y install redis
[root@Slave3 ~]#dnf -y install redis
所有主機修改redis配置文件
[root@Master1 ~]# vim /etc/redis.conf
bind 0.0.0.0
daemonize yes #以守護進程方式運行
masterauth 123456 #slave連接master密碼,master可省略
requirepass 123456 #設置本機redis連接密碼
cluster-enabled yes #取消此行注釋,開啟redis cluster集群功能
cluster-config-file nodes-6379.conf #取消此行注釋,此為集群狀態文件,記錄主從關系及slot范圍信息,由redis cluster 集群自動創建和維護
cluster-require-full-coverage no #集群請求槽位全部覆蓋,默認值為yes,設置為no則槽位不全也能對外提供服務
#啟動redis並設置開機自啟
[root@Master1 ~]#systemctl enable --now redis
創建redis集群
# redis-cli --cluster-replicas 1 表示每個master對應一個slave節點
[root@Master1 ~]# redis-cli -a 123456 --cluster create 192.168.18.80:6379 192.168.18.81:6379 192.168.18.82:6379 192.168.18.83:6379 192.168.18.84:6379 192.168.18.85: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 192.168.18.83:6379 to 192.168.18.80:6379
Adding replica 192.168.18.84:6379 to 192.168.18.81:6379
Adding replica 192.168.18.85:6379 to 192.168.18.82:6379
M: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379
slots:[0-5460] (5461 slots) master
M: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379
slots:[5461-10922] (5462 slots) master
M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379
slots:[10923-16383] (5461 slots) master
S: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379
replicates 07f6fea79e94447ae2116a212c575e87f726c0b2
S: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379
replicates f361eb219afe76e1d316ea75628d3f8f16e2b8cc
S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379
replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1
Can I set the above configuration? (type 'yes' to accept): yes #輸入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.18.80:6379)
M: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379
slots: (0 slots) slave
replicates 07f6fea79e94447ae2116a212c575e87f726c0b2
S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379
slots: (0 slots) slave
replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1
S: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379
slots: (0 slots) slave
replicates f361eb219afe76e1d316ea75628d3f8f16e2b8cc
M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379
slots:[10923-16383] (5461 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.
驗證集群狀態
[root@Master1 ~]# redis-cli -a 123456 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:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:453
cluster_stats_messages_pong_sent:454
cluster_stats_messages_sent:907
cluster_stats_messages_ping_received:449
cluster_stats_messages_pong_received:453
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:907
查看集群node對應關系
[root@Master1 ~]# redis-cli -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379@16379 master - 0 1628090052349 2 connected 5461-10922
a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379@16379 slave 07f6fea79e94447ae2116a212c575e87f726c0b2 0 1628090053355 4 connected
38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379@16379 slave 9ff40f467859b3daf016b9270c9f657eef11d3e1 0 1628090053000 6 connected
767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379@16379 slave f361eb219afe76e1d316ea75628d3f8f16e2b8cc 0 1628090054363 5 connected
9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379@16379 master - 0 1628090053000 3 connected 10923-16383
07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379@16379 myself,master - 0 1628090051000 1 connected 0-5460
Python腳本實現Redis Cluster 集群批量寫入數據
[root@Master1 ~]# which pip3
/usr/bin/pip3
#安裝redis集群模塊
[root@Master1 ~]# pip3 install redis-py-cluster
#編寫python腳本
[root@Master1 ~]# cat redis_cluster_test.py
#!/usr/bin/env python3
from rediscluster import RedisCluster
startup_nodes = [
{"host":"192.168.18.80", "port":6379},
{"host":"192.168.18.81", "port":6379},
{"host":"192.168.18.82", "port":6379},
{"host":"192.168.18.83", "port":6379},
{"host":"192.168.18.84", "port":6379},
{"host":"192.168.18.85", "port":6379}
]
redis_conn= RedisCluster(startup_nodes=startup_nodes,password='123456',
decode_responses=True)
for i in range(0, 10000):
redis_conn.set('key'+str(i),'value'+str(i))
print('key'+str(i)+':',redis_conn.get('key'+str(i)))
#執行python腳本
[root@Master1 ~]# python3 redis_cluster_test.py
#驗證數據寫入結果
[root@Master1 ~]# redis-cli -a 123456 -h 192.168.18.80
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.18.80:6379> dbsize
(integer) 3331
192.168.18.80:6379>
[root@Master1 ~]# redis-cli -a 123456 -h 192.168.18.81
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.18.81:6379> dbsize
(integer) 3340
[root@Master1 ~]# redis-cli -a 123456 -h 192.168.18.82
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.18.82:6379> dbsize
(integer) 3329
Redis集群故障轉移測試
[root@Master2 ~]# redis-cli -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379@16379 slave 9ff40f467859b3daf016b9270c9f657eef11d3e1 0 1628261594357 6 connected
9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379@16379 master - 0 1628261596372 3 connected 10923-16383
767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379@16379 slave f361eb219afe76e1d316ea75628d3f8f16e2b8cc 0 1628261595365 5 connected
a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379@16379 master - 0 1628261594000 7 connected 0-5460
07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379@16379 slave a2488deac6e75c4dee7e7546e55d81394a92de34 0 1628261592342 7 connected
f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379@16379 myself,master - 0 1628261593000 2 connected 5461-10922
#手動停止Master2的redis來模擬故障
[root@Master2 ~]# redis-cli -a 123456 shutdown
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
[root@Master2 ~]# ss -ntlp |grep redis
[root@Master2 ~]# redis-cli -a 123456 --cluster info 192.168.18.80:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 192.168.18.81:6379: Connection refused
192.168.18.83:6379 (a2488dea...) -> 3331 keys | 5461 slots | 1 slaves.
192.168.18.84:6379 (767f9c45...) -> 3340 keys | 5462 slots | 0 slaves. #192.168.18.84:6379 已成為新的master
192.168.18.82:6379 (9ff40f46...) -> 3329 keys | 5461 slots | 1 slaves.
[OK] 10000 keys in 3 masters.
0.61 keys per slot on average.
#檢查集群
[root@Master2 ~]# redis-cli -a 123456 --cluster check 192.168.18.80:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
Could not connect to Redis at 192.168.18.81:6379: Connection refused
192.168.18.83:6379 (a2488dea...) -> 3331 keys | 5461 slots | 1 slaves.
192.168.18.84:6379 (767f9c45...) -> 3340 keys | 5462 slots | 0 slaves.
192.168.18.82:6379 (9ff40f46...) -> 3329 keys | 5461 slots | 1 slaves.
[OK] 10000 keys in 3 masters.
0.61 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.18.80:6379)
S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379
slots: (0 slots) slave
replicates a2488deac6e75c4dee7e7546e55d81394a92de34
M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379
slots: (0 slots) slave
replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1
M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379
slots:[5461-10922] (5462 slots) master
M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379
slots:[10923-16383] (5461 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.
#啟用Master2節點的redis
[root@Master2 ~]# systemctl start redis
[root@Master2 ~]# ss -ntlp |grep redis
LISTEN 0 128 0.0.0.0:6379 0.0.0.0:* users:(("redis-server",pid=7301,fd=6))
LISTEN 0 128 0.0.0.0:16379 0.0.0.0:* users:(("redis-server",pid=7301,fd=8))
[root@Master2 ~]# redis-cli -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379@16379 master - 0 1628263118397 8 connected 5461-10922
f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379@16379 myself,slave 767f9c453c7e5ba27d51a65487428f65ae33d49c 0 1628263115000 2 connected #master2已自動成為slave節點
07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379@16379 slave a2488deac6e75c4dee7e7546e55d81394a92de34 0 1628263117389 7 connected
38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379@16379 slave 9ff40f467859b3daf016b9270c9f657eef11d3e1 0 1628263116000 6 connected
9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379@16379 master - 0 1628263117000 3 connected 10923-16383
a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379@16379 master - 0 1628263117000 7 connected 0-5460
實現redis集群動態擴容
隨着公司業務量並發的增長,往往需要對redis集群實現動態擴容。
由於本人物理機內存限制,這里將使用多實例的方式來實現redis cluster的動態擴容。
注意: 生產環境一般建議master節點為奇數個,比如:3,5,7,以防止腦裂現象
#創建redis多實例
[root@Slave3 ~]# mkdir -p /data/redis/638{0,1}
[root@Slave3 ~]# cp /etc/redis.conf /data/redis/6380/
[root@Slave3 ~]# cp /etc/redis.conf /data/redis/6381/
#分別修改各實例配置文件
[root@Slave3 ~]# sed -i -e 's#^port 6379#port 6380#' -e 's#^pidfile.*#pidfile /data/redis/6380/redis_6380.pid#' -e 's#^dir.*#dir /data/redis/6380#'
-e 's#^cluster-config-file.*#cluster-config-file nodes-6380.conf#' -e 's#^logfile.*#logfile /data/redis/6380/redis-6380.log#' -e 's#^dbfilename.*#dbfilename dump-6380.rdb#' /data/redis/6380/redis.conf
[root@Slave3 ~]# sed -i -e 's#^port 6379#port 6381#' -e 's#^pidfile.*#pidfile /data/redis/6381/redis_6381.pid#' -e 's#^dir.*#dir /data/redis/6381#'
-e 's#^cluster-config-file.*#cluster-config-file nodes-6381.conf#' -e 's#^logfile.*#logfile /data/redis/6381/redis-6381.log#' -e 's#^dbfilename.*#dbfilename dump-6381.rdb#' /data/redis/6381/redis.conf
#啟動redis多實例
root@Slave3 ~]# redis-server /data/redis/6380/redis.conf
[root@Slave3 ~]# redis-server /data/redis/6381/redis.conf
[root@Slave3 ~]# ss -ntlp |grep redis
LISTEN 0 128 0.0.0.0:16379 0.0.0.0:* users:(("redis-server",pid=4682,fd=8))
LISTEN 0 128 0.0.0.0:16380 0.0.0.0:* users:(("redis-server",pid=5493,fd=8))
LISTEN 0 128 0.0.0.0:16381 0.0.0.0:* users:(("redis-server",pid=5498,fd=8))
LISTEN 0 128 0.0.0.0:6379 0.0.0.0:* users:(("redis-server",pid=4682,fd=6))
LISTEN 0 128 0.0.0.0:6380 0.0.0.0:* users:(("redis-server",pid=5493,fd=6))
LISTEN 0 128 0.0.0.0:6381 0.0.0.0:* users:(("redis-server",pid=5498,fd=6))
#把redis:6380實例添加到集群中去
[root@Slave3 ~]# redis-cli -a 123456 --cluster add-node 192.168.18.85:6380 192.168.18.80:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.18.85:6380 to cluster 192.168.18.80:6379
>>> Performing Cluster Check (using node 192.168.18.80:6379)
S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379
slots: (0 slots) slave
replicates a2488deac6e75c4dee7e7546e55d81394a92de34
S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379
slots: (0 slots) slave
replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c
M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379
slots: (0 slots) slave
replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1
M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379
slots:[10923-16383] (5461 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.18.85:6380 to make it join the cluster.
[OK] New node added correctly.
#查看集群信息
[root@Slave3 ~]# redis-cli -a 123456 --cluster info 192.168.18.85:6380
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.18.85:6380 (fe8397f0...) -> 0 keys | 0 slots | 0 slaves. #可以看到新實例為master節點,但是沒有分配slot槽位,所以現在是無法寫入數據的
192.168.18.84:6379 (767f9c45...) -> 3340 keys | 5462 slots | 1 slaves.
192.168.18.82:6379 (9ff40f46...) -> 3329 keys | 5461 slots | 1 slaves.
192.168.18.83:6379 (a2488dea...) -> 3331 keys | 5461 slots | 1 slaves.
[OK] 10000 keys in 4 masters.
0.61 keys per slot on average.
#在新的master上重新分配槽位
新的node節點加到集群之后,默認是master節點,但是沒有slots,需要重新分配
添加主機之后需要對添加至集群種的新主機重新分片,否則其沒有分片也就無法寫入數據。
[root@Slave3 ~]# redis-cli -a 123456 --cluster reshard 192.168.18.85:6380
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 192.168.18.85:6380)
M: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 192.168.18.85:6380
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379
slots: (0 slots) slave
replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1
S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379
slots: (0 slots) slave
replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c
M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379
slots: (0 slots) slave
replicates a2488deac6e75c4dee7e7546e55d81394a92de34
M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83: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)? 4096 #輸入需要新分配多少個槽位 16384/4 =4096
What is the receiving node ID? fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a #輸入新的master的ID
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots. #從所有節點中划分出4096個哈希槽分配給新節點
Type 'done' once you entered all the source nodes IDs. #從redis cluster刪除某個主機可以使用此方式將指定主機上的槽位全部移動到其他節點
Source node #1: all #輸入all即可
Do you want to proceed with the proposed reshard plan (yes/no)? yes #確認分配
...
Moving slot 1359 from 192.168.18.83:6379 to 192.168.18.85:6380: .
Moving slot 1360 from 192.168.18.83:6379 to 192.168.18.85:6380: .
Moving slot 1361 from 192.168.18.83:6379 to 192.168.18.85:6380:
Moving slot 1362 from 192.168.18.83:6379 to 192.168.18.85:6380:
Moving slot 1363 from 192.168.18.83:6379 to 192.168.18.85:6380:
Moving slot 1364 from 192.168.18.83:6379 to 192.168.18.85:6380:
[root@Slave3 ~]#
#確定slot槽位分配成功
[root@Slave3 ~]# redis-cli -a 123456 --cluster check 192.168.18.85:6380
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.18.85:6380 (fe8397f0...) -> 2474 keys | 4096 slots | 0 slaves.
192.168.18.84:6379 (767f9c45...) -> 2515 keys | 4096 slots | 1 slaves.
192.168.18.82:6379 (9ff40f46...) -> 2500 keys | 4096 slots | 1 slaves.
192.168.18.83:6379 (a2488dea...) -> 2511 keys | 4096 slots | 1 slaves.
[OK] 10000 keys in 4 masters.
0.61 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.18.85:6380)
M: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 192.168.18.85:6380
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379
slots: (0 slots) slave
replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1
S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379
slots: (0 slots) slave
replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c
M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379
slots: (0 slots) slave
replicates a2488deac6e75c4dee7e7546e55d81394a92de34
M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83: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.
#確認新master是否有數據
[root@Slave3 ~]# redis-cli -a 123456 -p 6380 dbsize
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
(integer) 2474
#為新的master添加新的slave節點
[root@Slave3 ~]# redis-cli -a 123456 --cluster add-node 192.168.18.85:6381 192.168.18.85:6380 --cluster-slave --cluster-master-id fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.18.85:6381 to cluster 192.168.18.85:6380
>>> Performing Cluster Check (using node 192.168.18.85:6380)
M: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 192.168.18.85:6380
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379
slots: (0 slots) slave
replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1
S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379
slots: (0 slots) slave
replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c
M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379
slots: (0 slots) slave
replicates a2488deac6e75c4dee7e7546e55d81394a92de34
M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83: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.
>>> Send CLUSTER MEET to node 192.168.18.85:6381 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.18.85:6380.
[OK] New node added correctly.
#查看集群狀態
[root@Slave3 ~]# redis-cli -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
f3cf6c95950dbec83ab3a59bf7fd74739c89b774 192.168.18.85:6381@16381 slave fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 0 1628265662757 9 connected
9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379@16379 master - 0 1628265663762 3 connected 12288-16383
f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379@16379 slave 767f9c453c7e5ba27d51a65487428f65ae33d49c 0 1628265664770 8 connected
fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 192.168.18.85:6380@16380 master - 0 1628265662000 9 connected 0-1364 5461-6826 10923-12287
a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379@16379 master - 0 1628265661000 7 connected 1365-5460
38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379@16379 myself,slave 9ff40f467859b3daf016b9270c9f657eef11d3e1 0 1628265660000 6 connected
767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379@16379 master - 0 1628265662000 8 connected 6827-10922
07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379@16379 slave a2488deac6e75c4dee7e7546e55d81394a92de34 0 1628265661750 7 connected
實現redis集群動態縮容
刪除節點過程:
添加節點的時候是先添加node節點到集群,然后分配槽位,刪除節點的操作與添加節點的操作正好相
反,是先將被刪除的Redis node上的槽位遷移到集群中的其他Redis node節點上,然后再將其刪除,
如果一個Redis node節點上的槽位沒有被完全遷移,刪除該node的時候會提示有數據且無法刪除。
#將192.168.18.85:6380節點的4096個槽位分別挪到其他三個主節點上
[root@Slave3 ~]# redis-cli -a 123456 --cluster reshard 192.168.18.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 192.168.18.81:6379)
S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379
slots: (0 slots) slave
replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c
M: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 192.168.18.85:6380
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
1 additional replica(s)
S: f3cf6c95950dbec83ab3a59bf7fd74739c89b774 192.168.18.85:6381
slots: (0 slots) slave
replicates fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379
slots: (0 slots) slave
replicates a2488deac6e75c4dee7e7546e55d81394a92de34
S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379
slots: (0 slots) slave
replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1
M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83: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)? 1356 #將4096個槽位分配給其它三個master節點
What is the receiving node ID? 767f9c453c7e5ba27d51a65487428f65ae33d49c #分配給第一個主節點 192.168.18.84:6379
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: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a #輸入要刪除192.168.18.85:6380節點ID
Source node #2: done #輸入源節點后鍵入done
...
Moving slot 1350 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Moving slot 1351 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Moving slot 1352 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Moving slot 1353 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Moving slot 1354 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Moving slot 1355 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Do you want to proceed with the proposed reshard plan (yes/no)? yes #確認分配
...
Moving slot 1350 from 192.168.18.85:6380 to 192.168.18.84:6379: .
Moving slot 1351 from 192.168.18.85:6380 to 192.168.18.84:6379:
Moving slot 1352 from 192.168.18.85:6380 to 192.168.18.84:6379:
Moving slot 1353 from 192.168.18.85:6380 to 192.168.18.84:6379:
Moving slot 1354 from 192.168.18.85:6380 to 192.168.18.84:6379: .
Moving slot 1355 from 192.168.18.85:6380 to 192.168.18.84:6379: .
[root@Slave3 ~]# redis-cli -a 123456 --cluster reshard 192.168.18.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 192.168.18.81:6379)
S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379
slots: (0 slots) slave
replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c
M: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 192.168.18.85:6380
slots:[1356-1364],[5461-6826],[10923-12287] (2740 slots) master
1 additional replica(s)
S: f3cf6c95950dbec83ab3a59bf7fd74739c89b774 192.168.18.85:6381
slots: (0 slots) slave
replicates fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379
slots:[0-1355],[6827-10922] (5452 slots) master
1 additional replica(s)
S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379
slots: (0 slots) slave
replicates a2488deac6e75c4dee7e7546e55d81394a92de34
S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379
slots: (0 slots) slave
replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1
M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83: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)? 1356
What is the receiving node ID? 9ff40f467859b3daf016b9270c9f657eef11d3e1 #將1356個槽位分配給第二個主節點 192.168.18.82:6379
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: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Source node #2: done
...
Moving slot 6801 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Moving slot 6802 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Moving slot 6803 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Moving slot 6804 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Moving slot 6805 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Moving slot 6806 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Moving slot 6807 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Do you want to proceed with the proposed reshard plan (yes/no)? yes
...
Moving slot 6802 from 192.168.18.85:6380 to 192.168.18.82:6379:
Moving slot 6803 from 192.168.18.85:6380 to 192.168.18.82:6379: ..
Moving slot 6804 from 192.168.18.85:6380 to 192.168.18.82:6379:
Moving slot 6805 from 192.168.18.85:6380 to 192.168.18.82:6379:
Moving slot 6806 from 192.168.18.85:6380 to 192.168.18.82:6379:
Moving slot 6807 from 192.168.18.85:6380 to 192.168.18.82:6379:
[root@Slave3 ~]# redis-cli -a 123456 --cluster reshard 192.168.18.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 192.168.18.81:6379)
S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379
slots: (0 slots) slave
replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c
M: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 192.168.18.85:6380
slots:[6808-6826],[10923-12287] (1384 slots) master
1 additional replica(s)
S: f3cf6c95950dbec83ab3a59bf7fd74739c89b774 192.168.18.85:6381
slots: (0 slots) slave
replicates fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379
slots:[0-1355],[6827-10922] (5452 slots) master
1 additional replica(s)
S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379
slots: (0 slots) slave
replicates a2488deac6e75c4dee7e7546e55d81394a92de34
S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379
slots: (0 slots) slave
replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1
M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379
slots:[1356-1364],[5461-6807],[12288-16383] (5452 slots) master
1 additional replica(s)
M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83: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)? 1384
What is the receiving node ID? a2488deac6e75c4dee7e7546e55d81394a92de34 #將剩下的1384個槽位分配給第三個主節點 192.168.18.83:6379
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: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Source node #2: done
...
Moving slot 12254 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Moving slot 12255 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Moving slot 12256 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Moving slot 12257 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Moving slot 12258 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Moving slot 12259 from fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Do you want to proceed with the proposed reshard plan (yes/no)? yes
...
Moving slot 12255 from 192.168.18.85:6380 to 192.168.18.83:6379:
Moving slot 12256 from 192.168.18.85:6380 to 192.168.18.83:6379: .
Moving slot 12257 from 192.168.18.85:6380 to 192.168.18.83:6379:
Moving slot 12258 from 192.168.18.85:6380 to 192.168.18.83:6379:
Moving slot 12259 from 192.168.18.85:6380 to 192.168.18.83:6379:
[root@Slave2 ~]# redis-cli -a 123456 --cluster check 192.168.18.81:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.18.85:6380 (fe8397f0...) -> 0 keys | 0 slots | 0 slaves.
192.168.18.84:6379 (767f9c45...) -> 3331 keys | 5452 slots | 1 slaves.
192.168.18.82:6379 (9ff40f46...) -> 3316 keys | 5452 slots | 1 slaves.
192.168.18.83:6379 (a2488dea...) -> 3353 keys | 5480 slots | 2 slaves.
[OK] 10000 keys in 4 masters.
0.61 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.18.81:6379)
S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379
slots: (0 slots) slave
replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c
M: fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a 192.168.18.85:6380
slots: (0 slots) master
S: f3cf6c95950dbec83ab3a59bf7fd74739c89b774 192.168.18.85:6381
slots: (0 slots) slave
replicates a2488deac6e75c4dee7e7546e55d81394a92de34
M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379
slots:[0-1355],[6827-10922] (5452 slots) master
1 additional replica(s)
S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379
slots: (0 slots) slave
replicates a2488deac6e75c4dee7e7546e55d81394a92de34
S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379
slots: (0 slots) slave
replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1
M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379
slots:[1356-1364],[5461-6807],[12288-16383] (5452 slots) master
1 additional replica(s)
M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379
slots:[1365-5460],[6808-6826],[10923-12287] (5480 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.
#192.168.18.85:6381已自動成為192.168.18.83:6379的slave
[root@Slave2 ~]# redis-cli -a 123456 -h 192.168.18.83 info replication
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
# Replication
role:master
connected_slaves:2
slave0:ip=192.168.18.80,port=6379,state=online,offset=253994,lag=0
slave1:ip=192.168.18.85,port=6381,state=online,offset=253994,lag=1
master_replid:42a58a2dda54c0b602f3022fb4015defce8f5182
master_replid2:f2f7b980ff32bde88c17cf822092a3868d04e5b6
master_repl_offset:253994
second_repl_offset:4453
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:253994
#將192.168.18.85:6380從集群中移除
[root@Slave3 ~]# redis-cli -a 123456 --cluster del-node 192.168.18.85:6380 fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node fe8397f0ec7fb9bc1dfa464cf7a126afe78edf1a from cluster 192.168.18.85:6380
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
#刪除節點后,redis進程自動關閉
#刪除多余的slave從節點
[root@Slave3 ~]# redis-cli -a 123456 --cluster del-node 192.168.18.85:6381 f3cf6c95950dbec83ab3a59bf7fd74739c89b774
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node f3cf6c95950dbec83ab3a59bf7fd74739c89b774 from cluster 192.168.18.85:6381
>>> Sending CLUSTER FORGET messages to the cluster...
>>> SHUTDOWN the node.
#節點刪除成功
[root@Slave2 ~]# redis-cli -a 123456 --cluster check 192.168.18.81:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.18.84:6379 (767f9c45...) -> 3331 keys | 5452 slots | 1 slaves.
192.168.18.82:6379 (9ff40f46...) -> 3316 keys | 5452 slots | 1 slaves.
192.168.18.83:6379 (a2488dea...) -> 3353 keys | 5480 slots | 1 slaves.
[OK] 10000 keys in 3 masters.
0.61 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.18.81:6379)
S: f361eb219afe76e1d316ea75628d3f8f16e2b8cc 192.168.18.81:6379
slots: (0 slots) slave
replicates 767f9c453c7e5ba27d51a65487428f65ae33d49c
M: 767f9c453c7e5ba27d51a65487428f65ae33d49c 192.168.18.84:6379
slots:[0-1355],[6827-10922] (5452 slots) master
1 additional replica(s)
S: 07f6fea79e94447ae2116a212c575e87f726c0b2 192.168.18.80:6379
slots: (0 slots) slave
replicates a2488deac6e75c4dee7e7546e55d81394a92de34
S: 38eeae587bd8df56fe71740f1b488c37ec3f97e4 192.168.18.85:6379
slots: (0 slots) slave
replicates 9ff40f467859b3daf016b9270c9f657eef11d3e1
M: 9ff40f467859b3daf016b9270c9f657eef11d3e1 192.168.18.82:6379
slots:[1356-1364],[5461-6807],[12288-16383] (5452 slots) master
1 additional replica(s)
M: a2488deac6e75c4dee7e7546e55d81394a92de34 192.168.18.83:6379
slots:[1365-5460],[6808-6826],[10923-12287] (5480 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.
[root@Slave2 ~]# redis-cli -a 123456 --cluster info 192.168.18.81:6379
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.18.84:6379 (767f9c45...) -> 3331 keys | 5452 slots | 1 slaves.
192.168.18.82:6379 (9ff40f46...) -> 3316 keys | 5452 slots | 1 slaves.
192.168.18.83:6379 (a2488dea...) -> 3353 keys | 5480 slots | 1 slaves.
[OK] 10000 keys in 3 masters.
0.61 keys per slot on average.