【kubernetes-kafka】使用kafka client测试

背景

现在假设k8s中已经启动了kafka容器

kafka所在Namespace为xxx

1、启动一个kafka client的pod

kubectl run kafka-client --restart='Never' --image bitnami/kafka:3.1.0-debian-10-r85 --namespace xxx --command -- sleep infinity

2、配置

运行脚本生成client.properties

复制代码
# 1.从 secret 中获取kafka 密码
password="$(kubectl get secret kafka-jaas --namespace xxx -o jsonpath='{.data.client-passwords}' | base64 --decode | cut -d , -f 1)"
# 2.创建配置文件: client.properties
echo "security.protocol=SASL_PLAINTEXT
sasl.mechanism=PLAIN
sasl.jaas.config=org.apache.kafka.common.security.scram.ScramLoginModule required username='qtUser' password='$password';" > client.properties

将配置文件copy到client容器

复制代码
kubectl cp --namespace xxx client.properties kafka-client:/tmp/client.properties
3、进入client
复制代码
kubectl exec --tty -i kafka-client --namespace xxx -- bash
4、测试

kafka的client可以直接发送消息和接受消息

发送消息

复制代码
kafka-console-producer.sh \
            --producer.config /tmp/client.properties \
            --broker-list kafka-0.kafka-headless.xxx.svc.cluster.local:9092 \
            --topic test

接受消息

  • 可以用来测试项目中已有的消息,因为kafka的消息是可以多次消费的,这里指定了从头开始。

    kafka-console-consumer.sh
    --consumer.config /tmp/client.properties
    --bootstrap-server kafka-headless.xxx:9092
    --topic test
    --from-beginning

列出所有topic

复制代码
kafka-topics.sh --list --bootstrap-server kafka-headless.xxx:9092  --command -config /tmp/client.properties
相关推荐
东风微鸣1 分钟前
提升效率:AI 知识库在软件开发中的应用
docker·云原生·kubernetes·可观察性
Lyndon11072 分钟前
K8S 八 数据存储-高级存储PV PVC 生命周期;配置存储ConfigMap Secret
云原生·容器·kubernetes
刘一说1 小时前
梳理一些 Docker 常用命令
docker·容器·eureka
虚伪的空想家1 小时前
记录es收集日志报错问题as the final mapping would have more than 1 type[XXX,doc]
大数据·elasticsearch·搜索引擎·容器·kubernetes·log-pilot
Shier833_Ww2 小时前
Windows10+WSL2+Docker相关整理
docker·容器·ubuntu20.04·windows10·wsl2
勇哥的编程江湖11 小时前
starrocks官网docker部署mysql无法连接
运维·docker·容器
itLaity13 小时前
基于Kafka实现简单的延时队列
spring boot·分布式·kafka
终端行者19 小时前
k8s之Ingress服务接入控制器
云原生·容器·kubernetes
longxibo1 天前
飞牛系统安装DataEase自定义Docker包
运维·docker·容器
IT邦德1 天前
OGG同步Oracle到Kafka不停库,全量加增量
数据库·oracle·kafka