CubicLouve

Spring_MTの技術ブログ

create-clusterコマンドを使ってローカルでredis clusterを簡単に作成する方法

redis の中に 含まれている create-cluster コマンドを使うと、Redis Clusterを簡単にローカルで立てることができたのでそのメモ。

手順

redisをcloneしてくる

git clone git@github.com:redis/redis.git
cd redis

redisをビルドする

make

自分の環境ではmacOSでビルドは通っている。

create-clusterコマンドがあるdirectoryまで移動

cd utils/create-cluster

必要であれば config.sh を置いて環境変数を設定する

config.sh というファイルがあるとcreate-cluster は自動的にそれを読み込んでくれる。

設定可能な環境変数は下記を参照

github.com

BIN_PATHを設定すれば、任意のredis-cliコマンドの場所を指定できる。

redis clusterに必要なnodeを立てる

create-cluster start を実行してnodeを作る

 % ./create-cluster start
Starting 30001
Starting 30002
Starting 30003
Starting 30004
Starting 30005
Starting 30006

作成したnodeをclusterにする

% ./create-cluster create
>>> Performing hash slots allocation on 6 nodes...
Master[0] -> Slots 0 - 5460
Master[1] -> Slots 5461 - 10922
Master[2] -> Slots 10923 - 16383
Adding replica 127.0.0.1:30005 to 127.0.0.1:30001
Adding replica 127.0.0.1:30006 to 127.0.0.1:30002
Adding replica 127.0.0.1:30004 to 127.0.0.1:30003
>>> Trying to optimize slaves allocation for anti-affinity
[WARNING] Some slaves are in the same host as their master
M: df85f1ae9a3ac6728d80db7e7545c036eefff225 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
M: c195571fa6c08fef6319591e71148817934f87b4 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
M: 34a8974de69408ec6956834363392f3735cf6f96 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
S: ca3c5200b2b57ea7242955024418df051a16609a 127.0.0.1:30004
   replicates df85f1ae9a3ac6728d80db7e7545c036eefff225
S: 15aebd8d8d14bd3df30ee5ee3c72d02b75101d12 127.0.0.1:30005
   replicates c195571fa6c08fef6319591e71148817934f87b4
S: 69f8274ef598072797ada3e2b7d458a50ebd4f62 127.0.0.1:30006
   replicates 34a8974de69408ec6956834363392f3735cf6f96
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

>>> Performing Cluster Check (using node 127.0.0.1:30001)
M: df85f1ae9a3ac6728d80db7e7545c036eefff225 127.0.0.1:30001
   slots:[0-5460] (5461 slots) master
   1 additional replica(s)
S: 15aebd8d8d14bd3df30ee5ee3c72d02b75101d12 127.0.0.1:30005
   slots: (0 slots) slave
   replicates c195571fa6c08fef6319591e71148817934f87b4
M: c195571fa6c08fef6319591e71148817934f87b4 127.0.0.1:30002
   slots:[5461-10922] (5462 slots) master
   1 additional replica(s)
S: 69f8274ef598072797ada3e2b7d458a50ebd4f62 127.0.0.1:30006
   slots: (0 slots) slave
   replicates 34a8974de69408ec6956834363392f3735cf6f96
S: ca3c5200b2b57ea7242955024418df051a16609a 127.0.0.1:30004
   slots: (0 slots) slave
   replicates df85f1ae9a3ac6728d80db7e7545c036eefff225
M: 34a8974de69408ec6956834363392f3735cf6f96 127.0.0.1:30003
   slots:[10923-16383] (5461 slots) master
   1 additional replica(s)
[OK] All nodes agree about slots configuration.
>>> Check for open slots...
>>> Check slots coverage...
[OK] All 16384 slots covered.

[OK] All 16384 slots covered. がでれば、clusterの作成が完了となる。

その他

ログの確認

% ./create-cluster tailall 

クラスタを止める

% ./create-cluster stop