#Kafka学習用の環境構築(Kafka×Jupyter)
大規模なデータの分散メッセージング・ストリーム処理を処理する場合、Kafkaが広く活用されている。
本記事のメモでは、Docker コンテナを用いて Kafka と Jupyter を連携させ、開発環境を構築した際の手順とメモを整理する。
Dockerコンテナが上手く立ち上がると、Jupyter側の開発環境は「http://localhost:8888」でアクセスできるようになり、Kaflaは「http://localhost:8080」からGUI画面で管理することができる。
#ディレクトリ構造#
mykafka
- Mount_Directory # Jupyter側のマウント先ディレクトリ(任意の命名)
- docker-compose.yml # 立ち上げ用($Docker-compose up -d)
- Dockerfile # Jupyter側の環境構築
- requirements.txt # Jupyter側で利用するライブラリ
#Docker-compose.yml#
services:
# ================== #
# JupyterLab(連携用)
# ================== #
datasciense:
container_name: jupyterlab
build:
context: .
dockerfile: Dockerfile
ports:
- "8888:8888"
volumes:
- ./Mount_Directory:/Mount_Directory
environment:
- LANG=ja_JP.UTF-8
- TZ=Asia/Tokyo
command: jupyter lab --ip=0.0.0.0 --allow-root --NotebookApp.token=''
# ========== #
# Zookeeper
# ========== #
zookeeper:
image: confluentinc/cp-zookeeper:7.6.0
container_name: zookeeper
environment:
ZOOKEEPER_CLIENT_PORT: 2181
ZOOKEEPER_TICK_TIME: 2000
ports:
- "2181:2181"
# ====== #
# Kafka
# ====== #
kafka:
image: confluentinc/cp-kafka:7.6.0
container_name: kafka
ports:
- "9092:9092"
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
depends_on:
- zookeeper
# ================= #
# Kafka UI(管理画面)
# ================= #
kafka-ui:
image: provectuslabs/kafka-ui:v0.7.2 # https://tinyurl.com/2be3bjzj #
container_name: kafka-ui
ports:
- "8080:8080"
environment:
- KAFKA_CLUSTERS_0_NAME=local
- KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS=kafka:9092
depends_on:
- kafka
#Dockerfile#
FROM ubuntu:22.04
ENV TZ=Asia/Tokyo
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
## 必要なパッケージ(apt-get)
RUN apt-get update && apt-get install -yq --no-install-recommends \
python3-pip \
python3-dev \
sudo \
vim \
curl \
wget \
unzip \
tzdata \
git \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
## 必要なパッケージ(pip)
COPY requirements.txt .
RUN pip install --upgrade pip && \
pip install --break-system-packages -r requirements.txt
## Jupyter NotebookのPDF出力を日本語に対応する
RUN sed -i 's/\\documentclass\[11pt\]{article}/\\documentclass\[xelatex,ja=standard\]{bxjsarticle}/' \
/usr/local/share/jupyter/nbconvert/templates/latex/index.tex.j2
## JupyterLab 作業ディレクトリ
WORKDIR /Mount_Directory
CMD ["jupyter", "lab", "--ip=0.0.0.0", "--allow-root", "--LabApp.token=''"]
#requirements.txt#
# 共通 (JupyterLab)
jupyterlab==4.2.5
jupyterlab-language-pack-ja-JP
# データサイエンス
numpy==2.1.1
pandas==2.2.2
matplotlib==3.9.2
# kafka
kafka-python==2.0.2
コメント(一覧)