返回
手把手教你给 Kafka 设置 SASL 认证(二)
后端
2023-11-16 15:25:15
需求:
确保您已经熟悉以下内容:
- Kafka入门教程
- 什么是SASL
- 什么是 Kerberos
指引:
-
创建 Kerberos 用户
kadmin -r krb5admin/admin addprinc -pw hadoop admin/kafka.kafka
-
配置 Kafka 服务端安全选项
在 Kafka 服务端配置文件中,添加以下配置:
# Enable SASL authentication security.inter.broker.protocol=SASL_PLAINTEXT # Specify the list of authorized users sasl.authorized.users=admin/kafka.kafka
-
配置 Kafka 客户端安全选项
在 Kafka 客户端配置文件中,添加以下配置:
# Enable SASL authentication security.protocol=SASL_PLAINTEXT # Specify the Kerberos principal sasl.kerberos.service.name=kafka # Specify the location of the Kerberos keytab file sasl.kerberos.keytab=/etc/krb5.keytab
-
重启 Kafka 集群
重新启动 Kafka 服务端和客户端。
-
测试 SASL 认证
使用 Kafka 命令行工具测试 SASL 认证是否成功:
kafka-topics --list --bootstrap-server localhost:9092
如果认证成功,您将看到 Kafka 集群中的所有主题列表。
-
添加 SSL 加密
为了进一步提高安全性,您还可以添加 SSL 加密:
# Enable SSL encryption on the server side listeners=SASL_PLAINTEXT://:9092,SSL://:9093 # Specify the location of the SSL certificate and key ssl.keystore.location=/etc/kafka/kafka.keystore.jks ssl.keystore.password=secret # Enable SSL encryption on the client side security.protocol=SSL # Specify the location of the SSL trust store ssl.truststore.location=/etc/kafka/kafka.truststore.jks ssl.truststore.password=secret
-
添加 ACL 控制
为了控制对 Kafka 集群的访问,您还可以添加 ACL 控制:
# Create a role for the admin user kafka-acls --authorizer-properties zookeeper.connect=localhost:2181 --add --role admin --principal User:admin # Grant the admin role full access to the Kafka cluster kafka-acls --authorizer-properties zookeeper.connect=localhost:2181 --add --allow-principal User:admin --operation All --resource-type Group --resource-name * # Grant the admin role permission to create topics kafka-acls --authorizer-properties zookeeper.connect=localhost:2181 --add --allow-principal User:admin --operation Create --resource-type Topic --resource-name * # Grant the admin role permission to delete topics kafka-acls --authorizer-properties zookeeper.connect=localhost:2181 --add --allow-principal User:admin --operation Delete --resource-type Topic --resource-name *
总结:
通过以上步骤,您可以在 Kafka 集群中添加 SASL 认证、SSL 加密和 ACL 控制,从而提高 Kafka 集群的安全性。