集群规划
节点规划
redis-01 192.168.174.108
redis-02 192.168.174.112
redis-03 192.168.174.117
redis-04 192.168.174.114
redis-05 192.168.174.115
redis-06 192.168.174.116
redis-07 192.168.174.118
redis-08 192.168.174.119
redis 服务配置
redis.conf 配置
sed -i -e 's@port 6379@port 26379@' -e 's@^pidfile .*@pidfile /data/apps/redis/redis.pid@' -e 's@^logfile .*@logfile /data/apps/redis/logs/redis.log@' -e 's@dir ./@dir /data/apps/redis/data@' -e 's/bind 127.0.0.1/bind 0.0.0.0/' -e 's/# cluster-enabled yes/cluster-enabled yes/' -e 's@# cluster-config-file nodes-6379.conf@cluster-config-file /usr/local/redis/etc/nodes-6379.conf@' -e 's/# cluster-node-timeout 15000/cluster-node-timeout 5000/' -e 's/# cluster-require-full-coverage yes/cluster-require-full-coverage no/' -e 's/appendonly no/appendonly yes/' -e '/# requirepass /a requirepass 123456' -e '/masterauth/a masterauth 123456' /usr/local/redis/etc/redis.conf
systemctl restart redis-server
验证 redis 服务状态
tcp 0 0 0.0.0.0:36379 0.0.0.0:* LISTEN 7826/redis-server 0
tcp 0 0 0.0.0.0:26379 0.0.0.0:* LISTEN 7826/redis-server 0
tcp6 0 0 ::1:26379 :::* LISTEN 7826/redis-server 0
tcp6 0 0 ::1:36379 :::* LISTEN 7826/redis-server 0
创建 redis 集群
#命令redis-c1i的选项 --cluster-replicas 1 表示每个master对应一个s1ave节点,注意:所有节点数据必须清空.
选择一个节点执行。
redis-cli -a 123456 --cluster create 192.168.174.108:26379 192.168.174.112:26379 192.168.174.114:26379 192.168.174.115:26379 192.168.174.116:26379 192.168.174.117:26379 --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.174.116:26379 to 192.168.174.108:26379
Adding replica 192.168.174.117:26379 to 192.168.174.112:26379
Adding replica 192.168.174.115:26379 to 192.168.174.114:26379
M: 2c2f409a5893ed9c6087d705126f8df80a746841 192.168.174.108:26379 # master 节点
slots:[0-5460] (5461 slots) master # 槽位起始
M: 1538d933904b5093bfb92f4ef8df33d218cee3a8 192.168.174.112:26379
slots:[5461-10922] (5462 slots) master
M: 53eadf87db7fd4efa4a14924eb80178f6e92c5e5 192.168.174.114:26379
slots:[10923-16383] (5461 slots) master
S: c95a6697886121a6743e50d41a84512e496bc663 192.168.174.115:26379 # slave 节点
replicates 53eadf87db7fd4efa4a14924eb80178f6e92c5e5
S: c807ec73ea369b4dd88f9a6e16cd3abb8b01732b 192.168.174.116:26379
replicates 2c2f409a5893ed9c6087d705126f8df80a746841
S: 4fdec8f3a6f42aee6c892fb175335c746da7f749 192.168.174.117:26379
replicates 1538d933904b5093bfb92f4ef8df33d218cee3a8
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.174.108:26379)
M: 2c2f409a5893ed9c6087d705126f8df80a746841 192.168.174.108:26379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 53eadf87db7fd4efa4a14924eb80178f6e92c5e5 192.168.174.114:26379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 1538d933904b5093bfb92f4ef8df33d218cee3a8 192.168.174.112:26379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: c807ec73ea369b4dd88f9a6e16cd3abb8b01732b 192.168.174.116:26379
slots: (0 slots) slave
replicates 2c2f409a5893ed9c6087d705126f8df80a746841 # 对于 master 节点信息
S: 4fdec8f3a6f42aee6c892fb175335c746da7f749 192.168.174.117:26379
slots: (0 slots) slave
replicates 1538d933904b5093bfb92f4ef8df33d218cee3a8
S: c95a6697886121a6743e50d41a84512e496bc663 192.168.174.115:26379
slots: (0 slots) slave
replicates 53eadf87db7fd4efa4a14924eb80178f6e92c5e5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
验证 redis 集群
验证 redis 主从状态
/usr/local/redis/bin/redis-cli -p 26379 -a 123456 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:1
slave0:ip=192.168.174.117,port=26379,state=online,offset=392,lag=1
master_failover_state:no-failover
master_replid:25eaf79068f713d372537ecedd96c66c94c61750
master_replid2:0000000000000000000000000000000000000000
master_repl_offset:392
second_repl_offset:-1
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:392
查看 redis 集群对应关系
/usr/local/redis/bin/redis-cli -p 26379 -a 123456 cluster nodes
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
4fdec8f3a6f42aee6c892fb175335c746da7f749 192.168.174.117:26379@36379 slave 1538d933904b5093bfb92f4ef8df33d218cee3a8 0 1734515192571 2 connected
c95a6697886121a6743e50d41a84512e496bc663 192.168.174.115:26379@36379 slave 53eadf87db7fd4efa4a14924eb80178f6e92c5e5 0 1734515191159 3 connected
53eadf87db7fd4efa4a14924eb80178f6e92c5e5 192.168.174.114:26379@36379 master - 0 1734515191000 3 connected 10923-16383
1538d933904b5093bfb92f4ef8df33d218cee3a8 192.168.174.112:26379@36379 myself,master - 0 0 2 connected 5461-10922
2c2f409a5893ed9c6087d705126f8df80a746841 192.168.174.108:26379@36379 master - 0 1734515192067 1 connected 0-5460
c807ec73ea369b4dd88f9a6e16cd3abb8b01732b 192.168.174.116:26379@36379 slave 2c2f409a5893ed9c6087d705126f8df80a746841 0 1734515192169 1 connected
查看 redis 集群状态
/usr/local/redis/bin/redis-cli -p 26379 -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:2
cluster_stats_messages_ping_sent:1254
cluster_stats_messages_pong_sent:1194
cluster_stats_messages_meet_sent:1
cluster_stats_messages_sent:2449
cluster_stats_messages_ping_received:1194
cluster_stats_messages_pong_received:1255
cluster_stats_messages_received:2449
total_cluster_links_buffer_limit_exceeded:0
查看节点状态
/usr/local/redis/bin/redis-cli -p 26379 -a 123456 --cluster info 192.168.174.114:26379
192.168.174.114:26379 (53eadf87...) -> 0 keys | 5461 slots | 1 slaves.
192.168.174.112:26379 (1538d933...) -> 0 keys | 5462 slots | 1 slaves.
192.168.174.108:26379 (2c2f409a...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
检查节点状态
/usr/local/redis/bin/redis-cli -p 26379 -a 123456 --cluster check 192.168.174.114:26379
192.168.174.114:26379 (53eadf87...) -> 0 keys | 5461 slots | 1 slaves.
192.168.174.112:26379 (1538d933...) -> 0 keys | 5462 slots | 1 slaves.
192.168.174.108:26379 (2c2f409a...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 0 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.174.114:26379)
M: 53eadf87db7fd4efa4a14924eb80178f6e92c5e5 192.168.174.114:26379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: c807ec73ea369b4dd88f9a6e16cd3abb8b01732b 192.168.174.116:26379
slots: (0 slots) slave
replicates 2c2f409a5893ed9c6087d705126f8df80a746841
S: 4fdec8f3a6f42aee6c892fb175335c746da7f749 192.168.174.117:26379
slots: (0 slots) slave
replicates 1538d933904b5093bfb92f4ef8df33d218cee3a8
S: c95a6697886121a6743e50d41a84512e496bc663 192.168.174.115:26379
slots: (0 slots) slave
replicates 53eadf87db7fd4efa4a14924eb80178f6e92c5e5
M: 1538d933904b5093bfb92f4ef8df33d218cee3a8 192.168.174.112:26379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: 2c2f409a5893ed9c6087d705126f8df80a746841 192.168.174.108:26379
slots:[0-5460] (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.
测试 redis 集群写入数据
写入数据测试
/usr/local/redis/bin/redis-cli -p 26379 -a 123456 --no-auth-warning -h 192.168.174.108 set test-key1 test1
(error) MOVED 10183 192.168.174.112:26379
槽位不对,无法写入
/usr/local/redis/bin/redis-cli -p 26379 -a 123456 --no-auth-warning -h 192.168.174.112 set test-key1 test1
OK
计算 key 所在的slot
/usr/local/redis/bin/redis-cli -p 26379 -a 123456 --no-auth-warning -h 192.168.174.112 cluster keyslot helloworld
(integer) 2739
写入数据
/usr/local/redis/bin/redis-cli -p 26379 -a 123456 --no-auth-warning -h 192.168.174.108 set helloworld 121
OK
redis 集群模式连接
/usr/local/redis/bin/redis-cli -c -p 26379 -a 123456 --no-auth-warning -h 192.168.174.108 set test-key1 test1
OK
/usr/local/redis/bin/redis-cli -c -p 26379 -a 123456 --no-auth-warning -h 192.168.174.108 get test-key1
"test1"
redis 集群故障转移
确认 master 节点
/usr/local/redis/bin/redis-cli -c -p 26379 -a 123456 --no-auth-warning -h 192.168.174.108 --cluster info 192.168.174.108
192.168.174.108:26379 (2c2f409a...) -> 2 keys | 5461 slots | 1 slaves.
192.168.174.112:26379 (1538d933...) -> 1 keys | 5462 slots | 1 slaves.
192.168.174.114:26379 (53eadf87...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 3 keys in 3 masters.
0.00 keys per slot on average.
停止 114 节点 redis 服务
systemctl stop redis-server
验证 redis 集群故障切换
/usr/local/redis/bin/redis-cli -c -p 26379 -a 123456 --no-auth-warning -h 192.168.174.108 --cluster info 192.168.174.108:26379
Could not connect to Redis at 192.168.174.114:26379: Connection refused
192.168.174.108:26379 (2c2f409a...) -> 2 keys | 5461 slots | 1 slaves.
192.168.174.112:26379 (1538d933...) -> 1 keys | 5462 slots | 1 slaves.
192.168.174.115:26379 (c95a6697...) -> 0 keys | 5461 slots | 0 slaves. # 故障切换
[OK] 3 keys in 3 masters.
0.00 keys per slot on average.
验证 115 节点角色
/usr/local/redis/bin/redis-cli -c -p 26379 -a 123456 --no-auth-warning -h 192.168.174.115 info replication
# Replication
role:master
connected_slaves:1
slave0:ip=192.168.174.114,port=26379,state=online,offset=2380,lag=0
master_failover_state:no-failover
master_replid:3a388d50efa50a9aed772ae06cbb18744e99ebc5
master_replid2:5cd401c39e7fa5a24dd20a32d2e06a73a7d217dc
master_repl_offset:2380
second_repl_offset:2157
repl_backlog_active:1
repl_backlog_size:1048576
repl_backlog_first_byte_offset:1
repl_backlog_histlen:2380
启用 114 节点 redis 服务
systemctl start redis-server
验证 redis 集群
/usr/local/redis/bin/redis-cli -c -p 26379 -a 123456 --no-auth-warning -h 192.168.174.108 --cluster check 192.168.174.108:26379
192.168.174.108:26379 (2c2f409a...) -> 2 keys | 5461 slots | 1 slaves.
192.168.174.112:26379 (1538d933...) -> 1 keys | 5462 slots | 1 slaves.
192.168.174.115:26379 (c95a6697...) -> 0 keys | 5461 slots | 1 slaves.
[OK] 3 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.174.108:26379)
M: 2c2f409a5893ed9c6087d705126f8df80a746841 192.168.174.108:26379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 1538d933904b5093bfb92f4ef8df33d218cee3a8 192.168.174.112:26379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: c807ec73ea369b4dd88f9a6e16cd3abb8b01732b 192.168.174.116:26379
slots: (0 slots) slave
replicates 2c2f409a5893ed9c6087d705126f8df80a746841
S: 4fdec8f3a6f42aee6c892fb175335c746da7f749 192.168.174.117:26379
slots: (0 slots) slave
replicates 1538d933904b5093bfb92f4ef8df33d218cee3a8
S: 53eadf87db7fd4efa4a14924eb80178f6e92c5e5 192.168.174.114:26379
slots: (0 slots) slave
replicates c95a6697886121a6743e50d41a84512e496bc663
M: c95a6697886121a6743e50d41a84512e496bc663 192.168.174.115:26379
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.
redis 集群扩容
master 节点数量为奇数个。先新增节点,在分配槽位。
新 master 节点
添加新节点为 master
node07 节点配置要和集群配置,版本保持一致。新增的节点角色为 master .
/usr/local/redis/bin/redis-cli -a 123456 --no-auth-warning --cluster add-node 192.168.174.118:26379 192.168.174.108:26379
>>> Adding node 192.168.174.118:26379 to cluster 192.168.174.108:26379
>>> Performing Cluster Check (using node 192.168.174.108:26379)
M: 2c2f409a5893ed9c6087d705126f8df80a746841 192.168.174.108:26379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 53eadf87db7fd4efa4a14924eb80178f6e92c5e5 192.168.174.114:26379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 1538d933904b5093bfb92f4ef8df33d218cee3a8 192.168.174.112:26379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: c807ec73ea369b4dd88f9a6e16cd3abb8b01732b 192.168.174.116:26379
slots: (0 slots) slave
replicates 2c2f409a5893ed9c6087d705126f8df80a746841
S: 4fdec8f3a6f42aee6c892fb175335c746da7f749 192.168.174.117:26379
slots: (0 slots) slave
replicates 1538d933904b5093bfb92f4ef8df33d218cee3a8
S: c95a6697886121a6743e50d41a84512e496bc663 192.168.174.115:26379
slots: (0 slots) slave
replicates 53eadf87db7fd4efa4a14924eb80178f6e92c5e5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Getting functions from cluster
>>> Send FUNCTION LIST to 192.168.174.118:26379 to verify there is no functions in it
>>> Send FUNCTION RESTORE to 192.168.174.118:26379
>>> Send CLUSTER MEET to node 192.168.174.118:26379 to make it join the cluster.
[OK] New node added correctly.
查看新 master 节点信息
/usr/local/redis/bin/redis-cli -c -p 26379 -a 123456 --no-auth-warning -h 192.168.174.108 --cluster check 192.168.174.108:26379
192.168.174.108:26379 (2c2f409a...) -> 2 keys | 5461 slots | 1 slaves.
192.168.174.118:26379 (a3b2c71e...) -> 0 keys | 0 slots | 0 slaves.
192.168.174.114:26379 (53eadf87...) -> 0 keys | 5461 slots | 1 slaves.
192.168.174.112:26379 (1538d933...) -> 1 keys | 5462 slots | 1 slaves.
[OK] 3 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.174.108:26379)
M: 2c2f409a5893ed9c6087d705126f8df80a746841 192.168.174.108:26379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: a3b2c71e30e81d017ffa8583da175fc8a9e5ff08 192.168.174.118:26379
slots: (0 slots) master # 角色为 master, 没有分配槽位
M: 53eadf87db7fd4efa4a14924eb80178f6e92c5e5 192.168.174.114:26379
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 1538d933904b5093bfb92f4ef8df33d218cee3a8 192.168.174.112:26379
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: c807ec73ea369b4dd88f9a6e16cd3abb8b01732b 192.168.174.116:26379
slots: (0 slots) slave
replicates 2c2f409a5893ed9c6087d705126f8df80a746841
S: 4fdec8f3a6f42aee6c892fb175335c746da7f749 192.168.174.117:26379
slots: (0 slots) slave
replicates 1538d933904b5093bfb92f4ef8df33d218cee3a8
S: c95a6697886121a6743e50d41a84512e496bc663 192.168.174.115:26379
slots: (0 slots) slave
replicates 53eadf87db7fd4efa4a14924eb80178f6e92c5e5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
为新 master 节点分配槽位
/usr/local/redis/bin/redis-cli -a 123456 --no-auth-warning --cluster reshard 192.168.174.108:26379
....
How many slots do you want to move (from 1 to 16384)? # 新分配的槽位=16384/master 个数
what is the receiving node ID? # 新 master ID
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 Ios.
source node #1:a11 #输入a11,将哪些源主机的槽位分配给新的节点,a11是自动在所有的redisnode选择划分,如果是从redis cluster删除某个主机可以使用此方式将指定主机上的槽位全部移动到别的redis主机
...
Do you want to proceed with the proposed reshard plan (yes/no)? yes #确认分配
确认新 mster 分配槽位信息
/usr/local/redis/bin/redis-cli -c -p 26379 -a 123456 --no-auth-warning -h 192.168.174.108 --cluster check 192.168.174.108:26379
192.168.174.108:26379 (2c2f409a...) -> 2 keys | 4096 slots | 1 slaves.
192.168.174.118:26379 (a3b2c71e...) -> 0 keys | 4096 slots | 0 slaves.
192.168.174.114:26379 (53eadf87...) -> 0 keys | 4096 slots | 1 slaves.
192.168.174.112:26379 (1538d933...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 3 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.174.108:26379)
M: 2c2f409a5893ed9c6087d705126f8df80a746841 192.168.174.108:26379
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
M: a3b2c71e30e81d017ffa8583da175fc8a9e5ff08 192.168.174.118:26379
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
M: 53eadf87db7fd4efa4a14924eb80178f6e92c5e5 192.168.174.114:26379
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
M: 1538d933904b5093bfb92f4ef8df33d218cee3a8 192.168.174.112:26379
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: c807ec73ea369b4dd88f9a6e16cd3abb8b01732b 192.168.174.116:26379
slots: (0 slots) slave
replicates 2c2f409a5893ed9c6087d705126f8df80a746841
S: 4fdec8f3a6f42aee6c892fb175335c746da7f749 192.168.174.117:26379
slots: (0 slots) slave
replicates 1538d933904b5093bfb92f4ef8df33d218cee3a8
S: c95a6697886121a6743e50d41a84512e496bc663 192.168.174.115:26379
slots: (0 slots) slave
replicates 53eadf87db7fd4efa4a14924eb80178f6e92c5e5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
新 slave 节点方式一
添加新节点为 slave
/usr/local/redis/bin/redis-cli -a 123456 --no-auth-warning --cluster add-node 192.168.174.119:26379 192.168.174.108:26379 --cluster-slave --cluster-master-id a3b2c71e30e81d017ffa8583da175fc8a9e5ff08
>>> Adding node 192.168.174.119:26379 to cluster 192.168.174.108:26379
>>> Performing Cluster Check (using node 192.168.174.108:26379)
M: 2c2f409a5893ed9c6087d705126f8df80a746841 192.168.174.108:26379
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
M: a3b2c71e30e81d017ffa8583da175fc8a9e5ff08 192.168.174.118:26379
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
M: 53eadf87db7fd4efa4a14924eb80178f6e92c5e5 192.168.174.114:26379
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
M: 1538d933904b5093bfb92f4ef8df33d218cee3a8 192.168.174.112:26379
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: c807ec73ea369b4dd88f9a6e16cd3abb8b01732b 192.168.174.116:26379
slots: (0 slots) slave
replicates 2c2f409a5893ed9c6087d705126f8df80a746841
S: 4fdec8f3a6f42aee6c892fb175335c746da7f749 192.168.174.117:26379
slots: (0 slots) slave
replicates 1538d933904b5093bfb92f4ef8df33d218cee3a8
S: c95a6697886121a6743e50d41a84512e496bc663 192.168.174.115:26379
slots: (0 slots) slave
replicates 53eadf87db7fd4efa4a14924eb80178f6e92c5e5
[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.174.119:26379 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.174.118:26379.
[OK] New node added correctly.
查看新 slave 节点
/usr/local/redis/bin/redis-cli -c -p 26379 -a 123456 --no-auth-warning -h 192.168.174.108 --cluster check 192.168.174.108:26379
192.168.174.108:26379 (2c2f409a...) -> 2 keys | 4096 slots | 1 slaves.
192.168.174.118:26379 (a3b2c71e...) -> 0 keys | 4096 slots | 1 slaves.
192.168.174.114:26379 (53eadf87...) -> 0 keys | 4096 slots | 1 slaves.
192.168.174.112:26379 (1538d933...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 3 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.174.108:26379)
M: 2c2f409a5893ed9c6087d705126f8df80a746841 192.168.174.108:26379
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
M: a3b2c71e30e81d017ffa8583da175fc8a9e5ff08 192.168.174.118:26379
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
1 additional replica(s)
M: 53eadf87db7fd4efa4a14924eb80178f6e92c5e5 192.168.174.114:26379
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: f83dc9acb624c72b75ecd10d6e760b6cd9942e2a 192.168.174.119:26379
slots: (0 slots) slave
replicates a3b2c71e30e81d017ffa8583da175fc8a9e5ff08
M: 1538d933904b5093bfb92f4ef8df33d218cee3a8 192.168.174.112:26379
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: c807ec73ea369b4dd88f9a6e16cd3abb8b01732b 192.168.174.116:26379
slots: (0 slots) slave
replicates 2c2f409a5893ed9c6087d705126f8df80a746841
S: 4fdec8f3a6f42aee6c892fb175335c746da7f749 192.168.174.117:26379
slots: (0 slots) slave
replicates 1538d933904b5093bfb92f4ef8df33d218cee3a8
S: c95a6697886121a6743e50d41a84512e496bc663 192.168.174.115:26379
slots: (0 slots) slave
replicates 53eadf87db7fd4efa4a14924eb80178f6e92c5e5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
查看 redis 集群信息
/usr/local/redis/bin/redis-cli -c -p 26379 -a 123456 --no-auth-warning -h 192.168.174.108 cluster info
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:8 # 8 个节点
cluster_size:4 # 4 组主从
cluster_current_epoch:9
cluster_my_epoch:1
cluster_stats_messages_ping_sent:5620
cluster_stats_messages_pong_sent:5502
cluster_stats_messages_auth-ack_sent:1
cluster_stats_messages_update_sent:1
cluster_stats_messages_sent:11124
cluster_stats_messages_ping_received:5500
cluster_stats_messages_pong_received:9713
cluster_stats_messages_meet_received:2
cluster_stats_messages_fail_received:2
cluster_stats_messages_auth-req_received:1
cluster_stats_messages_received:15218
total_cluster_links_buffer_limit_exceeded:0
新 slave 节点方式二
添加新节点
/usr/local/redis/bin/redis-cli -a 123456 --no-auth-warning --cluster add-node 192.168.174.119:26379 192.168.174.108:26379
连接新节点
/usr/local/redis/bin/redis-cli -a 123456 -h 192.168.174.119 -p 26379 --no-auth-warning
修改角色为 slave
CLUSTER REPLICATE <master ID>
192.168.174.108:26379> CLUSTER REPLICATE a3b2c71e30e81d017ffa8583da175fc8a9e5ff08
redis 集群缩容
先迁移槽位,在删除节点。从4个 master 节点缩容到 3个 master 节点。 需要迁移的槽位=16384/4=4096. 将4096个槽位分给3个 master 节点。 每次迁移 4096/3=1735 个槽位。
槽位迁移完成后, 这组主从会变成其它master节点 slave。
查看 redis 集群状态
/usr/local/redis/bin/redis-cli -c -p 26379 -a 123456 --no-auth-warning -h 192.168.174.108 --cluster check 192.168.174.108:26379
192.168.174.108:26379 (2c2f409a...) -> 2 keys | 4096 slots | 1 slaves.
192.168.174.118:26379 (a3b2c71e...) -> 0 keys | 4096 slots | 1 slaves.
192.168.174.114:26379 (53eadf87...) -> 0 keys | 4096 slots | 1 slaves.
192.168.174.112:26379 (1538d933...) -> 1 keys | 4096 slots | 1 slaves.
[OK] 3 keys in 4 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.174.108:26379)
M: 2c2f409a5893ed9c6087d705126f8df80a746841 192.168.174.108:26379
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
M: a3b2c71e30e81d017ffa8583da175fc8a9e5ff08 192.168.174.118:26379
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
1 additional replica(s)
M: 53eadf87db7fd4efa4a14924eb80178f6e92c5e5 192.168.174.114:26379
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: f83dc9acb624c72b75ecd10d6e760b6cd9942e2a 192.168.174.119:26379
slots: (0 slots) slave
replicates a3b2c71e30e81d017ffa8583da175fc8a9e5ff08
M: 1538d933904b5093bfb92f4ef8df33d218cee3a8 192.168.174.112:26379
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: c807ec73ea369b4dd88f9a6e16cd3abb8b01732b 192.168.174.116:26379
slots: (0 slots) slave
replicates 2c2f409a5893ed9c6087d705126f8df80a746841
S: 4fdec8f3a6f42aee6c892fb175335c746da7f749 192.168.174.117:26379
slots: (0 slots) slave
replicates 1538d933904b5093bfb92f4ef8df33d218cee3a8
S: c95a6697886121a6743e50d41a84512e496bc663 192.168.174.115:26379
slots: (0 slots) slave
replicates 53eadf87db7fd4efa4a14924eb80178f6e92c5e5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
迁移槽位到 108 master节点
/usr/local/redis/bin/redis-cli -a 123456 --no-auth-warning --cluster reshard 192.168.174.108:26379
>>> Performing Cluster Check (using node 192.168.174.108:26379)
M: 2c2f409a5893ed9c6087d705126f8df80a746841 192.168.174.108:26379
slots:[1365-5460] (4096 slots) master
1 additional replica(s)
M: a3b2c71e30e81d017ffa8583da175fc8a9e5ff08 192.168.174.118:26379
slots:[0-1364],[5461-6826],[10923-12287] (4096 slots) master
1 additional replica(s)
M: 53eadf87db7fd4efa4a14924eb80178f6e92c5e5 192.168.174.114:26379
slots:[12288-16383] (4096 slots) master
1 additional replica(s)
S: f83dc9acb624c72b75ecd10d6e760b6cd9942e2a 192.168.174.119:26379
slots: (0 slots) slave
replicates a3b2c71e30e81d017ffa8583da175fc8a9e5ff08
M: 1538d933904b5093bfb92f4ef8df33d218cee3a8 192.168.174.112:26379
slots:[6827-10922] (4096 slots) master
1 additional replica(s)
S: c807ec73ea369b4dd88f9a6e16cd3abb8b01732b 192.168.174.116:26379
slots: (0 slots) slave
replicates 2c2f409a5893ed9c6087d705126f8df80a746841
S: 4fdec8f3a6f42aee6c892fb175335c746da7f749 192.168.174.117:26379
slots: (0 slots) slave
replicates 1538d933904b5093bfb92f4ef8df33d218cee3a8
S: c95a6697886121a6743e50d41a84512e496bc663 192.168.174.115:26379
slots: (0 slots) slave
replicates 53eadf87db7fd4efa4a14924eb80178f6e92c5e5
[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)? 1365 # 4096/3=1365。 将4096个槽位迁移到其它3个节点。
What is the receiving node ID? 2c2f409a5893ed9c6087d705126f8df80a746841 # 最后1365个槽位分配的 master ID
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: a3b2c71e30e81d017ffa8583da175fc8a9e5ff08 # 需要删除的节点 ID
Source node #2: done
....
Do you want to proceed with the proposed reshard plan (yes/no)? yes
...
迁移槽位到 114 master节点
/usr/local/redis/bin/redis-cli -a 123456 --no-auth-warning --cluster reshard 192.168.174.108:26379 --cluster-slots 1365 --cluster-from a3b2c71e30e81d017ffa8583da175fc8a9e5ff08 --cluster-to 53eadf87db7fd4efa4a14924eb80178f6e92c5e5 --cluster-yes
迁移槽位到 112 master节点
/usr/local/redis/bin/redis-cli -a 123456 --no-auth-warning --cluster reshard 192.168.174.108:26379 --cluster-slots 1365 --cluster-from a3b2c71e30e81d017ffa8583da175fc8a9e5ff08 --cluster-to 1538d933904b5093bfb92f4ef8df33d218cee3a8 --cluster-yes
查看 redis 集群信息
/usr/local/redis/bin/redis-cli -c -p 26379 -a 123456 --no-auth-warning -h 192.168.174.108 --cluster check 192.168.174.108:26379
192.168.174.108:26379 (2c2f409a...) -> 2 keys | 5461 slots | 1 slaves.
192.168.174.114:26379 (53eadf87...) -> 0 keys | 5462 slots | 3 slaves. # 增加了2个slave节点
192.168.174.112:26379 (1538d933...) -> 1 keys | 5461 slots | 1 slaves.
[OK] 3 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.174.108:26379)
M: 2c2f409a5893ed9c6087d705126f8df80a746841 192.168.174.108:26379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: a3b2c71e30e81d017ffa8583da175fc8a9e5ff08 192.168.174.118:26379
slots: (0 slots) slave
replicates 53eadf87db7fd4efa4a14924eb80178f6e92c5e5
M: 53eadf87db7fd4efa4a14924eb80178f6e92c5e5 192.168.174.114:26379
slots:[8191],[10923-16383] (5462 slots) master
3 additional replica(s)
S: f83dc9acb624c72b75ecd10d6e760b6cd9942e2a 192.168.174.119:26379
slots: (0 slots) slave
replicates 53eadf87db7fd4efa4a14924eb80178f6e92c5e5
M: 1538d933904b5093bfb92f4ef8df33d218cee3a8 192.168.174.112:26379
slots:[5461-8190],[8192-10922] (5461 slots) master
1 additional replica(s)
S: c807ec73ea369b4dd88f9a6e16cd3abb8b01732b 192.168.174.116:26379
slots: (0 slots) slave
replicates 2c2f409a5893ed9c6087d705126f8df80a746841
S: 4fdec8f3a6f42aee6c892fb175335c746da7f749 192.168.174.117:26379
slots: (0 slots) slave
replicates 1538d933904b5093bfb92f4ef8df33d218cee3a8
S: c95a6697886121a6743e50d41a84512e496bc663 192.168.174.115:26379
slots: (0 slots) slave
replicates 53eadf87db7fd4efa4a14924eb80178f6e92c5e5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
redis 集群删除节点
删除 119 节点
/usr/local/redis/bin/redis-cli -a 123456 --no-auth-warning --cluster del-node 192.168.174.108:26379 f83dc9acb624c72b75ecd10d6e760b6cd9942e2a
>>> Removing node f83dc9acb624c72b75ecd10d6e760b6cd9942e2a from cluster 192.168.174.108:26379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
删除 118 节点
/usr/local/redis/bin/redis-cli -a 123456 --no-auth-warning --cluster del-node 192.168.174.108:26379 a3b2c71e30e81d017ffa8583da175fc8a9e5ff08
>>> Removing node a3b2c71e30e81d017ffa8583da175fc8a9e5ff08 from cluster 192.168.174.108:26379
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
验证 redis 集群
/usr/local/redis/bin/redis-cli -c -p 26379 -a 123456 --no-auth-warning -h 192.168.174.108 --cluster check 192.168.174.108:26379
192.168.174.108:26379 (2c2f409a...) -> 2 keys | 5461 slots | 1 slaves.
192.168.174.114:26379 (53eadf87...) -> 0 keys | 5462 slots | 1 slaves.
192.168.174.112:26379 (1538d933...) -> 1 keys | 5461 slots | 1 slaves.
[OK] 3 keys in 3 masters.
0.00 keys per slot on average.
>>> Performing Cluster Check (using node 192.168.174.108:26379)
M: 2c2f409a5893ed9c6087d705126f8df80a746841 192.168.174.108:26379
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 53eadf87db7fd4efa4a14924eb80178f6e92c5e5 192.168.174.114:26379
slots:[8191],[10923-16383] (5462 slots) master
1 additional replica(s)
M: 1538d933904b5093bfb92f4ef8df33d218cee3a8 192.168.174.112:26379
slots:[5461-8190],[8192-10922] (5461 slots) master
1 additional replica(s)
S: c807ec73ea369b4dd88f9a6e16cd3abb8b01732b 192.168.174.116:26379
slots: (0 slots) slave
replicates 2c2f409a5893ed9c6087d705126f8df80a746841
S: 4fdec8f3a6f42aee6c892fb175335c746da7f749 192.168.174.117:26379
slots: (0 slots) slave
replicates 1538d933904b5093bfb92f4ef8df33d218cee3a8
S: c95a6697886121a6743e50d41a84512e496bc663 192.168.174.115:26379
slots: (0 slots) slave
replicates 53eadf87db7fd4efa4a14924eb80178f6e92c5e5
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
redis 集群偏斜
Redis 集群偏斜(skew)是指在集群中,某些节点的负载显著高于其他节点。这通常会导致性能问题,因为高负载节点可能会变得瓶颈。
Redis 集群偏斜的原因
1. 节点和槽分配不均
2. 不同槽对应键值数量差异较大
3. 包含bigkey,建议少用
4. 内存相关配置不一致
5. 热点数据不均衡:一致性不高时,可以使用本缓存和MQ
查询 slot 对应 key 个数
# cluster countkeysinslot <slot 值>
/usr/local/redis/bin/redis-cli -c -p 26379 -a 123456 --no-auth-warning -h 192.168.174.108 cluster countkeysinslot 1
查询 bigkey
/usr/local/redis/bin/redis-cli -c -p 26379 -a 123456 --no-auth-warning -h 192.168.174.108 --bigkeys
# Scanning the entire keyspace to find biggest keys as well as
# average sizes per key type. You can use -i 0.1 to sleep 0.1 sec
# per 100 SCAN commands (not usually needed).
100.00% ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Keys sampled: 2
-------- summary -------
Total key length in bytes is 18 (avg len 9.00)
Biggest string found "test-key" has 4 bytes
0 lists with 0 items (00.00% of keys, avg size 0.00)
0 hashs with 0 fields (00.00% of keys, avg size 0.00)
0 streams with 0 entries (00.00% of keys, avg size 0.00)
2 strings with 7 bytes (100.00% of keys, avg size 3.50)
0 sets with 0 members (00.00% of keys, avg size 0.00)
0 zsets with 0 members (00.00% of keys, avg size 0.00)
slot 从新平衡分配(慎用)
/usr/local/redis/bin/redis-cli -a 123456 --no-auth-warning --cluster rebalance 192.168.174.108:26379
>>> Performing Cluster Check (using node 192.168.174.108:26379)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
*** No rebalancing needed! All nodes are within the 2.00% threshold.
参考文档
https://redis.io/docs/latest/operate/oss_and_stack/management/scaling/