AWS MSK的连接

kafka客户端需要Java依赖,所以先安装Java11,

bash 复制代码
sudo yum install java-11

https://docs.aws.amazon.com/zh_cn/msk/latest/developerguide/port-info.html

匿名连接

bash 复制代码
# 匿名使用9092端口
BootstrapServerString=b-2.xxxx.kafka.cn-north-1.amazonaws.com.cn:9092,b-1.xxxx.c4.kafka.cn-north-1.amazonaws.com.cn:9092

# 创建topic
./kafka-topics.sh --create --bootstrap-server $BootstrapServerString --replication-factor 2 --partitions 1 --topic MSKTutorialTopic

# 生产者
./kafka-console-producer.sh --broker-list $BootstrapServerString --topic MSKTutorialTopic

# 消费者
./kafka-console-consumer.sh --bootstrap-server $BootstrapServerString --topic MSKTutorialTopic --from-beginning

TLS

匿名TLS传输和mTLS认证都使用9094端口。

匿名TLS和匿名纯文本只能二选一

client_tls.properties

复制代码
security.protocol=SSL

./kafka-console-producer.sh --broker-list $BootstrapServerString --topic MSKTutorialTopic --producer.config  client_tls.properties

./kafka-console-consumer.sh --bootstrap-server $BootstrapServerString --topic MSKTutorialTopic --from-beginning --from-beginning --consumer.config client_tls.properties

中国区目前不支持TLS双向认证。

IAM 连接

IAM认证是Amazon自己加的功能,所以需要下载额外的包,https://github.com/aws/aws-msk-iam-auth

复制代码
cp aws-msk-iam-auth-1.1.1-all.jar kafka_2.13-2.8.1/libs/

IAM 创建topic命令:

复制代码
BootstrapServerString=b-1.xxxx.c4.kafka.cn-north-1.amazonaws.com.cn:9098,b-2.xxxx.c4.kafka.cn-north-1.amazonaws.com.cn:9098
./kafka-topics.sh --create --bootstrap-server $BootstrapServerString --command-config client.properties --replication-factor 2 --partitions 1 --topic MSKTutorialTopic

client.properties

复制代码
security.protocol=SASL_SSL 
sasl.mechanism=AWS_MSK_IAM 
sasl.jaas.config=software.amazon.msk.auth.iam.IAMLoginModule required; 
sasl.client.callback.handler.class=software.amazon.msk.auth.iam.IAMClientCallbackHandler

要使用您为 AWS 凭证创建的命名配置文件,请将 awsProfileName="`your profile name`"

https://docs.aws.amazon.com/zh_cn/msk/latest/developerguide/iam-access-control.html

SASL

这个需要关联Secrets Manager,而且Secrets Manager必须以AmazonMSK_开头,多租户的话需要关联多个Secrets Manager,如果在Secrets Manage中修改用户密码,那么需要重新绑定,否则新旧密码都能使用,这显然不符合预期。

创建users_jaas.conf文件,这个是用户名密码:

复制代码
KafkaClient {
   org.apache.kafka.common.security.scram.ScramLoginModule required
   username="alice"
   password="alice-secret";
};

然后导出到环境变量:

复制代码
export KAFKA_OPTS=-Djava.security.auth.login.config=$PWD/users_jaas.conf

配置信息:

复制代码
security.protocol=SASL_SSL
sasl.mechanism=SCRAM-SHA-512

连接:

复制代码
BootstrapBrokerStringSaslScram=b-1.xxxx.c4.kafka.cn-north-1.amazonaws.com.cn:9096,b-2.xxxx.kafka.cn-north-1.amazonaws.com.cn:9096

[ec2-user@ip-10-0-10-91 bin]$ ./kafka-console-producer.sh --broker-list $BootstrapServerString --topic MSKTutorialTopic --producer.config client_sasl.properties

./kafka-console-consumer.sh --bootstrap-server $BootstrapBrokerStringSaslScram --topic MSKTutorialTopic --from-beginning --consumer.config client_sasl.properties

https://repost.aws/zh-Hans/knowledge-ceneccccvfubhkhugdueicbcbvubnlrgefkkldbeurkjfenter/msk-sasl-scram-issues

公有访问

  1. 集群必须是公有子网
  2. 关闭匿名访问
  3. 必须开启集群间加密,关闭纯文本连接
  4. 如果是SASL/SCRAM 或者 mTLS,那么需要配置Apache Kafka ACL(allow.everyone.if.no.acl.found=false),这时候用其他认证去连接。

kafka ACL:https://docs.aws.amazon.com/zh_cn/msk/latest/developerguide/msk-acls.html

https://cwiki.apache.org/confluence/display/KAFKA/Kafka+Authorization+Command+Line+Interface

多VPC连接

待测试

相关推荐
SaaS_Product4 天前
从实用性与体验角度出发,OneDrive有什么替代品
云计算·saas·onedrive
小扎仙森4 天前
关于阿里云实时语音翻译-Gummy推送WebSocket
websocket·阿里云·云计算
大尚来也5 天前
从调度到实时:Linux 下 DolphinScheduler 驱动 Flink 消费 Kafka 的实战指南
c#·linq
Elastic 中国社区官方博客5 天前
Elastic 公共 roadmap 在此
大数据·elasticsearch·ai·云原生·serverless·全文检索·aws
Shacoray5 天前
OpenClaw 接入阿里云百炼 Coding Plan 指南
阿里云·ai·云计算·qwen3·openclaw·coding plan
TG_yunshuguoji5 天前
阿里云代理商:2026 年阿里云国际站上云接入指南
服务器·阿里云·云计算
阿里云云原生5 天前
阿里云可观测 2026 年 1 月产品动态
阿里云·云计算
TG_yunshuguoji5 天前
亚马逊云代理商:AWS 国际站缺卡新用户创建邮箱怎么选择?
安全·云计算·aws
峰顶听歌的鲸鱼5 天前
Zabbix监控系统
linux·运维·笔记·安全·云计算·zabbix·学习方法
TG_yunshuguoji5 天前
亚马逊云代理商:如何监控AWS RDS使用率并设置报警?
运维·云计算·aws