赞
踩
对于Kafka中经常用到的术语,可参考confluent的官方文档,这里不再赘述。
镜像选择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日志打开,通过环境变量
BITNAMI_DEBUG=true
控制
由于Docker Hub的说明字数限制,可以在Github上查看完整文档
对于Kafka的UI工具,没有仔细调查,原因是在使用初期阶段,还不知道对于Kafka的监控和管理的痛点在哪,所以先用起来再说。
Kafka UI官方Github地址 > 文档网址 > Compose examples 下面可以找打很多Kafak ui的compose文件示例,不仅对UI的配置很有帮助,而且对刚入门Kafka的同学,也提供了非常好的示例,包括Kraft模式的Kafka集群等。
其他配置则阅读官方文档即可。
version: "3" services: kafka: image: 'bitnami/kafka:latest' container_name: kafka ports: - "9092:9092" - "9093:9093" - "9998:9998" 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 # 开启JMX监控 - JMX_PORT=9998 - KAFKA_JMX_OPTS=-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=kafka -Dcom.sun.management.jmxremote.rmi.port=9998 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: 9998 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
这里我们的配置是选择的是KRaft,因为Kafka官方已经计划在Kafak中移除Zookeeper。至于为什么要移除?confluentinc官方写了很多文章,这里不再一一列举,在Google上一搜就一大堆
KRaft site:confluent.io
下面是几篇文章
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.
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
A comma-separated list of the names of the listeners used by the controller. This is required if running in KRaft mode
一句话解释:
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
当时看官方文档的时候,这部分一直被搞得晕头转向,直到看到这篇关于Kfaka的Listener文章,才彻底明白了各种listener,强烈推荐读一下这篇文章。
listener可分为3种:
了解了3中controller,结合上面的这篇文章+Apache Kafka官方文档的配置说明,配置listener就变得很容易了。
注意UI配置中的METRICS_PORT要设置成Kafka配置中的JMX_PORT,至于为什么,了解JMX原理的同学应该都知道,不清楚JMX的可参考JMX快速入门。
稍微有心的同学,可能发现,在docker compose中要访问另外一个服务,需要使用container_service_host_name:port(Docker官方文档Networking in Compose部分),但是这里配置项中只配置了port,Kafka UI是如何拼接JMX的URL呢?我去看了一下它的源码,发现它是可以拿到host的,所以这里不用担心
对于其他UI配置项没什么特别要说的,这里只是提一下,注意这里的docker-compose.yml中environment的写法,和上面的Kafka镜像中environment的写法不同,这是两种不同的写法
详细文档见Docker Compose文档规范中environment章节
使用上述docker-compose.yml文件,启动
docker-compose -f docker-compose.yml up -d
在本地浏览器打开
http://localhost:9095/kafkaui/auth
输入用户名、密码,进入UI界面
本篇文章重点在于搭建单机版Kfaka环境,集群不在文章讨论范围之内,如果对集群配置感兴趣和有需要的同学,这里仅给出几个示例的Github仓库仅供参考,并且全部使用KRaft而不是Zookeeper
我个人的理解,最小的集群应该是3个controller+3个broker, 为什么是3个broker呢?Kafka官方文档Replication一节 提到过
With this ISR model and f+1 replicas, a Kafka topic can tolerate f failures without losing committed messages
意思是,如果topic的复制因子replication factor是2(复制因子是包括leader的,见官方文档:The total number of replicas including the leader constitute the replication factor),那么在一个节点失败的情况下,Kafka还是可以正常工作的。这里Kafka采用的算法和ZooKeeper, Elasticsearch集群的算法是不一样的。如果换成ZK和ES,只有两个节点,这时ZK和ES是无法工作的。
The downside of majority vote is that it doesn’t take many failures to leave you with no electable leaders. To tolerate one failure requires three copies of the data, and to tolerate two failures requires five copies of the data. In our experience having only enough redundancy to tolerate a single failure is not enough for a practical system, but doing every write five times, with 5x the disk space requirements and 1/5th the throughput, is not very practical for large volume data problems. This is likely why quorum algorithms more commonly appear for shared cluster configuration such as ZooKeeper but are less common for primary data storage
那这么看来,Kafka集群其实只需要2个节点就可以了,为什么还是3个节点呢?带着这个疑问,我查了一下stackoverflow,还真有人问过这个问题,见in kafka ha why minimum number of brokers required are 3 and not 2
Kafka关于KRaft模式下Controller的部署建议
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。