文章目录
- Kafka中涉及到的术语
- Kafka镜像选择
- [Kafka UI镜像选择](#Kafka UI镜像选择)
- [Docker Compose文件](#Docker Compose文件)
-
- Kafka配置项说明
-
- [KRaft vs Zookeeper](#KRaft vs Zookeeper)
- 和KRaft有关的配置
- 关于Controller和Broker的概念解释
- Listener的各种配置
- [Kafka UI配置项说明](#Kafka UI配置项说明)
- 测试
- [Kafka集群Docker Compose示例配置](#Kafka集群Docker Compose示例配置)
Kafka中涉及到的术语
对于Kafka中经常用到的术语,可参考confluent的官方文档,这里不再赘述。
Kafka镜像选择
镜像选择Docker Hub上使用最多的bitnami Kafka,主要注意的点是环境变量和Kafka配置的映射关系
Additionally, any environment variable beginning with KAFKA_CFG_ will be mapped to its corresponding Apache Kafka key. For example, use KAFKA_CFG_BACKGROUND_THREADS in order to set background.threads or KAFKA_CFG_AUTO_CREATE_TOPICS_ENABLE in order to configure auto.create.topics.enable
还有就是,当使用任何来自于bitnami的镜像,如何遇到了问题,想查看日志,可以将镜像的Debug日志打开,通过环境变量
yaml
BITNAMI_DEBUG=true
控制
由于Docker Hub的说明字数限制,可以在Github上查看完整文档
Kafka UI镜像选择
对于Kafka的UI工具,没有仔细调查,原因是在使用初期阶段,还不知道对于Kafka的监控和管理的痛点在哪,所以先用起来再说。
Kafka UI官方Github地址 > 文档网址 > Compose examples 下面可以找打很多Kafak ui的compose文件示例,不仅对UI的配置很有帮助,而且对刚入门Kafka的同学,也提供了非常好的示例,包括Kraft模式的Kafka集群等。
其他配置则阅读官方文档即可。
Docker Compose文件
yaml
version: "3"
services:
kafka:
image: 'bitnami/kafka:latest'
container_name: kafka
ports:
- "9092:9092"
- "9093:9093"
volumes:
- type: volume
source: kafka_standalone_data
target: /bitnami/kafka
read_only: false
environment:
- BITNAMI_DEBUG=yes
# 启用KRaft模式必须设置下面三个属性
- KAFKA_CFG_NODE_ID=1
- KAFKA_CFG_PROCESS_ROLES=broker,controller
- KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
# broker id
- KAFKA_BROKER_ID=1
# listener的各种配置
- KAFKA_CFG_LISTENERS=CONTROLLER://:9094,BROKER://:9092,EXTERNAL://:9093
- KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,BROKER:PLAINTEXT,EXTERNAL:PLAINTEXT
# 注意EXTERNAL配置的是当前Docker所在的主机地址,BROKER可以使用Docker内部的网络地址即可
- KAFKA_CFG_ADVERTISED_LISTENERS=BROKER://kafka:9092,EXTERNAL://192.168.0.101:9093
# 内部各个broker之间通信用的listener
- KAFKA_CFG_INTER_BROKER_LISTENER_NAME=BROKER
# 用来进行选举的Controller服务器,如果有多个Controller则都需要写上,这里本机
- KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=1@127.0.0.1:9094
- ALLOW_PLAINTEXT_LISTENER=yes
kafka-ui:
container_name: kafka-ui
image: provectuslabs/kafka-ui:latest
ports:
- "9095:8080"
depends_on:
- kafka
environment:
KAFKA_CLUSTERS_0_NAME: kafka-stand-alone
KAFKA_CLUSTERS_0_BOOTSTRAPSERVERS: kafka:9092
KAFKA_CLUSTERS_0_METRICS_PORT: 9997
SERVER_SERVLET_CONTEXT_PATH: /kafkaui
AUTH_TYPE: "LOGIN_FORM"
SPRING_SECURITY_USER_NAME: admin
SPRING_SECURITY_USER_PASSWORD: kafkauipassword
DYNAMIC_CONFIG_ENABLED: 'true'
volumes:
kafka_standalone_data:
driver: local
Kafka配置项说明
KRaft vs Zookeeper
这里我们的配置是选择的是KRaft,因为Kafka官方已经计划在Kafak中移除Zookeeper。至于为什么要移除?confluentinc官方写了很多文章,这里不再一一列举,在Google上一搜就一大堆
shell
KRaft site:confluent.io
下面是几篇文章
- why move to kraft
- Why ZooKeeper Was Replaced with KRaft
- Apache Kafka Without ZooKeeper
- Getting Started with the KRaft Protocol
和KRaft有关的配置
- node.id The node ID associated with the roles this process is playing when
process.roles
is non-empty. This is required configuration when running in KRaft mode. - porcess.roles The roles that this process plays: 'broker', 'controller', or 'broker,controller' if it is both. This configuration is only applicable for clusters in KRaft (Kafka Raft) mode (instead of ZooKeeper). Leave this config undefined or empty for Zookeeper clusters
- controller.listener.names A comma-separated list of the names of the listeners used by the controller. This is required if running in KRaft mode
关于Controller和Broker的概念解释
一句话解释:
Controller负责协调Broker(详细解释可见Kafak权威指南的第5章,该书可在Apache Kafak官网 > Get Started > Books 中找到免费下载)
To summarize, Kafka uses Zookeeper's ephemeral node feature to elect a controller
and to notify the controller when nodes join and leave the cluster. The controller is
responsible for electing leaders among the partitions and replicas whenever it notices
nodes join and leave the cluster. The controller uses the epoch number to prevent a
"split brain" scenario where two nodes believe each is the current controller.
Broker负责处理生产者生产消息的请求、存储消息、消费者消费消息的请求。
A single Kafka server is called a broker. The broker receives messages from producers,
assigns offsets to them, and commits the messages to storage on disk. It also services
consumers, responding to fetch requests for partitions and responding with the mes‐
sages that have been committed to disk
来自Kafka权威指南第1章>Enter Kafka > Broker And Clusters
Listener的各种配置
当时看官方文档的时候,这部分一直被搞得晕头转向,直到看到这篇关于Kfaka的Listener文章,才彻底明白了各种listener,强烈推荐读一下这篇文章。
listener可分为3种:
- 用来选举Controller的listener
- kafka集群内部各broker节点通信的listener
- 外部客户端,例如Java Client连接Kafka
了解了3中controller,结合上面的这篇文章+Apache Kafka官方文档的配置说明,配置listener就变得很容易了。
Kafka UI配置项说明
对于UI配置项没什么特别要说的,这里只是提一下,注意这里的docker-compose.yml中environment的写法,和上面的Kafka镜像中environment的写法不同,这是两种不同的写法
详细文档见Docker Compose文档规范中environment章节
测试
使用上述docker-compose.yml文件,启动
shell
docker-compose -f docker-compose.yml up -d
在本地浏览器打开
shell
http://localhost:9095/kafkaui/auth
输入用户名、密码,进入UI界面
Kafka集群Docker Compose示例配置
本篇文章重点在于搭建单机版Kfaka环境,集群不在文章讨论范围之内,如果对集群配置感兴趣和有需要的同学,这里仅给出几个示例的Github仓库仅供参考,并且全部使用KRaft而不是Zookeeper
我个人的理解,最小的集群应该是3个controller+3个broker, Kafka关于KRaft模式下Controller的部署建议
- 来自bitnami的Kafka集群示例,三个节点皆为controller和broker。
- 来自confluentic的Kafka集群配置,我个人倾向于这个配置,毕竟confluentic是Kafka商业化的公司,其创始人来自LinkedIn。这里是四个节点,一个controller和3个broker。
- 来自Github的Kafka-In-Box,使用4个节点。