Search code examples
kubernetesapache-kafkakubectlstrimzi

Is there a kubectl command to create kafka topic without using import yaml file option?


I have deployed strimzi kafka on kubernetes and I have kube setup in my local as well. But each time I want a new topic in kafka, I need to create one by importing the yaml file via rancher and provide the topic name.

Is there a way to create a kafka topic directly via kubectl command?

These are the commands I use to run kafka:

Producer: kubectl run kafka-producer1 -ti --image=strimzi/kafka:0.18.0-kafka-2.4.0 --rm=true --restart=Never -- bin/kafka-console-producer.sh --broker-list 11.23.41.32:31025 --topic topic-name

Consumer: kubectl run kafka-consumer1 -ti --image=strimzi/kafka:0.18.0-kafka-2.4.0 --restart=Never -- bin/kafka-console-consumer.sh --bootstrap-server 11.23.41.32:31025 --topic topic-name --from-beginning


Solution

  • Steps to create topic via commandline : (assumed zookeeper running on port 2181 and kafka server on 9092)

    1. Get inside the kafka pod by using this command

    kubectl exec -it kafka-pod-name -- /bin/bash

    1. Create the topic by using below command

    kafka-topics --bootstrap-server localhost:9092 --create --topic topic-name --replication-factor 1 --partitions 3

    1. you can verify the message produce and consume using below commands-

    a) produce-->

    1. kafka-console-producer --broker-list localhost:9092 --topic <topic-you-created-before>
    2. provide some message

    b) consume--> kafka-console-consumer --bootstrap-server localhost:9092 --topic audit --from-beginning

    you can see the message(provided by producer) here