编写 docker-compose.yaml
主要参照:https://www.cnblogs.com/wangguishe/p/17563274.html
version: "3"
services:
kafka1:
image: 'bitnami/kafka:3.4.1'
container_name: kafka1
environment:
- KAFKA_HEAP_OPTS=-Xmx1024m -Xms1024m
- KAFKA_ENABLE_KRAFT=yes
- KAFKA_CFG_PROCESS_ROLES=broker,controller
- KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
- KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093
- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://192.168.3.21:19092 # 传递回客户端的元数据,填写宿主机IP地址
- KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=1@127.0.0.1:9093
- KAFKA_BROKER_ID=1
- KAFKA_CFG_NODE_ID=1
- KAFKA_KRAFT_CLUSTER_ID=jkUlhzQmQkic54LMxrB1oV
- KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=1@kafka1:9093,2@kafka2:9093,3@kafka3:9093
- ALLOW_PLAINTEXT_LISTENER=yes
volumes:
- "/f/kafkadata/apps/kafka/kafka1_data:/bitnami"
ports:
- "19092:9092"
networks:
kafka:
aliases:
- kafka
kafka2:
image: 'bitnami/kafka:3.4.1'
container_name: kafka2
environment:
- KAFKA_HEAP_OPTS=-Xmx1024m -Xms1024m
- KAFKA_ENABLE_KRAFT=yes
- KAFKA_CFG_PROCESS_ROLES=broker,controller
- KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
- KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093
- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://192.168.3.21:29092 # 传递回客户端的元数据,填写宿主机IP地址
- KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=1@127.0.0.1:9093
- KAFKA_BROKER_ID=2
- KAFKA_CFG_NODE_ID=2
- KAFKA_KRAFT_CLUSTER_ID=jkUlhzQmQkic54LMxrB1oV
- KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=1@kafka1:9093,2@kafka2:9093,3@kafka3:9093
- ALLOW_PLAINTEXT_LISTENER=yes
volumes:
- "/f/kafkadata/apps/kafka/kafka2_data:/bitnami"
ports:
- "29092:9092"
networks:
kafka:
aliases:
- kafka
kafka3:
image: 'bitnami/kafka:3.4.1'
container_name: kafka3
environment:
- KAFKA_HEAP_OPTS=-Xmx1024m -Xms1024m
- KAFKA_ENABLE_KRAFT=yes
- KAFKA_CFG_PROCESS_ROLES=broker,controller
- KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
- KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093
- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
- KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://192.168.3.21:39092 # 传递回客户端的元数据,填写宿主机IP地址
- KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=1@127.0.0.1:9093
- KAFKA_BROKER_ID=3
- KAFKA_CFG_NODE_ID=3
- KAFKA_KRAFT_CLUSTER_ID=jkUlhzQmQkic54LMxrB1oV
- KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=1@kafka1:9093,2@kafka2:9093,3@kafka3:9093
- ALLOW_PLAINTEXT_LISTENER=yes
volumes:
- "/f/kafkadata/apps/kafka/kafka3_data:/bitnami"
ports:
- "39092:9092"
networks:
kafka:
aliases:
- kafka
kafka-ui:
image: provectuslabs/kafka-ui:master
container_name: kafka-ui
ports:
- "38080:8080"
restart: always
environment:
- KAFKA_CLUSTERS_0_NAME=local
- KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS=kafka1:9092,kafka2:9092,kafka3:9092
- KAFKA_CLUSTERS_0_READONLY=true
depends_on:
- kafka1
- kafka2
- kafka3
networks:
kafka:
aliases:
- kafka-ui
networks:
kafka:
driver: bridge
ipam:
config:
- subnet: 172.31.16.0/24
注:KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://192.168.3.21:39092 IP一定要改成宿主机IP,不然Kafka-ui访问报错!!!
错误如下:
2024-04-03 20:07:15 2024-04-03 12:07:15,491 ERROR [parallel-3] c.p.k.u.s.StatisticsService: Failed to collect cluster local info
2024-04-03 20:07:15 java.lang.IllegalStateException: Error while creating AdminClient for Cluster local
2024-04-03 20:07:15 at com.provectus.kafka.ui.service.AdminClientServiceImpl.lambda$createAdminClient$5(AdminClientServiceImpl.java:56)
2024-04-03 20:07:15 at reactor.core.publisher.Mono.lambda$onErrorMap$28(Mono.java:3783)
2024-04-03 20:07:15 at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:94)
2024-04-03 20:07:15 at reactor.core.publisher.MonoPeekTerminal$MonoTerminalPeekSubscriber.onError(MonoPeekTerminal.java:258)
2024-04-03 20:07:15 at reactor.core.publisher.FluxMap$MapConditionalSubscriber.onError(FluxMap.java:265)
2024-04-03 20:07:15 at reactor.core.publisher.Operators$MonoSubscriber.onError(Operators.java:1886)
2024-04-03 20:07:15 at reactor.core.publisher.MonoCacheTime$CoordinatorSubscriber.signalCached(MonoCacheTime.java:340)
2024-04-03 20:07:15 at reactor.core.publisher.MonoCacheTime$CoordinatorSubscriber.onError(MonoCacheTime.java:363)
2024-04-03 20:07:15 at reactor.core.publisher.MonoFlatMap$FlatMapMain.onError(MonoFlatMap.java:180)
2024-04-03 20:07:15 at reactor.core.publisher.MonoIgnoreThen$ThenIgnoreMain.onError(MonoIgnoreThen.java:278)
2024-04-03 20:07:15 at reactor.core.publisher.MonoPublishOn$PublishOnSubscriber.run(MonoPublishOn.java:187)
2024-04-03 20:07:15 at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:68)
2024-04-03 20:07:15 at reactor.core.scheduler.SchedulerTask.call(SchedulerTask.java:28)
2024-04-03 20:07:15 at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
2024-04-03 20:07:15 at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
2024-04-03 20:07:15 at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
2024-04-03 20:07:15 at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
2024-04-03 20:07:15 at java.base/java.lang.Thread.run(Thread.java:833)
2024-04-03 20:07:15 Caused by: org.apache.kafka.common.errors.TimeoutException: Timed out waiting to send the call. Call: listNodes
在docker-compose.yaml所有的目录中执行安装命令:
docker-compose up -d
执行结果:
[+] Running 12/12
✔ kafka1 Pulled 135.4s
✔ kafka2 1 layers [⣿] 0B/0B Pulled 135.4s
✔ e711576d4690 Pull complete 117.1s
✔ kafka3 Pulled 135.4s
✔ kafka-ui 7 layers [⣿⣿⣿⣿⣿⣿⣿] 0B/0B Pulled 98.4s
✔ 0ce1dd7918a4 Pull complete 8.2s
✔ 396900a6066f Pull complete 69.9s
✔ ea77a99f32d6 Pull complete 12.1s
✔ d8a12b986814 Pull complete 13.9s
✔ ac59f2acb415 Pull complete 16.8s
✔ a5385df9cb3a Pull complete 19.9s
✔ 91a81fafb194 Pull complete 90.8s
[+] Running 4/5
- Network kafkadata_kafka Created 2.9s
✔ Container kafka2 Started 2.5s
✔ Container kafka3 Started 2.5s
✔ Container kafka1 Started 2.5s
✔ Container kafka-ui Started 1.4s
在Docker Desktop中查看是否启动成功:
查看启动日志是否报错:
进入控制台查看: