1、源码编译安装Redis(不用启动Redis服务)
参考:https://blog.csdn.net/a120608yby/article/details/141261514
2、快速创建集群
# 编辑create-cluster脚本内容
# vim create-cluster
...
# Settings
BIN_PATH="/usr/local/redis/bin/" #修改可执行文件路径
CLUSTER_HOST=127.0.0.1
PORT=30000
TIMEOUT=2000
NODES=6
REPLICAS=1
PROTECTED_MODE=yes
ADDITIONAL_OPTIONS=""
...
# 快速启动6个实例
# cd /usr/local/src/redis-stable/utils/create-cluster
# ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
# 把以上创建的6个实例组成一个集群
# ./create-cluster create
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:30005 to 127.0.0.1:30001
Adding replica 127.0.0.1:30006 to 127.0.0.1:30002
Adding replica 127.0.0.1:30004 to 127.0.0.1:30003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: 1c3eb264d601748e4636895552cd159478aeb621 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
M: 213d6b0820219bb5ac99b77b2dd982c74aebdece 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
M: 8dae6d5c4c458c2410c8e8e13d9714248c93ce02 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
S: 9d0a5f21888665fdf8406b19389a7d238eeddbab 127.0.0.1:30004
replicates 1c3eb264d601748e4636895552cd159478aeb621
S: 041e507b11a44242dd2c4a0d1126ae753b969783 127.0.0.1:30005
replicates 213d6b0820219bb5ac99b77b2dd982c74aebdece
S: 967174dbae97def304e409f720fc0615d0e88195 127.0.0.1:30006
replicates 8dae6d5c4c458c2410c8e8e13d9714248c93ce02
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 127.0.0.1:30001)
M: 1c3eb264d601748e4636895552cd159478aeb621 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 9d0a5f21888665fdf8406b19389a7d238eeddbab 127.0.0.1:30004
slots: (0 slots) slave
replicates 1c3eb264d601748e4636895552cd159478aeb621
S: 967174dbae97def304e409f720fc0615d0e88195 127.0.0.1:30006
slots: (0 slots) slave
replicates 8dae6d5c4c458c2410c8e8e13d9714248c93ce02
M: 8dae6d5c4c458c2410c8e8e13d9714248c93ce02 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
M: 213d6b0820219bb5ac99b77b2dd982c74aebdece 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 041e507b11a44242dd2c4a0d1126ae753b969783 127.0.0.1:30005
slots: (0 slots) slave
replicates 213d6b0820219bb5ac99b77b2dd982c74aebdece
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
3、连接并查看集群node信息
redis-cli -c -p 30001 cluster nodes
4、查看集群
redis-cli --cluster check 127.0.0.1:30001
5、增加节点并加入集群
# 编辑create-cluster脚本内容
# vim create-cluster
...
# Settings
BIN_PATH="/usr/local/redis/bin/"
CLUSTER_HOST=127.0.0.1
PORT=30000
TIMEOUT=2000
NODES=8 #修改Node数量
REPLICAS=1
PROTECTED_MODE=yes
ADDITIONAL_OPTIONS=""
...
# 启动实例
# cd /usr/local/src/redis-stable/utils/create-cluster
# ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
Starting 30007
Starting 30008
# 把以上增加的2个实例加入到集群
# 先将30007作为主节点加入集群
# redis-cli --cluster add-node 127.0.0.1:30007 127.0.0.1:30001
>>> Adding node 127.0.0.1:30007 to cluster 127.0.0.1:30001
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 50830de7dc4d33814b8db237c92ce103b6969aee 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 5765b98169fd58a0db73784ccd632b371bbf5618 127.0.0.1:30004
slots: (0 slots) slave
replicates 50830de7dc4d33814b8db237c92ce103b6969aee
S: e021318df630af0355f255f9e735074e047a40b4 127.0.0.1:30006
slots: (0 slots) slave
replicates 2e85ee0391b376a31baed1c6df919646a09721e1
M: 1ee741c70a319f82e479695a6c5b54e07a2434e3 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 08562c0f0b2fe5f46e0d96f5d06aaaf97209e3d8 127.0.0.1:30005
slots: (0 slots) slave
replicates 1ee741c70a319f82e479695a6c5b54e07a2434e3
M: 2e85ee0391b376a31baed1c6df919646a09721e1 127.0.0.1:30003
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.
>>> Getting functions from cluster
>>> Send FUNCTION LIST to 127.0.0.1:30007 to verify there is no functions in it
>>> Send FUNCTION RESTORE to 127.0.0.1:30007
>>> Send CLUSTER MEET to node 127.0.0.1:30007 to make it join the cluster.
[OK] New node added correctly.
# 查看集群信息
# redis-cli -c -p 30001 cluster nodes
5765b98169fd58a0db73784ccd632b371bbf5618 127.0.0.1:30004@40004 slave 50830de7dc4d33814b8db237c92ce103b6969aee 0 1679276447279 1 connected
332eb73b0c34f911792421164c23eb4550fa1b35 127.0.0.1:30007@40007 master - 0 1679276447279 0 connected
e021318df630af0355f255f9e735074e047a40b4 127.0.0.1:30006@40006 slave 2e85ee0391b376a31baed1c6df919646a09721e1 0 1679276447178 3 connected
1ee741c70a319f82e479695a6c5b54e07a2434e3 127.0.0.1:30002@40002 master - 0 1679276447279 2 connected 5461-10922
08562c0f0b2fe5f46e0d96f5d06aaaf97209e3d8 127.0.0.1:30005@40005 slave 1ee741c70a319f82e479695a6c5b54e07a2434e3 0 1679276447077 2 connected
2e85ee0391b376a31baed1c6df919646a09721e1 127.0.0.1:30003@40003 master - 0 1679276447178 3 connected 10923-16383
50830de7dc4d33814b8db237c92ce103b6969aee 127.0.0.1:30001@40001 myself,master - 0 1679276447000 1 connected 0-5460
#将30008作为30007的从节点加入集群
# redis-cli --cluster add-node 127.0.0.1:30008 127.0.0.1:30001 --cluster-slave --cluster-master-id 332eb73b0c34f911792421164c23eb4550fa1b35
>>> Adding node 127.0.0.1:30008 to cluster 127.0.0.1:30001
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 50830de7dc4d33814b8db237c92ce103b6969aee 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 5765b98169fd58a0db73784ccd632b371bbf5618 127.0.0.1:30004
slots: (0 slots) slave
replicates 50830de7dc4d33814b8db237c92ce103b6969aee
M: 332eb73b0c34f911792421164c23eb4550fa1b35 127.0.0.1:30007
slots: (0 slots) master
S: e021318df630af0355f255f9e735074e047a40b4 127.0.0.1:30006
slots: (0 slots) slave
replicates 2e85ee0391b376a31baed1c6df919646a09721e1
M: 1ee741c70a319f82e479695a6c5b54e07a2434e3 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: 08562c0f0b2fe5f46e0d96f5d06aaaf97209e3d8 127.0.0.1:30005
slots: (0 slots) slave
replicates 1ee741c70a319f82e479695a6c5b54e07a2434e3
M: 2e85ee0391b376a31baed1c6df919646a09721e1 127.0.0.1:30003
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.
>>> Send CLUSTER MEET to node 127.0.0.1:30008 to make it join the cluster.
Waiting for the cluster to join
>>> Configure node as replica of 127.0.0.1:30007.
[OK] New node added correctly.
# 重新分配集群的槽号
# redis-cli --cluster reshard 127.0.0.1:30001
>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: 50830de7dc4d33814b8db237c92ce103b6969aee 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: 5765b98169fd58a0db73784ccd632b371bbf5618 127.0.0.1:30004
slots: (0 slots) slave
replicates 50830de7dc4d33814b8db237c92ce103b6969aee
M: 332eb73b0c34f911792421164c23eb4550fa1b35 127.0.0.1:30007
slots: (0 slots) master
1 additional replica(s)
S: e021318df630af0355f255f9e735074e047a40b4 127.0.0.1:30006
slots: (0 slots) slave
replicates 2e85ee0391b376a31baed1c6df919646a09721e1
M: 1ee741c70a319f82e479695a6c5b54e07a2434e3 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
S: baa87c527982432ee752fd6f7bf8c75561d19d60 127.0.0.1:30008
slots: (0 slots) slave
replicates 332eb73b0c34f911792421164c23eb4550fa1b35
S: 08562c0f0b2fe5f46e0d96f5d06aaaf97209e3d8 127.0.0.1:30005
slots: (0 slots) slave
replicates 1ee741c70a319f82e479695a6c5b54e07a2434e3
M: 2e85ee0391b376a31baed1c6df919646a09721e1 127.0.0.1:30003
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.
How many slots do you want to move (from 1 to 16384)? 2000 #移动多少槽号到新的节点上
What is the receiving node ID? 332eb73b0c34f911792421164c23eb4550fa1b35 #新加入的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: all #从所有节点上移动一部分槽号到新的master节点上
Do you want to proceed with the proposed reshard plan (yes/no)? yes
...
# 再次查看集群信息确认槽号分配情况
# redis-cli -c -p 30001 cluster nodes
5765b98169fd58a0db73784ccd632b371bbf5618 127.0.0.1:30004@40004 slave 50830de7dc4d33814b8db237c92ce103b6969aee 0 1679277369182 1 connected
332eb73b0c34f911792421164c23eb4550fa1b35 127.0.0.1:30007@40007 master - 0 1679277369081 7 connected 0-665 5461-6127 10923-11588
e021318df630af0355f255f9e735074e047a40b4 127.0.0.1:30006@40006 slave 2e85ee0391b376a31baed1c6df919646a09721e1 0 1679277369081 3 connected
1ee741c70a319f82e479695a6c5b54e07a2434e3 127.0.0.1:30002@40002 master - 0 1679277369081 2 connected 6128-10922
baa87c527982432ee752fd6f7bf8c75561d19d60 127.0.0.1:30008@40008 slave 332eb73b0c34f911792421164c23eb4550fa1b35 0 1679277368580 7 connected
08562c0f0b2fe5f46e0d96f5d06aaaf97209e3d8 127.0.0.1:30005@40005 slave 1ee741c70a319f82e479695a6c5b54e07a2434e3 0 1679277369081 2 connected
2e85ee0391b376a31baed1c6df919646a09721e1 127.0.0.1:30003@40003 master - 0 1679277369081 3 connected 11589-16383
50830de7dc4d33814b8db237c92ce103b6969aee 127.0.0.1:30001@40001 myself,master - 0 1679277368000 1 connected 666-5460
6、关闭并清理集群
# 关闭集群
./create-cluster stop
# 清理集群
./create-cluster clean
7、查看集群各项性能参数
# redis-cli -c -p 30001 info stats
# Stats
total_connections_received:17
total_commands_processed:5056
instantaneous_ops_per_sec:0
total_net_input_bytes:412791
total_net_output_bytes:102315
total_net_repl_input_bytes:0
total_net_repl_output_bytes:2596
instantaneous_input_kbps:0.02
instantaneous_output_kbps:0.00
instantaneous_input_repl_kbps:0.00
instantaneous_output_repl_kbps:0.00
rejected_connections:0
...
# redis-cli -c -p 30001 info | grep -e "connection_clients" -e "blocked_clients" -e "used_memory_human" -e "used_memory_peak_human" -e "rejected_connections" -e "evicted_keys" -e "instantaneous"
blocked_clients:0
used_memory_human:1.92M
used_memory_peak_human:1.95M
instantaneous_ops_per_sec:0
instantaneous_input_kbps:0.02
instantaneous_output_kbps:0.00
instantaneous_input_repl_kbps:0.00
instantaneous_output_repl_kbps:0.00
rejected_connections:0
evicted_keys:0
参考:
https://redis.io/docs/management/scaling/