背景
- Kafka安装目录的config路径下,有
server.properties
文件
- 通常情况下,会指定
server.properties
来启动Broker
- 如果要设置Broker端的任何参数,必须要显式修改
server.properties
,然后重启Broker,让参数生效
- 但在生产环境,不能随意重启Broker,因此需要能够动态修改Broker端参数
- 社区于1.1.0正式引入了动态Broker参数
- 动态指的是修改参数后,无需重启Broker就能立即生效,而之前
server.properties
中配置的参数称为静态参数
- 并非所有Broker端参数都可以动态调整的,官方文档中有
Dynamic Update Mode
一列
- read-only
- 与原来的参数行为一样,只有重启Broker,才能令修改生效
- per-broker
- 动态参数,修改之后,只会在对应的Broker上生效
- cluster-wide
使用场景
- 动态调整Broker端各种线程池大小,实时应对突发流量 – 比较常用
- 动态调整Broker端连接信息或安全配置信息
- 动态更新SSL KeyStore有效期
- 动态调整Broker端Compact操作性能
- 实时变更JMX指标收集器(JMX Metrics Reporter)
保存机制
- Kafka将动态Broker参数保存在ZK中
changes
节点用来实时监测动态参数变更的,不会保存参数值
topics
节点用来保存Kafka主题级别参数的
users
节点和clients
节点用来动态调整客户端配额
- 配额:限制连入集群的客户端的吞吐量或使用的CPU资源
brokers
节点用来保存动态Broker端参数
<default>
节点用来保存cluster-wide
范围的动态参数
broker_id
节点用来保存特定Broker的per-broker
范围的动态参数
- 参数优先级
per-broker
参数 > cluster-wide
参数 > static参数 > Kafka默认值
- 下图中的ephemeralOwner字段都是
0x0
,表示这些znode都是持久化节点,即使ZK集群重启,动态参数也不会丢失
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
| [zk: localhost:2181(CONNECTED) 21] ls /config [changes, clients, brokers, topics, users]
[zk: localhost:2181(CONNECTED) 22] ls /config/brokers [0, <default>]
[zk: localhost:2181(CONNECTED) 23] get /config/brokers/<default> {"version":1,"config":{"unclean.leader.election.enable":"true"}} cZxid = 0xe89 ctime = Thu Oct 24 09:28:50 CST 2019 mZxid = 0xe89 mtime = Thu Oct 24 09:28:50 CST 2019 pZxid = 0xe89 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 64 numChildren = 0
[zk: localhost:2181(CONNECTED) 24] get /config/brokers/0 {"version":1,"config":{"leader.replication.throttled.rate":"104857600","follower.replication.throttled.rate":"104857600"}} cZxid = 0xdef ctime = Mon Oct 21 09:50:13 CST 2019 mZxid = 0xe07 mtime = Mon Oct 21 10:07:23 CST 2019 pZxid = 0xdef cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 122 numChildren = 0
|
配置命令
设置cluster-wide参数
如果要设置cluster-wide
范围的动态参数,需要显式指定**entity-default
**
1 2
| $ kafka-configs --bootstrap-server localhost:9092 --entity-type brokers --entity-default --alter --add-config unclean.leader.election.enable=true Completed updating default config for brokers in the cluster,
|
查看配置是否成功,sensitive=false
表明要调整的参数不是敏感数据
1 2 3
| $ kafka-configs --bootstrap-server localhost:9092 --entity-type brokers --entity-default --describe Default config for brokers in the cluster are: unclean.leader.election.enable=true sensitive=false synonyms={DYNAMIC_DEFAULT_BROKER_CONFIG:unclean.leader.election.enable=true}
|
设置per-broker参数
1 2
| $ kafka-configs --bootstrap-server localhost:9092 --entity-type brokers --entity-name 0 --alter --add-config unclean.leader.election.enable=false,leader.replication.throttled.rate=104857600,follower.replication.throttled.rate=104857600 Completed updating config for broker: 0.
|
查看配置是否成功,重点关注
实际值:unclean.leader.election.enable=false
per-broker参数:DYNAMIC_BROKER_CONFIG:unclean.leader.election.enable=false
cluster-wide参数:DYNAMIC_DEFAULT_BROKER_CONFIG:unclean.leader.election.enable=true
Kafka默认值:DEFAULT_CONFIG:unclean.leader.election.enable=false
1 2 3 4 5
| $ kafka-configs --bootstrap-server localhost:9092 --entity-type brokers --entity-name 0 --describe Configs for broker 0 are: leader.replication.throttled.rate=null sensitive=true synonyms={DYNAMIC_BROKER_CONFIG:leader.replication.throttled.rate=null} follower.replication.throttled.rate=null sensitive=true synonyms={DYNAMIC_BROKER_CONFIG:follower.replication.throttled.rate=null} unclean.leader.election.enable=false sensitive=false synonyms={DYNAMIC_BROKER_CONFIG:unclean.leader.election.enable=false, DYNAMIC_DEFAULT_BROKER_CONFIG:unclean.leader.election.enable=true, DEFAULT_CONFIG:unclean.leader.election.enable=false}
|
删除cluster-wide参数
1 2 3 4 5
| $ kafka-configs --bootstrap-server localhost:9092 --entity-type brokers --entity-default --alter --delete-config unclean.leader.election.enable Completed updating default config for brokers in the cluster,
$ kafka-configs --bootstrap-server localhost:9092 --entity-type brokers --entity-default --describe Default config for brokers in the cluster are:
|
删除per-broker参数
1 2 3 4 5
| $ kafka-configs --bootstrap-server localhost:9092 --entity-type brokers --entity-name 0 --alter --delete-config unclean.leader.election.enable,leader.replication.throttled.rate,follower.replication.throttled.rate Completed updating config for broker: 0.
$ kafka-configs --bootstrap-server localhost:9092 --entity-type brokers --entity-name 0 --describe Configs for broker 0 are:
|
常用动态参数
- log.retention.ms
- num.io.threads、num.network.threads
- 与SSL相关:ssl.keystore.type、ssl.keystore.location、ssl.keystore.password 和 ssl.key.password
- 允许动态实时调整这些参数后,可以创建那些过期时间很短的SSL证书
- 每当调整这些参数后,Kafka底层会重新配置Socket连接通道并更新Keystore
- 新的连接会使用新的Keystore,阶段性地调整这些参数,有利于增加安全性
- num.replica.fetchers
参考资料
Kafka核心技术与实战