redis-快速搭建集群
Posted 青乡
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了redis-快速搭建集群相关的知识,希望对你有一定的参考价值。
下载源码
源码自带了快速搭建集群的脚本create-cluster。
所在目录:redis安装目录/util/create-cluster。
目录包含内容:create-cluster脚本程序。
除了创建集群脚本那个文件,一开始没有其他文件,其他文件都是创建集群成功之后,在当前目录生成的。
目标
用官方自带的脚本创建3主3从集群。
这种快速方法搭建的集群,主要是在本地用于自己测试。
步骤
切换到redis安装目录/util/create-cluster目录
启动进程
启动进程
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$ ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
启动之后,用ps -ef | grep redis命令,可以看到6个redis进程。
虽然启动了6个进程,但是还没有创建节点之间的集群关系。
创建集群
创建集群
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$ ./create-cluster create
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from ../../src/redis-trib.rb:25:in `<main>'
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$
创建集群的时候报错了。原因?没有安装ruby依赖。解决方法?安装ruby依赖。
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$ gem install redis
Fetching: redis-4.2.5.gem (100%)
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory.
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$ sudo gem install redis
Password:
Fetching: redis-4.2.5.gem (100%)
Successfully installed redis-4.2.5
Parsing documentation for redis-4.2.5
Installing ri documentation for redis-4.2.5
Done installing documentation for redis after 1 seconds
1 gem installed
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$
安装ruby依赖成功。
然后再次创建集群
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$ ./create-cluster create
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:30001
127.0.0.1:30002
127.0.0.1:30003
Adding replica 127.0.0.1:30004 to 127.0.0.1:30001
Adding replica 127.0.0.1:30005 to 127.0.0.1:30002
Adding replica 127.0.0.1:30006 to 127.0.0.1:30003
M: e2daf8ae63e645a64d14d4167c0381d46b0e0447 127.0.0.1:30001
slots:0-5460 (5461 slots) master
M: 3e727eb07a42834b639d6f259b120ad97bf8659f 127.0.0.1:30002
slots:5461-10922 (5462 slots) master
M: 1f87f72a47b02faaf243080801a582b958f57f5b 127.0.0.1:30003
slots:10923-16383 (5461 slots) master
S: f630102c69083eb11ee9230e89c6bbf368ded923 127.0.0.1:30004
replicates e2daf8ae63e645a64d14d4167c0381d46b0e0447
S: 0ef557462c465b2c94c35a2f9bdc159e803313ef 127.0.0.1:30005
replicates 3e727eb07a42834b639d6f259b120ad97bf8659f
S: 2da73b14ee2e360becd4fe26f7c6af531d9cc019 127.0.0.1:30006
replicates 1f87f72a47b02faaf243080801a582b958f57f5b
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: e2daf8ae63e645a64d14d4167c0381d46b0e0447 127.0.0.1:30001
slots:0-5460 (5461 slots) master
M: 3e727eb07a42834b639d6f259b120ad97bf8659f 127.0.0.1:30002
slots:5461-10922 (5462 slots) master
M: 1f87f72a47b02faaf243080801a582b958f57f5b 127.0.0.1:30003
slots:10923-16383 (5461 slots) master
M: f630102c69083eb11ee9230e89c6bbf368ded923 127.0.0.1:30004
slots: (0 slots) master
replicates e2daf8ae63e645a64d14d4167c0381d46b0e0447
M: 0ef557462c465b2c94c35a2f9bdc159e803313ef 127.0.0.1:30005
slots: (0 slots) master
replicates 3e727eb07a42834b639d6f259b120ad97bf8659f
M: 2da73b14ee2e360becd4fe26f7c6af531d9cc019 127.0.0.1:30006
slots: (0 slots) master
replicates 1f87f72a47b02faaf243080801a582b958f57f5b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$
创建集群成功,主从节点什么的,还有槽slot什么的,就分配好了。
停止进程
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$ ./create-cluster stop
Stopping 30001
Stopping 30002
Stopping 30003
Stopping 30004
Stopping 30005
Stopping 30006
用ps命令看不到进程,集群也不能读写。
再次启动进程
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$ ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
用ps命令可以看到进程,集群也恢复正常读写。
清除集群节点信息和槽数据
如果不想要了刚才创建的集群,就可以彻底删除。
./create-cluster clean
完整操作日志
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$ pwd
/Users/gongzhihao/IdeaProjects/ll/xxx/redis-3.0.7/utils/create-cluster
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$ ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$ ./create-cluster create
/System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- redis (LoadError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.3/usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from ../../src/redis-trib.rb:25:in `<main>'
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$ gem install redis
Fetching: redis-4.2.5.gem (100%)
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory.
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$ sudo gem install redis
Password:
Fetching: redis-4.2.5.gem (100%)
Successfully installed redis-4.2.5
Parsing documentation for redis-4.2.5
Installing ri documentation for redis-4.2.5
Done installing documentation for redis after 1 seconds
1 gem installed
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$ ./create-cluster create
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:30001
127.0.0.1:30002
127.0.0.1:30003
Adding replica 127.0.0.1:30004 to 127.0.0.1:30001
Adding replica 127.0.0.1:30005 to 127.0.0.1:30002
Adding replica 127.0.0.1:30006 to 127.0.0.1:30003
M: e2daf8ae63e645a64d14d4167c0381d46b0e0447 127.0.0.1:30001
slots:0-5460 (5461 slots) master
M: 3e727eb07a42834b639d6f259b120ad97bf8659f 127.0.0.1:30002
slots:5461-10922 (5462 slots) master
M: 1f87f72a47b02faaf243080801a582b958f57f5b 127.0.0.1:30003
slots:10923-16383 (5461 slots) master
S: f630102c69083eb11ee9230e89c6bbf368ded923 127.0.0.1:30004
replicates e2daf8ae63e645a64d14d4167c0381d46b0e0447
S: 0ef557462c465b2c94c35a2f9bdc159e803313ef 127.0.0.1:30005
replicates 3e727eb07a42834b639d6f259b120ad97bf8659f
S: 2da73b14ee2e360becd4fe26f7c6af531d9cc019 127.0.0.1:30006
replicates 1f87f72a47b02faaf243080801a582b958f57f5b
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: e2daf8ae63e645a64d14d4167c0381d46b0e0447 127.0.0.1:30001
slots:0-5460 (5461 slots) master
M: 3e727eb07a42834b639d6f259b120ad97bf8659f 127.0.0.1:30002
slots:5461-10922 (5462 slots) master
M: 1f87f72a47b02faaf243080801a582b958f57f5b 127.0.0.1:30003
slots:10923-16383 (5461 slots) master
M: f630102c69083eb11ee9230e89c6bbf368ded923 127.0.0.1:30004
slots: (0 slots) master
replicates e2daf8ae63e645a64d14d4167c0381d46b0e0447
M: 0ef557462c465b2c94c35a2f9bdc159e803313ef 127.0.0.1:30005
slots: (0 slots) master
replicates 3e727eb07a42834b639d6f259b120ad97bf8659f
M: 2da73b14ee2e360becd4fe26f7c6af531d9cc019 127.0.0.1:30006
slots: (0 slots) master
replicates 1f87f72a47b02faaf243080801a582b958f57f5b
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$ ./create-cluster stop
Stopping 30001
Stopping 30002
Stopping 30003
Stopping 30004
Stopping 30005
Stopping 30006
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$ ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006
gongzhihaodeMacBook-Pro:create-cluster gongzhihao$
实现原理-脚本包含内容?
其实就是执行了安装目录/src/redis-server和redis-cli脚本
redis-server和redis-cli脚本所在目录
参考
黄建宏的书《redis使用手册》
以上是关于redis-快速搭建集群的主要内容,如果未能解决你的问题,请参考以下文章