開発環境で9ノードのRedisクラスタを構築し、使用しているバージョンはRedis 3.2です。強制終了によりクラスタ設定ファイルが破損する問題が発生しました。具体的にはUnrecoverable error: corrupted cluster config file.というエラーがログに記録されました。この問題を解決するために以下の手順を試みました。
まず、以下の手順でクラスタを再構築します:
FLUSHDBコマンドを使ってデータをクリア/var/lib/redis/ディレクトリにある設定ファイル(例:nodes-7003.conf)を削除- Redisインスタンスを再起動:
/usr/bin/redis-server /data1/redis_cluster/7003/redis.conf - 各Redisインスタンスを
CLUSTER MEETコマンドでクラスタに追加 CLUSTER NODESとCLUSTER INFOコマンドでクラスタの状態を確認
次に、スロットの割り当てを行います。Redisの自動機能CLUSTER ADDSLOTSを使用してスロットを手動で割り当てる必要があります。Pythonスクリプトを使ってスロットを生成します。
def allocate_slots():
slot_count = 0
slots_list = []
with open("slot_allocation.txt", "w") as file:
for slot in range(16384):
slot_count += 1
if slot_count <= 1820:
slots_list.append(str(slot))
else:
command = 'CLUSTER ADDSLOTS ' + ' '.join(slots_list)
print(command)
file.write(command + '\n')
slots_list = [str(slot)]
slot_count = 1
if len(slots_list) > 0:
command = 'CLUSTER ADDSLOTS ' + ' '.join(slots_list)
print(command)
file.write(command + '\n')
生成されたslot_allocation.txtファイルを開き、各行を対応するRedisインスタンスに対して実行します。例えば、ポート7000のインスタンスに対して以下のように実行します:
redis-cli -p 7000 -c <貼り付けたコマンド>
スロットの割り当て結果を確認するには以下を実行します:
127.0.0.1:7000> CLUSTER NODES
...
127.0.0.1:7000> CLUSTER INFO
cluster_state:ok
cluster_slots_assigned:16384
cluster_slots_ok:16384
cluster_slots_pfail:0
cluster_slots_fail:0
cluster_known_nodes:9
cluster_size:9
cluster_current_epoch:8
cluster_my_epoch:2
cluster_stats_messages_sent:6827
cluster_stats_messages_received:6800
これで、スロットの割り当てが完了し、Redisクラスタが正常に動作していることが確認できます。