Redis叢集部署(redis-trib.rb搭建)

翔之天空發表於2016-11-02

 

一:建立redis節點

 

測試我們選擇2臺伺服器,分別為:192.168.56.56,192.168.56.57每個伺服器有3個節點。

 

在192.168.56.56建立3個節點

 

建立叢集的目錄
[root@localhost ~]# cd /
[root@localhost /]# mkdir redis_cluster
[root@localhost /]# cd redis_cluster/

 
建立節點的目錄
[root@localhost redis_cluster]# mkdir 7000
[root@localhost redis_cluster]# mkdir 7001
[root@localhost redis_cluster]# mkdir 7002

把配置檔案複製到每個節點目錄中
[root@localhost redis_cluster]# cp /redis/redis-3.2.1/redis.conf  /redis_cluster/7000
[root@localhost redis_cluster]# cp /redis/redis-3.2.1/redis.conf  /redis_cluster/7001
[root@localhost redis_cluster]# cp /redis/redis-3.2.1/redis.conf  /redis_cluster/7002

 
依次修改以上的每個redis.conf檔案
vi  redis.conf

daemonize    yes                      //redis後臺執行
pidfile  /var/run/redis_7000.pid      //pidfile檔案對應7000,7001,7002
port  7000                           //埠7000,7001,7002
cluster-enabled  yes                 //開啟叢集  把註釋#去掉
cluster-config-file nodes-7000.conf  //叢集的配置  配置檔案首次啟動自動生成
cluster-node-timeout 5000           //請求超時  設定5秒夠了
bind 192.168.56.56                  //繫結本機的IP
appendonly  yes                     //aof日誌開啟  有需要就開啟,它會每次寫操作都記錄一條日誌

 
依次啟動每個節點
[root@localhost src]# ./redis-server   /redis_cluster/7000/redis.conf
[root@localhost src]# ./redis-server   /redis_cluster/7001/redis.conf
[root@localhost src]# ./redis-server   /redis_cluster/7002/redis.conf

在192.168.56.57伺服器也建立3個節點7003、7004、7005,和上面操作一致,這裡不再做演示。

 

 檢視redis啟動情況

 

在192.168.56.56檢視redis啟動情況,無問題
[root@shanxi 7002]# ps -ef|grep redis
root    15665     1  015:46 ?     00:00:04 ./redis-server 192.168.56.56:7000 [cluster] 
root    15680     1  015:48 ?     00:00:04 ./redis-server 192.168.56.56:7001 [cluster] 
root    15684     1  015:48 ?     00:00:04 ./redis-server 192.168.56.56:7002 [cluster]


在192.168.56.57檢視redis啟動情況,無問題
[root@localhost 7003]# ps -ef|grep redis
root    28183    1  0 15:50 ?     00:00:03 ./redis-server 192.168.56.57:7003 [cluster] 
root    28187     1  015:50 ?     00:00:03 ./redis-server 192.168.56.57:7004 [cluster] 
root    28191     1  0 15:50 ?    00:00:03 ./redis-server 192.168.56.57:7005 [cluster]

 

 

 

 

二:叢集部署

 

注:Redis是用./redis-trib.rb來建立叢集的,需要ruby執行,詳見附一ruby的安裝

 

1、引數如下:

 

[root@localhost src]# ./redis-trib.rb

Usage: redis-trib <command><options> <arguments ...>

create          host1:port1 ... hostN:portN
                  --replicas <arg>
 check           host:port
 info            host:port
 fix             host:port
                  --timeout <arg>
 reshard         host:port
                  --from <arg>
                  --to <arg>
                  --slots <arg>
                  --yes
                  --timeout <arg>
                  --pipeline <arg>
 rebalance       host:port
                  --weight <arg>
                  --auto-weights
                 --use-empty-masters
                  --timeout <arg>
                  --simulate
                  --pipeline <arg>
                  --threshold <arg>
 add-node        new_host:new_portexisting_host:existing_port
                  --slave
                  --master-id <arg>
 del-node        host:port node_id
 set-timeout     host:portmilliseconds
 call            host:port commandarg arg .. arg
 import          host:port
                  --from <arg>
                  --copy
                  --replace
 help            (show this help)
 
For check, fix, reshard, del-node,set-timeout you can specify the host and port of any working node in thecluster.

 

 

 

2、建立叢集

 

[root@localhost src]# /redis/redis-3.2.1/src/redis-trib.rb   create --replicas  1  192.168.56.56:7000 192.168.56.56:7001  192.168.56.56:7002  192.168.56.57:7003  192.168.56.57:7004  192.168.56.57:7005

>>> Creating cluster
>>> Performing hash slotsallocation on 6 nodes...
Using 3 masters:
192.168.56.56:7000
192.168.56.57:7003
192.168.56.56:7001
Adding replica 192.168.56.57:7004 to 192.168.56.56:7000
Adding replica 192.168.56.56:7002 to192.168.56.57:7003
Adding replica 192.168.56.57:7005 to192.168.56.56:7001
M: d2bf521e5a9b53c069da7507aafd69538ef693b5192.168.56.56:7000
  slots:0-5460 (5461 slots) master
M: 3eb2c64654dc0559e83498753cd674805ba5fa42192.168.56.56:7001
  slots:10923-16383 (5461 slots) master
S: d2ced0c0f1cf9158cdd40cdd910dacd4283ff6c1192.168.56.56:7002
  replicates 5b75b320e858ec83fc39a40666700964ff598729
M: 5b75b320e858ec83fc39a40666700964ff598729192.168.56.57:7003
  slots:5461-10922 (5462 slots) master
S: 20061dd97978756fa93dfe969a1d3acb3721b7a1192.168.56.57:7004
  replicates d2bf521e5a9b53c069da7507aafd69538ef693b5
S: 70648f5b00ae17f2ca85adc405066aa7ef4d8007192.168.56.57:7005
  replicates 3eb2c64654dc0559e83498753cd674805ba5fa42
Can I set the above configuration? (type'yes' to accept): yes
>>> Nodes configuration updated
>>> Assign a different configepoch to each node
>>> Sending CLUSTER MEET messagesto join the cluster
Waiting for the cluster to join..
>>> Performing Cluster Check(using node 192.168.56.56:7000)
M: d2bf521e5a9b53c069da7507aafd69538ef693b5192.168.56.56:7000
  slots:0-5460 (5461 slots) master
M: 3eb2c64654dc0559e83498753cd674805ba5fa42192.168.56.56:7001
  slots:10923-16383 (5461 slots) master
M: d2ced0c0f1cf9158cdd40cdd910dacd4283ff6c1192.168.56.56:7002
  slots: (0 slots) master
  replicates 5b75b320e858ec83fc39a40666700964ff598729
M: 5b75b320e858ec83fc39a40666700964ff598729192.168.56.57:7003
  slots:5461-10922 (5462 slots) master
M: 20061dd97978756fa93dfe969a1d3acb3721b7a1192.168.56.57:7004
  slots: (0 slots) master
  replicates d2bf521e5a9b53c069da7507aafd69538ef693b5
M: 70648f5b00ae17f2ca85adc405066aa7ef4d8007192.168.56.57:7005
  slots: (0 slots) master
  replicates 3eb2c64654dc0559e83498753cd674805ba5fa42
[OK] All nodes agree about slotsconfiguration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

 

 

 

3、測試

 

在192.168.56.56主機登入192.168.56.57的埠為7005的redis服務,設定一個key為cluster,value為hahaha。
[root@shanxi src]# ./redis-cli  -c -h 192.168.56.57 -p 7005
192.168.56.57:7005> set cluster"hahaha"
-> Redirected to slot [14041] located at192.168.56.56:7001
OK

 
在192.168.56.57主機登入192.168.56.56的埠為7000的redis服務,可以取得cluster的值。
[root@localhost src]# ./redis-cli   -c -h 192.168.56.56 -p 7000
192.168.56.56:7000> get cluster
-> Redirected to slot [14041] located at192.168.56.56:7001
"hahaha"

 

 

 

附一:ruby的安裝

 

1、安裝ruby , ruby下載地址:http://www.ruby-lang.org/en/downloads/

 

下載 Ruby 之後,解壓到新建立的目錄下:
[root@localhost redis]# tar -xvf ruby-2.3.1.tar
[root@localhost redis]# cd ruby-2.3.1

 
現在,配置並編譯原始碼,如下所示:
[root@localhost ruby-2.3.1]# ./configure
[root@localhost ruby-2.3.1]# make
[root@localhost ruby-2.3.1]# sudo make install

 
安裝後,通過在命令列中輸入以下命令來確保一切工作正常:
[root@localhost ruby-2.3.1]# ruby -v
ruby 2.3.1p112 (2016-04-26 revision 54768)[x86_64-linux]

 

2、安裝RubyGems,  RubyGems下載地址:https://rubygems.org/pages/download

 

 

下載 RubyGems 之後,解壓到新建立的目錄下:
[root@localhost redis]# unzip rubygems-2.6.8.zip

執行命令
[root@localhostrubygems-2.6.8]# ruby setup.rb

 

3、安裝RubyGems 後,用gem來安裝 redis介面   gem是ruby的一個工具包

 

 

[root@localhost rubygems-2.6.8]# gem install redis
ERROR: While executing gem ... (Gem::Exception)
Unable torequire openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPSsources


如上報錯,沒有redis包,到taobao下載一個,下載地址:https://rubygems.org/gems/redis

 
再次安裝無問題。
[root@localhost gems]# gem install redis
Successfully installed redis-3.3.1
Parsing documentation for redis-3.3.1
Done installing documentation for redisafter 0 seconds
1 gem installed

 

 

 

 

相關文章