获取镜像
# 查看可用的稳定版本
sudo docker search kafka
sudo docker pull wurstmeister/kafka
sudo docker image ls |grep kafka
服务编排
mkdir -p /share/kafka && cd /share/kafka
rm -rf /share/kafka/docker-compose.yml
vi /share/kafka/docker-compose.yml
单节点kafka编排
version: '2'
services:
zoo1:
image: zookeeper:3.4.10
restart: always
hostname: zoo1
privileged: true
container_name: zoo1
networks:
my_net:
ipv4_address: 172.24.12.5
ports:
- 2181:2181
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=0.0.0.0:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
zoo2:
image: zookeeper:3.4.10
restart: always
hostname: zoo2
privileged: true
container_name: zoo2
networks:
my_net:
ipv4_address: 172.24.12.6
ports:
- 2182:2181
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=0.0.0.0:2888:3888 server.3=zoo3:2888:3888
zoo3:
image: zookeeper:3.4.10
restart: always
hostname: zoo3
privileged: true
container_name: zoo3
networks:
my_net:
ipv4_address: 172.24.12.7
ports:
- 2183:2181
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=0.0.0.0:2888:3888
kafka1:
image: wurstmeister/kafka
networks:
my_net:
ipv4_address: 172.24.12.8
ports:
- "9092:9092"
environment:
KAFKA_ADVERTISED_HOST_NAME: localhost
KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181,zoo2:2181,zoo3:2181"
KAFKA_BROKER_ID: 1
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_CREATE_TOPICS: "mykafka1,mykafka2"
depends_on:
- zoo1
- zoo2
- zoo3
container_name: kafka
networks:
my_net:
ipam:
driver: default
config:
- subnet: "172.24.12.0/24"
多节点kafka编排
version: '2'
services:
zoo1:
image: zookeeper:3.4.10
restart: always
hostname: zoo1
privileged: true
container_name: zoo1
networks:
my_net:
ipv4_address: 172.24.12.5
ports:
- 2181:2181
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=0.0.0.0:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
zoo2:
image: zookeeper:3.4.10
restart: always
hostname: zoo2
privileged: true
container_name: zoo2
networks:
my_net:
ipv4_address: 172.24.12.6
ports:
- 2182:2181
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=0.0.0.0:2888:3888 server.3=zoo3:2888:3888
zoo3:
image: zookeeper:3.4.10
restart: always
hostname: zoo3
privileged: true
container_name: zoo3
networks:
my_net:
ipv4_address: 172.24.12.7
ports:
- 2183:2181
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=0.0.0.0:2888:3888
kafka1:
image: wurstmeister/kafka
networks:
my_net:
ipv4_address: 172.24.12.8
ports:
- "9092:9092"
environment:
KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181,zoo2:2181,zoo3:2181"
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://172.24.12.8:9092
KAFKA_BROKER_ID: 1
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
depends_on:
- zoo1
- zoo2
- zoo3
container_name: kafka1
kafka2:
image: wurstmeister/kafka
networks:
my_net:
ipv4_address: 172.24.12.9
ports:
- "9093:9093"
environment:
KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181,zoo2:2181,zoo3:2181"
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://172.24.12.9:9093
KAFKA_BROKER_ID: 2
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9093
depends_on:
- zoo1
- zoo2
- zoo3
container_name: kafka2
kafka3:
image: wurstmeister/kafka
networks:
my_net:
ipv4_address: 172.24.12.10
ports:
- "9094:9094"
environment:
KAFKA_ZOOKEEPER_CONNECT: "zoo1:2181,zoo2:2181,zoo3:2181"
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://172.24.12.10:9094
KAFKA_BROKER_ID: 3
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9094
depends_on:
- zoo1
- zoo2
- zoo3
container_name: kafka3
networks:
my_net:
ipam:
driver: default
config:
- subnet: "172.24.12.0/24"
服务构建
cd /share/kafka
sudo docker-compose -f docker-compose.yml build --no-cache # 不带缓存构建(只创建镜像,不会启动容器)
sudo docker-compose -f docker-compose.yml up -d # 构建后运行
sudo docker-compose -f docker-compose.yml up --build # 跟踪方式构建,可用于调试
验证
cd /share/kafka
# 查看进程
sudo docker-compose -f docker-compose.yml ps
# 列出所有Kafka brokers
docker exec zoo1 bin/zkCli.sh ls /brokers/ids
# 进入终端
sudo docker exec -it kafka /bin/bash
sudo docker exec -it kafka1 /bin/bash
# 查看kafka主题列表
kafka-topics.sh --zookeeper zoo1:2181,zoo2:2181,zoo3:2181 --list
# 删除kafka主题
kafka-topics.sh --zookeeper zoo1:2181,zoo2:2181,zoo3:2181 --delete --topic mykafka
# 创建kafka主题
kafka-topics.sh --create --zookeeper zoo1:2181,zoo2:2181,zoo3:2181 --replication-factor 1 --partitions 3 --topic mykafka
# kafka生产者
kafka-console-producer.sh --broker-list localhost:9092 --topic mykafka
# kafka消费者
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic mykafka --from-beginning
# 查看日志
sudo docker logs kafka
sudo docker logs -f -t --tail=50 kafka
# 查看网络
sudo docker network ls
sudo docker inspect kafka
# 停止
sudo docker-compose -f docker-compose.yml stop
# 移除
sudo docker-compose -f docker-compose.yml down
参考
博文:docker安装kafka
https://www.cnblogs.com/linjiqin/p/11891776.html