Skip to content
Published on

Apache Kafkaインストールガイド(Zookeeper付き)

Authors
  • Name
    Twitter

Overview

Apache Kafkaのインストール方法について学ぶ。

Zookeeperのインストール方法

Kafkaをインストールするには、Zookeeperが必要だ。最近ではZookeeperの代わりにKRaftというものを使用できるようになったという。 Zookeeperに馴染みのない方のために簡単に紹介すると、分散アプリケーションのための設定ストアと考えればよい。 Hadoopの特性上、分散環境でアプリケーションが動作するため、必然的にグローバル情報を保存したい場合や、同期のためのロックのようなものが必要な場合がある。 その時にZookeeperを使えばよい。Zookeeper公式サイトでは以下のように説明している。

ZooKeeper is a centralized service for maintaining configuration information, naming, providing distributed synchronization, and providing group services.

Zookeeper tarのダウンロードと解凍

現時点(20221214)のZookeeperの最新リリースバージョンは3.8.0で、以下のサイトからtar.gzをダウンロードできる。 https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.8.0/apache-zookeeper-3.8.0-bin.tar.gz

wget https://dlcdn.apache.org/zookeeper/zookeeper-3.8.0/apache-zookeeper-3.8.0-bin.tar.gz
tar -zxvf apache-zookeeper-3.8.0-bin.tar.gz

zoo.cfgの修正

Zookeeperサーバーを3台で構成するため、server.1server.2server.3にそれぞれZookeeperサーバーのFQDNと使用するポート範囲2888:3888を入力する。(FQDNの代わりにIPアドレスを入れても構わない。) dataDir、つまりmyidファイルを保存する場所を修正する。筆者は/var/zookeeperに指定した。

# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
dataDir=/var/zookeeper
# the port at which the clients will connect
clientPort=2181
server.1=server1:2888:3888
server.2=server2:2888:3888
server.3=server3:2888:3888

myidファイルの作成

上で指定したdataDirにサーバーごとに固有のmyidを入力する。

例えば、server1には以下のように入力する。

1

server2には以下のように入力する。

2

1から255までの数字のみ入力可能だ。

Zookeeperサーバーの起動

Zookeeperがインストールされたサーバーでコマンドを実行し、Zookeeperを起動する。

apache-zookeeper-3.8.0-bin/bin/zkServer.sh start

Kafkaのインストール

Kafkaバイナリのダウンロードと解凍

wget https://dlcdn.apache.org/kafka/3.3.1/kafka_2.13-3.3.1.tgz
tar -xzf kafka_2.13-3.3.1.tgz

server.propertiesの修正

Zookeeperインストール時にサーバーごとにmyidファイルを変更したように、Kafkaサーバーごとにbroker.idを一意に設定する。 zookeeper.connectにはZookeeperクラスタ情報を入力する。

broker.id=1
zookeeper.connect=server1:2181,server2:2181,server3:2181

Kafkaブローカーの実行

kafka_2.13-3.3.1/bin/kafka-server-start.sh config/server.properties

トピックの作成

Kafkaを実行したノードの一つで、以下のコマンドでtestというトピックを作成してみよう。Created topic test.というメッセージが表示されれば正常にインストールされたことになる。

bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 3 --partitions 20 --topic test