1. 程式人生 > >Redis-Cluster 叢集搭建(三主三從)

Redis-Cluster 叢集搭建(三主三從)

搭建Ruby

yum install ruby
yum install rubygems

安裝redis 和 gcc,tcl

[[email protected] src]# yum install -y gcc-c++
[[email protected] src]# wget http://download.redis.io/releases/redis-3.0.0.tar.gz
[[email protected] src]# tar -xvzf redis-2.8.3.tar.gz -C /agui/redis/

[[email protected]
redis-3.0.0]# ll total 144 -rw-rw-r-- 1 root root 25890 Apr 1 2015 00-RELEASENOTES -rw-rw-r-- 1 root root 53 Apr 1 2015 BUGS -rw-rw-r-- 1 root root 1439 Apr 1 2015 CONTRIBUTING -rw-rw-r-- 1 root root 1487 Apr 1 2015 COPYING drwxrwxr-x 6 root root 4096 Apr 1 2015 deps -rw-rw-r-- 1 root root 11
Apr 1 2015 INSTALL
-rw-rw-r-- 1 root root 151 Apr 1 2015 Makefile -rw-rw-r-- 1 root root 4223 Apr 1 2015 MANIFESTO -rw-rw-r-- 1 root root 5201 Apr 1 2015 README -rw-rw-r-- 1 root root 41403 Apr 1 2015 redis.conf -rwxrwxr-x 1 root root 271 Apr 1 2015 runtest -rwxrwxr-x 1 root root 280
Apr 1 2015 runtest-cluster
-rwxrwxr-x 1 root root 281 Apr 1 2015 runtest-sentinel -rw-rw-r-- 1 root root 7109 Apr 1 2015 sentinel.conf drwxrwxr-x 2 root root 4096 Apr 1 2015 src drwxrwxr-x 10 root root 4096 Apr 1 2015 tests drwxrwxr-x 5 root root 4096 Apr 1 2015 utils [[email protected] src]# yum install tcl [[email protected] redis-3.0.0]# make //最後如下則完成呢 CC latency.o CC sparkline.o LINK redis-server INSTALL redis-sentinel CC redis-cli.o LINK redis-cli CC redis-benchmark.o LINK redis-benchmark CC redis-check-dump.o LINK redis-check-dump CC redis-check-aof.o LINK redis-check-aof Hint: It's a good idea to run 'make test' ;) make[1]: Leaving directory `/agui/redis/redis-3.0.0/src' [[email protected] redis-3.0.0]# make install PREFIX=/agui/redis cd src && make install make[1]: Entering directory `/agui/redis/redis-3.0.0/src' Hint: It's a good idea to run 'make test' ;) INSTALL install INSTALL install INSTALL install INSTALL install INSTALL install make[1]: Leaving directory `/agui/redis/redis-3.0.0/src' [[email protected] redis]# cd bin [[email protected] bin]# ll total 8492 -rwxr-xr-x 1 root root 2075208 Mar 16 13:56 redis-benchmark -rwxr-xr-x 1 root root 25216 Mar 16 13:56 redis-check-aof -rwxr-xr-x 1 root root 56064 Mar 16 13:56 redis-check-dump -rwxr-xr-x 1 root root 2199368 Mar 16 13:56 redis-cli lrwxrwxrwx 1 root root 12 Mar 16 13:56 redis-sentinel -> redis-server -rwxr-xr-x 1 root root 4331072 Mar 16 13:56 redis-server [[email protected] bin]# cd ../ [[email protected] redis]# cd redis-3.0.0/ [[email protected] redis-3.0.0]# ll total 144 -rw-rw-r-- 1 root root 25890 Apr 1 2015 00-RELEASENOTES -rw-rw-r-- 1 root root 53 Apr 1 2015 BUGS -rw-rw-r-- 1 root root 1439 Apr 1 2015 CONTRIBUTING -rw-rw-r-- 1 root root 1487 Apr 1 2015 COPYING drwxrwxr-x 6 root root 4096 Mar 16 13:38 deps -rw-rw-r-- 1 root root 11 Apr 1 2015 INSTALL -rw-rw-r-- 1 root root 151 Apr 1 2015 Makefile -rw-rw-r-- 1 root root 4223 Apr 1 2015 MANIFESTO -rw-rw-r-- 1 root root 5201 Apr 1 2015 README -rw-rw-r-- 1 root root 41403 Apr 1 2015 redis.conf -rwxrwxr-x 1 root root 271 Apr 1 2015 runtest -rwxrwxr-x 1 root root 280 Apr 1 2015 runtest-cluster -rwxrwxr-x 1 root root 281 Apr 1 2015 runtest-sentinel -rw-rw-r-- 1 root root 7109 Apr 1 2015 sentinel.conf drwxrwxr-x 2 root root 4096 Mar 16 13:39 src drwxrwxr-x 10 root root 4096 Apr 1 2015 tests drwxrwxr-x 5 root root 4096 Apr 1 2015 utils [[email protected] redis-3.0.0]# cp redis.conf /agui/redis/bin/ [[email protected] redis-3.0.0]# cd ../bin/ [[email protected] bin]# ll total 8536 -rwxr-xr-x 1 root root 2075208 Mar 16 13:56 redis-benchmark -rwxr-xr-x 1 root root 25216 Mar 16 13:56 redis-check-aof -rwxr-xr-x 1 root root 56064 Mar 16 13:56 redis-check-dump -rwxr-xr-x 1 root root 2199368 Mar 16 13:56 redis-cli -rw-r--r-- 1 root root 41403 Mar 16 14:03 redis.conf lrwxrwxrwx 1 root root 12 Mar 16 13:56 redis-sentinel -> redis-server -rwxr-xr-x 1 root root 4331072 Mar 16 13:56 redis-server [[email protected] bin]# vim redis.conf //修改redis.conf 中的 daemonize 為 yes //如下就可以後端啟動了 [[email protected] bin]# ./redis-server redis.conf [[email protected] bin]# ps -ef|grep redis root 5311 1 0 14:07 ? 00:00:00 ./redis-server *:6379 root 5315 2184 0 14:07 pts/0 00:00:00 grep --color=auto redis [[email protected] bin]# cd ../redis-3.0.0/src/ [[email protected] src]# ll *.rb -rwxrwxr-x 1 root root 48141 Apr 1 2015 redis-trib.rb [[email protected] src]# //將該檔案傳到伺服器:redis-3.0.0.gem [[email protected] redis]# gem install redis-3.0.0.gem Successfully installed redis-3.0.0 Parsing documentation for redis-3.0.0 Installing ri documentation for redis-3.0.0 1 gem installed [[email protected] redis]# //以上ruby 環境裝好了。 //複製一份 redis install 後的 bin 資料夾。 //修改複製後文件夾中的 redis.conf , 修改埠為 6380 ,開啟 cluster-enabled yes //我是三臺伺服器,每一臺上有一個主一個從,所以我要複製三分,修改三次。 //將我們redis-trib.rb複製出來好操作(不復制也行)。 [[email protected] redis]# ll total 72 drwxr-xr-x 2 root root 4096 Mar 16 15:15 bin drwxr-xr-x 2 root root 4096 Mar 16 15:16 bin-slave drwxrwxr-x 6 root root 4096 Apr 1 2015 redis-3.0.0 -rw-r--r-- 1 root root 57856 Mar 16 14:35 redis-3.0.0.gem [[email protected] redis]# cp redis-3.0.0/src/*.rb /agui/redis/ [[email protected] redis]# ll total 120 drwxr-xr-x 2 root root 4096 Mar 16 15:15 bin drwxr-xr-x 2 root root 4096 Mar 16 15:16 bin-slave drwxrwxr-x 6 root root 4096 Apr 1 2015 redis-3.0.0 -rw-r--r-- 1 root root 57856 Mar 16 14:35 redis-3.0.0.gem -rwxr-xr-x 1 root root 48141 Mar 16 15:21 redis-trib.rb [[email protected] redis]# //啟動6個redis例項 // 開始建立叢集: // 說明: 如果叢集帶密碼需要找到ruby 安裝的 client.rb 檔案,去設定密碼 // [[email protected] redis]# find / -name "client.rb" /usr/share/ruby/xmlrpc/client.rb /usr/local/share/gems/gems/redis-3.0.0/lib/redis/client.rb //[[email protected] redis]# vim 上面的路徑,編輯 password= “43jnrwer34tr5d6”,儲存即可。所有的都要改。 ./redis-trib.rb create --replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 192.168.25.153:7004 192.168.25.153:7005 192.168.25.153:7006 [[email protected] redis-cluster]# ./redis-trib.rb create --replicas 1 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 192.168.25.153:7004 192.168.25.153:7005 192.168.25.153:7006 >>> Creating cluster Connecting to node 192.168.25.153:7001: OK Connecting to node 192.168.25.153:7002: OK Connecting to node 192.168.25.153:7003: OK Connecting to node 192.168.25.153:7004: OK Connecting to node 192.168.25.153:7005: OK Connecting to node 192.168.25.153:7006: OK >>> Performing hash slots allocation on 6 nodes... Using 3 masters: 192.168.25.153:7001 192.168.25.153:7002 192.168.25.153:7003 Adding replica 192.168.25.153:7004 to 192.168.25.153:7001 Adding replica 192.168.25.153:7005 to 192.168.25.153:7002 Adding replica 192.168.25.153:7006 to 192.168.25.153:7003 M: 5a8523db7e12ca600dc82901ced06741b3010076 192.168.25.153:7001 slots:0-5460 (5461 slots) master M: bf6f0929044db485dea9b565bb51e0c917d20a53 192.168.25.153:7002 slots:5461-10922 (5462 slots) master M: c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 192.168.25.153:7003 slots:10923-16383 (5461 slots) master S: 2a61b87b49e5b1c84092918fa2467dd70fec115f 192.168.25.153:7004 replicates 5a8523db7e12ca600dc82901ced06741b3010076 S: 14848b8c813766387cfd77229bd2d1ffd6ac8d65 192.168.25.153:7005 replicates bf6f0929044db485dea9b565bb51e0c917d20a53 S: 3192cbe437fe67bbde9062f59d5a77dabcd0d632 192.168.25.153:7006 replicates c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 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..... // 坑說明 : // redis叢集建立執行 /*一直等待 Waiting for the cluster to join 很久都沒有反應 /*原因: /*redis叢集不僅需要開通redis客戶端連線的埠,而且需要開通叢集匯流排埠 /*叢集匯流排埠為redis客戶端連線的埠 + 10000 /*如redis埠為6379 /*則叢集匯流排埠為16379 /*故,所有伺服器的點需要開通redis的客戶端連線埠和叢集匯流排埠 /*注意:iptables 放開,如果有安全組,也要放開這兩個埠*/ >>> Performing Cluster Check (using node 192.168.25.153:7001) M: 5a8523db7e12ca600dc82901ced06741b3010076 192.168.25.153:7001 slots:0-5460 (5461 slots) master M: bf6f0929044db485dea9b565bb51e0c917d20a53 192.168.25.153:7002 slots:5461-10922 (5462 slots) master M: c5e334dc4a53f655cb98fa3c3bdef8a808a693ca 192.168.25.153:7003 slots:10923-16383 (5461 slots) master M: 2a61b87b49e5b1c84092918fa2467dd70fec115f 192.168.25.153:7004 slots: (0 slots) master replicates 5a8523db7e12ca600dc82901ced06741b3010076 M: 14848b8c813766387cfd77229bd2d1ffd6ac8d65 192.168.25.153:7005 slots: (0 slots) master replicates bf6f0929044db485dea9b565bb51e0c917d20a53 M: 3192cbe437fe67bbde9062f59d5a77dabcd0d632 192.168.25.153:7006 slots: (0 slots) master replicates c5e334dc4a53f655cb98fa3c3bdef8a808a693ca [OK] All nodes agree about slots configuration. >>> Check for open slots... >>> Check slots coverage... [OK] All 16384 slots covered. [[email protected] redis-cluster]#

如上配置完畢,接下來測試:

// 說明,不加 -c set 值等會報錯。注意這個坑。
[root@bogon redis-cluster]# ./redis-cli -h 192.168.25.153 -p 7002 -c

給叢集設定密碼 :

//說明 : 所有節點的配置檔案redis.conf 中 的 # requirepass foobared  和  masterauth foobared  必須設定為一樣。

//第二 修改 client.rb 中的密碼 和 上面的密碼一樣 。 client.rb 是 gem install redis-3.0.0.gem 安裝後的產物
[root@iZ2ze9nu4bhzqni394evcsZ bin]# find / -name "client.rb"
/usr/share/ruby/xmlrpc/client.rb
/usr/local/share/gems/gems/redis-3.0.0/lib/redis/client.rb
[root@iZ2ze9nu4bhzqni394evcsZ bin]# vim /usr/local/share/gems/gems/redis-3.0.0/lib/redis/client.rb

class Redis
  class Client

    DEFAULTS = {
      :scheme => "redis",
      :host => "127.0.0.1",
      :port => 6379,
      :path => nil,
      :timeout => 5.0,
      :password => "43jd6fd",
      :db => 0,
    }
......................省略。

修改如上密碼 和 redis.conf 密碼一致。從啟 6個節點的服務即可。

測試 :

E:\redis>redis-cli.exe -h 39.134.231 -p 7001 -a 43jnrwed6fd -c
39.107.234.231:7001> set agui guige
-> Redirected to slot [15294] located at 120.79.246.84:7001
OK
120.79.246.84:7001> get agui
"guige"
120.79.246.84:7001> ^C
E:\redis>redis-cli.exe -h 39.107.234.231 -p 7001 -c
39.107.234.231:7001> set agui
(error) ERR wrong number of arguments for 'set' command
39.107.234.231:7001> get agui
(error) NOAUTH Authentication required.
39.107.234.231:7001> ^C

注意 : 客戶端連線的時候,要加 -c ,否則無法對redis 叢集操作。通過以上也可以看出,redis 設定密碼成功。不通過密碼連線會進行 身份的驗證。