【kafka】docker容器bitnami/kafka使用SASL鉴权(无TLS)

背景

最近在学习kafka消息队列,了解到kafka是通过SASL来进行用户认证的。起初,因为btinami/kafka官方的一段内容让我以为SASL和TLS是绑定使用的,导致心思花在解决TLS配置上去,官方原文如下:

You must also use your own certificates for SSL. You can drop your Java Key Stores or PEM files into /opt/bitnami/kafka/config/certs. If the JKS or PEM certs are password protected (recommended), you will need to provide it to get access to the keystores:

后来发现其实是可以单独使用SASL,而且官方文档没有完整的配置资料,最后在Issue上找到解决方案。

过程

1.编写一个包含SASL配置的docker-compose.yml文件

yml 复制代码
version: "2"
services:
  kafka:
    image: 'bitnami/kafka:latest'
    hostname: localhost
    ports:
      - '9092:9092'
    environment:
      - ALLOW_PLAINTEXT_LISTENER=yes
      - KAFKA_CFG_NODE_ID=0
      - KAFKA_CFG_PROCESS_ROLES=controller,broker
      - KAFKA_CLIENT_LISTENER_NAME=CLIENT
      - KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
      - KAFKA_CFG_LISTENERS=CLIENT://:9092,CONTROLLER://:9093
      - KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,CLIENT:SASL_PLAINTEXT
      - KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=0@kafka:9093
      - KAFKA_CFG_SASL_ENABLED_MECHANISMS=SCRAM-SHA-512
      - KAFKA_CLIENT_USERS=test
      - KAFKA_CLIENT_PASSWORDS=123456

将容器跑起来看日志,发现报错:

复制代码
Exception in thread "main" java.lang.IllegalArgumentException: requirement failed: inter.broker.listener.name must be a listener name defined in advertised.listeners. The valid options based on currently configured listeners are CLIENT

需要配置inter.broker.listener.name 参数,去官网看一下介绍。

以下是inter.broker.listener.name 参数的官方描述:

Name of listener used for communication between brokers. If this is unset, the listener name is defined by security.inter.broker.protocol. It is an error to set this and security.inter.broker.protocol properties at the same time.

以下是security.inter.broker.protocol参数的官方描述:

Security protocol used to communicate between brokers. Valid values are: PLAINTEXT, SSL, SASL_PLAINTEXT, SASL_SSL. It is an error to set this and inter.broker.listener.name properties at the same time.

以下是对两个参数关系的一些官方描述:

Inter-broker listener must be configured using the static broker configuration inter.broker.listener.name or security.inter.broker.protocol.
Among the listeners in this list, it is possible to declare the listener to be used for inter-broker communication by setting the inter.broker.listener.name configuration to the name of the listener. The primary purpose of the inter-broker listener is partition replication. If not defined, then the inter-broker listener is determined by the security protocol defined by security.inter.broker.protocol, which defaults to PLAINTEXT.

由上述可知,inter.broker.listener.namesecurity.inter.broker.protocol 参数的作用是一致的,都是为了设置broker之间通信的配置,由于security.inter.broker.protocol设置之后依然存在问题,本文暂不讨论,后续就以inter.broker.listener.name 参数进行处理。

2.设置inter.broker.listener.name

inter.broker.listener.name 可以设置为一个新的listener,也可以设置为现有的listener(controller除外),本文就以设置现有的listener为例,将改值设置为CLIENT:

yml 复制代码
version: "2"
services:
  kafka:
    image: 'bitnami/kafka:latest'
    hostname: localhost
    ports:
      - '9092:9092'
    environment:
      - KAFKA_CFG_INTER_BROKER_LISTENER_NAME=CLIENT
      # ... 和上面的配置一样

此时把容器跑起来,会得到以下错误:

复制代码
ERROR ==> When using SASL for inter broker comunication the mechanism should be provided using KAFKA_CFG_SASL_MECHANISM_INTER_BROKER_PROTOCOL

只需要设置KAFKA_CFG_SASL_MECHANISM_INTER_BROKER_PROTOCOL,可选值为PLAIN, SCRAM-SHA-256, SCRAM-SHA-512 。在官网介绍中,其实应该还有GSSAPI默认值的,但是这个值在容器使用中会出现错误,而且在bitnami/kafka文档中也没有写支持改值,因此不作讨论。

3.设置KAFKA_CFG_SASL_MECHANISM_INTER_BROKER_PROTOCOL

KAFKA_CFG_SASL_MECHANISM_INTER_BROKER_PROTOCOL设置为PLAINSCRAM-SHA-256SCRAM-SHA-512

yml 复制代码
version: "2"
services:
  kafka:
    image: 'bitnami/kafka:latest'
    hostname: localhost
    ports:
      - '9092:9092'
    environment:
      - KAFKA_CFG_SASL_MECHANISM_INTER_BROKER_PROTOCOL=SCRAM-SHA-512
      # ... 和上面的配置一样

这里需要注意是,KAFKA_CFG_SASL_MECHANISM_INTER_BROKER_PROTOCOL 的值需要在KAFKA_CFG_SASL_ENABLED_MECHANISMS 范围内,否则你将看到以下错误:

复制代码
Exception in thread "main" java.lang.IllegalArgumentException: requirement failed: sasl.mechanism.inter.broker.protocol must be included in sasl.enabled.mechanisms when SASL is used for inter-broker communication

KAFKA_CFG_SASL_ENABLED_MECHANISMS 支持多个值,以 , 隔开即可,如:
KAFKA_CFG_SASL_ENABLED_MECHANISMS=PLAIN,SCRAM-SHA-512

至此,一个简单的SASL的docker-compose.yml配置就完成了

完整配置文件

yml 复制代码
version: "2"
services:
  kafka:
    image: 'bitnami/kafka:latest'
    hostname: localhost
    ports:
      - '9092:9092'
    environment:
      - ALLOW_PLAINTEXT_LISTENER=yes
      - KAFKA_CFG_NODE_ID=0
      - KAFKA_CFG_PROCESS_ROLES=controller,broker
      - KAFKA_CLIENT_LISTENER_NAME=CLIENT
      - KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
      - KAFKA_CFG_INTER_BROKER_LISTENER_NAME=CLIENT
      - KAFKA_CFG_LISTENERS=CLIENT://:9092,CONTROLLER://:9093
      - KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,CLIENT:SASL_PLAINTEXT
      - KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=0@kafka:9093
      - KAFKA_CFG_SASL_MECHANISM_INTER_BROKER_PROTOCOL=SCRAM-SHA-512
      - KAFKA_CFG_SASL_ENABLED_MECHANISMS=SCRAM-SHA-512
      - KAFKA_CLIENT_USERS=test
      - KAFKA_CLIENT_PASSWORDS=123456

参考文章

相关推荐
db_murphy29 分钟前
知识篇 | 中间件会话保持和会话共享有啥区别?
中间件
m0_748254092 小时前
2025最新华为云国际版注册图文流程-不用绑定海外信用卡注册
服务器·数据库·华为云
MUY09902 小时前
应用控制技术、内容审计技术、AAA服务器技术
运维·服务器
Sadsvit3 小时前
源码编译安装LAMP架构并部署WordPress(CentOS 7)
linux·运维·服务器·架构·centos
苦学编程的谢3 小时前
Linux
linux·运维·服务器
Gss7775 小时前
源代码编译安装lamp
linux·运维·服务器
敲上瘾5 小时前
Linux I/O 多路复用实战:Select/Poll 编程指南
linux·服务器·c语言·c++·select·tcp·poll
huangyuchi.5 小时前
【Linux系统】匿名管道以及进程池的简单实现
linux·运维·服务器·c++·管道·匿名管道·进程池简单实现
元清加油6 小时前
【Goland】:协程和通道
服务器·开发语言·后端·网络协议·golang
拷贝码农卡卡东6 小时前
宿主机与容器通过 rmw_cyclonedds_cpp中间件进行ros2结点之间的通讯的相关注意事项
中间件