使用docker compsose部署kafka,方便快捷,启动方便。
1. 拉去镜像
docker pull bitnami/zookeeper:3.6
docker pull bitnami/kafka:3.0
docker pull hlebalbau/kafka-manager
2. 编辑docker compose文件
version: "3"
services:
zookeeper1:
image: 'bitnami/zookeeper:3.6'
container_name: zookeeper1
ports:
- '2181:2181'
environment:
# 匿名登录--必须开启
- ALLOW_ANONYMOUS_LOGIN=yes
volumes:
- ./zookeeper:/home/docker-compose/kafka/zookeeper
# restart: always
zookeeper2:
image: 'bitnami/zookeeper:3.6'
container_name: zookeeper2
ports:
- '2182:2182'
environment:
# 匿名登录--必须开启
- ALLOW_ANONYMOUS_LOGIN=yes
volumes:
- ./zookeeper:/home/docker-compose/kafka/zookeeper
# restart: always
zookeeper3:
image: 'bitnami/zookeeper:3.6'
container_name: zookeeper3
ports:
- '2183:2183'
environment:
# 匿名登录--必须开启
- ALLOW_ANONYMOUS_LOGIN=yes
volumes:
- ./zookeeper:/home/docker-compose/kafka/zookeeper
# restart: always
kafka1:
image: 'bitnami/kafka:3.0'
container_name: kafka1
ports:
- '9092:9092'
environment:
- KAFKA_BROKER_ID=1
- KAFKA_CFG_LISTENERS=PLAINTEXT://:9092
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://192.168.17.188:9092
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper1:2181,zookeeper2:2182,zookeeper3:2183
- ALLOW_PLAINTEXT_LISTENER=yes
# restart: always
depends_on:
- zookeeper1
- zookeeper2
- zookeeper3
kafka2:
image: 'bitnami/kafka:3.0'
container_name: kafka2
ports:
- '9093:9093'
environment:
- KAFKA_BROKER_ID=2
- KAFKA_CFG_LISTENERS=PLAINTEXT://:9093
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://192.168.17.188:9093
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper1:2181,zookeeper2:2182,zookeeper3:2183
- ALLOW_PLAINTEXT_LISTENER=yes
# restart: always
depends_on:
- zookeeper1
- zookeeper2
- zookeeper3
kafka3:
image: 'bitnami/kafka:3.0'
container_name: kafka3
ports:
- '9094:9094'
environment:
- KAFKA_BROKER_ID=3
- KAFKA_CFG_LISTENERS=PLAINTEXT://:9094
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://192.168.17.188:9094
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper1:2181,zookeeper2:2182,zookeeper3:2183
- ALLOW_PLAINTEXT_LISTENER=yes
# restart: always
depends_on:
- zookeeper1
- zookeeper2
- zookeeper3
# Web 管理界面 另外也可以用exporter+prometheus+grafana的方式来监控 https://github.com/danielqsj/kafka_exporter
kafka_manager:
image: 'hlebalbau/kafka-manager:latest'
container_name: kafka-manager
ports:
- "9009:9000"
environment:
ZK_HOSTS: "zookeeper:2181"
# restart: always
depends_on:
- zookeeper1
- zookeeper2
- zookeeper3
- kafka1
- kafka2
- kafka3
3. 执行启动命令
docker compose -f kafka.yml up -d
docker compose -f kafka.yml stop
4. 验证