kafka docker docker部署kafka单机和集群 hiriki 2024-04-09 2024-04-09 从 3.3 版本后,Kafka 引入了 KRaft 来替代 ZooKeeper,所以我们不必再部署 zk 了。
版本选择 v3.3.1
单机部署 创建Kafka目录
编写docker-compose.yml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 version: "3" services: kafka1: image: 'bitnami/kafka:3.3.1' network_mode: kafka-net container_name: kafka1 user: root ports: - 9192 :9092 - 9193 :9093 environment: - KAFKA_ENABLE_KRAFT=yes - KAFKA_CFG_NODE_ID=1 - 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_KRAFT_CLUSTER_ID=xYcCyHmJlIaLzLoBzVwIcP - KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=1@kafka1:9093 - ALLOW_PLAINTEXT_LISTENER=yes - KAFKA_HEAP_OPTS=-Xmx512M -Xms256M - KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE=false - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://ip:9192 - KAFKA_BROKER_ID=1 volumes: - /kafka-cluster/kafka1:/bitnami/kafka
集群部署 创建Kafka集群目录
编写docker-compose.yml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 version: "3" services: kafka1: image: 'bitnami/kafka:3.3.1' network_mode: kafka-net container_name: kafka1 user: root ports: - 9192 :9092 - 9193 :9093 environment: - 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_KRAFT_CLUSTER_ID=LelM2dIFQkiUFvXCEcqRWA - KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=1@kafka1:9093,2@kafka2:9093,3@kafka3:9093 - ALLOW_PLAINTEXT_LISTENER=yes - KAFKA_HEAP_OPTS=-Xmx512M -Xms256M - KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE=false - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://ip:9192 - KAFKA_BROKER_ID=1 volumes: - /kafka-cluster/kafka1:/bitnami/kafka kafka2: image: 'bitnami/kafka:3.3.1' network_mode: kafka-net container_name: kafka2 user: root ports: - 9292 :9092 - 9293 :9093 environment: - 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_KRAFT_CLUSTER_ID=LelM2dIFQkiUFvXCEcqRWA - KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=1@kafka1:9093,2@kafka2:9093,3@kafka3:9093 - ALLOW_PLAINTEXT_LISTENER=yes - KAFKA_HEAP_OPTS=-Xmx512M -Xms256M - KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE=false - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://ip:9292 - KAFKA_BROKER_ID=2 volumes: - /kafka-cluster/kafka2:/bitnami/kafka kafka3: image: 'bitnami/kafka:3.3.1' network_mode: kafka-net container_name: kafka3 user: root ports: - 9392 :9092 - 9393 :9093 environment: - 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_KRAFT_CLUSTER_ID=LelM2dIFQkiUFvXCEcqRWA - KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=1@kafka1:9093,2@kafka2:9093,3@kafka3:9093 - ALLOW_PLAINTEXT_LISTENER=yes - KAFKA_HEAP_OPTS=-Xmx512M -Xms256M - KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE=false - KAFKA_CFG_ADVERTISED_LISTENERS=PLAINTEXT://ip:9392 - KAFKA_BROKER_ID=3 volumes: - /kafka-cluster/kafka3:/bitnami/kafka
Kafka可视化页面 创建Kafka UI目录
编写docker-compose.yml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 version: "3" services: kafka-ui: image: provectuslabs/kafka-ui:latest network_mode: kafka-net container_name: kafka-ui restart: always ports: - 8090 :8080 volumes: - /etc/localtime:/etc/localtime environment: - KAFKA_CLUSTERS_0_NAME=local - KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS=kafka1:9092
Kafka相关命令操作 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 docker exec -it kafka1 /bin/bash cd /opt/bitnami/kafka/bin/kafka-topics.sh --create --bootstrap-server kafka1:9092 --replication-factor 1 --partitions 3 --topic test Created topic test . kafka-topics.sh --list --bootstrap-server kafka1:9092 test kafka-topics.sh --describe --bootstrap-server kafka1:9092 --topic test Topic: test TopicId: JDBdVHIYTe6y3iS6xGU-NQ PartitionCount: 3 ReplicationFactor: 1 Configs: Topic: test Partition: 0 Leader: 1 Replicas: 1 Isr: 1 Topic: test Partition: 1 Leader: 1 Replicas: 1 Isr: 1 Topic: test Partition: 2 Leader: 1 Replicas: 1 Isr: 1 kafka-console-producer.sh --broker-list kafka1:9092 --topic test [等待输入自己的内容 出现>输入即可] >i am a new msg ! >i am a good msg ? kafka-console-consumer.sh --bootstrap-server kafka1:9092 --topic test --from-beginning [等待消息] i am a new msg ! i am a good msg ?
参考 https://juejin.cn/post/7187301063832109112