在Ubuntu上配置Apache Kafka的認證與授權是一個復雜的過程,涉及到多個步驟和組件。以下是一個基本的指南,幫助你在Ubuntu上配置Kafka的認證與授權。
安裝Java:Kafka需要Java運行環境。
sudo apt update
sudo apt install openjdk-11-jdk
下載并解壓Kafka:
wget https://downloads.apache.org/kafka/3.2.0/kafka_2.13-3.2.0.tgz
tar -xzf kafka_2.13-3.2.0.tgz
cd kafka_2.13-3.2.0
編輯config/server.properties
文件,啟用SASL認證和SSL加密。
# 啟用SASL認證
listeners=SASL_PLAINTEXT://:9092
security.inter.broker.protocol=SASL_PLAINTEXT
sasl.mechanism.inter.broker.protocol=PLAIN
sasl.enabled.mechanisms=PLAIN
# 啟用SSL加密
listeners=SSL://:9093
ssl.keystore.location=/path/to/keystore.jks
ssl.keystore.password=keystore-password
ssl.key.password=key-password
ssl.truststore.location=/path/to/truststore.jks
ssl.truststore.password=truststore-password
# 啟用認證和授權
authorizer.class.name=kafka.security.auth.SimpleAclAuthorizer
allow.everyone.if.no.acl.found=false
super.users=User:admin
使用OpenSSL生成自簽名證書和密鑰。
# 生成密鑰庫
keytool -genkey -alias kafka -keystore keystore.jks -storepass keystore-password -validity 3650
# 生成信任庫
keytool -import -alias kafka -file kafka.crt -keystore truststore.jks -storepass truststore-password
編輯config/producer.properties
文件,啟用SASL認證和SSL加密。
bootstrap.servers=localhost:9093
security.protocol=SSL
ssl.truststore.location=/path/to/truststore.jks
ssl.truststore.password=truststore-password
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="admin-secret";
編輯config/consumer.properties
文件,啟用SASL認證和SSL加密。
bootstrap.servers=localhost:9093
security.protocol=SSL
ssl.truststore.location=/path/to/truststore.jks
ssl.truststore.password=truststore-password
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="admin-secret";
bin/kafka-server-start.sh config/server.properties
創建一個主題并配置ACL以進行授權。
bin/kafka-topics.sh --create --topic test-topic --bootstrap-server localhost:9093 --partitions 1 --replication-factor 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:9093 --topic test-topic --property security.protocol=SSL --property ssl.truststore.location=/path/to/truststore.jks --property ssl.truststore.password=truststore-password --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:9093 --topic test-topic --from-beginning --property security.protocol=SSL --property ssl.truststore.location=/path/to/truststore.jks --property ssl.truststore.password=truststore-password --property sasl.mechanism=PLAIN --property sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required username="admin" password="admin-secret"
通過以上步驟,你應該能夠在Ubuntu上成功配置Kafka的認證與授權。請根據實際需求調整配置文件中的路徑和密碼。