首页 > 数据库 >redis7.4 集群配置

redis7.4 集群配置

时间:2024-12-25 17:56:57浏览次数:3  
标签:redis7.4 配置 redis 192.168 cluster 集群 26379 master slots

集群规划

节点规划

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/

标签:redis7.4,配置,redis,192.168,cluster,集群,26379,master,slots
From: https://www.cnblogs.com/wangguishe/p/18612845

相关文章

  • manage项目引入axios,并完成axios基础配置
    1、npminstallaxios引入axios2、src下新建http.ts,可根据项目实际情况配置importaxiosfrom'axios';consthttp=axios.create({baseURL:'http://localhost:8080',//基础URL可以根据你的情况自行修改timeout:10000,//请求超时时间//其他配置...});//请求拦......
  • 配置动态更新
    动态更新配置的步骤注意:Viper本身不是并发安全的,在实现配置动态更新时,要注意采用锁机制等方式来保证Viper并发读写的安全。1,更新配置源中的配置在Viper支持的配置源中,命令行参数、环境变量是在进程启动时一次性读取的,不支持动态更新。本地配置文件和远程配置文件可以支持动态更......
  • VScode中配置ESlint+Prettier详细步骤(图文详情)
    VScode中配置ESlint+Prettier详细步骤(图文详情)前置环境:node18.19.0vite3.2.11vue3.2.47本文将不在演示vue3基础工程创建,如果还没有vue3项目工程请参考文章:Vite创建Vue3工程并引入ElementPlus(图文详细)本文的ESlint+Prettier版本:ESlint8.57.1Prettier3.2.51ed......
  • spark内存配置,你一定不能忽略的知识点
    前言Spark是一个强大的分布式计算框架,它可以处理大规模数据集,并通过内存缓存来提高计算性能。但如果不正确地配置Spark的内存,可能会导致缓存滥用和性能问题。本文将介绍如何有效地配置Spark的内存设置,以避免缓存滥用和提高性能。一、Spark内存配置在Spark中,内存主要用于两个......
  • 配置manage路由,实现嵌套路由
    1、npminstallvue-router引入vue-routermain.ts增加配置importrouterfrom'./routes'createApp(App).use(router)2、src下新建目录routes,新建index.ts//index.tsimport{createRouter,createWebHistory}from'vue-router';//引入Vue组件importHomefrom......
  • Greenplum分布键及配置规则
    Greenplum分布键及配置规则基本概念Greenplum是分布式系统,创建表时需要指定分布键(创建表需要CREATEDBA权限),目的在于将数据平均分布到各个segment。选择分布键非常重要,选择错了会导致数据不唯一,更严重的是会造成SQL性能急剧下降。Greenplum有两种分布策略:1、hash分布Greenplum......
  • 国标GB28181网页直播平台LiteGBS摄像机怎么配置连接4G无线网络?
    在现代安防监控领域,LiteGBS国标GB28181软件作为一款符合国标GB28181的视频平台,其在4G无线网络环境下的摄像机配置连接能力显得尤为重要。随着4G技术的普及和网络覆盖的扩大,LiteGBS能够为远程监控提供更加灵活和高效的解决方案。由于应用场景差异,部分网络摄像机的安装位置无法布设......
  • mongodb配置zabbix监控
    mongodb配置zabbix监控目录mongodb配置zabbix监控说明一、添加mongodb模版二、配置宏三测试连接:注意事项通过脚本方式添加监控项1.修改运行zabbixagent脚本的用户2.添加如下用户自定义脚本3.添加如下用户自定义脚本附脚本:db_monitor.confjkalert.shjkcur_connect.shjkdelay.shjk......
  • 计算机网络--某网络拓扑如下图所示,其中R为路由器,主机H1-H4的IP地址配置以及R的各接口I
    题目:某网络拓扑如下图所示,其中R为路由器,主机H1-H4的IP地址配置以及R的各接口IP地址配置如图中所示。现有若干台以太网交换机(无VLAN功能)和路由器两类网络互连设备可供选择。请回答下列问题。1)设备1(第1空)、设备2(第2空)和设备3(第3空)分别应选择什么类型网络设备?2)设备1中IF1需......
  • Centos 7.4 配置RAC+ASM( oracle19c)
    Centos7.4配置RAC+ASM(oracle19c)一、安装准备1环境规划:类型配置操作系统vmare+Centos7.4两台磁盘50G内存4G网络建议绑定双网卡数据库版本oracle19c共享存储ASM需配置两个网卡,一块共享磁盘。说明:当前为测试环境,生产环境建议参考官方建......