集群节点信息
192.168.222.131:46379 主要节点1
192.168.222.131:46380 从节点1
192.168.222.131:46381 从节点2192.168.222.132:46379 主要节点2
192.168.222.132:46380 从节点1
192.168.222.132:46381 从节点2192.168.222.133:46379 主要节点3
192.168.222.133:46380 从节点1
192.168.222.133:46381 从节点2
集群节点配置
集群主节点配置
[ root@localhost redis ]
port 46379
cluster-enabled yes
cluster-config-file nodes-46379.conf
cluster-node-timeout 5000
requirepass F3EE4Dk44ddDSLKL
masterauth F3EE4Dk44ddDSLKL
daemonize yes
appendonly yes
appendfilename "appendonly.aof"
dbfilename dump-46379.rdb
dir /data/redis /46379
logfile /data/redis /46379/redis .log
集群从节点1 配置
[ root@localhost redis ]
port 46380
cluster-enabled yes
cluster-config-file nodes-46380.conf
cluster-node-timeout 5000
requirepass F3EE4Dk44ddDSLKL
masterauth F3EE4Dk44ddDSLKL
daemonize yes
appendonly yes
appendfilename "appendonly.aof"
dbfilename dump-46380.rdb
dir /data/redis /46380
logfile /data/redis /46380/redis .log
集群从节点2 配置
[ root@localhost redis ]
port 46381
cluster-enabled yes
cluster-config-file nodes-46381.conf
cluster-node-timeout 5000
requirepass F3EE4Dk44ddDSLKL
masterauth F3EE4Dk44ddDSLKL
daemonize yes
appendonly yes
appendfilename "appendonly.aof"
dbfilename dump-46381.rdb
dir /data/redis /46381
logfile /data/redis /46381/redis .log
节点启动命令
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -server /data/redis /redis -46379.conf
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -server /data/redis /redis -46380.conf
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -server /data/redis /redis -46381.conf
节点停止命令
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -p 46379 -a F3EE4Dk44ddDSLKL shutdown
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -p 46380 -a F3EE4Dk44ddDSLKL shutdown
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -p 46381 -a F3EE4Dk44ddDSLKL shutdown
集群构建
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster create \
192.168 .222.131:46379 192.168 .222.131:46380 192.168 .222.131:46381 \
192.168 .222.132:46379 192.168 .222.132:46380 192.168 .222.132:46381 \
192.168 .222.133:46379 192.168 .222.133:46380 192.168 .222.133:46381 --cluster-replicas 2
检查集群节点信息
[ root@localhost ~]
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
a2b16bfa39fafe9b536ac6c952ae2dec58a8e35e 192.168 .222.133:46379@56379 master - 0 1732698781824 7 connected 10923 -16383
c6927c5de96959a24d04defe1227ca338089219e 192.168 .222.131:46379@56379 myself,master - 0 1732698781000 1 connected 0 -5460
964f27d3abd421ce99d6814cbc33f7efc3bd473d 192.168 .222.132:46379@56379 master - 0 1732698779513 4 connected 5461 -10922
4870524e9a47506996015387ea67e6491dd3adac 192.168 .222.131:46380@56380 slave a2b16bfa39fafe9b536ac6c952ae2dec58a8e35e 0 1732698781522 7 connected
4891c05297daa8e310ac616d159b444d54ba7b51 192.168 .222.132:46381@56381 slave a2b16bfa39fafe9b536ac6c952ae2dec58a8e35e 0 1732698780517 7 connected
0b88544c661dafef8f79c5204a5f6d7a8abde20b 192.168 .222.133:46380@56380 slave c6927c5de96959a24d04defe1227ca338089219e 0 1732698780819 1 connected
bf5549f442b6c096efda3c97472813fdfd1338f3 192.168 .222.132:46380@56380 slave c6927c5de96959a24d04defe1227ca338089219e 0 1732698780000 1 connected
b4e716e733cfbe3b61130981fe479f9b38ba2f1e 192.168 .222.131:46381@56381 slave 964f27d3abd421ce99d6814cbc33f7efc3bd473d 0 1732698780517 4 connected
504cbbeb8a73512127a1566ac91f846145b11a58 192.168 .222.133:46381@56381 slave 964f27d3abd421ce99d6814cbc33f7efc3bd473d 0 1732698781000 4 connected
数据写入测试
from redis cluster import RedisCluster
startup_nodes = [ { "host" : "192.168.222.131" , "port" : "46379" } ]
rc = RedisCluster( startup_nodes= startup_nodes, decode_responses= True , password= 'F3EE4Dk44ddDSLKL' )
for i in range ( 100 ) : data_size_kb = 1 data = 'kjasjkldfjalsdflasdflasdfjllkasjdflasf' * ( data_size_kb * 1024 * 1024 ) rc. set ( f'key: { i} ' , data) print ( f'Written key: { i} with { data_size_kb} KB of data' )
print ( "Data written." )
主机内存查看
[ root@localhost ~] total used free shared buff/cache available
Mem: 7 .8G 2 .2G 5 .5G 11M 120M 5 .4G
Swap: 3 .6G 0B 3 .6G
节点信息查询
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL -c -p 46379 -h 192.168 .222.131
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL -p 46379 -h 192.168 .222.131 --stat
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL -p 46379 -h 192.168 .222.131 INFO
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL -p 46379 -h 192.168 .222.131 CLUSTER INFO
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL -p 46379 -h 192.168 .222.131 CLUSTER NODES
新添加集群节点信息
192.168.222.134:46379
192.168.222.134:46380
192.168.222.134:46381192.168.222.135:46379
192.168.222.135:46380
192.168.222.135:46381192.168.222.136:46379
192.168.222.136:46380
192.168.222.136:46381
添加新节点到集群
将新节点加入集群
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster add-node 192.168 .222.134:46379 192.168 .222.131:46379
[ root@localhost ~]
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
7e4645f509d071be565d53f7167464456f869094 192.168 .222.134:46379@56379 master - 0 1732699138000 0 connected
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster add-node 192.168 .222.134:46380 192.168 .222.131:46379 --cluster-slave --cluster-master-id 7e4645f509d071be565d53f7167464456f869094
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster add-node 192.168 .222.134:46381 192.168 .222.131:46379 --cluster-slave --cluster-master-id 7e4645f509d071be565d53f7167464456f869094
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster add-node 192.168 .222.135:46379 192.168 .222.131:46379
[ root@localhost ~]
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
68520244e77a3a69122816496f5c9773b50e5277 192.168 .222.135:46379@56379 master - 0 1732699237110 11 connected
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster add-node 192.168 .222.135:46380 192.168 .222.131:46379 --cluster-slave --cluster-master-id 68520244e77a3a69122816496f5c9773b50e5277
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster add-node 192.168 .222.135:46381 192.168 .222.131:46379 --cluster-slave --cluster-master-id 68520244e77a3a69122816496f5c9773b50e5277
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster add-node 192.168 .222.136:46379 192.168 .222.131:46379
[ root@localhost ~]
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
fa79672847de00a6590a0ebb2ee7d93b5ca11c82 192.168 .222.136:46379@56379 master - 0 1732699305971 0 connected
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster add-node 192.168 .222.136:46380 192.168 .222.131:46379 --cluster-slave --cluster-master-id fa79672847de00a6590a0ebb2ee7d93b5ca11c82
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster add-node 192.168 .222.136:46381 192.168 .222.131:46379 --cluster-slave --cluster-master-id fa79672847de00a6590a0ebb2ee7d93b5ca11c82
检查集群节点信息
[ root@localhost ~]
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
849bcb37b280614a2314a989114f204546f17d7d 192.168 .222.134:46380@56380 slave 7e4645f509d071be565d53f7167464456f869094 0 1732699378000 12 connected
fa79672847de00a6590a0ebb2ee7d93b5ca11c82 192.168 .222.136:46379@56379 master - 0 1732699377568 0 connected
c530f5086b3c5da80e3d8351d5572880f74a8761 192.168 .222.136:46380@56380 slave fa79672847de00a6590a0ebb2ee7d93b5ca11c82 0 1732699377000 0 connected
bf534fe091be5239c268d0aa90a4fb937c493f72 192.168 .222.136:46381@56381 slave fa79672847de00a6590a0ebb2ee7d93b5ca11c82 0 1732699377000 0 connected
964f27d3abd421ce99d6814cbc33f7efc3bd473d 192.168 .222.132:46379@56379 master - 0 1732699378071 4 connected 5461 -10922
bbb3838d05683746ff5a848a0b4ef08fee49c1fb 192.168 .222.135:46381@56381 slave 68520244e77a3a69122816496f5c9773b50e5277 0 1732699377065 11 connected
4ea5c56c01a07f9cec7d6ba2c7b7e3b2a0743642 192.168 .222.134:46381@56381 slave 7e4645f509d071be565d53f7167464456f869094 0 1732699377000 12 connected
bf5549f442b6c096efda3c97472813fdfd1338f3 192.168 .222.132:46380@56380 slave c6927c5de96959a24d04defe1227ca338089219e 0 1732699378372 1 connected
68520244e77a3a69122816496f5c9773b50e5277 192.168 .222.135:46379@56379 master - 0 1732699378000 11 connected
a2b16bfa39fafe9b536ac6c952ae2dec58a8e35e 192.168 .222.133:46379@56379 master - 0 1732699377568 7 connected 10923 -16383
c6927c5de96959a24d04defe1227ca338089219e 192.168 .222.131:46379@56379 myself,master - 0 1732699377000 1 connected 0 -5460
4891c05297daa8e310ac616d159b444d54ba7b51 192.168 .222.132:46381@56381 slave a2b16bfa39fafe9b536ac6c952ae2dec58a8e35e 0 1732699378575 7 connected
4870524e9a47506996015387ea67e6491dd3adac 192.168 .222.131:46380@56380 slave a2b16bfa39fafe9b536ac6c952ae2dec58a8e35e 0 1732699378574 7 connected
0b88544c661dafef8f79c5204a5f6d7a8abde20b 192.168 .222.133:46380@56380 slave c6927c5de96959a24d04defe1227ca338089219e 0 1732699377568 1 connected
7e4645f509d071be565d53f7167464456f869094 192.168 .222.134:46379@56379 master - 0 1732699378575 12 connected
b4e716e733cfbe3b61130981fe479f9b38ba2f1e 192.168 .222.131:46381@56381 slave 964f27d3abd421ce99d6814cbc33f7efc3bd473d 0 1732699377366 4 connected
504cbbeb8a73512127a1566ac91f846145b11a58 192.168 .222.133:46381@56381 slave 964f27d3abd421ce99d6814cbc33f7efc3bd473d 0 1732699377568 4 connected
9343f98f07ee8a0f442bdfd5197ee13c40e5e203 192.168 .222.135:46380@56380 slave 68520244e77a3a69122816496f5c9773b50e5277 0 1732699378575 11 connected
槽位重新分配
a2b16bfa39fafe9b536ac6c952ae2dec58a8e35e 192.168.222.133:46379@56379 master - 0 1732699377568 7 connected 10923-16383
一共是0-16383 个槽位,平均分布在3个节点上,我现在需要平局分布在6个节点上,每个节点大概是2730个槽位,也可以把现有主节点槽位移动一半到没有槽位的主节点上
迁移到哪里去
7e4645f509d071be565d53f7167464456f869094 192.168 .222.134:46379@56379 master - 0 1732699378575 12 connected
68520244e77a3a69122816496f5c9773b50e5277 192.168 .222.135:46379@56379 master - 0 1732699378000 11 connected
fa79672847de00a6590a0ebb2ee7d93b5ca11c82 192.168 .222.136:46379@56379 master - 0 1732699377568 0 connected从哪里迁移
c6927c5de96959a24d04defe1227ca338089219e 192.168 .222.131:46379@56379 myself,master - 0 1732699377000 1 connected 0 -5460
964f27d3abd421ce99d6814cbc33f7efc3bd473d 192.168 .222.132:46379@56379 master - 0 1732699378071 4 connected 5461 -10922
a2b16bfa39fafe9b536ac6c952ae2dec58a8e35e 192.168 .222.133:46379@56379 master - 0 1732699377568 7 connected 10923 -16383迁移的命令
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster reshard 192.168 .222.131:46379
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster reshard 192.168 .222.132:46379
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster reshard 192.168 .222.133:46379fa79672847de00a6590a0ebb2ee7d93b5ca11c82 192.168 .222.136:46379@56379 master - 0 1732700418532 0 connected
b4e716e733cfbe3b61130981fe479f9b38ba2f1e 192.168 .222.131:46381@56381 master - 0 1732700419538 19 connected 5461 -10922
槽位置迁移示范
[ root@localhost ~]
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 .222.131:46379)
M: c6927c5de96959a24d04defe1227ca338089219e 192.168 .222.131:46379slots:[ 0 -5460] ( 5461 slots) master2 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 ) ? 2730
What is the receiving node ID? 7e4645f509d071be565d53f7167464456f869094
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
Source node
.. .. .. .. .. .
Moving slot 2729 from c6927c5de96959a24d04defe1227ca338089219e
Do you want to proceed with the proposed reshard plan ( yes/no) ? yes
.. .. .. .. .. .
Moving slot 2729 from 192.168 .222.131:46379 to 192.168 .222.134:46379:
检查集群节点信息
[ root@localhost ~]
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
849bcb37b280614a2314a989114f204546f17d7d 192.168 .222.134:46380@56380 slave 7e4645f509d071be565d53f7167464456f869094 0 1732700757000 25 connected
fa79672847de00a6590a0ebb2ee7d93b5ca11c82 192.168 .222.136:46379@56379 master - 0 1732700756000 27 connected 5461 -8190
c530f5086b3c5da80e3d8351d5572880f74a8761 192.168 .222.136:46380@56380 slave fa79672847de00a6590a0ebb2ee7d93b5ca11c82 0 1732700757583 27 connected
bf534fe091be5239c268d0aa90a4fb937c493f72 192.168 .222.136:46381@56381 slave fa79672847de00a6590a0ebb2ee7d93b5ca11c82 0 1732700758085 27 connected
964f27d3abd421ce99d6814cbc33f7efc3bd473d 192.168 .222.132:46379@56379 slave b4e716e733cfbe3b61130981fe479f9b38ba2f1e 0 1732700757482 19 connected
bbb3838d05683746ff5a848a0b4ef08fee49c1fb 192.168 .222.135:46381@56381 slave 68520244e77a3a69122816496f5c9773b50e5277 0 1732700757000 26 connected
4ea5c56c01a07f9cec7d6ba2c7b7e3b2a0743642 192.168 .222.134:46381@56381 slave 7e4645f509d071be565d53f7167464456f869094 0 1732700756000 25 connected
bf5549f442b6c096efda3c97472813fdfd1338f3 192.168 .222.132:46380@56380 slave c6927c5de96959a24d04defe1227ca338089219e 0 1732700757583 1 connected
68520244e77a3a69122816496f5c9773b50e5277 192.168 .222.135:46379@56379 master - 0 1732700756476 26 connected 10923 -13652
a2b16bfa39fafe9b536ac6c952ae2dec58a8e35e 192.168 .222.133:46379@56379 slave 4870524e9a47506996015387ea67e6491dd3adac 0 1732700758085 17 connected
c6927c5de96959a24d04defe1227ca338089219e 192.168 .222.131:46379@56379 myself,master - 0 1732700757000 1 connected 2730 -5460
4891c05297daa8e310ac616d159b444d54ba7b51 192.168 .222.132:46381@56381 slave 4870524e9a47506996015387ea67e6491dd3adac 0 1732700758085 17 connected
4870524e9a47506996015387ea67e6491dd3adac 192.168 .222.131:46380@56380 master - 0 1732700757079 17 connected 13653 -16383
0b88544c661dafef8f79c5204a5f6d7a8abde20b 192.168 .222.133:46380@56380 slave c6927c5de96959a24d04defe1227ca338089219e 0 1732700756000 1 connected
7e4645f509d071be565d53f7167464456f869094 192.168 .222.134:46379@56379 master - 0 1732700756000 25 connected 0 -2729
b4e716e733cfbe3b61130981fe479f9b38ba2f1e 192.168 .222.131:46381@56381 master - 0 1732700758085 19 connected 8191 -10922
504cbbeb8a73512127a1566ac91f846145b11a58 192.168 .222.133:46381@56381 slave b4e716e733cfbe3b61130981fe479f9b38ba2f1e 0 1732700757000 19 connected
9343f98f07ee8a0f442bdfd5197ee13c40e5e203 192.168 .222.135:46380@56380 slave 68520244e77a3a69122816496f5c9773b50e5277 0 1732700756000 26 connected
查看节点内存使用信息
[ root@localhost ~] total used free shared buff/cache available
Mem: 7 .8G 1 .2G 6 .5G 11M 121M 6 .4G
Swap: 3 .6G 0B 3 .6G
[ root@localhost ~] total used free shared buff/cache available
Mem: 7 .8G 1 .2G 6 .5G 11M 112M 6 .4G
Swap: 3 .6G 0B 3 .6G
集群各种操作命令
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL -c -p 46379 -h 192.168 .222.131
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL -p 46379 -h 192.168 .222.131 --stat
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL -p 46379 -h 192.168 .222.131 INFO
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL -p 46379 -h 192.168 .222.131 CLUSTER INFO
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL -p 46379 -h 192.168 .222.131 CLUSTER NODES
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL -p 46379 -h 192.168 .222.131 CONFIG SET < 配置项> < 值>
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster add-node < 新节点IP> :< 新节点端口> < 已存在节点IP> :< 已存在节点端口>
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster del-node < 已存在节点IP> :< 已存在节点端口> < 节点ID>
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster del-node 192.168 .222.134:46379 05ddef40e900862d61ea895568dc0b65b562eb99
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster del-node 192.168 .222.134:46380 1f02da1cd8339ae877f50ab7b24a46b2de0a4c2d
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster del-node 192.168 .222.134:46381 eb0eb008856d63c7ae7671f70050939aac36a64f
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL --cluster fix < 任意节点IP> :< 任意节点端口>
/data/redis /redis -stack-server-7.2.0-v8/bin/redis -cli -a F3EE4Dk44ddDSLKL -p 46379 -h 192.168 .222.131 BGSAVE
cp /path/to/your/backup.rdb /var/lib/redis /dump.rdb
/data/redis /bin/redis -server /etc/redis /redis .conf查看日志:
Redis 日志文件的位置根据你的安装和配置可能不同,但通常你可以这样查看:
tail -f /var/log/redis /redis -server.log