Docker部署kafka集群

1,编写Docker Compose文件

编写一个docker-compose.yml文件来定义服务,以下定义了一个Zookeeper服务和三个Kafka Broker服务:

注意:把10.0.8.4替换成宿主IP

java 复制代码
version: '3.8'

services:
  zookeeper:
    image: 'bitnami/zookeeper:latest'
    container_name: ncc-zookeeper
    ports:
      - '2181:2181'
    environment:
      - ALLOW_ANONYMOUS_LOGIN=yes
    networks:
      - kafka-net

  kafka1:
    image: 'bitnami/kafka:latest'
    container_name: ncc-kafka1
    ports:
      - '9092:9092'
    environment:
      - KAFKA_BROKER_ID=1
      - KAFKA_ADVERTISED_HOST_NAME=10.0.8.4
      - KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
      - KAFKA_LISTENERS=PLAINTEXT://:9092
      - KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.0.8.4:9092
      - KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      - KAFKA_INTER_BROKER_LISTENER_NAME=PLAINTEXT
      - KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1
    depends_on:
      - zookeeper
    networks:
      - kafka-net

  kafka2:
    image: 'bitnami/kafka:latest'
    container_name: ncc-kafka2
    ports:
      - '9093:9092'
    environment:
      - KAFKA_BROKER_ID=2
      - KAFKA_ADVERTISED_HOST_NAME=10.0.8.4
      - KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
      - KAFKA_LISTENERS=PLAINTEXT://:9092
      - KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.0.8.4:9093
      - KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      - KAFKA_INTER_BROKER_LISTENER_NAME=PLAINTEXT
      - KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1
    depends_on:
      - zookeeper
    networks:
      - kafka-net

  kafka3:
    image: 'bitnami/kafka:latest'
    container_name: ncc-kafka3
    ports:
      - '9094:9092'
    environment:
      - KAFKA_BROKER_ID=3
      - KAFKA_ADVERTISED_HOST_NAME=10.0.8.4
      - KAFKA_ZOOKEEPER_CONNECT=zookeeper:2181
      - KAFKA_LISTENERS=PLAINTEXT://:9092
      - KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.0.8.4:9094
      - KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
      - KAFKA_INTER_BROKER_LISTENER_NAME=PLAINTEXT
      - KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1
    depends_on:
      - zookeeper
    networks:
      - kafka-net

networks:
  kafka-net:
    driver: bridge
    external: false

2,启动Kafka集群

bash 复制代码
sudo docker-compose up -d

验证安装

一旦所有服务都已启动并运行,你可以通过连接到任意一个Kafka Broker来验证安装是否成功。例如,可以使用Kafka自带的命令行工具创建一个测试主题:

3,在ncc-kafka1创建主题,然后在ncc-kafka2查看主题列表

bash 复制代码
sudo docker exec -it ncc-kafka1 /opt/bitnami/kafka/bin/kafka-topics.sh --create --topic test-topic --bootstrap-server ncc-kafka1:9092 --replication-factor 3 --partitions 1
bash 复制代码
sudo docker exec -it ncc-kafka2 /opt/bitnami/kafka/bin/kafka-topics.sh --list --bootstrap-server ncc-kafka2:9092

4,验证发送消息

bash 复制代码
sudo docker exec -it ncc-kafka1 /opt/bitnami/kafka/bin/kafka-console-producer.sh --broker-list ncc-kafka1:9092 --topic test-topic

另外新开一个窗口,执行

bash 复制代码
sudo docker exec -it ncc-kafka3 /opt/bitnami/kafka/bin/kafka-console-consumer.sh --bootstrap-server ncc-kafka3:9092 --topic test-topic --from-beginning

测试验证成功

相关推荐
程序员劝退师_10 分钟前
Kafka学习笔记
笔记·学习·kafka
scc214014 分钟前
kafka:使用flume自定义拦截器,将json文件抽取到kafka的消息队列(topic)中,再从topic中将数据抽取到hdfs上
hdfs·kafka·flume
scc214031 分钟前
kafka中topic的数据抽取不到hdfs上问题解决
分布式·hdfs·kafka
水宝的滚动歌词2 小时前
K8S单节点部署及集群部署
云原生·容器·kubernetes
Z y..3 小时前
记:Docker部署kafka消费者注册不到问题
docker·容器·kafka
ly49833 小时前
docker--工作目录迁移
docker·容器·eureka
yohoo菜鸟5 小时前
kubernetes简单入门实战
云原生·容器·kubernetes
落非5 小时前
Ubuntu配置阿里云docker apt源
ubuntu·阿里云·docker
CodingBrother5 小时前
Kafka简单实践
分布式·kafka