Chapter Directory
-
- 1. Reference link
- 2. Excerpt Citation
- 3. Installation process
- 4. Kafka Shell
- 5. Test
- 6. Run Kafka-docker on a MAC computer
- 7. Troubleshooting
1. Reference link
- Docker quickly builds Kafka 1.x cluster
- Kafka Docker - Run multiple Kafka brokers in Docker
- Running a Multi-Broker Apache Kafka 0.8 Cluster on a Single Node
- Using Vagrant to get up and running with Apache Kafka
- The difference and application of kafka listeners and advertised.listeners
- Kafka configures KAFKA_LISTENERS and KAFKA_ADVERTISED_LISTENERS
2. Excerpt Citation
2.1 Docker image selection
Kafka does not have an official Docker image, so either write a Dockerfile yourself, or use a third-party built one.
It is not impossible to write a Dockerfile by yourself, but it does not meet my "fastest" goal, so it is the fastest to use a third-party built image.
Since it is a third-party mirror, I hope that the more people who have used it, the better, so that there will be relatively fewer pitfalls.
With the above requirements in mind, we began to look for suitable third-party mirrors. The more famous ones are as follows:
wurstmeister/kafka
Features: The number of stars is the largest, the version is updated to Kafka 1.0, and zookeeper and kafka are separated from different mirrors.spotify/kafka
Features: The number of stars is large, and many articles or tutorials recommend it. Zookeeper and Kafka are placed in the same image; but the version of Kafka is relatively old (still at 0.10.1.0).confluent/kafka
Background: Confluent is a new company founded by Jay Kreps, who developed Kafka mentioned in the book, after leaving LinkedIn. Confluent Platform is a streaming data platform that has built a series of products around Kafka. Features: It is written by a big guy, with detailed documentation, but it is also bundled with Confluent Platform.
Among the above three projects, the one that was finally selected for use wurstmeister/kafka
has the most stars, and the version has been kept updated, so it should be more reassuring to use.
2.2 wurstmeister/kafka
Prerequisites for Introduction to Mirroring
- Install
docker-compose
- Modify in
docker-compose.yml
the configuration fileKAFKA_ADVERTISED_HOST_NAME
to match your docker host IP (note: if you want to run a multi-broker cluster, do not uselocalhost
or127.0.0.1
as the host ip) - If you want to add custom Kafka parameters, just add them to
docker-compose.yml
. For example:- Add
message.max.bytes
parameter, addKAFKA_MESSAGE_MAX_BYTES: 2000000
toenvironment
section. - Turn off automatic topic creation set, configure
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'false'
- Add
LOG4J_
Kafka can customize log4j usage by prefixing environment variables . These environment variables are mapped tolog4j.properties
. For example:LOG4J_LOGGER_KAFKA-AUTHORIZER_LOGGER=DEBUG,authorizerAppender
**NOTE:** There are several "problems" with configuring the network. If you don't know what the requirements are, check out the hookup guide
2.3 Easy to use
Start the cluster:
docker-compose up -d
Add more brokers
docker-compose scale kafka=3
destroy a cluster
docker-compose stop
3. Installation process
3.1 Install Docker (omitted)
3.2 Install Docker Compose (omitted)
3.3 Pull the latest packaged resources from github
# 克隆docker制作脚本
git clone https://github.com/wurstmeister/kafka-docker.git
# 进入目录查看目录文件
cd kafka-docker
# 查看所有标签,找到最新版本
git tag
# 切换到最新版本
git checkout <last_tag_name>
Kafka-docker warehouse directory file display:
[root@jiewli kafka-docker]# ll
总用量 72
-rwxr-xr-x. 1 root root 210 7月 7 17:18 broker-list.sh
-rw-r--r--. 1 root root 969 7月 7 17:18 CHANGELOG.md
-rwxr-xr-x. 1 root root 1221 7月 7 17:18 create-topics.sh
-rw-r--r--. 1 root root 367 7月 7 17:08 docker-compose-single-broker.yml
-rw-r--r--. 1 root root 705 7月 7 17:08 docker-compose-swarm.yml
-rw-r--r--. 1 root root 324 7月 7 17:18 docker-compose.yml
-rw-r--r--. 1 root root 1132 7月 7 17:18 Dockerfile
-rwxr-xr-x. 1 root root 395 7月 7 17:18 download-kafka.sh
-rw-r--r--. 1 root root 11325 7月 7 17:08 LICENSE
-rw-r--r--. 1 root root 9887 7月 7 17:18 README.md
-rwxr-xr-x. 1 root root 4591 7月 7 17:18 start-kafka.sh
-rwxr-xr-x. 1 root root 131 7月 7 17:08 start-kafka-shell.sh
drwxr-xr-x. 2 root root 4096 7月 7 17:18 test
3.4 Update docker-compose.yml
your docker host ip
Use the command vim docker-compose.yml
to view the configuration files in the warehouse directory docker-compose.yml
.
version: '2'
services:
zookeeper:
image: wurstmeister/zookeeper
ports:
- "2181:2181"
kafka:
build: .
ports:
- "9092"
environment:
DOCKER_API_VERSION: 1.22
KAFKA_ADVERTISED_HOST_NAME: 192.168.99.100
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
volumes:
- /var/run/docker.sock:/var/run/docker.sock
Important:
Versions after kafka 0.9.x add a newadvertised.listeners
configuration.
Versions after kafka 0.9.x do not useadvertised.host.name
andadvertised.host.port
have been deprecated
host.name
andport
are deprecated, uselisteners
insteadTherefore,
KAFKA_ADVERTISED_HOST_NAME
the configuration should also be changed toKAFKA_ADVERTISED_LISTENERS
, and the latter requires configurationKAFKA_LISTENERS
, otherwise an exception will be thrown:
ERROR: Missing environment variable KAFKA_LISTENERS. Must be specified when using KAFKA_ADVERTISED_LISTENERS
version: '2'
services:
zookeeper:
image: wurstmeister/zookeeper
ports:
- "2181:2181"
kafka:
build: .
ports:
- "9092"
environment:
DOCKER_API_VERSION: 1.22
KAFKA_LISTENERS: PLAINTEXT://:9092
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.99.100:9094
KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
volumes:
- /var/run/docker.sock:/var/run/docker.sock
Change the above KAFKA_ADVERTISED_HOST_NAME
to your docker host ip.
If you want to add custom Kafka parameters, just add them to docker-compose.yml
. For example:
- Add
message.max.bytes
parameter, addKAFKA_MESSAGE_MAX_BYTES: 2000000
toenvironment
section. - Turn off automatic topic creation set, configure
KAFKA_AUTO_CREATE_TOPICS_ENABLE: 'false'
Startup failed:
[root@jiewli kafka-docker]# docker logs -f --tail=300 kafka-docker_kafka_1
ERROR: Missing environment variable KAFKA_LISTENERS. Must be specified when using KAFKA_ADVERTISED_LISTENERS
ERROR: Missing environment variable KAFKA_LISTENERS. Must be specified when using KAFKA_ADVERTISED_LISTENERS
ERROR: Missing environment variable KAFKA_LISTENERS. Must be specified when using KAFKA_ADVERTISED_LISTENERS
ERROR: Missing environment variable KAFKA_LISTENERS. Must be specified when using KAFKA_ADVERTISED_LISTENERS
ERROR: Missing environment variable KAFKA_LISTENERS. Must be specified when using KAFKA_ADVERTISED_LISTENERS
ERROR: Missing environment variable KAFKA_LISTENERS. Must be specified when using KAFKA_ADVERTISED_LISTENERS
ERROR: Missing environment variable KAFKA_LISTENERS. Must be specified when using KAFKA_ADVERTISED_LISTENERS
ERROR: Missing environment variable KAFKA_LISTENERS. Must be specified when using KAFKA_ADVERTISED_LISTENERS
ERROR: Missing environment variable KAFKA_LISTENERS. Must be specified when using KAFKA_ADVERTISED_LISTENERS
3.5 Start the cluster
$ docker-compose up -d
For example, to start a broker
cluster with two
$ docker-compose scale kafka=2
This starts a single zookeeper
instance, and two Kafka
instances. You can use docker-compose ps
the command to display running instances.
If you want to add more Kafka
brokers, just docker-compose scale kafka=n
increase the value through the command.
docker-compose -f docker-compose.yml up -d
3.5.1 Error encountered when executing command
wget: server returned error: HTTP/1.1 404 Not Found
The command '/bin/sh -c apk add --no-cache bash curl jq docker && mkdir /opt && chmod a+x /tmp/*.sh && mv /tmp/start-kafka.sh /tmp/broker-list.sh /tmp/create-topics.sh /usr/bin && sync && /tmp/download-kafka.sh && tar xfz /tmp/kafka_${SCALA_VERSION}-${KAFKA_VERSION}.tgz -C /opt && rm /tmp/kafka_${SCALA_VERSION}-${KAFKA_VERSION}.tgz && ln -s /opt/kafka_${SCALA_VERSION}-${KAFKA_VERSION} /opt/kafka && rm /tmp/* && wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/${GLIBC_VERSION}/glibc-${GLIBC_VERSION}.apk && apk add --no-cache --allow-untrusted glibc-${GLIBC_VERSION}.apk && rm glibc-${GLIBC_VERSION}.apk' returned a non-zero code: 1
ERROR: Service 'kafka' failed to build
According to the prompt, wget
404 was encountered when the command downloaded a file.
3.5.2 Analyzing script execution process
From the Dockerfile
instructions RUN
you can find:
After some investigation, I finally located the command download-kafka.sh
in this script . I added a line of command and printed the whole command to see:wget
echo
wget
# echo打印结果
wget -q "https://mirrors.tuna.tsinghua.edu.cn/apache/kafka/1.1.0/kafka_2.12-1.1.0.tgz" -O "/tmp/kafka_2.12-1.1.0.tgz"
Find this file according to the link, and found that it was downloaded from the "Tsinghua University Open Source Software Mirror Station", but 1.1.0
this directory is gone, replaced by 2.x.x
the directory.
Can't download, obviously this version is too behind. It turns out that the warehouse git-tag was created in 2018, and no new tag version has been released for more than three years.
3.5.3 Use the build script of the master branch to start 2.x.x
the version of kafka
Then go back to master
the branch and look at the latest submission, which was submitted in June 2021. After carefully studying the script and Dockerfile of the master branch, the author has already adjusted the kafka version to 2.x.x
, and modified the download address.
Then after switching to the master branch, re-use the command $ docker-compose up -d
to start the kafka cluster.
3.6 Start a single node
docker-compose -f docker-compose-single-broker.yml up -d
Opening the container looks like this:
[root@jiewli ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43fb3efd832f kafka-docker_kafka "start-kafka.sh" 24 hours ago Up 4 hours 0.0.0.0:9092->9092/tcp kafka-docker_kafka_1
b8bc06090259 wurstmeister/zookeeper "/bin/sh -c '/usr/sb…" 24 hours ago Up 4 hours 22/tcp, 2888/tcp, 3888/tcp, 0.0.0.0:2181->2181/tcp kafka-docker_zookeeper_1
4. Kafka Shell
Enter the docker container interactive command line
docker exec -it kafka-docker_kafka_1 /bin/bash
Check the kafka installation directory and find the script location
bash-5.1# echo $KAFKA_HOME
/opt/kafka
bash-5.1# cd $KAFKA_HOME
bash-5.1# ls
LICENSE NOTICE bin config libs licenses logs site-docs
By default, the kafka installation directory is/opt/kafka
You can interact with your Kafka cluster through the Kafka Shell.
$ $KAFKA_HOME/bin/start-kafka-shell.sh <DOCKER_HOST_IP> <ZK_HOST:ZK_PORT>
5. Test
Test your installation steps.
Start a shell terminal, create a topic and start a producer.
$ $KAFKA_HOME/bin/kafka-topics.sh --create --topic topic \
--partitions 4 --zookeeper $ZK --replication-factor 2
$ $KAFKA_HOME/bin/kafka-topics.sh --describe --topic topic --zookeeper $ZK
$ $KAFKA_HOME/bin/kafka-console-producer.sh --topic=topic \
--broker-list=`broker-list.sh`
Start another shell terminal, and start a consumer.
$ $KAFKA_HOME/bin/kafka-console-consumer.sh --topic=topic --zookeeper=$ZK
6. Run Kafka-docker on a MAC computer
Install Docker Toolbox
and docker-machine ip
configure the ip returned by the command toKAFKA_ADVERTISED_HOST_NAME
7. Troubleshooting
- By default, the Kafka broker uses 1GB of memory, so if you have problems starting the broker, check
docker-compose logs
/docker logs
find the container and make sure there is enough free memory on the host. - If you want to run multiple brokers, please do not use
localhost
or127.0.0.1
as the host IP, otherwise the brokers will not be able to communicate.