在Linux上配置Kafka的认证机制,通常涉及以下几个步骤:
Kafka依赖于Zookeeper进行集群管理。确保Zookeeper已经安装并运行。
# 下载并解压Zookeeper
wget https://downloads.apache.org/zookeeper/zookeeper-3.7.0/apache-zookeeper-3.7.0-bin.tar.gz
tar -xzf apache-zookeeper-3.7.0-bin.tar.gz
cd apache-zookeeper-3.7.0-bin
# 配置zoo.cfg
cp conf/zoo_sample.cfg conf/zoo.cfg
编辑conf/zoo.cfg文件,添加以下内容:
dataDir=/var/lib/zookeeper
clientPort=2181
创建数据目录并启动Zookeeper:
mkdir -p /var/lib/zookeeper
bin/zkServer.sh start
下载并解压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
Kafka支持多种认证机制,包括SSL/TLS、SASL/PLAIN、SASL/SCRAM-SHA-256等。这里以SASL/PLAIN为例。
编辑config/server.properties文件,添加以下内容:
# 启用SASL_PLAINTEXT监听器
listeners=SASL_PLAINTEXT://:9092
# SASL配置
security.inter.broker.protocol=SASL_PLAINTEXT
sasl.mechanism.inter.broker.protocol=PLAIN
sasl.enabled.mechanisms=PLAIN
# JAAS配置文件路径
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required \
username="admin" \
password="admin-secret";
# 启用Kafka ACL
authorizer.class.name=kafka.security.authorizer.AclAuthorizer
allow.everyone.if.no.acl.found=false
super.users=User:admin
创建一个JAAS配置文件,例如kafka_server_jaas.conf:
KafkaServer {
org.apache.kafka.common.security.plain.PlainLoginModule required
username="admin"
password="admin-secret";
};
编辑config/producer.properties文件,添加以下内容:
bootstrap.servers=localhost:9092
security.protocol=SASL_PLAINTEXT
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required \
username="admin" \
password="admin-secret";
编辑config/consumer.properties文件,添加以下内容:
bootstrap.servers=localhost:9092
security.protocol=SASL_PLAINTEXT
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
bin/kafka-topics.sh --create --topic test-topic --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1
bin/kafka-console-producer.sh --topic test-topic --bootstrap-server localhost:9092 --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 --topic test-topic --from-beginning --bootstrap-server localhost:9092 --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';"
通过以上步骤,你可以在Linux上配置Kafka的SASL/PLAIN认证机制。根据需要,你可以选择其他认证机制并进行相应的配置。