DIY

とりあえずやってみるのメモ。技術的なメモもありますが、独り言もあります。

Docker上でKafkaを動かす(2)

今回は前回構築した1コンテナでのKafkaを複数コンテナにしてみる。
複数コンテナを構成するには、docker-composeを利用すると便利らしい。
ということでdocker-composeを使ってみる。

1.Docker-composeのインストール

# curl -L https://github.com/docker/compose/releases/download/1.7.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose


2.Docker-composeのyamlファイルを作成する。
今回は4つコンテナを起動し、各々にBrokerが起動するようにする。
以下の様なyamlを作成した。

version: '2'

services:
  kafka1:
    image: kafka
    container_name: kafka1
    hostname: kafka1
    networks:
      kafka_default:
        ipv4_address: 172.16.238.10
    ports:
        - "2181"
        - "9092"
    extra_hosts:
        - "kafka2:172.16.238.11"
        - "kafka3:172.16.238.12"
        - "kafka4:172.16.238.13"
    stdin_open: true
    tty: true


  kafka2:
    image: kafka
    container_name: kafka2
    hostname: kafka2
    networks:
      kafka_default:
        ipv4_address: 172.16.238.11
    ports:
        - "2181"
        - "9092"
    extra_hosts:
        - "kafka1:172.16.238.10"
        - "kafka3:172.16.238.12"
        - "kafka4:172.16.238.13"
    stdin_open: true
    tty: true

  kafka3:
    image: kafka
    container_name: kafka3
    hostname: kafka3
    networks:
      kafka_default:
        ipv4_address: 172.16.238.12
    ports:
        - "2181"
        - "9092"
    extra_hosts:
        - "kafka1:172.16.238.10"
        - "kafka2:172.16.238.11"
        - "kafka4:172.16.238.13"
    stdin_open: true
    tty: true

  kafka4:
    image: kafka
    container_name: kafka4
    hostname: kafka4
    networks:
      kafka_default:
        ipv4_address: 172.16.238.13
    ports:
        - "2181"
        - "9092"
    extra_hosts:
        - "kafka1:172.16.238.10"
        - "kafka2:172.16.238.11"
        - "kafka3:172.16.238.12"
    stdin_open: true
    tty: true
networks:
  kafka_default:
    driver: bridge
    driver_opts:

networks:
  kafka_default:
    driver: bridge
    driver_opts:
      com.docker.network.enable_ipv6: "true"
    ipam:
      driver: default
      config:
      - subnet: 172.16.238.0/24
        gateway: 172.16.238.1
      - subnet: 2001:3984:3989::/64
        gateway: 2001:3984:3989::1


3.コンテナを起動&設定する

# docker-compose up

以下は各コンテナで設定する。
★zookeeper.properties
以下を追加(全コンテナ共通)

server.1=kafka1:2888:3888
server.2=kafka2:2888:3888
server.3=kafka3:2888:3888
server.4=kafka4:2888:3888
initLimit=5
syncLimit=2

★myid

kakfa1: # echo 1 > /tmp/zookeeper/myid
kakfa2: # echo 2 > /tmp/zookeeper/myid
kakfa3: # echo 3 > /tmp/zookeeper/myid
kakfa4: # echo 4 > /tmp/zookeeper/myid

★server.properties

kakfa1:
broker.id=1
zookeeper.connect=kafka1:2181,kafka2:2181,kafka3:2181,kafka4:2181
host.name=kafka1


kakfa2:
broker.id=2
zookeeper.connect=kafka1:2181,kafka2:2181,kafka3:2181,kafka4:2181
host.name=kafka2


kakfa3:
broker.id=3
zookeeper.connect=kafka1:2181,kafka2:2181,kafka3:2181,kafka4:2181
host.name=kafka3


kakfa3:
broker.id=3
zookeeper.connect=kafka1:2181,kafka2:2181,kafka3:2181,kafka4:2181
host.name=kafka3

Zookeeperとkafkaを起動する。面倒なので一括で起動する。

# docker ps | sed '1d' | awk '{print$1}' | xargs -I{} docker exec -d {} /usr/local/lib/kafka/bin/zookeeper-server-start.sh /usr/local/lib/kafka/config/zookeeper.properties
# docker ps | sed '1d' | awk '{print$1}' | xargs -I{} docker exec -d {} /usr/local/lib/kafka/bin/kafka-server-start.sh /usr/local/lib/kafka/config/server.properties

トピックを作成する。

# kafka/bin/kafka-topics.sh --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic mytopic

トピックが作成されたか確認する。

# docker ps | sed '1d' | awk '{print$1}' | xargs -I{} docker exec {} ls /tmp/kafka-logs


kafka1:
cleaner-offset-checkpoint
meta.properties
recovery-point-offset-checkpoint
replication-offset-checkpoint


kafka2:
cleaner-offset-checkpoint
meta.properties
recovery-point-offset-checkpoint
replication-offset-checkpoint


kafka3:
cleaner-offset-checkpoint
meta.properties
mytopic-0★(作成されたトピック)
recovery-point-offset-checkpoint
replication-offset-checkpoint


kafka4:
cleaner-offset-checkpoint
meta.properties
recovery-point-offset-checkpoint
replication-offset-checkpoint

(上記の表示は編集上加工してます)


今度はパーティションを4つにしてトピックを作成してみる。

# /usr/local/lib/kafka/bin/kafka-topics.sh --create --zookeeper kafka1:2181 --replication-factor 1 --partitions 4 --topic mytopic_part4
# docker ps | sed '1d' | awk '{print$1}' | xargs -I{} docker exec {} ls /tmp/kafka-logs

kafka1:
cleaner-offset-checkpoint
meta.properties
mytopic_part4-3★(作成されたトピック)
recovery-point-offset-checkpoint
replication-offset-checkpoint


kafka2
cleaner-offset-checkpoint
meta.properties
mytopic_part4-0★(作成されたトピック)
recovery-point-offset-checkpoint
replication-offset-checkpoint


kafka3:
cleaner-offset-checkpoint
meta.properties
mytopic-0
mytopic_part4-1★(作成されたトピック)
recovery-point-offset-checkpoint
replication-offset-checkpoint


kafka4:
cleaner-offset-checkpoint
meta.properties
mytopic_part4-2★(作成されたトピック)
recovery-point-offset-checkpoint
replication-offset-checkpoint


今度はパーティションを4つにして、かつレプリカ3でトピックを作成してみる。

# /usr/local/lib/kafka/bin/kafka-topics.sh --create --zookeeper kafka1:2181 --replication-factor 3 --partitions 4 --topic mytopic_part4_rep3

kafka1:
cleaner-offset-checkpoint
meta.properties
mytopic_part4-3
mytopic_part4_rep3-0★(作成されたトピック)
mytopic_part4_rep3-1★(作成されたトピック)
mytopic_part4_rep3-2★(作成されたトピック)
recovery-point-offset-checkpoint
replication-offset-checkpoint

kafka2:
cleaner-offset-checkpoint
meta.properties
mytopic_part4-0
mytopic_part4_rep3-1★(作成されたトピック)
mytopic_part4_rep3-2★(作成されたトピック)
mytopic_part4_rep3-3★(作成されたトピック)
recovery-point-offset-checkpoint
replication-offset-checkpoint

kafka3:
cleaner-offset-checkpoint
meta.properties
mytopic-0
mytopic_part4-1
mytopic_part4_rep3-0★(作成されたトピック)
mytopic_part4_rep3-2★(作成されたトピック)
mytopic_part4_rep3-3★(作成されたトピック)
recovery-point-offset-checkpoint
replication-offset-checkpoint

kafka4:
cleaner-offset-checkpoint
meta.properties
mytopic_part4-2
mytopic_part4_rep3-0★(作成されたトピック)
mytopic_part4_rep3-1★(作成されたトピック)
mytopic_part4_rep3-3★(作成されたトピック)
recovery-point-offset-checkpoint
replication-offset-checkpoint

トピックにメッセージを送る。

# /usr/local/lib/kafka/bin/kafka-console-producer.sh --broker-list localhost:9092 --topic mytopic_part4_rep
12345
(Ctrl-C)
# docker ps | sed '1d' | awk '{print$1}' | xargs -I{} docker exec {} ls -l /tmp/kafka-logs/mytopic_part4_rep3-0
total 4
-rw-r--r-- 1 root root 10485760 Jul 23 19:22 00000000000000000000.index
-rw-r--r-- 1 root root       34 Jul 23 19:45 00000000000000000000.log
ls: cannot access /tmp/kafka-logs/mytopic_part4_rep3-0: No such file or directory
total 4
-rw-r--r-- 1 root root 10485760 Jul 23 19:23 00000000000000000000.index
-rw-r--r-- 1 root root       34 Jul 23 19:45 00000000000000000000.log
total 4
-rw-r--r-- 1 root root 10485760 Jul 23 19:23 00000000000000000000.index
-rw-r--r-- 1 root root       34 Jul 23 19:45 00000000000000000000.log

# docker ps | sed '1d' | awk '{print$1}' | xargs -I{} docker exec {} ls -l /tmp/kafka-logs/mytopic_part4_rep3-1
total 4
-rw-r--r-- 1 root root 10485760 Jul 23 19:23 00000000000000000000.index
-rw-r--r-- 1 root root       34 Jul 23 19:45 00000000000000000000.log
total 4
-rw-r--r-- 1 root root 10485760 Jul 23 19:23 00000000000000000000.index
-rw-r--r-- 1 root root       34 Jul 23 19:45 00000000000000000000.log
ls: cannot access /tmp/kafka-logs/mytopic_part4_rep3-1: No such file or directory
total 4
-rw-r--r-- 1 root root 10485760 Jul 23 19:23 00000000000000000000.index
-rw-r--r-- 1 root root       34 Jul 23 19:45 00000000000000000000.log

# docker ps | sed '1d' | awk '{print$1}' | xargs -I{} docker exec {} ls -l /tmp/kafka-logs/mytopic_part4_rep3-2
total 0
-rw-r--r-- 1 root root 10485760 Jul 23 19:23 00000000000000000000.index
-rw-r--r-- 1 root root        0 Jul 23 19:23 00000000000000000000.log
total 0
-rw-r--r-- 1 root root 10485760 Jul 23 19:23 00000000000000000000.index
-rw-r--r-- 1 root root        0 Jul 23 19:23 00000000000000000000.log
total 0
-rw-r--r-- 1 root root 10485760 Jul 23 19:22 00000000000000000000.index
-rw-r--r-- 1 root root        0 Jul 23 19:22 00000000000000000000.log
ls: cannot access /tmp/kafka-logs/mytopic_part4_rep3-2: No such file or directory

# docker ps | sed '1d' | awk '{print$1}' | xargs -I{} docker exec {} ls -l /tmp/kafka-logs/mytopic_part4_rep3-3
ls: cannot access /tmp/kafka-logs/mytopic_part4_rep3-3: No such file or directory
total 4
-rw-r--r-- 1 root root 10485760 Jul 23 19:23 00000000000000000000.index
-rw-r--r-- 1 root root       34 Jul 23 19:45 00000000000000000000.log
total 4
-rw-r--r-- 1 root root 10485760 Jul 23 19:23 00000000000000000000.index
-rw-r--r-- 1 root root       34 Jul 23 19:45 00000000000000000000.log
total 4
-rw-r--r-- 1 root root 10485760 Jul 23 19:23 00000000000000000000.index
-rw-r--r-- 1 root root       34 Jul 23 19:45 00000000000000000000.log

全体ではパーティション✕レプリカの数だけディレクトリが作成され、
メッセージは上記の数に分散して保存される。
今回のメッセージはmytopic_part4_rep3-0, mytopic_part4_rep3-1, mytopic_part4_rep3-3に格納されている。

こんな感じでした。