温馨提示×

ubuntu kafka如何鉴权

小樊
42
2026-10-20 03:49:30
栏目: 智能运维

在 Ubuntu 上部署 Kafka 时,鉴权(Authentication)通常配合 授权(Authorization) 一起做。Kafka 常见的鉴权方式有:

  1. SASL/PLAIN(用户名密码,最常用)
  2. SASL/SCRAM
  3. SASL/GSSAPI(Kerberos)
  4. SSL 双向认证(mTLS)

下面以 Ubuntu + Kafka 3.x + SASL/PLAIN + ACL 为例,给你一套可落地的配置步骤。


一、环境准备(Ubuntu)

# 安装 Java
sudo apt update
sudo apt install openjdk-11-jdk -y

# 下载 Kafka
wget https://archive.apache.org/dist/kafka/3.6.1/kafka_2.13-3.6.1.tgz
tar -xzf kafka_2.13-3.6.1.tgz
cd kafka_2.13-3.6.1

二、配置 SASL/PLAIN 鉴权

1️⃣ 创建 JAAS 配置文件

sudo mkdir -p /opt/kafka/config/security
sudo vim /opt/kafka/config/security/kafka_server_jaas.conf

内容如下:

KafkaServer {
    org.apache.kafka.common.security.plain.PlainLoginModule required
    username="admin"
    password="admin-secret"
    user_admin="admin-secret"
    user_producer="producer-secret"
    user_consumer="consumer-secret";
};

2️⃣ 修改 server.properties

编辑:

vim config/server.properties

关键配置:

listeners=SASL_PLAINTEXT://:9092
advertised.listeners=SASL_PLAINTEXT://localhost:9092

security.inter.broker.protocol=SASL_PLAINTEXT
sasl.mechanism.inter.broker.protocol=PLAIN
sasl.enabled.mechanisms=PLAIN

# 开启 ACL
authorizer.class.name=kafka.security.authorizer.AclAuthorizer
allow.everyone.if.no.acl.found=false
super.users=User:admin

3️⃣ 启动 Kafka(带 JAAS)

export KAFKA_OPTS="-Djava.security.auth.login.config=/opt/kafka/config/security/kafka_server_jaas.conf"

bin/kafka-server-start.sh config/server.properties

三、客户端鉴权配置

1️⃣ Producer 配置

producer.properties

security.protocol=SASL_PLAINTEXT
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required \
  username="producer" \
  password="producer-secret";

2️⃣ Consumer 配置

consumer.properties

security.protocol=SASL_PLAINTEXT
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.plain.PlainLoginModule required \
  username="consumer" \
  password="consumer-secret";

四、配置 ACL(授权)

# producer 写权限
bin/kafka-acls.sh --bootstrap-server localhost:9092 \
  --command-config admin.properties \
  --add --allow-principal User:producer \
  --operation Write --topic test

# consumer 读权限
bin/kafka-acls.sh --bootstrap-server localhost:9092 \
  --command-config admin.properties \
  --add --allow-principal User:consumer \
  --operation Read --topic test

# consumer group
bin/kafka-acls.sh --bootstrap-server localhost:9092 \
  --command-config admin.properties \
  --add --allow-principal User:consumer \
  --operation Read --group test-group

五、生产 / 消费测试

bin/kafka-console-producer.sh \
  --bootstrap-server localhost:9092 \
  --topic test \
  --producer.config producer.properties

bin/kafka-console-consumer.sh \
  --bootstrap-server localhost:9092 \
  --topic test \
  --group test-group \
  --consumer.config consumer.properties

六、生产环境建议

✅ 不要用 PLAINTEXT(明文)
→ 用 SASL_SSL

✅ 优先 SCRAM + SSL

✅ Kafka + Kerberos(企业级)

✅ 使用 KRaft 模式(Kafka 3.x 新架构)


七、常见问题

❌ 鉴权失败

SASL authentication failed

✅ 检查 JAAS 用户名密码是否一致

❌ 无权限

Not authorized to access topics

✅ ACL 没配或 user 写错


如果你愿意,我可以:

  • 给你 SCRAM + SSL 完整配置
  • 给你 Docker / K8s 中 Kafka 鉴权方案
  • 给你 Kerberos 企业级方案

你现在是 测试环境还是生产环境?

0 踩