redis3.0クラスタ


オペレーティングシステム環境
[root@62290133db0c ~]# cat /etc/issue
CentOS release 6.7 (Final)
Kernel \r on an \m
[root@62290133db0c ~]# getconf LONG_BIT
64

redisインストールはredisインストールを参照してください.redisクラスタは3.0で発売されたクラスタ機能です.
リソースが限られているため、1台のマシンにredisクラスタ環境を構築するしかありません.redisバージョン3.0.6では、クラスタを正常に動作させるには少なくとも3つのプライマリノードが必要ですが、クラスタ機能の試用を開始したばかりの頃、6つのノードを強くお勧めします.そのうち3つはプライマリノードで、残りの3つは各プライマリノードのセカンダリノードです.
redis
dir
port
6381
/opt/6381
6381
6382
/opt/6382
6382
6383
/opt/6383
6383
6384
/opt/6384
6384
6385
/opt/6385
6385
6386
/opt/6386
6386
redis対応ディレクトリの作成
[root@62290133db0c ~]# mkdir /opt/638{1..6}
[root@62290133db0c ~]# tree /opt/
/opt/
├── 6381
├── 6382
├── 6383
├── 6384
├── 6385
└── 6386
6 directories, 0 files

プロファイルのコピーとredis起動redis-serverファイルの起動
[root@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/redis.conf /opt/6381/
[root@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6381/
[root@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6382/
[root@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6383/
[root@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6384/
[root@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6385/
[root@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-server /opt/6386/
[root@62290133db0c ~]# cd /opt/6381/

6381プロファイル
[root@62290133db0c 6381]# vim redis.conf
daemonize yes
pidfile /var/run/redis-6381.pid
port 6381
tcp-keepalive 60
logfile "/var/log/redis/6381.log"
dbfilename dump.rdb
dir /opt/6381/
slave-serve-stale-data yes
appendonly yes
appendfilename "appendonly-6381.aof"
appendfsync everysec
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000

redisプロファイルを他のredis対応ディレクトリにコピーし、redisのプロファイルを変更
[root@62290133db0c ~]# cp /opt/6381/redis.conf /opt/6382/
[root@62290133db0c ~]# cp /opt/6381/redis.conf /opt/6383/
[root@62290133db0c ~]# cp /opt/6381/redis.conf /opt/6384/
[root@62290133db0c ~]# cp /opt/6381/redis.conf /opt/6385/
[root@62290133db0c ~]# cp /opt/6381/redis.conf /opt/6386/
[root@62290133db0c ~]# sed -i "s#6381#6382#g" /opt/6382/redis.conf 
[root@62290133db0c ~]# sed -i "s#6381#6383#g" /opt/6383/redis.conf 
[root@62290133db0c ~]# sed -i "s#6381#6384#g" /opt/6384/redis.conf 
[root@62290133db0c ~]# sed -i "s#6381#6385#g" /opt/6385/redis.conf 
[root@62290133db0c ~]# sed -i "s#6381#6386#g" /opt/6386/redis.conf

それぞれ6381を起動する.6386
[root@62290133db0c ~]# /opt/6381/redis-server /opt/6381/redis.conf 
[root@62290133db0c ~]# /opt/6382/redis-server /opt/6382/redis.conf
[root@62290133db0c ~]# /opt/6383/redis-server /opt/6383/redis.conf 
[root@62290133db0c ~]# /opt/6384/redis-server /opt/6384/redis.conf 
[root@62290133db0c ~]# /opt/6385/redis-server /opt/6385/redis.conf 
[root@62290133db0c ~]# /opt/6386/redis-server /opt/6386/redis.conf

redisプロセスの表示
[root@62290133db0c ~]# ps -ef|grep redis|grep -v grep 
root        809      1  0 09:17 ?        00:00:00 /opt/6381/redis-server *:6381 [cluster]    
root        815      1  0 09:18 ?        00:00:00 /opt/6382/redis-server *:6382 [cluster]    
root        821      1  0 09:18 ?        00:00:00 /opt/6383/redis-server *:6383 [cluster]    
root        825      1  0 09:18 ?        00:00:00 /opt/6384/redis-server *:6384 [cluster]    
root        829      1  0 09:18 ?        00:00:00 /opt/6385/redis-server *:6385 [cluster]    
root        833      1  0 09:18 ?        00:00:00 /opt/6386/redis-server *:6386 [cluster]
 :       redis      

redisクラスタの作成
[root@62290133db0c ~]# redis-trib.rb create ----replicas 1 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6385 172.17.0.1:6386
>>> Creating cluster
>>> Performing hash slots allocation on 6 nodes...
Using 3 masters:
172.17.0.1:6381
172.17.0.1:6382
172.17.0.1:6383
Adding replica 172.17.0.1:6384 to 172.17.0.1:6381
Adding replica 172.17.0.1:6385 to 172.17.0.1:6382
Adding replica 172.17.0.1:6386 to 172.17.0.1:6383
M: d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658 172.17.0.1:6381
   slots:0-5460 (5461 slots) master
M: be37cf292b638d919e9e39fc3ebe3984697f6026 172.17.0.1:6382
   slots:5461-10922 (5462 slots) master
M: 5e5f54956d424ac491820f10ef0e8be443e1d98f 172.17.0.1:6383
   slots:10923-16383 (5461 slots) master
S: ddf29c1716a7b111f26cfa2376edbf84b7e4f471 172.17.0.1:6384
   replicates d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658
S: 54c405412761e7714062260c7080e0f904d04cfe 172.17.0.1:6385
   replicates be37cf292b638d919e9e39fc3ebe3984697f6026
S: b0d8b47bf039f65e452a661613d97f62ba569dfb 172.17.0.1:6386
   replicates 5e5f54956d424ac491820f10ef0e8be443e1d98f
Can I set the above configuration? (type 'yes' to accept): yes //    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 172.17.0.1:6381)
M: d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658 172.17.0.1:6381
   slots:0-5460 (5461 slots) master
M: be37cf292b638d919e9e39fc3ebe3984697f6026 172.17.0.1:6382
   slots:5461-10922 (5462 slots) master
M: 5e5f54956d424ac491820f10ef0e8be443e1d98f 172.17.0.1:6383
   slots:10923-16383 (5461 slots) master
M: ddf29c1716a7b111f26cfa2376edbf84b7e4f471 172.17.0.1:6384
   slots: (0 slots) master
   replicates d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658
M: 54c405412761e7714062260c7080e0f904d04cfe 172.17.0.1:6385
   slots: (0 slots) master
   replicates be37cf292b638d919e9e39fc3ebe3984697f6026
M: b0d8b47bf039f65e452a661613d97f62ba569dfb 172.17.0.1:6386
   slots: (0 slots) master
   replicates 5e5f54956d424ac491820f10ef0e8be443e1d98f
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

任意のredisノードにログイン
[root@62290133db0c ~]# redis-cli -c -p 6381
127.0.0.1:6381> CLUSTER NODES //             
54c405412761e7714062260c7080e0f904d04cfe 172.17.0.1:6385 slave be37cf292b638d919e9e39fc3ebe3984697f6026 0 1451889401026 5 connected
5e5f54956d424ac491820f10ef0e8be443e1d98f 172.17.0.1:6383 master - 0 1451889401529 3 connected 10923-16383
ddf29c1716a7b111f26cfa2376edbf84b7e4f471 172.17.0.1:6384 slave d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658 0 1451889400017 4 connected
d3b7db6b7b2696be3e2dbb74d99d33e6a3b31658 172.17.0.1:6381 myself,master - 0 0 1 connected 0-5460
b0d8b47bf039f65e452a661613d97f62ba569dfb 172.17.0.1:6386 slave 5e5f54956d424ac491820f10ef0e8be443e1d98f 0 1451889402033 6 connected
be37cf292b638d919e9e39fc3ebe3984697f6026 172.17.0.1:6382 master - 0 1451889400522 2 connected 5461-10922

登録redis 6381
[root@62290133db0c ~]# redis-cli -c -p 6381
127.0.0.1:6381> set name zxl
-> Redirected to slot [5798] located at 172.17.0.1:6382
OK
172.17.0.1:6382> set age 33
-> Redirected to slot [741] located at 172.17.0.1:6381
OK
172.17.0.1:6381> KEYS *
1) "age"
172.17.0.1:6381> GET age
"33"
172.17.0.1:6381> 
[root@62290133db0c ~]# redis-cli -c -p 6382
127.0.0.1:6382> KEYS *
1) "name"
127.0.0.1:6382> get name
"zxl"
127.0.0.1:6382>

注意:以上がredisクラスタの作成です
redisクラスタを作成すると、エラーは次のようにまとめられます.
[root@62290133db0c ~]# cp /usr/local/src/redis-3.0.6/src/redis-trib.rb /usr/local/bin/
[root@62290133db0c ~]# redis-trib.rb create --replicas 1 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6385 172.17.0.1:6386
/usr/bin/env: ruby: No such file or directory

注意:ruby環境が不足しています.インストールする必要があります.
[root@62290133db0c ~]# yum install ruby -y

ヒントは次のとおりです.
[root@62290133db0c ~]# redis-trib.rb create --replicas 1 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6385 172.17.0.1:6386
/usr/local/bin/redis-trib.rb:24:in `require': no such file to load -- rubygems (LoadError)
from /usr/local/bin/redis-trib.rb:24

注意:rubygemsをインストールする必要があります
[root@62290133db0c ~]# yum install rubygems -y

ヒントは次のとおりです.
[root@62290133db0c ~]# redis-trib.rb create --replicas 1 172.17.0.1:6381 172.17.0.1:6382 172.17.0.1:6383 172.17.0.1:6384 172.17.0.1:6385 172.17.0.1:6386
/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 /usr/local/bin/redis-trib.rb:25

注意:ヒントredisをロードできないのは、redisとrubyのインタフェースが欠けているため、gemを使用してインストールします.
[root@62290133db0c ~]# gem install redis
Successfully installed redis-3.2.1
1 gem installed
Installing ri documentation for redis-3.2.1...
Installing RDoc documentation for redis-3.2.1...

本文は“村の男の子”のブログから出て、転載をお断りします!