データの自動バランス調整

警告

  • Confluent Platform 6.0.0 以降では Self-Balancing Clusters が提供されており、Auto Data Balancer の代わりに使用することが推奨されています。詳細な機能の比較については、「Self-Balancing と Auto Data Balancer」を参照してください。
  • Auto Data Balancer と Self-Balancing は同時に使用できません。Auto Data Balancer を実行する場合は、まず Self-Balancing がオフであることを確認する必要があります。

confluent-rebalancer ツールは、データの移動を最小限に抑えながら、ブローカーとラックの間で、トピックおよびクラスターごとにリーダー数とディスク使用量が均等になるように、データのバランスを調整します。さらに、 Apache Kafka® のレプリケーションクォータ機能と緊密に統合されており、データのバランスを考慮したトラフィックを動的に調整します。

このツールは Confluent Platform の一部であり、confluent-rebalancer パッケージを使用して単独でインストールすることもできます。

要件

バランス調整計画を策定するために、このツールは Apache Kafka® クラスターから収集したメトリクスを利用します。このデータは Confluent Metrics Reporter により、構成可能な Kafka クラスターにある構成可能な Kafka トピック(デフォルトは _confluent-metrics)にパブリッシュされます。

Metrics Reporter を有効にするには、 インストール手順 を参照してください。

Confluent Auto Data Balancer クイックスタート

Kafka クラスターの起動

ZooKeeper サーバーを起動します。この例では、各サービスが localhost で実行されていると仮定します。

# Start ZooKeeper.  Run this command in its own terminal.
./bin/zookeeper-server-start ./etc/kafka/zookeeper.properties

ちなみに

これらの手順では、ZIP または TAR アーカイブを使用して Confluent Platform をインストールしているという想定に基づいています。詳細については「オンプレミスのデプロイ」を参照してください。

ブローカー構成ファイルを一時的な場所にコピーし、Metrics Reporter を有効にして、追加ブローカーの構成を複製します。

# Copy the configuration files to /tmp
cp ./etc/kafka/server.properties /tmp/server0.properties

# Add metrics reporter configurations (alternatively, you could uncomment the configurations)
echo "" >> /tmp/server0.properties
echo "metric.reporters=io.confluent.metrics.reporter.ConfluentMetricsReporter" >> /tmp/server0.properties
echo "confluent.metrics.reporter.bootstrap.servers=localhost:9092" >> /tmp/server0.properties
echo "confluent.metrics.reporter.topic.replicas=1" >> /tmp/server0.properties

# properties for broker.id=1
cp /tmp/server0.properties /tmp/server1.properties
sed -i'' -e "s/broker.id=0/broker.id=1/g" /tmp/server1.properties
sed -i'' -e "s/9092/9082/g" /tmp/server1.properties
sed -i'' -e "s/#listen/listen/g" /tmp/server1.properties
sed -i'' -e "s/kafka-logs/kafka-logs-1/g" /tmp/server1.properties

# properties for broker.id=2
cp /tmp/server0.properties /tmp/server2.properties
sed -i'' -e "s/broker.id=0/broker.id=2/g" /tmp/server2.properties
sed -i'' -e "s/9092/9072/g" /tmp/server2.properties
sed -i'' -e "s/#listen/listen/g" /tmp/server2.properties
sed -i'' -e "s/kafka-logs/kafka-logs-2/g" /tmp/server2.properties

# properties for broker.id=3
cp /tmp/server0.properties /tmp/server3.properties
sed -i'' -e "s/broker.id=0/broker.id=3/g" /tmp/server3.properties
sed -i'' -e "s/9092/9062/g" /tmp/server3.properties
sed -i'' -e "s/#listen/listen/g" /tmp/server3.properties
sed -i'' -e "s/kafka-logs/kafka-logs-3/g" /tmp/server3.properties

Kafka ブローカーを起動します。

# Start Kafka.  Run these commands in a separate terminal:
./bin/kafka-server-start /tmp/server0.properties &
./bin/kafka-server-start /tmp/server1.properties &
./bin/kafka-server-start /tmp/server2.properties &
./bin/kafka-server-start /tmp/server3.properties &

これらのサービスを立ち上げて動作させる方法の詳細については、Confluent Platform の クイックスタート を参照してください。

トピックの作成とデータの生成

複数のトピックを作成し、それぞれに 4 つのパーティションとレプリケーション係数 2 を設定します。均等ではない割り当てを意図的に作成します。

./bin/kafka-topics --create --topic topic-a --replica-assignment 0:1,0:1,0:1,0:1 --bootstrap-server localhost:9092
./bin/kafka-topics --create --topic topic-b --replica-assignment 1:0,2:1,1:2,2:1 --bootstrap-server localhost:9092

それぞれの内容を調べます。

./bin/kafka-topics --describe --topic topic-a --bootstrap-server localhost:9092

Topic:topic-a        PartitionCount:4        ReplicationFactor:2     Configs:
   Topic: topic-a    Partition: 0    Leader: 0       Replicas: 0,1   Isr: 0,1
   Topic: topic-a    Partition: 1    Leader: 0       Replicas: 0,1   Isr: 0,1
   Topic: topic-a    Partition: 2    Leader: 0       Replicas: 0,1   Isr: 0,1
   Topic: topic-a    Partition: 3    Leader: 0       Replicas: 0,1   Isr: 0,1

./bin/kafka-topics --describe --topic topic-b --bootstrap-server localhost:9092

Topic:topic-b        PartitionCount:4        ReplicationFactor:2     Configs:
  Topic: topic-b     Partition: 0    Leader: 1       Replicas: 1,0   Isr: 1,0
  Topic: topic-b     Partition: 1    Leader: 2       Replicas: 2,1   Isr: 2,1
  Topic: topic-b     Partition: 2    Leader: 1       Replicas: 1,2   Isr: 1,2
  Topic: topic-b     Partition: 3    Leader: 2       Replicas: 2,1   Isr: 2,1

ここでいくつかのデータを生成します。

./bin/kafka-producer-perf-test --topic topic-a --num-records 200000 --record-size 1000 --throughput 10000000 --producer-props bootstrap.servers=localhost:9092
./bin/kafka-producer-perf-test --topic topic-b --num-records 800000 --record-size 1000 --throughput 10000000 --producer-props bootstrap.servers=localhost:9092

最後に、コンシューマーを実行して、オフセットトピックを強制的に作成します。

./bin/kafka-consumer-perf-test --topic topic-a --broker-list localhost:9092 --messages 10

リバランサーの実行

開始前に知っておくべきこととして、./bin/confluent-rebalancer を引数なしで実行すると、サポートされているコマンドのリストが説明とともに出力されます。

execute コマンドを使用して開始します。メトリクストピックを含む Kafka クラスター用のブートストラップサーバーと、レプリカを移動するために割り当てる最大帯域幅(バイト/秒単位)を指定します。verbose フラグにより、CLI 出力にブローカー単位の統計情報が含まれます。これはブローカー数が非常に多くなければ便利です。

./bin/confluent-rebalancer execute --bootstrap-server localhost:9092 --metrics-bootstrap-server localhost:9092 --throttle 10000000 --verbose

バランス調整計画が提示されます。これは Kafka ブローカーが(この例で示すとおり)単一のログディレクトリを使用するか、複数のログディレクトリを使用するかの構成によって多少変わります。単一の場合、リバランサーはバランス調整の途中とその後で、ログディレクトリが含まれるボリュームの空き領域の指定割合を必ず保持します(デフォルトは 20%)。

Computing the rebalance plan (this may take a while) ...
You are about to move 17 replica(s) for 14 partitions to 4 broker(s) with total size 827.2 MB.
The preferred leader for 14 partition(s) will be changed.
In total, the assignment for 15 partitions will be changed.
The minimum free volume space is set to 20.0%.

The following brokers will have less than 40% of free volume space during the rebalance:
      Broker     Current Size (MB)  Size During Rebalance (MB)   Free % During Rebalance      Size After Rebalance (MB)    Free % After Rebalance
      0          413.6              620.4                        30.1                         519.6                        30.5
      2          620.4              723.8                        30.1                         520.8                        30.5
      3          0                  517                          30.1                         520.8                        30.5
      1          1,034              1,034                        30.1                         519.6                        30.5

Min/max stats for brokers (before -> after):
      Type  Leader Count                 Replica Count                Size (MB)
      Min   12 (id: 3) -> 17 (id: 0)     37 (id: 3) -> 43 (id: 3)     0 (id: 3) -> 517 (id: 1)
      Max   21 (id: 0) -> 17 (id: 0)     51 (id: 1) -> 45 (id: 0)     1,034 (id: 1) -> 517 (id: 3)
No racks are defined.

Broker stats (before -> after):
  Broker     Leader Count    Replica Count   Size (MB)            Free Space (%)
      0          21 -> 17        48 -> 45        413.6 -> 517       30.5 -> 30.5
      1          20 -> 17        51 -> 44        1,034 -> 517       30.5 -> 30.5
      2          15 -> 17        40 -> 44        620.4 -> 517       30.5 -> 30.5
      3          12 -> 17        37 -> 43        0 -> 517           30.5 -> 30.5

Would you like to continue? (y/n):

同一ボリュームですべてのブローカーを実行しているので、空き領域のサイズがバランス調整後に変わることはありません。複数のログディレクトリを構成している場合、バランス調整の途中とその後でディスク領域の追加要件に十分注意し、ブローカーでディスク領域が足りなくならないようにします。

また、min/max 統計情報では、バランス調整が完了した後に、データのバランスの改善状況のサマリを示します。目標は、バランス調整後の minmax の値が互いに近い値であることです。この例の場合は、各数値がほぼ同じであり、最適に近いバランスを達成しています。

続行する場合、次を確認します(警告は無視しないでください)。

Rebalance started, its status can be checked via the status command.

Warning: You must run the status or finish command periodically, until the rebalance completes, to ensure the throttle is removed. You can also alter the throttle by re-running the execute command passing a new value.

この時点で、ツールは終了し、バランス調整はバックグラウンドで実行(Kafka Controller で処理)されます。

ステータスのチェック、終了とキャンセル

バランス調整のステータスをチェックします。

./bin/confluent-rebalancer status --bootstrap-server localhost:9092

Partitions being rebalanced:
        Topic topic-a: 1,2
        Topic topic-b: 0

最終的に、バランス調整は完了します(finishstatus は同様で、後者では簡略出力になり、バランス調整が終了したとき、0 終了ステータスが返ります)。

./bin/confluent-rebalancer finish --bootstrap-server localhost:9092                             ⏎

The rebalance has completed and throttling has been disabled

レプリカが均等に割り当てられているかどうかを確認します。

./bin/kafka-topics --describe --topic topic-a --bootstrap-server localhost:9092

Topic:topic-a        PartitionCount:4        ReplicationFactor:2     Configs:
        Topic: topic-a       Partition: 0    Leader: 3       Replicas: 3,2   Isr: 2,3
        Topic: topic-a       Partition: 1    Leader: 2       Replicas: 2,3   Isr: 2,3
        Topic: topic-a       Partition: 2    Leader: 0       Replicas: 1,0   Isr: 0,1
        Topic: topic-a       Partition: 3    Leader: 0       Replicas: 0,1   Isr: 0,1

./bin/kafka-topics --describe --topic topic-b --bootstrap-server localhost:9092

Topic:topic-b        PartitionCount:4        ReplicationFactor:2     Configs:
        Topic: topic-b       Partition: 0    Leader: 3       Replicas: 3,0   Isr: 0,3
        Topic: topic-b       Partition: 1    Leader: 0       Replicas: 0,3   Isr: 0,3
        Topic: topic-b       Partition: 2    Leader: 1       Replicas: 1,2   Isr: 1,2
        Topic: topic-b       Partition: 3    Leader: 2       Replicas: 2,1   Isr: 2,1

パーティションサイズは非同期で通信されます(デフォルトでは 15 秒間隔、confluent.metrics.reporter.publish.ms 構成で変更可能です)。したがって、バランス調整が完了した後に、ツールが正しい情報をレポートするまで時間がかかることがあります。このようなローカルテストを実行するときに顕著です。

cancel コマンドを使用すると、進行中のどの再割り当ても停止できます。

./bin/confluent-rebalancer cancel --bootstrap-server localhost:9092

リーダーのバランス

auto.leader.rebalance.enable がブローカーで無効である場合、バランス調整が完了した後に優先リーダー選出ツールを実行します。これにより、優先リーダーだけではなく、実際のリーダー間のバランスが確実に調整されます。

./bin/kafka-leader-election --election-type PREFERRED --bootstrap-server localhost:9092

Created preferred replica election path with _confluent-metrics-7,_confluent-metrics-2,_confluent-metrics-5,_confluent-metrics-11,_confluent-metrics-10,topic-b-2,topic-a-1,_confluent-metrics-1,topic-a-3,__confluent.support.metrics-0,topic-a-0,_confluent-metrics-9,_confluent-metrics-0,topic-a-2,topic-b-3,_confluent-metrics-3,_confluent-metrics-4,_confluent-metrics-6,topic-b-1,_confluent-metrics-8,topic-b-0
Successfully started preferred replica election for partitions Set(_confluent-metrics-7, _confluent-metrics-2, _confluent-metrics-5, _confluent-metrics-11, _confluent-metrics-10, topic-b-2, topic-a-1, _confluent-metrics-1, topic-a-3, __confluent.support.metrics-0, topic-a-0, _confluent-metrics-9, _confluent-metrics-0, topic-a-2, topic-b-3, _confluent-metrics-3, _confluent-metrics-4, _confluent-metrics-6, topic-b-1, _confluent-metrics-8, topic-b-0)
# run again to see the completion
./bin/kafka-leader-election --election-type PREFERRED --bootstrap-server localhost:9092

Successfully completed preferred replica election for partitions topic-b-3, topic-b-0, _confluent-metrics-1, _confluent-metrics-5, _confluent-metrics-9, topic-a-1, _confluent-metrics-2, topic-b-1, _confluent-metrics-6, __confluent.support.metrics-0, _confluent-metrics-10, topic-a-2, _confluent-metrics-3, topic-b-2, topic-a-3, _confluent-metrics-7, _confluent-metrics-4, _confluent-metrics-11, _confluent-metrics-8, _confluent-metrics-0, topic-a-0

ブローカーの廃止

confluent-rebalancer ツールは、--remove-broker-ids オプションを使用して、廃止ブローカーのバランス調整計画を自動的に生成します。このクラスターのバランスは同じ実行で調整されます。

./bin/confluent-rebalancer execute --bootstrap-server localhost:9092 --metrics-bootstrap-server localhost:9092 --throttle 100000 --remove-broker-ids 1

Computing the rebalance plan (this may take a while) ...
You are about to move 48 replica(s) for 48 partitions to 3 broker(s) with total size 775.5 MB.
The preferred leader for 20 partition(s) will be changed.
In total, the assignment for 49 partitions will be changed.

You have requested all replicas to be moved out of 1 broker(s) with ID(s): 1.
After the rebalance, these broker(s) will have no replicas.

The following brokers will require more disk space during the rebalance and, in some cases, after the rebalance:
     Broker     Current (MB)    During Rebalance (MB)  After Rebalance (MB)
     0          517             775.5                  568.7
     2          517             775.5                  723.8
     3          517             775.5                  775.5

Min/max stats for brokers (before -> after):
     Type  Leader Count                 Replica Count                Size (MB)
     Min   17 (id: 0) -> 0 (id: 1)      43 (id: 3) -> 0 (id: 1)      517 (id: 2) -> 0 (id: 1)
     Max   17 (id: 0) -> 23 (id: 0)     45 (id: 0) -> 59 (id: 0)     517 (id: 1) -> 775.5 (id: 3)
No racks are defined.

Would you like to continue? (y/n):

8 つのパーティションのデータ量がほぼ同じであるため、残りの 3 つのブローカーでデータを同量にすることはできません。ブローカー 2 のバランス調整後のパーティションは 2 つになり、したがってデータ量が少なくなります。

データ移行中の使用帯域幅の制限

--throttle オプションを使用して、レプリケーションに使用する帯域幅を制限します。バランス調整の進行中でも、ツールを再実行するだけで、この値を更新することができます。

./bin/confluent-rebalancer execute --bootstrap-server localhost:9092 --metrics-bootstrap-server localhost:9092 --throttle 100000

The throttle rate was updated to 100000 bytes/sec.
A rebalance is currently in progress for:
        Topic topic-b: 0,1

詳細については、 スロットリング を参照してください。

再割り当て範囲の制限

--topics フラグと --exclude-internal-topics フラグを使用して、再割り当ての対象になるトピックの範囲を制限できます。--replica-placement-only を使用すると、レプリカ配置の制約を満たさないパーティションのみで再割り当てを実行できます。トピック topic-c はレプリカ配置制約を使用するように構成されていると仮定して、次のコマンドで、制約を満たすために必要な再割り当てを実行します。

./bin/confluent-rebalancer execute --bootstrap-server localhost:9092 --metrics-bootstrap-server localhost:9092 --throttle 100000 --topics topic-c --replica-placement-only

レプリカ配置制約の詳細については、「レプリカ配置」を参照してください。

順次再割り当て

多数のトピックパーティションがある Kafka クラスターの場合、1 つのバッチで再割り当てを実行すると、パフォーマンスや可用性が低下することがあります。リーダーごとの再割り当ての最大数を制限するには、フラグ --incremental と構成パラメーター max.concurrent.moves.per.leader` を使用します。

./bin/confluent-rebalancer execute --incremental --bootstrap-server localhost:9092 --metrics-bootstrap-server localhost:9092 --throttle 100000

モニタリングでの JMX の使用

認証は、Kafka の JMX(Java Management Extensions)ではデフォルトで無効です。環境変数を使用して、これらのデフォルトをオーバーライドする必要があります。

CLI を使用して開始したプロセスに対して KAFKA_JMX_OPTS を使用します。または、適切な Java システムプロパティを設定します。

各プラットフォームコンポーネントに対しては、 JMX のデフォルトをオーバーライドするオプション<component-name>_JMX_OPTS の形式をとります。

confluent-rebalancer に対しては、CLI で開始したプロセスには環境変数 REBALANCER_JMX_OPTS を使用するか、または適切な Java システムプロパティを設定します。

ライセンス設定

ライセンスはクイックスタートの中では指定されていません。Confluent ライセンスを保持している場合、confluent.license 構成で指定できます。ライセンスについて詳細は、「Confluent Platform ライセンスの管理」を参照してください。