CLOVER🍀

That was when it all began.

Apache Kafkaでクラスタを構成してみる

Apache KafkaをしばらくSingle Nodeで扱ってきましたが、そろそろクラスタを構成してみたいと思います。

クラスタの構成の仕方は、Apache Kafkaのドキュメント、Quick Startを見ると書いてあったりします。

Quick Start / Step 6: Setting up a multi-broker cluster

また、このあたりも参考に。

CentOS 7でKafkaクラスタを構築する | 俺的備忘録 〜なんかいろいろ〜

複数台マシンを用いたKafkaクラスタの構築方法 - 夢とガラクタの集積場

Apache Kafkaのクラスタ構成を試してみる

各Brokerのidを設定し、Apache ZooKeeperの接続先を合わせてあげればいいみたいです。

というわけで、クラスタを組んでみましょう。今回は、こんな構成でいきます。

  • Apache ZooKeeper … 172.21.0.2
  • Apache Kafka - Broker 1 … 172.21.0.3
  • Apache Kafka - Broker 2 … 172.21.0.4
  • Apache Kafka - Broker 3 … 172.21.0.5

Apache ZooKeeper 1台に、Apache Kafka 3台という構成。

Apache Kafka側で設定するポイントは、config/server.propertiesです。設定したポイントを抜粋すると、こんな感じ。

## Broker 1
broker.id=1
listeners=PLAINTEXT://172.21.0.3:9092
zookeeper.connect=172.21.0.2:2181


## Broker 2
broker.id=2
listeners=PLAINTEXT://172.21.0.4:9092
zookeeper.connect=172.21.0.2:2181


## Broker 3
broker.id=3
listeners=PLAINTEXT://172.21.0.5:9092
zookeeper.connect=172.21.0.2:2181

listenersはリモート接続を受け付けるためですが、BrokerにはユニークなIDを設定する必要があるみたいで、それが「broker.id」です。
あとは、Apache ZooKeeprの接続先を合わせておきました。

Apache ZooKeeperは、とりあえずふつうに起動します。

$ bin/zookeeper-server-start.sh -daemon config/zookeeper.properties

あとは、各Brokerを順次起動。

## Broker 1
$ bin/kafka-server-start.sh -daemon config/server.properties

## Broker 2
$ bin/kafka-server-start.sh -daemon config/server.properties

## Broker 3
$ bin/kafka-server-start.sh -daemon config/server.properties

kafka-managerで見ると、こんな感じになっています。

Topicを追加してみましょう。Broker 1で実行します。Partition数は3、レプリケーションは2に設定。

$ bin/kafka-topics.sh --create --zookeeper 172.21.0.2:2181 --replication-factor 2 --partitions 3 --topic replicated-topic
Created topic "replicated-topic".

describeして、状態を見てみます。

$ bin/kafka-topics.sh --describe --zookeeper 172.21.0.2:2181 --topic replicated-topic
Topic:replicated-topic	PartitionCount:3	ReplicationFactor:2	Configs:
	Topic: replicated-topic	Partition: 0	Leader: 1	Replicas: 1,2	Isr: 1,2
	Topic: replicated-topic	Partition: 1	Leader: 2	Replicas: 2,3	Isr: 2,3
	Topic: replicated-topic	Partition: 2	Leader: 3	Replicas: 3,1	Isr: 3,1

それぞれの意味は、こんな感じ。

・"leader" is the node responsible for all reads and writes for the given partition. Each node will be the leader for a randomly selected portion of the partitions.
・"replicas" is the list of nodes that replicate the log for this partition regardless of whether they are the leader or even if they are currently alive.
・"isr" is the set of "in-sync" replicas. This is the subset of the replicas list that is currently alive and caught-up to the leader.

https://kafka.apache.org/documentation/#quickstart_multibroker

Partition 0のリーダーはIDが1のBroker、バックアップは2、Partition 1はリーダーが2、バックアップが3、Partition 2はリーダーが3、バックアップが1
という感じですね。

データを登録(Producer)してみましょう。Broker 1を「--broker-list」に指定して実行します。

$ bin/kafka-console-producer.sh --broker-list 172.21.0.3:9092 --topic replicated-topic

Consumerは、Broker 2から読み出してみます。

$ bin/kafka-console-consumer.sh --bootstrap-server 172.21.0.4:9092 --topic replicated-topic --from-beginning

では、データを登録して読み出してみます。

## Producer
>Hello Workd
>Kafka Cluster
>Apache ZooKeeper
>Oops!!

## Consumer
Hello Workd
Kafka Cluster
Apache ZooKeeper
Oops!!

OKそうですね。

どのBrokerから読み出してもOK。

$ bin/kafka-console-consumer.sh --bootstrap-server 172.21.0.3:9092  --topic replicated-topic --from-beginning
Hello Workd
Oops!!
Kafka Cluster
Apache ZooKeeper

$ bin/kafka-console-consumer.sh --bootstrap-server 172.21.0.5:9092 --topic replicated-topic --from-beginning
Hello Workd
Oops!!
Apache ZooKeeper
Kafka Cluster

「--from-beginning」をつけているので、最初から読み出してくれます。

全Brokerを指定してもかまいません。

## Producer
$ bin/kafka-console-producer.sh --broker-list 172.21.0.3:9092,172.21.0.4:9092,172.21.0.5:9092 --topic replicated-topic

## Consumer
$ bin/kafka-console-consumer.sh --bootstrap-server 172.21.0.3:9092,172.21.0.4:9092,172.21.0.5:9092 --topic replicated-topic

Consumerは、今度は最後から読むことにします。

実行。

## Producer
>Test
>Foo
>Bar
>Fuga
>Hoge

## Consumer
Test
Foo
Bar
Fuga
Hoge

こちらもOK、と。

まとめ

Apache Kafkaを使ってクラスタを構成して、付属のコンソールツールでデータの登録と読み出しをやってみました。

とりあえず組んでみる、というのであれば割と簡単にできるようです。