1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Apache Kafkaをdocker-composeで試してみる

1
Posted at

走らせるだけで意外と調べることが多かったので、メモです。

参考

環境

  • Macbook Pro 16inch (Intel)
  • Mac OS Big Sur

前提

  • docker-composeが入っている
  • dockerが入っている

コード

まず、自分のデバイスに割り当てられたIPアドレスを取得しましょう。

$ ipconfig getifaddr en0
192.168.11.17

これを以下のdocker-compose.ymlに貼り付けます。

version: '3'

services:
  zookeeper:
    image: wurstmeister/zookeeper
    ports:
      - "2181:2181"
  kafka:
    image: wurstmeister/kafka
    ports:
      - "9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: 192.168.11.17 # must be your IP, run `ipconfig getifaddr en0`
      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

走らせてみる

docker-compose up

別のディレクトリでトピック登録

$ docker-compose exec kafka /opt/kafka/bin/kafka-topics.sh \
--create --zookeeper zookeeper:2181 --replication-factor 1 \
--partitions 1 --topic test
Created topic test.

トピックの確認

$ docker-compose exec kafka \
/opt/kafka/bin/kafka-topics.sh --list --zookeeper zookeeper:2181
test

メッセージを送信してみる

docker-compose exec kafka \
/opt/kafka/bin/kafka-console-producer.sh \
--broker-list kafka:9092 --topic test
>lol
>haha

メッセージの受信

docker-compose exec kafka \
/opt/kafka/bin/kafka-console-consumer.sh \
--bootstrap-server kafka:9092 --topic test --from-beginning
lol
haha
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?