macOSでRedis Clusterを動かしてみた。
前提
- redis-server、redis-cliはインストール済み
- redis-server、redis-cliのバージョンは6.2.5
構築
設定ファイルを配置するディレクトリを作成。redis-clusterとしておく。
mkdir redis-cluster
少なくとも3つのマスターノードが必要ということなので、マスター3つ、スレーブ3つ、あわせて6つのノードを持つ構成にする。各インスタンスのポート番号をディレクトリ名にして6ディレクトリを作成する。
cd redis-cluster
mkdir 7000 7001 7002 7003 7004 7005
各ポートごとフォルダに最小限の設定ファイル(redis.conf)を用意する。
port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes
各ノードを起動する。シェルを6つ起動して以下のコマンドを実行する。
cd 7000
redis-server redis.conf
さらにシェルを起動してクラスター作成のコマンドを実行する。
redis-cli --cluster create 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 --cluster-replicas 1
--cluster-replicas 1 は マスターに1つのレプリカ(スレーブ)を配置するということ。
操作
実際に操作してみた。
redis-cli -h 127.0.0.1 -p 7000 -c
127.0.0.1:7000> set hoge fuga
OK
127.0.0.1:7000> get hoge
"fuga"
127.0.0.1:7000> set base ball
-> Redirected to slot [14824] located at 127.0.0.1:7002
OK
127.0.0.1:7002> get base
"ball"
127.0.0.1:7002> set soccer player
-> Redirected to slot [7526] located at 127.0.0.1:7001
OK
127.0.0.1:7001> get soccer
"player"
127.0.0.1:7001> get hoge
-> Redirected to slot [1525] located at 127.0.0.1:7000
"fuga"
127.0.0.1:7000> get base
-> Redirected to slot [14824] located at 127.0.0.1:7002
"ball"
127.0.0.1:7002> set laravel_session901 zzzzzzxxxxxxxx
OK
127.0.0.1:7002> set 901aravel_session zzzzzzxxxxxxxx
-> Redirected to slot [7888] located at 127.0.0.1:7001
OK
127.0.0.1:7001> set z73619 angou
-> Redirected to slot [14666] located at 127.0.0.1:7002
OK
127.0.0.1:7002> set ZZZZ yyyy
-> Redirected to slot [7764] located at 127.0.0.1:7001
OK
127.0.0.1:7001> set 9ZBgsja kjhdafowe
-> Redirected to slot [253] located at 127.0.0.1:7000
OK
127.0.0.1:7000> set zxsdhjhfeoweq9ZBgsja kjdadddddhdafowe
-> Redirected to slot [11316] located at 127.0.0.1:7002
OK
127.0.0.1:7002>
補足
クラスター作成時に以下のエラーが出る場合、
[ERR] Node 127.0.0.1:7000 is not empty. Either the node already knows other nodes (check with CLUSTER NODES) or contains some key in database 0.
以下を全てのノードに対して実行する。
redis-cli -h 127.0.0.1 -p 7000 -c CLUSTER RESET
それでもエラーが解消しないときは、各ノードのデータをクリアする。(flushdb、flushall)
参考サイト