redis-trib.rb是採用ruby實現的redis叢集管理工具。內部通過cluster相關命令幫我們簡化叢集建立,檢查,槽遷移和均衡等常見的營運操作
1.ruby安裝
下載地址: wget https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.1.tar.gz
./configure -prefix=/usr/local/ruby make make install cd /usr/local/ruby cp /usr/local/ruby/bin/ruby /usr/local/bin cp /usr/local/ruby/bin/gem /usr/local/bin |
下載: https://rubygems.org/downloads/redis-4.0.1.gem
[root@localhost tools]# gem install -l redis-4.0.1.gem ERROR: Loading command: install (LoadError) cannot load such file -- zlib ERROR: While executing gem ... (NoMethodError) undefined method `invoke_with_build_args' for nil:NilClass |
解決辦法是:
yum -y install zlib-devel 進入ruby源碼檔案夾 安裝ruby自身提供的zlib包 cd ruby-2.5.1/ext/zlib ruby ./extconf.rb [root@localhost zlib]# make make: *** No rule to make target `/include/ruby.h', needed by `zlib.o'. Stop. 這個時候開啟ext/zlib/Makefile檔案,找到下面一行把路徑進行修改一下。 #zlib.o: $(top_srcdir)/include/ruby.h zlib.o: ../../include/ruby.h |
make make install
2.安裝
[root@localhost tools]# gem install -l redis-4.0.1.gem gem list --check redis gem cp /usr/local/redis/src/redis-trib.rb /usr/local/bin/ |
3. 安裝ruby環境後,執行redis-trib.rb命令確認是否正確
[root@localhost src]# redis-trib.rb Usage: redis-trib <command> <options> <arguments ...> create host1:port1 ... hostN:portN --replicas <arg> ...... |
可以使用redi-trib.rb create命令可以快速搭建叢集
4.準備節點 配置設定檔,並啟動
port 6000 daemonize yes logfile "6000.log" dir "/data/redis/data/" dbfilename dump-6000.rdb cluster-enabled yes #開啟叢集模式 cluster-node-timeout 15000 #節點逾時時間,單位毫秒 cluster-config-file "nodes-6000.conf" #叢集內部設定檔 |
5.啟動所有節點
redis-server /data/redis/conf/redis-6001.conf redis-server /data/redis/conf/redis-6002.conf redis-server /data/redis/conf/redis-6003.conf redis-server /data/redis/conf/redis-6004.conf redis-server /data/redis/conf/redis-6005.conf redis-server /data/redis/conf/redis-6006.conf |
6.建立叢集 啟動好6個節點後,使用redis-trib.rb create命令完成握手和分配槽的過程
redis-trib.rb create --replicas 1 127.0.0.1:6001 127.0.0.1:6002 127.0.0.1:6003 127.0.0.1:6004 127.0.0.1:6005 127.0.0.1:6006
|
>>> Creating cluster >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 127.0.0.1:6001 127.0.0.1:6002 127.0.0.1:6003 Adding replica 127.0.0.1:6004 to 127.0.0.1:6001 Adding replica 127.0.0.1:6005 to 127.0.0.1:6002 Adding replica 127.0.0.1:6006 to 127.0.0.1:6003 M: be3270c92ba98c1b67ce4c76569680e1ce873a10 127.0.0.1:6001 slots:0-5460 (5461 slots) master M: 59fe00990b0675fbf9b5693aa875ab94b1a642ce 127.0.0.1:6002 |