搭建单机版kafka
version: '3'
services:
zookeeper:
image: wurstmeister/zookeeper
volumes:
- ./zookeeper_data:/opt/zookeeper/data
container_name: zookeeper
ports:
- "2181:2181"
restart: always
kafka:
image: wurstmeister/kafka
container_name: kafka_01
depends_on:
- zookeeper
ports:
- "9092:9092"
volumes:
- ./kafka_log:/kafka
environment:
- KAFKA_BROKER_NO=0
- KAFKA_BROKER_ID=0
- KAFKA_LISTENERS=PLAINTEXT://kafka_01:9092
- KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://192.168.26.129:9092
- KAFKA_INTER_BROKER_LISTENER_NAME=PLAINTEXT
- KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
- KAFKA_HEAP_OPTS=-Xmx512M -Xms16M
restart: always
kafka_manager:
image: sheepkiller/kafka-manager
ports:
- "9000:9000"
environment:
- ZK_HOSTS=zookeeper:2181
depends_on:
- zookeeper
- kafka
restart: always
其中192.168.26.129需要替换为真实的ip
启动命令:sudo docker-compose up -d
单机模拟搭建kafka集群
Docker-compose文件内容
version: "3"
services:
zookeeper:
image: 'bitnami/zookeeper:3.6'
container_name: zookeeper
ports:
- '2181:2181'
environment:
# 匿名登录--必须开启
- ALLOW_ANONYMOUS_LOGIN=yes
#volumes:
#- ./zookeeper:/bitnami/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.26.129:9092
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
- ALLOW_PLAINTEXT_LISTENER=yes
# restart: always
depends_on:
- zookeeper
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.26.129:9093
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
- ALLOW_PLAINTEXT_LISTENER=yes
# restart: always
depends_on:
- zookeeper
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.26.129:9094
- KAFKA_CFG_ZOOKEEPER_CONNECT=zookeeper:2181
- ALLOW_PLAINTEXT_LISTENER=yes
# restart: always
depends_on:
- zookeeper
# 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:
- zookeeper
- kafka1
- kafka2
- kafka3
其中192.168.26.129需要替换为真实的ip
启动命令:sudo docker-compose up -d
部分kafka命令:
#创建frist主题
kafka-topics.sh --bootstrap-server 192.168.26.129:9092 --create --partitions 1 --replication-factor 3 --topic first
#向first主题中发送消息
kafka-console-producer.sh --bootstrap-server 192.168.26.129:9092 --topic first
#消费frist主题中的消息
kafka-console-consumer.sh --bootstrap-server 192.168.26.129:9092 --topic first
#消费frist主题中的消息(包括历史消息)
kafka-console-consumer.sh --bootstrap-server 192.168.26.129:9092 --from-beginning --topic first