在Debian上配置Apache Kafka的认证和授权涉及几个步骤。以下是一个基本的指南,帮助你在Debian系统上配置Kafka的认证和授权。
Kafka需要Java运行时环境。你可以使用以下命令安装OpenJDK:
sudo apt update
sudo apt install openjdk-11-jdk
从Apache Kafka官方网站下载最新版本的Kafka,并解压到你的目录中。
wget https://downloads.apache.org/kafka/3.0.0/kafka_2.13-3.0.0.tgz
tar -xzf kafka_2.13-3.0.0.tgz
cd kafka_2.13-3.0.0
编辑config/server.properties
文件以启用认证和授权。
sudo nano /etc/kafka/kafka_server_jaas.conf
添加以下内容:
KafkaServer {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="admin"
password="admin-secret"
user_admin="admin-secret";
};
server.properties
文件:sudo nano config/server.properties
添加或修改以下配置:
listeners=SASL_PLAINTEXT://:9092
security.inter.broker.protocol=SASL_PLAINTEXT
sasl.mechanism.inter.broker.protocol=PLAIN
sasl.enabled.mechanisms=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="admin-secret" user_admin="admin-secret";
authorizer.class.name=kafka.security.authorizer.AclAuthorizer
allow.everyone.if.no.acl.found=true
super.users=User:admin
编辑config/client.properties
文件以启用认证。
sudo nano config/client.properties
添加或修改以下配置:
security.protocol=SASL_PLAINTEXT
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="admin-secret";
启动Zookeeper和Kafka服务器:
# 启动Zookeeper
bin/zookeeper-server-start.sh config/zookeeper.properties &
# 启动Kafka服务器
bin/kafka-server-start.sh config/server.properties &
创建一个主题并配置ACL以进行授权。
# 创建主题
bin/kafka-topics.sh --create --topic test-topic --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1
# 配置ACL
bin/kafka-acls.sh --authorizer-properties zookeeper.connect=localhost:2181 \
--add --allow-principal User:admin --operation Read --topic test-topic
使用Kafka客户端连接到服务器并测试认证和授权。
# 创建一个生产者
bin/kafka-console-producer.sh --broker-list localhost:9092 --topic test-topic --property security.protocol=SASL_PLAINTEXT --property sasl.mechanism=PLAIN --property sasl.jaas.config="org.apache.kafka.common.security.plain.PlainLoginModule required username='admin' password='admin-secret';"
# 创建一个消费者
bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test-topic --from-beginning --property security.protocol=SASL_PLAINTEXT --property sasl.mechanism=PLAIN --property sasl.jaas.config="org.apache.kafka.common.security.plain.PlainLoginModule required username='admin' password='admin-secret';"
通过以上步骤,你应该能够在Debian上成功配置Kafka的认证和授权。根据你的具体需求,你可能需要进一步调整配置。