集群
配置
sh
bind 0.0.0.0
port 6371
daemonize yes
logfile "/usr/local/redis/log/redis-6371.log"
dbfilename dump.rdb
dir /usr/local/redis/data
appendonly yes
appendfilename 'appendonly.aof'
requirepass password
# 从节点访问主节点密码,与requirepass一致
masterauth password
# 是否开启集群模式, 默认no
cluster-enabled yes
# 集群节点信息文件,会保存在dir配置对应的目录下
cluster-config-file nodes-6371.conf
#集群节点链接超时时间
cluster-node-timeout 15000
#集群节点ip
cluster-announce-ip 192.168.0.90
#集群节点映射端口
cluster-announce-port 6371
#集群节点总线端口
cluster-announce-bus-port 16371
启动
在构建集群前,现将每个节点启动redis-server redis-637[1-6].conf
sh
# 选取任一节点,执行。 按1:1 master:slave启动集群
/usr/local/redis/bin/redis-cli -p 6371 -a password --cluster create 192.168.0.90:6371 192.168.0.90:6372 192.168.0.90:6373 192.168.0.90:6374 192.168.0.90:6375 192.168.0.90:6376 --cluster-replicas 1
>>> 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.0.90:6375 to 192.168.0.90:6371
Adding replica 192.168.0.90:6376 to 192.168.0.90:6372
Adding replica 192.168.0.90:6374 to 192.168.0.90:6373
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: d7ea878329b7c105d7f49a03904d6d61407067a9 192.168.0.90:6371
slots:[0-5460] (5461 slots) master
M: 782211e0f4f4768d72b91de9ae27f97d2b5e0513 192.168.0.90:6372
slots:[5461-10922] (5462 slots) master
M: 3d610306e271415688f1d4db2b7e56a644c84b15 192.168.0.90:6373
slots:[10923-16383] (5461 slots) master
S: dc8fc39197d371c32bf7822d8eb8311345ce2ccb 192.168.0.90:6374
replicates 3d610306e271415688f1d4db2b7e56a644c84b15
S: 7b69d93c42c523ec6cf9e0334ace182fa128d43b 192.168.0.90:6375
replicates d7ea878329b7c105d7f49a03904d6d61407067a9
S: e4784c4e9fe9e32e0f166d59ce48c181a30cfc81 192.168.0.90:6376
replicates 782211e0f4f4768d72b91de9ae27f97d2b5e0513
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.0.90:6371)
M: d7ea878329b7c105d7f49a03904d6d61407067a9 192.168.0.90:6371
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 782211e0f4f4768d72b91de9ae27f97d2b5e0513 192.168.0.90:6372
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 7b69d93c42c523ec6cf9e0334ace182fa128d43b 192.168.0.90:6375
slots: (0 slots) slave
replicates d7ea878329b7c105d7f49a03904d6d61407067a9
M: 3d610306e271415688f1d4db2b7e56a644c84b15 192.168.0.90:6373
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: e4784c4e9fe9e32e0f166d59ce48c181a30cfc81 192.168.0.90:6376
slots: (0 slots) slave
replicates 782211e0f4f4768d72b91de9ae27f97d2b5e0513
S: dc8fc39197d371c32bf7822d8eb8311345ce2ccb 192.168.0.90:6374
slots: (0 slots) slave
replicates 3d610306e271415688f1d4db2b7e56a644c84b15
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
Redis 的总slots数是16384个
检查
sh
# 任何一个节点都可以
/usr/local/redis/bin/redis-cli -a password --cluster check 192.168.0.90:6371
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
192.168.0.90:6371 (d7ea8783...) -> 0 keys | 5461 slots | 1 slaves.
192.168.0.90:6372 (782211e0...) -> 0 keys | 5462 slots | 1 slaves.
192.168.0.90:6373 (3d610306...) -> 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.0.90:6371)
M: d7ea878329b7c105d7f49a03904d6d61407067a9 192.168.0.90:6371
slots:[0-5460] (5461 slots) master
1 additional replica(s)
M: 782211e0f4f4768d72b91de9ae27f97d2b5e0513 192.168.0.90:6372
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 7b69d93c42c523ec6cf9e0334ace182fa128d43b 192.168.0.90:6375
slots: (0 slots) slave
replicates d7ea878329b7c105d7f49a03904d6d61407067a9
M: 3d610306e271415688f1d4db2b7e56a644c84b15 192.168.0.90:6373
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: e4784c4e9fe9e32e0f166d59ce48c181a30cfc81 192.168.0.90:6376
slots: (0 slots) slave
replicates 782211e0f4f4768d72b91de9ae27f97d2b5e0513
S: dc8fc39197d371c32bf7822d8eb8311345ce2ccb 192.168.0.90:6374
slots: (0 slots) slave
replicates 3d610306e271415688f1d4db2b7e56a644c84b15
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
以集群的方式连接
sh
/usr/local/redis/bin/redis-cli -a password -c -h 192.168.0.90 -p 6371
#集群信息
cluster info
192.168.0.90:6371> 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:1334
cluster_stats_messages_pong_sent:1356
cluster_stats_messages_sent:2690
cluster_stats_messages_ping_received:1351
cluster_stats_messages_pong_received:1334
cluster_stats_messages_meet_received:5
cluster_stats_messages_received:2690
#节点信息
CLUSTER nodes
192.168.0.90:6371> CLUSTER nodes
782211e0f4f4768d72b91de9ae27f97d2b5e0513 192.168.0.90:6372@16372 master - 0 1738642034228 2 connected 5461-10922
7b69d93c42c523ec6cf9e0334ace182fa128d43b 192.168.0.90:6375@16375 slave d7ea878329b7c105d7f49a03904d6d61407067a9 0 1738642035233 1 connected
3d610306e271415688f1d4db2b7e56a644c84b15 192.168.0.90:6373@16373 master - 0 1738642033223 3 connected 10923-16383
e4784c4e9fe9e32e0f166d59ce48c181a30cfc81 192.168.0.90:6376@16376 slave 782211e0f4f4768d72b91de9ae27f97d2b5e0513 0 1738642033000 2 connected
dc8fc39197d371c32bf7822d8eb8311345ce2ccb 192.168.0.90:6374@16374 slave 3d610306e271415688f1d4db2b7e56a644c84b15 0 1738642036239 3 connected
d7ea878329b7c105d7f49a03904d6d61407067a9 192.168.0.90:6371@16371 myself,master - 0 1738642035000 1 connected 0-5460
从节点只读模式
sh
#在从节点执行readonly
readonly
动态添加节点
创建节点
sh
/usr/local/redis/bin/redis-server /usr/local/redis/conf/redis-6377.conf
查看节点
sh
redis-cli -a password -p 6371 cluster nodes
添加主节点
sh
/usr/local/redis/bin/redis-cli -a password --cluster add-node new_host_new_port existing_host:existing_port --cluster-master-id node_id
# example
/usr/local/redis/bin/redis-cli -a password --cluster add-node 192.168.0.90:6377 192.168.0.90:6373 --cluster-master-id 4bceee56011a7f2d0b704c3d7e069496d80c7c20
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.0.90:6377 to cluster 192.168.0.90:6373
>>> Performing Cluster Check (using node 192.168.0.90:6373)
M: 4bceee56011a7f2d0b704c3d7e069496d80c7c20 192.168.0.90:6373
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: c24e8448880920db3f8b6cc4a7b5718072d49acc 192.168.0.90:6372
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: f876b53b68662b4964e40a55636255397fb13c05 192.168.0.90:6371
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 47825a9aa0c52787b853ac04c0b01c414b855400 192.168.0.90:6375
slots: (0 slots) slave
replicates 4bceee56011a7f2d0b704c3d7e069496d80c7c20
S: 2ca69f0dcbe4421ae95aabd9e2d3cec0c59f1b7c 192.168.0.90:6374
slots: (0 slots) slave
replicates c24e8448880920db3f8b6cc4a7b5718072d49acc
S: 837f6fe17398b5b25dc78d676a2f02929b347c9b 192.168.0.90:6376
slots: (0 slots) slave
replicates f876b53b68662b4964e40a55636255397fb13c05
[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.0.90:6377 to make it join the cluster.
[OK] New node added correctly.
6377端口对应的节点已经加入到集群中,是主节点,但是没有从节点,也没用分配槽数。
重新分片
sh
redis-cli --cluster reshard host:port --cluster-from node_id --cluster-to node_id --cluster-slots <args> --cluster-yes
/usr/local/redis/bin/redis-cli -a password --cluster reshard 192.168.0.90:6372 --cluster-from f876b53b68662b4964e40a55636255397fb13c05 --cluster-to 86c736d760b47e8c17a42340acd3556bf1ee7df3 --cluster-slots 2000
添加从节点
sh
/usr/local/redis/bin/redis-cli -a password --cluster add-node new_host_new_port existing_host:existing_port --cluster-slave --cluster-master-id node_id
/usr/local/redis/bin/redis-cli -a password --cluster add-node 192.168.0.90:6378 192.168.0.90:6377 --cluster-slave --cluster-master-id 86c736d760b47e8c17a42340acd3556bf1ee7df3
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Adding node 192.168.0.90:6378 to cluster 192.168.0.90:6377
>>> Performing Cluster Check (using node 192.168.0.90:6377)
M: 86c736d760b47e8c17a42340acd3556bf1ee7df3 192.168.0.90:6377
slots:[0-1999] (2000 slots) master
M: f876b53b68662b4964e40a55636255397fb13c05 192.168.0.90:6371
slots:[2000-5460] (3461 slots) master
1 additional replica(s)
M: c24e8448880920db3f8b6cc4a7b5718072d49acc 192.168.0.90:6372
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 837f6fe17398b5b25dc78d676a2f02929b347c9b 192.168.0.90:6376
slots: (0 slots) slave
replicates f876b53b68662b4964e40a55636255397fb13c05
S: 47825a9aa0c52787b853ac04c0b01c414b855400 192.168.0.90:6375
slots: (0 slots) slave
replicates 4bceee56011a7f2d0b704c3d7e069496d80c7c20
M: 4bceee56011a7f2d0b704c3d7e069496d80c7c20 192.168.0.90:6373
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 2ca69f0dcbe4421ae95aabd9e2d3cec0c59f1b7c 192.168.0.90:6374
slots: (0 slots) slave
replicates c24e8448880920db3f8b6cc4a7b5718072d49acc
[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.0.90:6378 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 192.168.0.90:6377.
[OK] New node added correctly.
删除节点
删除从节点
sh
redis-cli --cluster del-node host:port node_id
/usr/local/redis/bin/redis-cli -a password --cluster del-node 192.168.0.90:6378 63b6ce4ca84b4c09f7f11d4faa2ad6eb5f7e8656
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 63b6ce4ca84b4c09f7f11d4faa2ad6eb5f7e8656 from cluster 192.168.0.90:6378
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.
删除主节点
sh
# 第一种
redis-cli --cluster reshard host:port --cluster-from node_id --cluster-to node_id --cluster-slots <args> --cluster-yes
# 第二种 询问式
/usr/local/redis/bin/redis-cli -a password --cluster reshard 192.168.0.90:6373
#第一步:询问转移主节点总数量
#第二步:询问转移到哪个主节点
#第三步:从哪个主节点转移,询问多个,最后完成 输入done
#第四部:yes
/usr/local/redis/bin/redis-cli -a password --cluster del-node 192.168.0.90:6377 86c736d760b47e8c17a42340acd3556bf1ee7df3
Warning: Using a password with '-a' or '-u' option on the command line interface may not be safe.
>>> Removing node 86c736d760b47e8c17a42340acd3556bf1ee7df3 from cluster 192.168.0.90:6377
>>> Sending CLUSTER FORGET messages to the cluster...
>>> Sending CLUSTER RESET SOFT to the deleted node.