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

测试验证成功

相关推荐
2501_941881406 小时前
Kubernetes 容器集群资源调度与弹性扩容高可用架构在互联网业务实战经验总结
云原生·容器·kubernetes
究極の法則に通じた野犬6 小时前
k8s设计理念-k8s中哪些服务要部署成StatefulSet哪些部署成Deployment
云原生·容器·kubernetes
wuxingge6 小时前
k8s集群误删node节点,怎么添加回去
云原生·容器·kubernetes
贝锐6 小时前
Docker部署Teemii本地漫画库,并通过花生壳内网穿透实现远程访问
docker
观测云9 小时前
Kubernetes CRD 方式配置容器日志采集最佳实践
容器·kubernetes·日志分析
i***220710 小时前
springboot整合libreoffice(两种方式,使用本地和远程的libreoffice);docker中同时部署应用和libreoffice
spring boot·后端·docker
羑悻的小杀马特12 小时前
轻量跨云·掌控无界:Portainer CE + cpolar 让远程容器运维像点外卖一样简单——免复杂配置,安全直达对应集群
运维·网络·安全·docker·cpolar
Sinowintop16 小时前
易连EDI-EasyLink无缝集成之消息队列Kafka
分布式·网络协议·kafka·集成·国产化·as2·国产edi
佛祖让我来巡山17 小时前
Kafka入门:从初识到Spring Boot实战
kafka·kafka入门·kafka案例
EAIReport17 小时前
企业级报表自动化:基于Docker的部署实践
运维·docker·自动化