【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
相关推荐
Dontla44 分钟前
Docker run --add-host参数解析(在容器启动时向/etc/hosts文件中添加自定义的主机名与IP映射)(适用于临时调试或测试)
tcp/ip·docker·容器
西西小飞龙1 小时前
Docker和K8S中pod、services、container的介绍和关系
docker·容器·kubernetes
元气满满的热码式1 小时前
Docker数据卷操作实战
linux·运维·docker·云原生·容器
后季暖4 小时前
kafka stream对比flink
分布式·flink·kafka
飞火流星0202712 小时前
docker安装etcd:docker离线安装etcd、docker在线安装etcd、etcd镜像下载、etcd配置详解、etcd常用命令、安装常见问题总结
docker·容器·etcd
信徒_14 小时前
kafka consumer 手动 ack
分布式·kafka·linq
you来有去14 小时前
记录一下在k3s快速创建gitlab
java·kubernetes·gitlab
magrich14 小时前
Docker usage on ubuntu
ubuntu·docker·容器
格桑阿sir15 小时前
Kubernetes控制平面组件:API Server Node 授权机制 详解
kubernetes·kubelet·authorization·apiserver·node授权机制·noderestriction·资源拦截