笔者使用ubuntu操作系统
下载redis地址:
Index of /releases/, 选择最新的版本下载。
解压后进入目录, 直接make就可以编译。编译成功后在src目录下会生成redis-server和redis-cli可执行文件。
进入redis目录下的utils/create-cluster目录,执行./create-cluster start, 快速启动6个实例
zy@zy-VirtualBox:~/software/redis-7.4.2/utils/create-cluster$ ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
运行 ./create-cluster create 把以上创建的6个实例组成一个集群
zy@zy-VirtualBox:~/software/redis-7.4.2/utils/create-cluster$ ./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: ee10799169a07eccbafe1d7688212401e29be791 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
M: ebf6cdf9a5e30f480264f44e56c33cfc4bc517ae 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
M: f135487a1a994bc2e99e4c4f89fddbf541a08f34 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
S: 82adfdbef3f803488644bf0778c5a4ffaa0101a5 127.0.0.1:30004
replicates ebf6cdf9a5e30f480264f44e56c33cfc4bc517ae
S: ea2299f8d282f3ae8e7c7c6e13f76407fce3db26 127.0.0.1:30005
replicates f135487a1a994bc2e99e4c4f89fddbf541a08f34
S: 7bf298dc1db0e4d3bbe626a35c0ee28fa1d5d239 127.0.0.1:30006
replicates ee10799169a07eccbafe1d7688212401e29be791
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: ee10799169a07eccbafe1d7688212401e29be791 127.0.0.1:30001
slots:[0-5460] (5461 slots) master
1 additional replica(s)
S: ea2299f8d282f3ae8e7c7c6e13f76407fce3db26 127.0.0.1:30005
slots: (0 slots) slave
replicates f135487a1a994bc2e99e4c4f89fddbf541a08f34
M: ebf6cdf9a5e30f480264f44e56c33cfc4bc517ae 127.0.0.1:30002
slots:[5461-10922] (5462 slots) master
1 additional replica(s)
M: f135487a1a994bc2e99e4c4f89fddbf541a08f34 127.0.0.1:30003
slots:[10923-16383] (5461 slots) master
1 additional replica(s)
S: 82adfdbef3f803488644bf0778c5a4ffaa0101a5 127.0.0.1:30004
slots: (0 slots) slave
replicates ebf6cdf9a5e30f480264f44e56c33cfc4bc517ae
S: 7bf298dc1db0e4d3bbe626a35c0ee28fa1d5d239 127.0.0.1:30006
slots: (0 slots) slave
replicates ee10799169a07eccbafe1d7688212401e29be791
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
再返回redis目录,进入src目录,运行./redis-cli -c -p 30001,成功后redis客户端连上了刚才创建的redis集群。
zy@zy-VirtualBox:~/software/redis-7.4.2/src$ ./redis-cli -c -p 30001
127.0.0.1:30001>
测试插入键值和获取键值
127.0.0.1:30001> set name xiaoming
-> Redirected to slot [5798] located at 127.0.0.1:30002
OK
127.0.0.1:30002> get name
"xiaoming"
127.0.0.1:30002>