用redis-trib.rb搭建集群
redis-trib.rb是采用Ruby实现的Redis集群管理工具。内部通过Cluster相关命令帮我们简化集群创建、检查、槽迁移和均衡等常见运维操作,使用之
前需要安装Ruby依赖环境。下面介绍搭建集群的详细步骤。
1.Ruby环境准备
安装Ruby:
-- 下载 ruby
wget https:// cache.ruby-lang.org/pub/ruby/2.3/ruby-2.3.1.tar.gz
-- 安装 ruby
tar xvf ruby-2.3.1.tar.gz
./configure -prefix=/usr/local/ruby
make
make install
cd /usr/local/ruby
sudo cp bin/ruby /usr/local/bin
sudo cp bin/gem /usr/local/bin
安装rubygem redis依赖:
wget http:// rubygems.org/downloads/redis-3.3.0.gem
gem install -l redis-3.3.0.gem
gem list --check redis gem
安装redis-trib.rb:
sudo cp /{redis_home}/src/redis-trib.rb /usr/local/bin
安装完Ruby环境后,执行redis-trib.rb命令确认环境是否正确,输出如
下:
# redis-trib.rb
Usage: redis-trib
create host1:port1 ... hostN:portN
--replicas
check host:port
info host:port
fix host:port
--timeout
reshard host:port
--from
--to
--slots
--yes
--timeout
--pipeline
从redis-trib.rb的提示信息可以看出,它提供了集群创建、检查、修复、均衡等命令行工具。这里我们关注集群创建命令,使用redis-trib.rb create命
令可快速搭建集群。
2.准备节点
首先我们跟之前内容一样准备好节点配置并启动:
[root@redis01 config]# more redis-7000.conf
port 7000
daemonize yes
bind 0.0.0.0
dir "/opt/moudels/redis/data"
logfile "7000.log"
dbfilename "dump-7000.rdb"
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-require-full-coverage no
[root@redis01 config]#[root@redis01 config]# sed 's/7000/8000/g' redis-7000.conf > redis-8000.conf
[root@redis01 config]# sed 's/7000/8001/g' redis-7000.conf > redis-8001.conf
[root@redis01 config]# sed 's/7000/8002/g' redis-7000.conf > redis-8002.conf
[root@redis01 config]# sed 's/7000/8003/g' redis-7000.conf > redis-8003.conf
[root@redis01 config]# sed 's/7000/8004/g' redis-7000.conf > redis-8004.conf
[root@redis01 config]# sed 's/7000/8005/g' redis-7000.conf > redis-8005.conf[root@redis01 config]# redis-server redis-8000.conf
[root@redis01 config]# redis-server redis-8001.conf
[root@redis01 config]# redis-server redis-8002.conf
[root@redis01 config]# redis-server redis-8003.conf
[root@redis01 config]# redis-server redis-8004.conf
[root@redis01 config]# redis-server redis-8005.conf
[root@redis01 config]# redis-trib.rb create --replicas 1 127.0.0.1:8000 127.0.0.1:8001 127.0.0.1:8002 127.0.0.1:8003 127.0.0.1:8004 127.0.0.1:8005
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:8000
127.0.0.1:8001
127.0.0.1:8002
Adding replica 127.0.0.1:8003 to 127.0.0.1:8000
Adding replica 127.0.0.1:8004 to 127.0.0.1:8001
Adding replica 127.0.0.1:8005 to 127.0.0.1:8002
M: d653ca291599b514871b8b8727eff044e3ac7fb1 127.0.0.1:8000
slots:0-5460 (5461 slots) master
M: 1719a132939ec05bf84a549966d0f16fa9a9416f 127.0.0.1:8001
slots:5461-10922 (5462 slots) master
M: 8f7c559989daaa0c0a0c831a55df9c245527d8ba 127.0.0.1:8002
slots:10923-16383 (5461 slots) master
S: 9f978edc42ef1fc3dc71bdb1294f52371879a1d1 127.0.0.1:8003
replicates d653ca291599b514871b8b8727eff044e3ac7fb1
S: 002a938f0a3f5d274068e43eb1cbb9bc913b5f99 127.0.0.1:8004
replicates 1719a132939ec05bf84a549966d0f16fa9a9416f
S: cd8cc04e2b1fe8bc02f7892ec9b12491dba10ea5 127.0.0.1:8005
replicates 8f7c559989daaa0c0a0c831a55df9c245527d8ba
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:8000)
M: d653ca291599b514871b8b8727eff044e3ac7fb1 127.0.0.1:8000
slots:0-5460 (5461 slots) master
M: 1719a132939ec05bf84a549966d0f16fa9a9416f 127.0.0.1:8001
slots:5461-10922 (5462 slots) master
M: 8f7c559989daaa0c0a0c831a55df9c245527d8ba 127.0.0.1:8002
slots:10923-16383 (5461 slots) master
M: 9f978edc42ef1fc3dc71bdb1294f52371879a1d1 127.0.0.1:8003
slots: (0 slots) master
replicates d653ca291599b514871b8b8727eff044e3ac7fb1
M: 002a938f0a3f5d274068e43eb1cbb9bc913b5f99 127.0.0.1:8004
slots: (0 slots) master
replicates 1719a132939ec05bf84a549966d0f16fa9a9416f
M: cd8cc04e2b1fe8bc02f7892ec9b12491dba10ea5 127.0.0.1:8005
slots: (0 slots) master
replicates 8f7c559989daaa0c0a0c831a55df9c245527d8ba
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.3.创建集群
启动好6个节点之后,使用redis-trib.rb create命令完成节点握手和槽分配过程,命令如下:
redis-trib.rb create --replicas 1 127.0.0.1:6481 127.0.0.1:6482 127.0.0.1:6483 127.0.0.1:6484 127.0.0.1:6485 127.0.0.1:6486
--replicas参数指定集群中每个主节点配备几个从节点,这里设置为1。
我们出于测试目的使用本地IP地址127.0.0.1,如果部署节点使用不同的IP地址,redis-trib.rb会尽可能保证主从节点不分配在同一机器下,因此会重新排序节点列表顺序。节点列表顺序用于确定主从角色,先主节点之后是从节点。创建过程中首先会给出主从节点角色分配的计划,如下所示。
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
127.0.0.1:6481
127.0.0.1:6482
127.0.0.1:6483
Adding replica 127.0.0.1:6484 to 127.0.0.1:6481
Adding replica 127.0.0.1:6485 to 127.0.0.1:6482
Adding replica 127.0.0.1:6486 to 127.0.0.1:6483
M: 869de192169c4607bb886944588bc358d6045afa 127.0.0.1:6481
slots:0-5460 (5461 slots) master
M: 6f9f24923eb37f1e4dce1c88430f6fc23ad4a47b 127.0.0.1:6482
slots:5461-10922 (5462 slots) master
M: 6228a1adb6c26139b0adbe81828f43a4ec196271 127.0.0.1:6483
slots:10923-16383 (5461 slots) master
S: 22451ea81fac73fe7a91cf051cd50b2bf308c3f3 127.0.0.1:6484
replicates 869de192169c4607bb886944588bc358d6045afa
S: 89158df8e62958848134d632e75d1a8d2518f07b 127.0.0.1:6485
replicates 6f9f24923eb37f1e4dce1c88430f6fc23ad4a47b
S: bcb394c48d50941f235cd6988a40e469530137af 127.0.0.1:6486
replicates 6228a1adb6c26139b0adbe81828f43a4ec196271
Can I set the above configuration (type 'yes' to accept):
当我们同意这份计划之后输入yes,redis-trib.rb开始执行节点握手和槽
分配操作,输出如下:
>>> 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:6481)
... 忽略 ...
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.
最后输出报告说明:16384个槽全部被分配,集群创建成功。这里需要注意给redis-trib.rb的节点地址必须是不包含任何槽/数据的节点,否则会拒绝创建集群。
4.集群完整性检查
集群完整性指所有的槽都分配到存活的主节点上,只要16384个槽中有一个没有分配给节点则表示集群不完整。可以使用redis-trib.rb check命令检
测之前创建的两个集群是否成功,check命令只需要给出集群中任意一个节点地址就可以完成整个集群的检查工作,命令如下:
redis-trib.rb check 127.0.0.1:6379
redis-trib.rb check 127.0.0.1:6481
当最后输出如下信息,提示集群所有的槽都已分配到节点:
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.