部署

三主

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
# 开启集群
cluster-enabled yes
cluster-config-file nodes-6379.conf
cluster-node-timeout 15000

# 如果有配置 auth,需配置密码
masterauth 123456

# 开启 AOF/RDB 混写
appendonly yes

# 关闭纯 RDB 模式
save ""
#save 900 1
#save 300 10
#save 60 10000
  • 创建集群
 1
 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
# 如果有密码的话,使用 -a 指定密码,加入集群的各个节点密码要求一致
$ /data/redis/redis_6379/bin/redis-cli --cluster create 192.168.210.131:6379 192.168.210.132:6379 192.168.210.133:6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Performing hash slots allocation on 3 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
M: 4e1fe909c069ba51c40f89a99e74e69eba77388e 192.168.210.131:6379
   slots:[0-5460] (5461 slots) master
M: 73f23fdf5374bf4e92dfbc39d112017f086d240c 192.168.210.132:6379
   slots:[5461-10922] (5462 slots) master
M: f8d9bef0c981349705733191bd9a6f9eaa6536fa 192.168.210.133:6379
   slots:[10923-16383] (5461 slots) master
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 192.168.210.131:6379)
M: 4e1fe909c069ba51c40f89a99e74e69eba77388e 192.168.210.131:6379
   slots:[0-5460] (5461 slots) master
M: 73f23fdf5374bf4e92dfbc39d112017f086d240c 192.168.210.132:6379
   slots:[5461-10922] (5462 slots) master
M: f8d9bef0c981349705733191bd9a6f9eaa6536fa 192.168.210.133:6379
   slots:[10923-16383] (5461 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
  • 查看集群信息
    • 使用 -c 以集群模式连接 redis
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
$ /data/redis/redis_6379/bin/redis-cli -h 192.168.210.131 -a 123456 -c
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.210.131:6379> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:3
cluster_size:3
cluster_current_epoch:3
cluster_my_epoch:1
cluster_stats_messages_ping_sent:180
cluster_stats_messages_pong_sent:178
cluster_stats_messages_sent:358
cluster_stats_messages_ping_received:176
cluster_stats_messages_pong_received:180
cluster_stats_messages_meet_received:2
cluster_stats_messages_received:358
  • 查看集群节点(前文ip不对应,此为补充内容)
1
2
3
4
172.16.2.181:6379> cluster nodes
cfa09e5796c17140cb7b53b23017426813044893 172.16.2.183:6379@16379 master - 0 1663903991026 3 connected 10923-16383
f14cac615366e053785e6aa330d3161ab9d21e54 172.16.2.182:6379@16379 master - 0 1663903989008 2 connected 5461-10922
15b5c284edacbae5d76d4e0f4c6cba2bb1066bf8 172.16.2.181:6379@16379 myself,master - 0 1663903990000 1 connected 0-5460

三主三从

  • 初始化时创建三主三从集群
1
2
3
/data/redis/redis_6379/bin/redis-cli --cluster create 172.16.2.181:6379 172.16.2.181:6380 \
172.16.2.182:6379 172.16.2.182:6380 172.16.2.183:6379 172.16.2.183:6380 \
--cluster-replicas 1 -a 123456 --cluster-yes
  • 验证
1
2
3
4
5
6
7
172.16.2.182:6379> CLUSTER nodes
7e3dd49572a1003f9395a5c868dc684e1663a342 172.16.2.183:6380@16380 slave 63630adb361f9e043762061f5959c653c399a18c 0 1663918447000 3 connected
d770f78e6dfd11267ffd2eecd780cfb91b1bc01b 172.16.2.183:6379@16379 master - 0 1663918447651 5 connected 10923-16383
a3d6eb2b78a7d079553dd64f8fb47881a9ab8e6c 172.16.2.182:6380@16380 slave e42a185aeaf247a5080319e3a15e091145b79134 0 1663918446631 1 connected
e42a185aeaf247a5080319e3a15e091145b79134 172.16.2.181:6379@16379 master - 0 1663918445000 1 connected 0-5460
f8befa79f2608f5c3b1ce19910d292b05e2b781e 172.16.2.181:6380@16380 slave d770f78e6dfd11267ffd2eecd780cfb91b1bc01b 0 1663918445612 5 connected
63630adb361f9e043762061f5959c653c399a18c 172.16.2.182:6379@16379 myself,master - 0 1663918445000 3 connected 5461-10922

添加新主节点

  • 将新节点加入集群作为主节点
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# redis-cli --cluster add-node {{ 新节点请求地址 }} {{ 集群原有节点地址 }}
$ /data/redis/redis_6379/bin/redis-cli  --cluster add-node 172.16.2.181:6380 172.16.2.181:6379 -a 123456
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 172.16.2.181:6380 to cluster 172.16.2.181:6379
>>> Performing Cluster Check (using node 172.16.2.181:6379)
M: 15b5c284edacbae5d76d4e0f4c6cba2bb1066bf8 172.16.2.181:6379
   slots:[0-5460] (5461 slots) master
M: cfa09e5796c17140cb7b53b23017426813044893 172.16.2.183:6379
   slots:[10923-16383] (5461 slots) master
M: f14cac615366e053785e6aa330d3161ab9d21e54 172.16.2.182:6379
   slots:[5461-10922] (5462 slots) master
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
>>> Send CLUSTER MEET to node 172.16.2.181:6380 to make it join the cluster.
[OK] New node added correctly.
[root@mgr01 install_redis_plus]#
  • 查看集群状态
    • 此时新节点由于没有分配 hash 槽,并不会被写入数据,可手动调整
1
2
3
4
5
172.16.2.181:6379> cluster nodes
c4efdc120b3a1053f094d7c7e1abb7b54d753fa9 172.16.2.181:6380@16380 master - 0 1663904193000 0 connected
cfa09e5796c17140cb7b53b23017426813044893 172.16.2.183:6379@16379 master - 0 1663904193767 3 connected 10923-16383
f14cac615366e053785e6aa330d3161ab9d21e54 172.16.2.182:6379@16379 master - 0 1663904192000 2 connected 5461-10922
15b5c284edacbae5d76d4e0f4c6cba2bb1066bf8 172.16.2.181:6379@16379 myself,master - 0 1663904194000 1 connected 0-5460
  • 手动调整分片
    • 每个节点移动指定个数分片至新加入的节点,具体分片数需要计算,如三节点加入一个新节点,则每个节点需要移动的分片数约为 1365
1
2
3
/data/redis/redis_6379/bin/redis-cli --cluster reshard 172.16.2.181:3306 --cluster-from cfa09e5796c17140cb7b53b23017426813044893 --cluster-to c4efdc120b3a1053f094d7c7e1abb7b54d753fa9 --cluster-slots 1365 --cluster-yes -a 123456
/data/redis/redis_6379/bin/redis-cli --cluster reshard 172.16.2.181:6379 --cluster-from cfa09e5796c17140cb7b53b23017426813044893 --cluster-to c4efdc120b3a1053f094d7c7e1abb7b54d753fa9 --cluster-slots 1365 --cluster-yes -a 123456
/data/redis/redis_6379/bin/redis-cli --cluster reshard 172.16.2.181:6379 --cluster-from  15b5c284edacbae5d76d4e0f4c6cba2bb1066bf8 --cluster-to c4efdc120b3a1053f094d7c7e1abb7b54d753fa9 --cluster-slots 1365 --cluster-yes -a 123456

添加从节点

  • 增加新的从经典并指定主节点
    • –cluster add-node <新节点 ip> <集群已有节点 ip>
    • –cluster-master-id:指定从节点的主节点 id,可通过 cluster nodes 命令查询
1
/data/redis/redis_6379/bin/redis-cli --cluster add-node 172.16.2.182:6380 172.16.2.181:6379  --cluster-slave --cluster-master-id c4efdc120b3a1053f094d7c7e1abb7b54d753fa9 -a 123456
  • 查看状态
1
2
3
4
5
6
172.16.2.181:6379> CLUSTER NODES
c4efdc120b3a1053f094d7c7e1abb7b54d753fa9 172.16.2.181:6380@16380 master - 1663917570133 1663917566095 4 disconnected 0-1364 5461-6825 10923-12287
15b5c284edacbae5d76d4e0f4c6cba2bb1066bf8 172.16.2.181:6379@16379 myself,master - 0 1663917566000 1 connected 1365-5460
f14cac615366e053785e6aa330d3161ab9d21e54 172.16.2.182:6379@16379 master - 0 1663917569000 2 connected 6826-10922
51bcb8bb20894b079b2acf1741ec92a7d530d52f 172.16.2.182:6380@16380 slave c4efdc120b3a1053f094d7c7e1abb7b54d753fa9 0 1663917569125 4 connected
cfa09e5796c17140cb7b53b23017426813044893 172.16.2.183:6379@16379 master - 0 1663917567103 3 connected 12288-16383