# 1. 介绍
官方地址:
- https://redis.io/topics/cluster-spec#cluster-nodes-attributes
- https://redis.io/topics/cluster-tutorial
前提条件:
- 先搭建单机版的redis(https://www.jianshu.com/p/97ec5299dc23)
# 2. 数据分片
Redis Cluster 不使用一致性哈希,而是使用不同形式的分片 每个键在概念上都是我们所谓的 哈希槽的一部分 。 Redis Cluster 有 16384 个哈希槽,计算哈希值是多少 给定密钥的插槽,我们只需取密钥的 CRC16 模 16384。 Redis 集群中的每个节点都负责哈希槽的一个子集, 例如,您可能有一个包含 3 个节点的集群,其中: 节点 A 包含从 0 到 5500 的哈希槽。 节点 B 包含从 5501 到 11000 的哈希槽。 节点 C 包含从 11001 到 16383 的哈希槽。
# 3. 查看集群节点
redis-cli cluster nodes
如果报错 ERR This instance has cluster support disabled
的话,说明集群未开启。
# 4.集群搭建
注:最小集群需要包含至少三个主节点。 强烈建议 启动一个具有三个主节点和三个从节点的六节点集群。
本例子是在一台服务器上面做的实验,只是创建了6个目录,启了6个redis-server而已,而后面2个7006
,7007
是用来演示扩容与缩容的实例。
名称 | IP | 端口 | 备注 |
---|---|---|---|
redis server 01节点 | 127.0.0.1 | 7000 | 演示中主节点 |
redis server 02节点 | 127.0.0.1 | 7001 | 演示中主节点 |
redis server 03节点 | 127.0.0.1 | 7002 | 演示中主节点 |
redis server 04节点 | 127.0.0.1 | 7003 | 演示中7000的从节点 |
redis server 05节点 | 127.0.0.1 | 7004 | 演示中7001的从节点 |
redis server 06节点 | 127.0.0.1 | 7005 | 演示中7002的从节点 |
redis server 07节点 | 127.0.0.1 | 7006 | 演示中扩容主节点 |
redis server 08节点 | 127.0.0.1 | 7007 | 演示中扩容7006从节点 |
# 4.1 创建6个目录
mkdir cluster-test
cd cluster-test
mkdir 7000 7001 7002 7003 7004 7005
2
3
创建一个 redis.conf每个目录中的文件,从 7000 到 7005。 作为配置文件的模板,只需使用上面的小示例, 但一定要更换端口号 7000使用正确的端口号 根据目录名。
# 4.2 模版配置如下
#7000端口
port 7000
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000
appendonly yes
masterauth 123456 #一定要加,因为配置了requirepass,主备需要使用
logfile "/usr/redis-6.2.5/bin/cluster-test/7000.log"
#7001端口
port 7001
cluster-enabled yes
cluster-config-file nodes-7001.conf
cluster-node-timeout 5000
appendonly yes
masterauth 123456 #一定要加,因为配置了requirepass,主备需要使用
logfile "/usr/redis-6.2.5/bin/cluster-test/7001.log"
#7002端口
port 7002
cluster-enabled yes
cluster-config-file nodes-7002.conf
cluster-node-timeout 5000
appendonly yes
masterauth 123456 #一定要加,因为配置了requirepass,主备需要使用
logfile "/usr/redis-6.2.5/bin/cluster-test/7002.log"
#7003端口
port 7003
cluster-enabled yes
cluster-config-file nodes-7003.conf
cluster-node-timeout 5000
appendonly yes
masterauth 123456 #一定要加,因为配置了requirepass,主备需要使用
logfile "/usr/redis-6.2.5/bin/cluster-test/7003.log"
#7004端口
port 7004
cluster-enabled yes
cluster-config-file nodes-7004.conf
cluster-node-timeout 5000
appendonly yes
masterauth 123456 #一定要加,因为配置了requirepass,主备需要使用
logfile "/usr/redis-6.2.5/bin/cluster-test/7004.log"
#7005端口
port 7005
cluster-enabled yes
cluster-config-file nodes-7005.conf
cluster-node-timeout 5000
appendonly yes
masterauth 123456 #一定要加,因为配置了requirepass,主备需要使用
logfile "/usr/redis-6.2.5/bin/cluster-test/7005.log"
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# 4.3 启动每个服务
cd /usr/redis-6.2.5/bin/cluster-test
../redis-server ./7000/redis.conf
../redis-server ./7001/redis.conf
../redis-server ./7002/redis.conf
../redis-server ./7003/redis.conf
../redis-server ./7004/redis.conf
../redis-server ./7005/redis.conf
2
3
4
5
6
7
通过 ps aux|grep redis
查看redis的启动情况。
# 4.4 构建cluster
# 4.4.1 redis-cli构建
redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1 -a 123456 2>/dev/null
[root@zjwoo bin]# redis-cli --cluster create 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 --cluster-replicas 1 -a 123456 2>/dev/null
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:7004 to 127.0.0.1:7000
Adding replica 127.0.0.1:7005 to 127.0.0.1:7001
Adding replica 127.0.0.1:7003 to 127.0.0.1:7002
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 0031fb0ea57c8b5312cb30253a512254e173e111 127.0.0.1:7000
slots:[0-5460] (5461 slots) master
M: 94a987da25c05560a7ace853d0001d4cb9e0c2fd 127.0.0.1:7001
slots:[5461-10922] (5462 slots) master
M: 5065f634980cec0c6e8934a37b1a81010094915e 127.0.0.1:7002
slots:[10923-16383] (5461 slots) master
S: 57c1f5983c51b1ce0633be325227206e755a2a6c 127.0.0.1:7003
replicates 0031fb0ea57c8b5312cb30253a512254e173e111
S: d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 127.0.0.1:7004
replicates 94a987da25c05560a7ace853d0001d4cb9e0c2fd
S: 029a142c9499b16a81c125ae5e39af0104e6795a 127.0.0.1:7005
replicates 5065f634980cec0c6e8934a37b1a81010094915e
Can I set the above configuration? (type 'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different config epoch to each node
>>> Sending CLUSTER MEET messages to join the cluster
Waiting for the cluster to join
.
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 0031fb0ea57c8b5312cb30253a512254e173e111 127.0.0.1:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 029a142c9499b16a81c125ae5e39af0104e6795a 127.0.0.1:7005
slots: (0 slots) slave
replicates 5065f634980cec0c6e8934a37b1a81010094915e
S: 57c1f5983c51b1ce0633be325227206e755a2a6c 127.0.0.1:7003
slots: (0 slots) slave
replicates 0031fb0ea57c8b5312cb30253a512254e173e111
M: 94a987da25c05560a7ace853d0001d4cb9e0c2fd 127.0.0.1:7001
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 5065f634980cec0c6e8934a37b1a81010094915e 127.0.0.1:7002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 127.0.0.1:7004
slots: (0 slots) slave
replicates 94a987da25c05560a7ace853d0001d4cb9e0c2fd
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 4.4.2 create-cluster脚本构建
如果不想通过配置和执行来创建Redis集群 如上所述,手动单个实例,有一个更简单的 系统(但您不会了解相同数量的操作细节)。
只需检查 utils/create-clusterRedis 发行版中的目录。 有一个脚本叫做 create-cluster里面(与目录同名 它包含在)中,它是一个简单的 bash 脚本。 为了开始 一个有 3 个主节点和 3 个从节点的 6 节点集群只需输入以下内容 命令:
create-cluster start
create-cluster create
2
回复 yes在第 2 步中,当 redis-cli实用程序要你接受 集群布局。
您现在可以与集群交互,第一个节点将从端口 30001 开始 默认情况下。 完成后,使用以下命令停止集群:
create-cluster stop
请阅读 README在此目录中以获取有关如何操作的更多信息 运行脚本。
create-cluster.sh
#!/bin/bash
# Settings
BIN_PATH="./"
CLUSTER_HOST=127.0.0.1
PORT=30000
TIMEOUT=2000
NODES=6
REPLICAS=1
PROTECTED_MODE=yes
ADDITIONAL_OPTIONS=""
# You may want to put the above config parameters into config.sh in order to
# override the defaults without modifying this script.
if [ -a config.sh ]
then
source "config.sh"
fi
# Computed vars
ENDPORT=$((PORT+NODES))
if [ "$1" == "start" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
echo "Starting $PORT"
$BIN_PATH/redis-server --port $PORT --protected-mode $PROTECTED_MODE --cluster-enabled yes --cluster-config-file nodes-${PORT}.conf --cluster-node-timeout $TIMEOUT --appendonly yes --appendfilename appendonly-${PORT}.aof --dbfilename dump-${PORT}.rdb --logfile ${PORT}.log --daemonize yes ${ADDITIONAL_OPTIONS}
done
exit 0
fi
if [ "$1" == "create" ]
then
HOSTS=""
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
HOSTS="$HOSTS $CLUSTER_HOST:$PORT"
done
OPT_ARG=""
if [ "$2" == "-f" ]; then
OPT_ARG="--cluster-yes"
fi
$BIN_PATH/redis-cli --cluster create $HOSTS --cluster-replicas $REPLICAS $OPT_ARG
exit 0
fi
if [ "$1" == "stop" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
echo "Stopping $PORT"
$BIN_PATH/redis-cli -p $PORT shutdown nosave
done
exit 0
fi
if [ "$1" == "watch" ]
then
PORT=$((PORT+1))
while [ 1 ]; do
clear
date
$BIN_PATH/redis-cli -p $PORT cluster nodes | head -30
sleep 1
done
exit 0
fi
if [ "$1" == "tail" ]
then
INSTANCE=$2
PORT=$((PORT+INSTANCE))
tail -f ${PORT}.log
exit 0
fi
if [ "$1" == "tailall" ]
then
tail -f *.log
exit 0
fi
if [ "$1" == "call" ]
then
while [ $((PORT < ENDPORT)) != "0" ]; do
PORT=$((PORT+1))
$BIN_PATH/redis-cli -p $PORT $2 $3 $4 $5 $6 $7 $8 $9
done
exit 0
fi
if [ "$1" == "clean" ]
then
rm -rf *.log
rm -rf appendonly*.aof
rm -rf dump*.rdb
rm -rf nodes*.conf
exit 0
fi
if [ "$1" == "clean-logs" ]
then
rm -rf *.log
exit 0
fi
echo "Usage: $0 [start|create|stop|watch|tail|clean|call]"
echo "start -- Launch Redis Cluster instances."
echo "create [-f] -- Create a cluster using redis-cli --cluster create."
echo "stop -- Stop Redis Cluster instances."
echo "watch -- Show CLUSTER NODES output (first 30 lines) of first node."
echo "tail <id> -- Run tail -f of instance at base port + ID."
echo "tailall -- Run tail -f for all the log files at once."
echo "clean -- Remove all instances data, logs, configs."
echo "clean-logs -- Remove just instances logs."
echo "call <cmd> -- Call a command (up to 7 arguments) on all nodes."
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
集群信息
127.0.0.1:7000> CLUSTER info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:6
cluster_size:3
cluster_current_epoch:6
cluster_my_epoch:1
cluster_stats_messages_ping_sent:127
cluster_stats_messages_pong_sent:126
cluster_stats_messages_sent:253
cluster_stats_messages_ping_received:121
cluster_stats_messages_pong_received:127
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:253
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
集群节点
redis-cli -p 7000 -a 123456 cluster nodes
127.0.0.1:7000> cluster nodes
029a142c9499b16a81c125ae5e39af0104e6795a 127.0.0.1:7005@17005 slave 5065f634980cec0c6e8934a37b1a81010094915e 0 1628662618666 3 connected
57c1f5983c51b1ce0633be325227206e755a2a6c 127.0.0.1:7003@17003 slave 0031fb0ea57c8b5312cb30253a512254e173e111 0 1628662617000 1 connected
94a987da25c05560a7ace853d0001d4cb9e0c2fd 127.0.0.1:7001@17001 master - 0 1628662617664 2 connected 5461-10922
0031fb0ea57c8b5312cb30253a512254e173e111 127.0.0.1:7000@17000 myself,master - 0 1628662616000 1 connected 0-5460
5065f634980cec0c6e8934a37b1a81010094915e 127.0.0.1:7002@17002 master - 0 1628662618000 3 connected 10923-16383
d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 127.0.0.1:7004@17004 slave 94a987da25c05560a7ace853d0001d4cb9e0c2fd 0 1628662618566 2 connected
2
3
4
5
6
7
8
9
# 4.5 测试
[root@zjwoo bin]# redis-cli -p 7000
127.0.0.1:7000> keys *
(error) NOAUTH Authentication required.
127.0.0.1:7000> auth 123456
OK
127.0.0.1:7000> keys *
(empty array)
127.0.0.1:7000> set foo bar
(error) MOVED 12182 127.0.0.1:7002
127.0.0.1:7000> get foo
(error) MOVED 12182 127.0.0.1:7002
127.0.0.1:7000> get hello
(nil)
2
3
4
5
6
7
8
9
10
11
12
13
我上面的例子我们可以看出,设置的key会被分配到不同的实例去。 现在很多的sdk都支持move,例如:phpredis。
redis-cli -c -p 7000
这样的话,会自动move。
# 5. 测试故障转移
我们先查看下当前的集群情况。
[root@zjwoo bin]# redis-cli -a 123456 -p 7000 cluster nodes
029a142c9499b16a81c125ae5e39af0104e6795a 127.0.0.1:7005@17005 slave 5065f634980cec0c6e8934a37b1a81010094915e 0 1628662618666 3 connected
57c1f5983c51b1ce0633be325227206e755a2a6c 127.0.0.1:7003@17003 slave 0031fb0ea57c8b5312cb30253a512254e173e111 0 1628662617000 1 connected
94a987da25c05560a7ace853d0001d4cb9e0c2fd 127.0.0.1:7001@17001 master - 0 1628662617664 2 connected 5461-10922
0031fb0ea57c8b5312cb30253a512254e173e111 127.0.0.1:7000@17000 myself,master - 0 1628662616000 1 connected 0-5460
5065f634980cec0c6e8934a37b1a81010094915e 127.0.0.1:7002@17002 master - 0 1628662618000 3 connected 10923-16383
d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 127.0.0.1:7004@17004 slave 94a987da25c05560a7ace853d0001d4cb9e0c2fd 0 1628662618566 2 connected
2
3
4
5
6
7
目前 7000
,7001
,7002
是 master
角色,7003
,7004
,7005
是 slave
角色。
然后我们将 7001
这个端口的redis服务的进程杀掉。
kill -9 29808 #29808是7001的进程号,具体以实际为准。
7004.log
日志,发现7001
主挂了,然后将自己升级为master。
[root@zjwoo cluster-test]# tail -100f 7004.log
30132:C 11 Aug 2021 14:14:05.420 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
30132:C 11 Aug 2021 14:14:05.420 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=30132, just started
30132:C 11 Aug 2021 14:14:05.420 # Configuration loaded
30132:M 11 Aug 2021 14:14:05.421 * monotonic clock: POSIX clock_gettime
30132:M 11 Aug 2021 14:14:05.422 * No cluster configuration found, I'm d4c10faf735b8d36f8c4ce4cb065640940f2e7ac
30132:M 11 Aug 2021 14:14:05.426 * Running mode=cluster, port=7004.
30132:M 11 Aug 2021 14:14:05.426 # Server initialized
30132:M 11 Aug 2021 14:14:05.426 * Ready to accept connections
30132:M 11 Aug 2021 14:15:30.635 # configEpoch set to 5 via CLUSTER SET-CONFIG-EPOCH
30132:M 11 Aug 2021 14:15:30.651 # IP address for this node updated to 127.0.0.1
30132:S 11 Aug 2021 14:15:32.639 * Before turning into a replica, using my own master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
30132:S 11 Aug 2021 14:15:32.639 * Connecting to MASTER 127.0.0.1:7001
30132:S 11 Aug 2021 14:15:32.639 * MASTER <-> REPLICA sync started
30132:S 11 Aug 2021 14:15:32.639 # Cluster state changed: ok
30132:S 11 Aug 2021 14:15:32.639 * Non blocking connect for SYNC fired the event.
30132:S 11 Aug 2021 14:15:32.640 * Master replied to PING, replication can continue...
30132:S 11 Aug 2021 14:15:32.641 * Trying a partial resynchronization (request f6b4528703426cf67b36bf65c48322eabd441a33:1).
30132:S 11 Aug 2021 14:15:32.641 * Full resync from master: 03a255ca5f70b0f761027fae072050ce8d6f2f43:0
30132:S 11 Aug 2021 14:15:32.641 * Discarding previously cached master state.
30132:S 11 Aug 2021 14:15:32.675 * MASTER <-> REPLICA sync: receiving 175 bytes from master to disk
30132:S 11 Aug 2021 14:15:32.675 * MASTER <-> REPLICA sync: Flushing old data
30132:S 11 Aug 2021 14:15:32.675 * MASTER <-> REPLICA sync: Loading DB in memory
30132:S 11 Aug 2021 14:15:32.679 * Loading RDB produced by version 6.2.5
30132:S 11 Aug 2021 14:15:32.679 * RDB age 0 seconds
30132:S 11 Aug 2021 14:15:32.679 * RDB memory usage when created 2.54 Mb
30132:S 11 Aug 2021 14:15:32.679 * MASTER <-> REPLICA sync: Finished with success
30132:S 11 Aug 2021 14:15:32.679 * Background append only file rewriting started by pid 1093
30132:S 11 Aug 2021 14:15:32.706 * AOF rewrite child asks to stop sending diffs.
1093:C 11 Aug 2021 14:15:32.706 * Parent agreed to stop sending diffs. Finalizing AOF...
1093:C 11 Aug 2021 14:15:32.706 * Concatenating 0.00 MB of AOF diff received from parent.
1093:C 11 Aug 2021 14:15:32.706 * SYNC append only file rewrite performed
1093:C 11 Aug 2021 14:15:32.706 * AOF rewrite: 0 MB of memory used by copy-on-write
30132:S 11 Aug 2021 14:15:32.772 * Background AOF rewrite terminated with success
30132:S 11 Aug 2021 14:15:32.772 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
30132:S 11 Aug 2021 14:15:32.772 * Background AOF rewrite finished successfully
30132:S 11 Aug 2021 14:18:02.100 # Connection with master lost.
30132:S 11 Aug 2021 14:18:02.100 * Caching the disconnected master state.
30132:S 11 Aug 2021 14:18:02.100 * Reconnecting to MASTER 127.0.0.1:7001
30132:S 11 Aug 2021 14:18:02.100 * MASTER <-> REPLICA sync started
30132:S 11 Aug 2021 14:18:02.100 # Error condition on socket for SYNC: Connection refused
30132:S 11 Aug 2021 14:18:02.297 * Connecting to MASTER 127.0.0.1:7001
30132:S 11 Aug 2021 14:18:02.297 * MASTER <-> REPLICA sync started
30132:S 11 Aug 2021 14:18:02.297 # Error condition on socket for SYNC: Connection refused
30132:S 11 Aug 2021 14:18:03.303 * Connecting to MASTER 127.0.0.1:7001
30132:S 11 Aug 2021 14:18:03.303 * MASTER <-> REPLICA sync started
30132:S 11 Aug 2021 14:18:03.303 # Error condition on socket for SYNC: Connection refused
30132:S 11 Aug 2021 14:18:04.312 * Connecting to MASTER 127.0.0.1:7001
30132:S 11 Aug 2021 14:18:04.313 * MASTER <-> REPLICA sync started
30132:S 11 Aug 2021 14:18:04.313 # Error condition on socket for SYNC: Connection refused
30132:S 11 Aug 2021 14:18:05.319 * Connecting to MASTER 127.0.0.1:7001
30132:S 11 Aug 2021 14:18:05.319 * MASTER <-> REPLICA sync started
30132:S 11 Aug 2021 14:18:05.319 # Error condition on socket for SYNC: Connection refused
30132:S 11 Aug 2021 14:18:06.324 * Connecting to MASTER 127.0.0.1:7001
30132:S 11 Aug 2021 14:18:06.324 * MASTER <-> REPLICA sync started
30132:S 11 Aug 2021 14:18:06.325 # Error condition on socket for SYNC: Connection refused
30132:S 11 Aug 2021 14:18:07.346 * Connecting to MASTER 127.0.0.1:7001
30132:S 11 Aug 2021 14:18:07.346 * MASTER <-> REPLICA sync started
30132:S 11 Aug 2021 14:18:07.347 # Error condition on socket for SYNC: Connection refused
30132:S 11 Aug 2021 14:18:08.360 * Connecting to MASTER 127.0.0.1:7001
30132:S 11 Aug 2021 14:18:08.360 * MASTER <-> REPLICA sync started
30132:S 11 Aug 2021 14:18:08.360 # Error condition on socket for SYNC: Connection refused
30132:S 11 Aug 2021 14:18:08.864 * FAIL message received from 57c1f5983c51b1ce0633be325227206e755a2a6c about 94a987da25c05560a7ace853d0001d4cb9e0c2fd
30132:S 11 Aug 2021 14:18:08.864 # Start of election delayed for 814 milliseconds (rank #0, offset 210).
30132:S 11 Aug 2021 14:18:08.864 # Cluster state changed: fail
30132:S 11 Aug 2021 14:18:09.371 * Connecting to MASTER 127.0.0.1:7001
30132:S 11 Aug 2021 14:18:09.371 * MASTER <-> REPLICA sync started
30132:S 11 Aug 2021 14:18:09.371 # Error condition on socket for SYNC: Connection refused
30132:S 11 Aug 2021 14:18:09.773 # Starting a failover election for epoch 7.
30132:S 11 Aug 2021 14:18:09.784 # Failover election won: I'm the new master.
30132:S 11 Aug 2021 14:18:09.784 # configEpoch set to 7 after successful failover
30132:M 11 Aug 2021 14:18:09.784 * Discarding previously cached master state.
30132:M 11 Aug 2021 14:18:09.784 # Setting secondary replication ID to 03a255ca5f70b0f761027fae072050ce8d6f2f43, valid up to offset: 211. New replication ID is 4cca7151c023650b7b07cf64020a461e705f1b7d
30132:M 11 Aug 2021 14:18:09.784 # Cluster state changed: ok
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
查询当前的集群状态如下:
127.0.0.1:7000> cluster nodes
029a142c9499b16a81c125ae5e39af0104e6795a 127.0.0.1:7005@17005 slave 5065f634980cec0c6e8934a37b1a81010094915e 0 1628662818603 3 connected
57c1f5983c51b1ce0633be325227206e755a2a6c 127.0.0.1:7003@17003 slave 0031fb0ea57c8b5312cb30253a512254e173e111 0 1628662818502 1 connected
94a987da25c05560a7ace853d0001d4cb9e0c2fd 127.0.0.1:7001@17001 master,fail - 1628662682600 1628662680000 2 disconnected
0031fb0ea57c8b5312cb30253a512254e173e111 127.0.0.1:7000@17000 myself,master - 0 1628662816000 1 connected 0-5460
5065f634980cec0c6e8934a37b1a81010094915e 127.0.0.1:7002@17002 master - 0 1628662817494 3 connected 10923-16383
d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 127.0.0.1:7004@17004 master - 0 1628662817000 7 connected 5461-10922
2
3
4
5
6
7
我们可以看出,7004
升级了master
,7001
是fail
状态。
启动7001
的redis服务。
7001.log
中可以发现,自己变成了slave
。
[root@zjwoo cluster-test]# tail -100f 7001.log
29808:C 11 Aug 2021 14:13:57.227 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
29808:C 11 Aug 2021 14:13:57.227 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=29808, just started
29808:C 11 Aug 2021 14:13:57.227 # Configuration loaded
29808:M 11 Aug 2021 14:13:57.228 * monotonic clock: POSIX clock_gettime
29808:M 11 Aug 2021 14:13:57.229 * No cluster configuration found, I'm 94a987da25c05560a7ace853d0001d4cb9e0c2fd
29808:M 11 Aug 2021 14:13:57.233 * Running mode=cluster, port=7001.
29808:M 11 Aug 2021 14:13:57.233 # Server initialized
29808:M 11 Aug 2021 14:13:57.233 * Ready to accept connections
29808:M 11 Aug 2021 14:15:30.634 # configEpoch set to 2 via CLUSTER SET-CONFIG-EPOCH
29808:M 11 Aug 2021 14:15:30.650 # IP address for this node updated to 127.0.0.1
29808:M 11 Aug 2021 14:15:32.641 * Replica 127.0.0.1:7004 asks for synchronization
29808:M 11 Aug 2021 14:15:32.641 * Partial resynchronization not accepted: Replication ID mismatch (Replica asked for 'f6b4528703426cf67b36bf65c48322eabd441a33', my replication IDs are 'ebc57408970e7eb9961c6c81d87bedc6bdec10a2' and '0000000000000000000000000000000000000000')
29808:M 11 Aug 2021 14:15:32.641 * Replication backlog created, my new replication IDs are '03a255ca5f70b0f761027fae072050ce8d6f2f43' and '0000000000000000000000000000000000000000'
29808:M 11 Aug 2021 14:15:32.641 * Starting BGSAVE for SYNC with target: disk
29808:M 11 Aug 2021 14:15:32.641 * Background saving started by pid 1089
1089:C 11 Aug 2021 14:15:32.657 * DB saved on disk
1089:C 11 Aug 2021 14:15:32.657 * RDB: 0 MB of memory used by copy-on-write
29808:M 11 Aug 2021 14:15:32.674 * Background saving terminated with success
29808:M 11 Aug 2021 14:15:32.675 * Synchronization with replica 127.0.0.1:7004 succeeded
29808:M 11 Aug 2021 14:15:35.608 # Cluster state changed: ok
16763:C 11 Aug 2021 14:22:15.453 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
16763:C 11 Aug 2021 14:22:15.453 # Redis version=6.2.5, bits=64, commit=00000000, modified=0, pid=16763, just started
16763:C 11 Aug 2021 14:22:15.453 # Configuration loaded
16763:M 11 Aug 2021 14:22:15.454 * monotonic clock: POSIX clock_gettime
16763:M 11 Aug 2021 14:22:15.455 * Node configuration loaded, I'm 94a987da25c05560a7ace853d0001d4cb9e0c2fd
16763:M 11 Aug 2021 14:22:15.456 * Running mode=cluster, port=7001.
16763:M 11 Aug 2021 14:22:15.456 # Server initialized
16763:M 11 Aug 2021 14:22:15.456 * Reading RDB preamble from AOF file...
16763:M 11 Aug 2021 14:22:15.456 * Loading RDB produced by version 6.2.5
16763:M 11 Aug 2021 14:22:15.456 * RDB age 403 seconds
16763:M 11 Aug 2021 14:22:15.456 * RDB memory usage when created 2.50 Mb
16763:M 11 Aug 2021 14:22:15.456 * RDB has an AOF tail
16763:M 11 Aug 2021 14:22:15.456 * Reading the remaining AOF tail...
16763:M 11 Aug 2021 14:22:15.456 * DB loaded from append only file: 0.000 seconds
16763:M 11 Aug 2021 14:22:15.456 * Ready to accept connections
16763:M 11 Aug 2021 14:22:15.458 # Configuration change detected. Reconfiguring myself as a replica of d4c10faf735b8d36f8c4ce4cb065640940f2e7ac
16763:S 11 Aug 2021 14:22:15.458 * Before turning into a replica, using my own master parameters to synthesize a cached master: I may be able to synchronize with the new master with just a partial transfer.
16763:S 11 Aug 2021 14:22:15.458 * Connecting to MASTER 127.0.0.1:7004
16763:S 11 Aug 2021 14:22:15.458 * MASTER <-> REPLICA sync started
16763:S 11 Aug 2021 14:22:15.458 # Cluster state changed: ok
16763:S 11 Aug 2021 14:22:15.461 * Non blocking connect for SYNC fired the event.
16763:S 11 Aug 2021 14:22:15.461 * Master replied to PING, replication can continue...
16763:S 11 Aug 2021 14:22:15.461 * Trying a partial resynchronization (request 7e3d0505848f348f0e57fb8b16f23669bae1cbd1:1).
16763:S 11 Aug 2021 14:22:15.462 * Full resync from master: 4cca7151c023650b7b07cf64020a461e705f1b7d:210
16763:S 11 Aug 2021 14:22:15.462 * Discarding previously cached master state.
16763:S 11 Aug 2021 14:22:15.536 * MASTER <-> REPLICA sync: receiving 176 bytes from master to disk
16763:S 11 Aug 2021 14:22:15.536 * MASTER <-> REPLICA sync: Flushing old data
16763:S 11 Aug 2021 14:22:15.537 * MASTER <-> REPLICA sync: Loading DB in memory
16763:S 11 Aug 2021 14:22:15.539 * Loading RDB produced by version 6.2.5
16763:S 11 Aug 2021 14:22:15.539 * RDB age 0 seconds
16763:S 11 Aug 2021 14:22:15.539 * RDB memory usage when created 2.53 Mb
16763:S 11 Aug 2021 14:22:15.540 * MASTER <-> REPLICA sync: Finished with success
16763:S 11 Aug 2021 14:22:15.540 * Background append only file rewriting started by pid 16769
16763:S 11 Aug 2021 14:22:15.566 * AOF rewrite child asks to stop sending diffs.
16769:C 11 Aug 2021 14:22:15.566 * Parent agreed to stop sending diffs. Finalizing AOF...
16769:C 11 Aug 2021 14:22:15.566 * Concatenating 0.00 MB of AOF diff received from parent.
16769:C 11 Aug 2021 14:22:15.567 * SYNC append only file rewrite performed
16769:C 11 Aug 2021 14:22:15.567 * AOF rewrite: 0 MB of memory used by copy-on-write
16763:S 11 Aug 2021 14:22:15.658 * Background AOF rewrite terminated with success
16763:S 11 Aug 2021 14:22:15.659 * Residual parent diff successfully flushed to the rewritten AOF (0.00 MB)
16763:S 11 Aug 2021 14:22:15.659 * Background AOF rewrite finished successfully
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
此时我们再来看下集群情况:
127.0.0.1:7000> cluster nodes
029a142c9499b16a81c125ae5e39af0104e6795a 127.0.0.1:7005@17005 slave 5065f634980cec0c6e8934a37b1a81010094915e 0 1628663030408 3 connected
57c1f5983c51b1ce0633be325227206e755a2a6c 127.0.0.1:7003@17003 slave 0031fb0ea57c8b5312cb30253a512254e173e111 0 1628663030000 1 connected
94a987da25c05560a7ace853d0001d4cb9e0c2fd 127.0.0.1:7001@17001 slave d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 0 1628663029395 7 connected
0031fb0ea57c8b5312cb30253a512254e173e111 127.0.0.1:7000@17000 myself,master - 0 1628663028000 1 connected 0-5460
5065f634980cec0c6e8934a37b1a81010094915e 127.0.0.1:7002@17002 master - 0 1628663028382 3 connected 10923-16383
d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 127.0.0.1:7004@17004 master - 0 1628663030000 7 connected 5461-10922
2
3
4
5
6
7
目前 7000
, 7002
,7004
是 master角色,7001
,7003
,7005
是 slave角色,自动进行了故障切换。
# 6. 注意事项
# 6.1 master与slave不能自动切换
如果配置了requirepass
的话,masterauth
必须需要配置,不然master
与slave
不能自动切换。
# 6.2 ERR This instance has cluster support disabled
修改配置文件 redis.conf
中的 cluster-enabled
参数的值为 yes
并把参数前#去除,重启Redis服务器。
# 6.3 redis输入密码去除提示Warning: Using a password redis输入密码去除提示Warning: Using a password
在命令尾部追加 2>/dev/null
即可。
# 6.4 [ERR] Node 127.0.0.1:7000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
- 将需要新增的节点下
aof
、rdb
等本地备份文件删除; - 同时将新Node的集群配置文件删除,即:删除你
redis.conf
里面cluster-config-file
所在的文件; - flushdb #清空当前数据库
# 7. 动态扩容/缩容
我们继续在 cluster-test
目录下,创建 7006
目录,配置和之前的一样,不同就是端口以及日志路径,然后启动服务即可。
# 7.1 添加新节点
redis-cli --cluster add-node 127.0.0.1:7006 127.0.0.1:7000 -a 123456
[root@zjwoo bin]# redis-cli --cluster add-node 127.0.0.1:7006 127.0.0.1:7000 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 127.0.0.1:7006 to cluster 127.0.0.1:7000
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 0031fb0ea57c8b5312cb30253a512254e173e111 127.0.0.1:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 029a142c9499b16a81c125ae5e39af0104e6795a 127.0.0.1:7005
slots: (0 slots) slave
replicates 5065f634980cec0c6e8934a37b1a81010094915e
S: 57c1f5983c51b1ce0633be325227206e755a2a6c 127.0.0.1:7003
slots: (0 slots) slave
replicates 0031fb0ea57c8b5312cb30253a512254e173e111
S: 94a987da25c05560a7ace853d0001d4cb9e0c2fd 127.0.0.1:7001
slots: (0 slots) slave
replicates d4c10faf735b8d36f8c4ce4cb065640940f2e7ac
M: 5065f634980cec0c6e8934a37b1a81010094915e 127.0.0.1:7002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 127.0.0.1:7004
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 127.0.0.1:7006 to make it join the cluster.
[OK] New node added correctly.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
看下最新的集群情况
127.0.0.1:7000> cluster nodes
029a142c9499b16a81c125ae5e39af0104e6795a 127.0.0.1:7005@17005 slave 5065f634980cec0c6e8934a37b1a81010094915e 0 1628663930598 3 connected
57c1f5983c51b1ce0633be325227206e755a2a6c 127.0.0.1:7003@17003 slave 0031fb0ea57c8b5312cb30253a512254e173e111 0 1628663929796 1 connected
94a987da25c05560a7ace853d0001d4cb9e0c2fd 127.0.0.1:7001@17001 slave d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 0 1628663929594 7 connected
ad210e2941786d75222486299caa6ee33c8c8a7c 127.0.0.1:7006@17006 master - 0 1628663930000 0 connected
0031fb0ea57c8b5312cb30253a512254e173e111 127.0.0.1:7000@17000 myself,master - 0 1628663929000 1 connected 0-5460
5065f634980cec0c6e8934a37b1a81010094915e 127.0.0.1:7002@17002 master - 0 1628663930000 3 connected 10923-16383
d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 127.0.0.1:7004@17004 master - 0 1628663930799 7 connected 5461-10922
2
3
4
5
6
7
8
添加一个 7006
的从节点。
在 cluster-test
目录下,创建 7007
目录,配置和之前的一样,不同就是端口以及日志路径,然后启动服务即可。
redis-cli -a 123456 --cluster add-node 127.0.0.1:7007 127.0.0.1:7000 --cluster-slave --cluster-master-id ad210e2941786d75222486299caa6ee33c8c8a7c
[root@zjwoo bin]# redis-cli -a 123456 --cluster add-node 127.0.0.1:7007 127.0.0.1:7000 --cluster-slave --cluster-master-id ad210e2941786d75222486299caa6ee33c8c8a7c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 127.0.0.1:7007 to cluster 127.0.0.1:7000
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 0031fb0ea57c8b5312cb30253a512254e173e111 127.0.0.1:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 029a142c9499b16a81c125ae5e39af0104e6795a 127.0.0.1:7005
slots: (0 slots) slave
replicates 5065f634980cec0c6e8934a37b1a81010094915e
S: 57c1f5983c51b1ce0633be325227206e755a2a6c 127.0.0.1:7003
slots: (0 slots) slave
replicates 0031fb0ea57c8b5312cb30253a512254e173e111
S: 94a987da25c05560a7ace853d0001d4cb9e0c2fd 127.0.0.1:7001
slots: (0 slots) slave
replicates d4c10faf735b8d36f8c4ce4cb065640940f2e7ac
M: ad210e2941786d75222486299caa6ee33c8c8a7c 127.0.0.1:7006
slots: (0 slots) master
M: 5065f634980cec0c6e8934a37b1a81010094915e 127.0.0.1:7002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 127.0.0.1:7004
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 127.0.0.1:7007 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 127.0.0.1:7006.
[OK] New node added correctly.
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
看下最新的集群情况
127.0.0.1:7000> cluster nodes
029a142c9499b16a81c125ae5e39af0104e6795a 127.0.0.1:7005@17005 slave 5065f634980cec0c6e8934a37b1a81010094915e 0 1628664289000 3 connected
57c1f5983c51b1ce0633be325227206e755a2a6c 127.0.0.1:7003@17003 slave 0031fb0ea57c8b5312cb30253a512254e173e111 0 1628664290000 1 connected
94a987da25c05560a7ace853d0001d4cb9e0c2fd 127.0.0.1:7001@17001 slave d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 0 1628664289000 7 connected
ad210e2941786d75222486299caa6ee33c8c8a7c 127.0.0.1:7006@17006 master - 0 1628664291155 0 connected
c614feeff585b44716db6e38eb02b2374ed35500 127.0.0.1:7007@17007 slave ad210e2941786d75222486299caa6ee33c8c8a7c 0 1628664290149 0 connected
0031fb0ea57c8b5312cb30253a512254e173e111 127.0.0.1:7000@17000 myself,master - 0 1628664290000 1 connected 0-5460
5065f634980cec0c6e8934a37b1a81010094915e 127.0.0.1:7002@17002 master - 0 1628664289644 3 connected 10923-16383
d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 127.0.0.1:7004@17004 master - 0 1628664289140 7 connected 5461-10922
2
3
4
5
6
7
8
9
从上面集群情况可以看出了问题,新增加的7006
,7007
的虽然增加到了集群里面,但是没有分配slot。
# 7.2 槽点重新分配
重新分配Redis槽位扩容----槽点中的数据会一起移动【槽点数可以自行设置大小,根据硬件情况设置】
redis-cli -a 123456 --cluster reshard 127.0.0.1:7000
[root@zjwoo bin]# redis-cli -a 123456 --cluster reshard 127.0.0.1:7000
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing Cluster Check (using node 127.0.0.1:7000)
M: 0031fb0ea57c8b5312cb30253a512254e173e111 127.0.0.1:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 029a142c9499b16a81c125ae5e39af0104e6795a 127.0.0.1:7005
slots: (0 slots) slave
replicates 5065f634980cec0c6e8934a37b1a81010094915e
S: 57c1f5983c51b1ce0633be325227206e755a2a6c 127.0.0.1:7003
slots: (0 slots) slave
replicates 0031fb0ea57c8b5312cb30253a512254e173e111
S: 94a987da25c05560a7ace853d0001d4cb9e0c2fd 127.0.0.1:7001
slots: (0 slots) slave
replicates d4c10faf735b8d36f8c4ce4cb065640940f2e7ac
M: ad210e2941786d75222486299caa6ee33c8c8a7c 127.0.0.1:7006
slots: (0 slots) master
1 additional replica(s)
S: c614feeff585b44716db6e38eb02b2374ed35500 127.0.0.1:7007
slots: (0 slots) slave
replicates ad210e2941786d75222486299caa6ee33c8c8a7c
M: 5065f634980cec0c6e8934a37b1a81010094915e 127.0.0.1:7002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 127.0.0.1:7004
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.
How many slots do you want to move (from 1 to 16384)? 4096
What is the receiving node ID? ad210e2941786d75222486299caa6ee33c8c8a7c
Please enter all the source node IDs.
Type 'all' to use all the nodes as source nodes for the hash slots.
Type 'done' once you entered all the source nodes IDs.
Source node #1: all
Ready to move 4096 slots.
Source nodes:
M: 0031fb0ea57c8b5312cb30253a512254e173e111 127.0.0.1:7000
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 5065f634980cec0c6e8934a37b1a81010094915e 127.0.0.1:7002
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 127.0.0.1:7004
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
Destination node:
M: ad210e2941786d75222486299caa6ee33c8c8a7c 127.0.0.1:7006
slots: (0 slots) master
1 additional replica(s)
Resharding plan:
Moving slot 5461 from d4c10faf735b8d36f8c4ce4cb065640940f2e7ac
Moving slot 5462 from d4c10faf735b8d36f8c4ce4cb065640940f2e7ac
...
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
分配后的集群情况
127.0.0.1:7000> cluster nodes
029a142c9499b16a81c125ae5e39af0104e6795a 127.0.0.1:7005@17005 slave 5065f634980cec0c6e8934a37b1a81010094915e 0 1628664705503 3 connected
57c1f5983c51b1ce0633be325227206e755a2a6c 127.0.0.1:7003@17003 slave 0031fb0ea57c8b5312cb30253a512254e173e111 0 1628664706000 1 connected
94a987da25c05560a7ace853d0001d4cb9e0c2fd 127.0.0.1:7001@17001 slave d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 0 1628664705704 7 connected
ad210e2941786d75222486299caa6ee33c8c8a7c 127.0.0.1:7006@17006 master - 0 1628664707121 8 connected 0-1364 5461-6826 10923-12287
c614feeff585b44716db6e38eb02b2374ed35500 127.0.0.1:7007@17007 slave ad210e2941786d75222486299caa6ee33c8c8a7c 0 1628664706508 8 connected
0031fb0ea57c8b5312cb30253a512254e173e111 127.0.0.1:7000@17000 myself,master - 0 1628664706000 1 connected 1365-5460
5065f634980cec0c6e8934a37b1a81010094915e 127.0.0.1:7002@17002 master - 0 1628664705503 3 connected 12288-16383
d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 127.0.0.1:7004@17004 master - 0 1628664706719 7 connected 6827-10922
2
3
4
5
6
7
8
9
# 7.3. 动态缩容
缩容前:
127.0.0.1:7000> cluster nodes
029a142c9499b16a81c125ae5e39af0104e6795a 127.0.0.1:7005@17005 slave 5065f634980cec0c6e8934a37b1a81010094915e 0 1628665018313 3 connected
57c1f5983c51b1ce0633be325227206e755a2a6c 127.0.0.1:7003@17003 slave 0031fb0ea57c8b5312cb30253a512254e173e111 0 1628665019530 1 connected
94a987da25c05560a7ace853d0001d4cb9e0c2fd 127.0.0.1:7001@17001 slave d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 0 1628665019530 7 connected
ad210e2941786d75222486299caa6ee33c8c8a7c 127.0.0.1:7006@17006 master - 0 1628665019831 8 connected 0-1364 5461-6826 10923-12287
c614feeff585b44716db6e38eb02b2374ed35500 127.0.0.1:7007@17007 slave ad210e2941786d75222486299caa6ee33c8c8a7c 0 1628665019000 8 connected
0031fb0ea57c8b5312cb30253a512254e173e111 127.0.0.1:7000@17000 myself,master - 0 1628665019000 1 connected 1365-5460
5065f634980cec0c6e8934a37b1a81010094915e 127.0.0.1:7002@17002 master - 0 1628665018000 3 connected 12288-16383
d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 127.0.0.1:7004@17004 master - 0 1628665019328 7 connected 6827-10922
2
3
4
5
6
7
8
9
将 7006
的slot给了 7000
。
redis-cli --cluster reshard 127.0.0.1:7000 --cluster-from ad210e2941786d75222486299caa6ee33c8c8a7c --cluster-to 0031fb0ea57c8b5312cb30253a512254e173e111 -a 123456
缩容后:
029a142c9499b16a81c125ae5e39af0104e6795a 127.0.0.1:7005@17005 slave 5065f634980cec0c6e8934a37b1a81010094915e 0 1628665353826 3 connected
57c1f5983c51b1ce0633be325227206e755a2a6c 127.0.0.1:7003@17003 slave 0031fb0ea57c8b5312cb30253a512254e173e111 0 1628665353021 9 connected
94a987da25c05560a7ace853d0001d4cb9e0c2fd 127.0.0.1:7001@17001 slave d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 0 1628665352518 7 connected
ad210e2941786d75222486299caa6ee33c8c8a7c 127.0.0.1:7006@17006 master - 0 1628665354328 8 connected
c614feeff585b44716db6e38eb02b2374ed35500 127.0.0.1:7007@17007 slave 0031fb0ea57c8b5312cb30253a512254e173e111 0 1628665353325 9 connected
0031fb0ea57c8b5312cb30253a512254e173e111 127.0.0.1:7000@17000 myself,master - 0 1628665352000 9 connected 0-6826 10923-12287
5065f634980cec0c6e8934a37b1a81010094915e 127.0.0.1:7002@17002 master - 0 1628665352317 3 connected 12288-16383
d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 127.0.0.1:7004@17004 master - 0 1628665353000 7 connected 6827-10922
2
3
4
5
6
7
8
# 7.4 删除节点
redis-cli -a 123456 --cluster del-node 127.0.0.1:7000 `<node-id>`
[root@zjwoo bin]# redis-cli -a 123456 --cluster del-node 127.0.0.1:7000 ad210e2941786d75222486299caa6ee33c8c8a7c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node ad210e2941786d75222486299caa6ee33c8c8a7c from cluster 127.0.0.1:7000
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
2
3
4
5
删除了 7006
,7007
后的集群情况
127.0.0.1:7000> cluster nodes
029a142c9499b16a81c125ae5e39af0104e6795a 127.0.0.1:7005@17005 slave 5065f634980cec0c6e8934a37b1a81010094915e 0 1628665471045 3 connected
57c1f5983c51b1ce0633be325227206e755a2a6c 127.0.0.1:7003@17003 slave 0031fb0ea57c8b5312cb30253a512254e173e111 0 1628665472000 9 connected
94a987da25c05560a7ace853d0001d4cb9e0c2fd 127.0.0.1:7001@17001 slave d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 0 1628665470542 7 connected
0031fb0ea57c8b5312cb30253a512254e173e111 127.0.0.1:7000@17000 myself,master - 0 1628665469000 9 connected 0-6826 10923-12287
5065f634980cec0c6e8934a37b1a81010094915e 127.0.0.1:7002@17002 master - 0 1628665471347 3 connected 12288-16383
d4c10faf735b8d36f8c4ce4cb065640940f2e7ac 127.0.0.1:7004@17004 master - 0 1628665472358 7 connected 6827-10922
2
3
4
5
6
7
# 来源
← Stream RediSearch →