環境準備

  1. centos6.7
  2. redis3.2.8

安裝步驟

  • 第二步: 解壓壓縮包到中/inner_dev_env/redis-3.2.8

[root@allere /inner_dev_env/redis-3.2.8]# pwd
/inner_dev_env/redis-3.2.8
  • 第三步: 新建一個目錄:redis-3.2.8-cluster(名稱隨意,位置任意)。然後再該目錄下新建叢集執行目錄(名稱任意)
mkdir redis-3.2.8-cluster
cd redis-3.2.8-cluster
mkdir 7000 7001 7002 7003 7004 7005
[root@allere /inner_dev_env/redis-3.2.8-cluster]# pwd
/inner_dev_env/redis-3.2.8-cluster
[root@allere /inner_dev_env/redis-3.2.8-cluster]# ls
7000  7001  7002  7003  7004  7005 
  • 第四步:修改redis的配置檔案redis.conf
[root@allere /inner_dev_env/redis-3.2.8-cluster]# vi /inner_dev_env/redis-3.2.8/redis.conf

讓一個叢集跑起來,只需以下五個引數
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes

由於預設的redis.conf檔案已經啟動了以下三個引數
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
因此只需要修改port為對應的啟動埠和啟動cluser-enable即可。

  82 # Accept connections on the specified port, default is 6379 (IANA #815344).
  83 # If port 0 is specified Redis will not listen on a TCP socket.
  84 port 7000
  717 # Normal Redis instances can't be part of a Redis Cluster; only nodes that are
  718 # started as cluster nodes can. In order to start a Redis instance as a
  719 # cluster node enable the cluster support uncommenting the following:
  720 #
  721 cluster-enabled yes
  • 第五步:redis.conf檔案拷貝到對應的埠的資料夾下,並且修改port這個引數為對應的埠號
[root@allere /inner_dev_env/redis-3.2.8]# cp ./redis.conf /inner_dev_env/redis-3.2.8-cluster/7000

[root@allere /inner_dev_env/redis-3.2.8]# cp ./redis.conf /inner_dev_env/redis-3.2.8-cluster/7001

[root@allere /inner_dev_env/redis-3.2.8]# cp ./redis.conf /inner_dev_env/redis-3.2.8-cluster/7002

[root@allere /inner_dev_env/redis-3.2.8]# cp ./redis.conf /inner_dev_env/redis-3.2.8-cluster/7003

[root@allere /inner_dev_env/redis-3.2.8]# cp ./redis.conf /inner_dev_env/redis-3.2.8-cluster/7004

[root@allere /inner_dev_env/redis-3.2.8]# cp ./redis.conf /inner_dev_env/redis-3.2.8-cluster/7005
  • 第六步:將可執行檔案redis-server拷貝到redis-3.2.8-cluster資料夾下,用於執行每個redis例項。
[root@allere /inner_dev_env/redis-3.2.8]# cp ./src/redis-server /inner_dev_env/redis-3.2.8-cluster/
  • 第七步:切換到對應檔案下啟動每個redis例項
[root@allere /inner_dev_env/redis-3.2.8-cluster]# cd 7000
[root@allere /inner_dev_env/redis-3.2.8-cluster/7000]# ../redis-server ./redis.conf 
[root@allere /inner_dev_env/redis-3.2.8-cluster]# cd 7001
[root@allere /inner_dev_env/redis-3.2.8-cluster/7000]# ../redis-server ./redis.con
[root@allere /inner_dev_env/redis-3.2.8-cluster]# cd 7002
[root@allere /inner_dev_env/redis-3.2.8-cluster/7000]# ../redis-server ./redis.con
[root@allere /inner_dev_env/redis-3.2.8-cluster]# cd 7003
[root@allere /inner_dev_env/redis-3.2.8-cluster/7000]# ../redis-server ./redis.con
[root@allere /inner_dev_env/redis-3.2.8-cluster]# cd 7004
[root@allere /inner_dev_env/redis-3.2.8-cluster/7000]# ../redis-server ./redis.con
[root@allere /inner_dev_env/redis-3.2.8-cluster]# cd 7005
[root@allere /inner_dev_env/redis-3.2.8-cluster/7000]# ../redis-server ./redis.con
  • 第八步:建立叢集。
    將跑起來的所有redis例項,組成叢集是通過src/包下面的redis-trib.rb這個工具完成的。但是redis-trib.rb需要ruby環境,所以先要安裝ruby環境(安裝ruby環境可能會報錯,本文附錄給出瞭解決方法),然後再執行redis-trib命令建立叢集。
[root@allere /inner_dev_env/redis-3.2.8/src]# ./redis-trib.rb create --replicas 1 127.0.0.1:7000 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003
 127.0.0.1:7004 127.0.0.1:7005

8.1執行上面的命令的時候會報錯,因為是執行的ruby的指令碼,需要ruby的環境
錯誤內容:/usr/bin/env: ruby: No such file or directory
所以需要安裝ruby的環境,這裡推薦使用yum install ruby安裝
yum install ruby

8.2然後再執行第八步的建立叢集命令,還會報錯,提示缺少rubygems元件,使用yum安裝

錯誤內容:
./redis-trib.rb:24:in require': no such file to load -- rubygems (LoadError)
from ./redis-trib.rb:24
yum install rubygems
8.3再次執行第八步的命令,還會報錯,提示不能載入redis,是因為缺少redis和ruby的介面,使用gem 安裝
錯誤內容:
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in
gem_original_require’: no such file to load – redis (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require’
from ./redis-trib.rb:25

gem install redis
注意:gem install redis 失敗的話,需要修改一下gem的源
gem sources –remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/

8.4執行第八步的時候,彈出提示輸入yes既可以,至此完成安裝。

測試

  • 第一步:連線上叢集。注意需要加上-c引數
[root@allere /inner_dev_env/redis-3.2.8]# src/redis-cli -p 7000 -c
127.0.0.1:7000> 
  • 第二步:進行一些簡單操作,檢視叢集狀態等…
127.0.0.1:7000> set key1 value1
-> Redirected to slot [9189] located at 192.168.0.102:7001
OK
192.168.0.102:7001> get key1
"value1"
192.168.0.102:7001> cluster nodes
99e6b250fd5c3622b6753cd8a3c5ef6ab946a5ec 192.168.0.102:7002 master - 0 1494483809589 3 connected 10923-16383
d13a6d014297d7164347f6ff07ceb50bce0fabdc 192.168.0.102:7004 slave 8ea553e0f9f502c01fc9565128085756ed2988e7 0 1494483811190 5 connected
1e51d8a6704e3c07276ba33c28d379f94b5544b7 192.168.0.102:7005 slave 99e6b250fd5c3622b6753cd8a3c5ef6ab946a5ec 0 1494483810088 6 connected
42b2c7f7afc2850199dee632bc02dc19e3d6c268 192.168.0.102:7003 slave a65d71bce6ea49032410378eb527973bc2249398 0 1494483810588 4 connected
a65d71bce6ea49032410378eb527973bc2249398 192.168.0.102:7000 master - 0 1494483809088 1 connected 0-5460
8ea553e0f9f502c01fc9565128085756ed2988e7 192.168.0.102:7001 myself,master - 0 0 2 connected 5461-10922
192.168.0.102:7001> 
  • 第三步:這6個節點的叢集搭建在一個獨立的主機上的(本例子主機ip是192.168.0.102),如果你從另外的一臺主機(我本地電腦的ip是192.168.0.36)訪問該叢集,進行操作需要進行額外的配置。登入每一個redis例項,進行類似如下的操作
[root@allere /inner_dev_env/redis-3.2.8]# src/redis-cli -p 7000 -c
127.0.0.1:7000> cluster meet 192.168.0.102 7001
OK
127.0.0.1:7000> cluster meet 192.168.0.102 7002
OK
127.0.0.1:7000> cluster meet 192.168.0.102 7003
OK
127.0.0.1:7000> cluster meet 192.168.0.102 7004
OK
127.0.0.1:7000> cluster meet 192.168.0.102 7005
OK
127.0.0.1:7000> 

總結

linux開放服務埠的設定

/etc/sysconfig/iptables

vi iptables

加入如下內容

 -A INPUT -p tcp -m tcp --dport 6001 -j ACCEPT 
然後儲存退出,重啟服務
/etc/init.d/iptables restarts

2 安裝ruby環境報錯 :執行gem install redis無法成功


8.1執行上面的命令的時候會報錯,因為是執行的ruby的指令碼,需要ruby的環境
錯誤內容:/usr/bin/env: ruby: No such file or directory
所以需要安裝ruby的環境,這裡推薦使用yum install ruby安裝
yum install ruby

8.2然後再執行第八步的建立叢集命令,還會報錯,提示缺少rubygems元件,使用yum安裝

錯誤內容:
./redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
from ./redis-trib.rb:24
yum install rubygems
8.3再次執行第八步的命令,還會報錯,提示不能載入redis,是因為缺少redis和ruby的介面,使用gem 安裝
錯誤內容:
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': no such file to load -- redis (LoadError)
from /usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from ./redis-trib.rb:25

gem install  redis 
注意:gem install redis 失敗的話,需要修改一下gem的源
gem sources --remove https://rubygems.org/
gem sources -a https://ruby.taobao.org/

3 遠端連線上了redis但不能夠使用:
程式一直卡在瞭如下所示的地方

E:\software\Redis>redis-cli.exe -c -h 192.168.0.102 -p
192.168.0.102:7000> get name
-> Redirected to slot [5798] located at 127.0.0.1:7001

解決方法

[root@allere /inner_dev_env/redis-3.2.8]# src/redis-cli -p 7000 -c
127.0.0.1:7000> cluster meet 192.168.0.102 7001
OK

附錄