centos kafka的权限设置
导读:CentOS 上 Kafka 权限设置实操指南 一 架构与准备 权限体系由三部分构成:认证(Authentication)、授权(Authorization,ACL)、信道加密(Encryption,可选 SSL)。常见认证机制包含 SA...
CentOS 上 Kafka 权限设置实操指南
一 架构与准备
- 权限体系由三部分构成:认证(Authentication)、授权(Authorization,ACL)、信道加密(Encryption,可选 SSL)。常见认证机制包含 SASL/PLAIN、SASL/SCRAM-SHA-256/512、SASL/GSSAPI(Kerberos);授权通过 ACL 控制主体(用户)对资源(Topic、Group、Cluster 等)的操作(Read、Write、Describe、Create、Delete、Alter、DescribeConfigs、AlterConfigs、IdempotentWrite 等)。生产建议至少启用 SASL + ACL,跨机房或公网建议叠加 SSL/TLS。Kafka 安全自 0.9.0.0 起逐步完善,SCRAM 自 0.10.2.0 引入,便于动态管理用户凭据。
二 快速落地方案 SASL SCRAM + ACL(推荐)
- 适用场景:内网可信网络、需要动态增删用户、脚本化运维。
- 步骤
- 创建 JAAS 配置(示例:/opt/kafka/config/kafka_server_jaas.conf)
KafkaServer { org.apache.kafka.common.security.scram.ScramLoginModule required username="admin" password="Admin@123" user_admin="Admin@123" user_producer="Prod@123" user_consumer="Cons@123"; } ; - 配置 server.properties(关键项)
listeners=SASL_PLAINTEXT://:9092 advertised.listeners=SASL_PLAINTEXT://< your-hostname-or-ip> :9092 security.inter.broker.protocol=SASL_PLAINTEXT sasl.enabled.mechanisms=SCRAM-SHA-256 sasl.mechanism.inter.broker.protocol=SCRAM-SHA-256 listener.name.sasl_plaintext.scram-sha-256.sasl.jaas.config=file:/opt/kafka/config/kafka_server_jaas.conf authorizer.class.name=kafka.security.authorizer.AclAuthorizer allow.everyone.if.no.acl.found=false super.users=User:admin - 启动前导出 JAAS(systemd 示例,/etc/systemd/system/kafka.service)
[Service] Environment="KAFKA_OPTS=-Djava.security.auth.login.config=/opt/kafka/config/kafka_server_jaas.conf" ExecStart=/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties - 创建 SCRAM 用户(先创建 broker 间通信与管理员账户)
bin/kafka-configs.sh --zookeeper localhost:2181 \ --alter --add-config 'SCRAM-SHA-256=[password=Admin@123]' \ --entity-type users --entity-name admin bin/kafka-configs.sh --zookeeper localhost:2181 \ --alter --add-config 'SCRAM-SHA-256=[password=Prod@123]' \ --entity-type users --entity-name producer bin/kafka-configs.sh --zookeeper localhost:2181 \ --alter --add-config 'SCRAM-SHA-256=[password=Cons@123]' \ --entity-type users --entity-name consumer - 配置 ACL(示例)
# 主题级别:producer 写,consumer 读,admin 全权 bin/kafka-acls.sh --bootstrap-server localhost:9092 \ --add --allow-principal User:producer --operation Write --operation Describe --topic test-topic bin/kafka-acls.sh --bootstrap-server localhost:9092 \ --add --allow-principal User:consumer --operation Read --operation Describe --topic test-topic bin/kafka-acls.sh --bootstrap-server localhost:9092 \ --add --allow-principal User:admin --operation All --topic test-topic # 消费者组(Group)权限 bin/kafka-acls.sh --bootstrap-server localhost:9092 \ --add --allow-principal User:consumer --operation Read --group test-group # 集群级运维权限(可选) bin/kafka-acls.sh --bootstrap-server localhost:9092 \ --add --allow-principal User:admin --operation ClusterAction --cluster - 客户端测试(控制台)
# 生产 bin/kafka-console-producer.sh --bootstrap-server localhost:9092 --topic test-topic \ --producer-property security.protocol=SASL_PLAINTEXT \ --producer-property sasl.mechanism=SCRAM-SHA-256 \ --producer-property sasl.jaas.config='org.apache.kafka.common.security.scram.ScramLoginModule required username="producer" password="Prod@123"; ' # 消费 bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test-topic \ --from-beginning --consumer-property security.protocol=SASL_PLAINTEXT \ --consumer-property sasl.mechanism=SCRAM-SHA-256 \ --consumer-property sasl.jaas.config='org.apache.kafka.common.security.scram.ScramLoginModule required username="consumer" password="Cons@123"; ' \ --group test-group
- 创建 JAAS 配置(示例:/opt/kafka/config/kafka_server_jaas.conf)
三 进阶方案 SASL PLAINTEXT + ACL(简单但不便动态增删用户)
- 适用场景:用户规模小、变更不频繁。
- 要点
- JAAS 使用 PlainLoginModule 在 KafkaServer 中预置用户(启动后不可动态新增)。
- server.properties 启用 SASL/PLAINTEXT 与 ACL(与 SCRAM 类似,仅更换机制与 JAAS 模块)。
- 示例 JAAS(片段):
KafkaServer { org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="Admin@123" user_admin="Admin@123" user_producer="Prod@123" user_consumer="Cons@123"; } ; - 客户端与 ACL 命令与 SCRAM 相同,仅将机制改为 SASL/PLAIN。
- 提示:SASL/PLAIN 的优点是配置简单,缺点是用户凭据固化在 JAAS,新增用户需重启或滚动升级,运维成本较高。
四 高安全方案 SASL + SSL + ACL(生产推荐)
- 适用场景:跨机房、公网访问、合规要求高。
- 要点
- 启用 SSL/TLS 对客户端与 Broker、Broker 与 Broker 链路加密(listeners 配置为 SASL_SSL://)。
- 证书与密钥管理(CA、server/client 证书),并在 server.properties 中配置相关 SSL 参数(如 ssl.keystore.location、ssl.truststore.location、security.inter.broker.protocol=SASL_SSL 等)。
- 认证与授权仍使用 SASL(SCRAM 或 Kerberos)+ ACL,实现身份鉴别与细粒度授权;SSL 仅负责加密传输通道。
- 说明:SSL 会增加 CPU 开销并影响零拷贝等特性,但在公网/跨域访问时收益显著;建议与 ACL 组合使用。
五 常见问题与运维要点
- 认证失败排查
- 核对 JAAS 文件路径与权限、用户名/密码、sasl.enabled.mechanisms 与 listener 配置是否一致;客户端需显式设置 security.protocol、sasl.mechanism、sasl.jaas.config。
- Zookeeper 安全
- 若 ZK 也启用 SASL,需在 zoo.cfg 配置 SASL 认证插件与 JAAS,并将 Kafka 相关依赖 JAR 放入 ZK 的 classpath;否则 Broker 无法与 ZK 建立安全会话。
- 防火墙与网络
- 仅开放必要端口(如 9092/9093),对管理网与业务网进行 白名单 控制(firewalld/安全组)。
- 版本与兼容性
- SCRAM 自 0.10.2.0 引入;确保客户端与服务端版本兼容,Java 客户端建议使用 Java 11+(如采用新版 Kafka 3.x)。
- 密码与凭据管理
- 定期轮换凭据(建议 90 天),避免明文落地,使用凭据管理工具或 KMS;生产环境建议 SASL + SSL + ACL 的组合。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: centos kafka的权限设置
本文地址: https://pptw.com/jishu/787843.html
