Kafka Chart on Kubernetes: Simple test string produce + consume - docker

I install the incubator Kafka chart. Version kafka-0.8.5 as of this writing.
helm install --name kafka \
--set replicas=1 \
--set persistence.enabled=false \
--set zookeeper.replicaCount=1 \
incubator/kafka
To try this out I run a separate pod as just a bash shell with Kafka cli tools. I'm using the exact same Docker image confluentinc/cp-kafka:4.1.1-2 that the kafka-0 pod is using so that there is a perfect version match between the client and server:
kubectl run shell --rm -i --tty --image confluentinc/cp-kafka:4.1.1-2 -- /bin/bash
Listing topics, publishing messages, getting topic offsets all works perfectly, as shown below. However, when I try to run kafka-console-consumer and see the test record in the topic, it hangs indefinitely. Why?
root#shell-5c6ddf5d99-tbsvm:/# /usr/bin/kafka-topics --zookeeper kafka-zookeeper:2181 --list
__confluent.support.metrics
root#shell-5c6ddf5d99-tbsvm:/# echo "abcxyz" | /usr/bin/kafka-console-producer --broker-list kafka:9092 --topic test-topic
>[2018-08-07 16:43:26,110] WARN [Producer clientId=console-producer] Error while fetching metadata with correlation id 1 : {test-topic=LEADER_NOT_AVAILABLE} (org.apache.kafka.clients.NetworkClient)
root#shell-5c6ddf5d99-tbsvm:/# /usr/bin/kafka-topics --zookeeper kafka-zookeeper:2181 --list
__confluent.support.metrics
test-topic
root#shell-5c6ddf5d99-tbsvm:/# /usr/bin/kafka-run-class kafka.tools.GetOffsetShell --broker-list kafka:9092 --topic test-topic --time -1
test-topic:0:1
root#shell-5c6ddf5d99-tbsvm:/# /usr/bin/kafka-console-consumer --bootstrap-server kafka:9092 --from-beginning --topic test-topic
<hangs indefinitely>
FYI, this is a local minikube development cluster with the latest minikube with Kubernetes 1.10.x server-side and 1.10.x kubectl client tools. This is a clean, new minikube, with nothing else running besides kafka, kafka-zookeeper, and my shell pod.
Also, writing a small Java client test app to consume gets a similar result of polling indefinitely with no messages. When my Java client subscribes to test-topic, it never gets notification callbacks of being assigned to the one topic partition.

This cost me hours myself, there's a bug in minikube which prevents Kafka from working.
I'm not familiar with the Helm deployment, but you have to make sure of two things. First, the Kafka advertised host has to be the same as your Kubernetes service IP (or DNS name in kube dns), and second, you have to put minikube's network interface in promiscuous mode:
minikube ssh sudo ip link set docker0 promisc on
If you don't do this workaround, kafka can't contant itself via the Kubernetes service, and its leader election fails. I've found it to be very fragile inside a container deployment environment.

Related

Get all Kafka topics from another Docker container?

When running on the host, I can get all the Kafka topics with:
docker exec broker kafka-topics --bootstrap-server broker:29092 --list
I can't run this from within a container because I'd get docker: not found, even if I installed Docker in the container I don't think it'll work anyway. Also, apparently it's hard and insecure to be able to run an arbitrary command in another Docker container. How else can I get all the Kafka topics from within another Docker container? E.g. can I interface with Kafka through http?
I get docker: not found
That seems to imply docker CLI command is not installed, and has nothing to do with Kafka.
docker is not (typically) installed in "another container", so that explains that... You'll need to install Java and download Kafka cli tools to run kafka-topics.sh in any other environment, and then not use docker exec.
Otherwise, your command is "correct", but if you are using Docker Compose, you should do it like this from your host (change port accordingly).
docker-compose exec broker bash -c \
"kafka-topics --list --bootstrap-server localhost:9092"

Run Multiple Terminals For Kafka Insite Docker

I want to run "standalone" Docker container with a configured Kafka server.
I found out on Kafka website (https://kafka.apache.org/quickstart) how to run Kafka topic:)
But when I'm doing everything as instructed I need to run three terminals:
One for run ZooKeeper server:
./bin/zookeeper-server-start.sh config/zookeeper.properties
Second for start kafka server:
./bin/kafka-server-start.sh config/server.properties
Third for create a topic:
./bin/kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test
The question is :
How do I run three independent terminals inside Docker while building the docker image?
Because 1 want to only use commands:
docker build . -t kafka
and then
docker start kafka
and have an up and running Kafka server with a created topic.
I done something but I stack on trying to create this terminals.
Here's the project:
https://github.com/mpawel1993/Kafka-Docker
I want to run "standalone" Docker container with a configured Kafka server
If you want a configured Kafka server, any of the existing Docker images work fine. landoop/fast-data-dev includes both Kafka, Zookeeper, Kafka Connect, and a Schema Registry, if by "standalone" you mean have all necessary components in one image
How do I run three independent terminals inside Docker while building the docker image
You wouldn't. Each RUN command its a single terminal
You also should not start Kafka and Zookeeper in one container for fault tolerance and scalability reasons
You also don't need to create Kafka topics while building the container, only once the container is built and server is running can you create topics

failed to connect to my local kafka broker from a local docker container

I have a question regarding connecting to kafka broker from docker.
I have zookeeper and kafka broker running on my local machine.
I have a docker container running on the same local machine with --network=host
I want to send message from inside the docker container to my local kafka broker.
From inside the docker, I can connect to zookeeper and seek the existing topicsby running:
./bin/kafka-topics.sh --zookeeper localhost:2181 --list
But I cannot connect to the kafka broker with either the kafka-console-consumer.sh or kafka-console-producer.sh,
by running : bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic test
I get:
[2019-10-17 19:12:04,097] WARN [Consumer clientId=consumer-1, groupId=console-consumer-99825] Error connecting to node aictjt:9092 (id: 0 rack: null) (org.apache.kafka.clients.NetworkClient)
java.net.UnknownHostException: aictjt
at java.net.InetAddress.getAllByName0(InetAddress.java:1281)
at java.net.InetAddress.getAllByName(InetAddress.java:1193)
at java.net.InetAddress.getAllByName(InetAddress.java:1127)
at org.apache.kafka.clients.ClientUtils.resolve(ClientUtils.java:104)
at org.apache.kafka.clients.ClusterConnectionStates$NodeConnectionState.currentAddress(ClusterConnectionStates.java:403)
at org.apache.kafka.clients.ClusterConnectionStates$NodeConnectionState.access$200(ClusterConnectionStates.java:363)
at org.apache.kafka.clients.ClusterConnectionStates.currentAddress(ClusterConnectionStates.java:151)
at org.apache.kafka.clients.NetworkClient.initiateConnect(NetworkClient.java:943)
at org.apache.kafka.clients.NetworkClient.access$600(NetworkClient.java:68)
at org.apache.kafka.clients.NetworkClient$DefaultMetadataUpdater.maybeUpdate(NetworkClient.java:1114)
at org.apache.kafka.clients.NetworkClient$DefaultMetadataUpdater.maybeUpdate(NetworkClient.java:1005)
at org.apache.kafka.clients.NetworkClient.poll(NetworkClient.java:537)
at org.apache.kafka.clients.consumer.internals.ConsumerNetworkClient.poll(ConsumerNetworkClient.java:262)
at org.apache.kafka.clients.consumer.internals.ConsumerNetworkClient.poll(ConsumerNetworkClient.java:233)
at org.apache.kafka.clients.consumer.internals.ConsumerNetworkClient.poll(ConsumerNetworkClient.java:224)
at org.apache.kafka.clients.consumer.internals.ConsumerNetworkClient.awaitMetadataUpdate(ConsumerNetworkClient.java:161)
at org.apache.kafka.clients.consumer.internals.AbstractCoordinator.ensureCoordinatorReady(AbstractCoordinator.java:259)
at org.apache.kafka.clients.consumer.internals.ConsumerCoordinator.poll(ConsumerCoordinator.java:326)
at org.apache.kafka.clients.consumer.KafkaConsumer.updateAssignmentMetadataIfNeeded(KafkaConsumer.java:1251)
at org.apache.kafka.clients.consumer.KafkaConsumer.poll(KafkaConsumer.java:1216)
at org.apache.kafka.clients.consumer.KafkaConsumer.poll(KafkaConsumer.java:1201)
at kafka.tools.ConsoleConsumer$ConsumerWrapper.receive(ConsoleConsumer.scala:439)
at kafka.tools.ConsoleConsumer$.process(ConsoleConsumer.scala:105)
at kafka.tools.ConsoleConsumer$.run(ConsoleConsumer.scala:77)
at kafka.tools.ConsoleConsumer$.main(ConsoleConsumer.scala:54)
at kafka.tools.ConsoleConsumer.main(ConsoleConsumer.scala)
Any help?
Thanks
The solution I found was:
In kafka/config/server.properties
set listeners=PLAINTEXT://localhost:9092, note the default listeners=PLAINTEXT://:9092 worked for some cases but for this specific machine I got I have to put localhost there.
re-run the kafka broker with the updated config.
And the connection works from inside my docker container (with --network=host).
The above solutions didn't work for me.
After spending some time i found the issue was because zookeeper and kafka instance was not running on my local. This ans is tested on mac but i think will work on all platform
If you installed your zookeeper and kafka via homebrew then:
First run zookeeper at your local by running command:
zookeeper-server-start /usr/local/etc/kafka/zookeeper.properties
Then run
kafka-server-start /usr/local/etc/kafka/server.properties
After this the commands for kafka will run without the error
java.net.UnknownHostException: aictjt
For example
kafka-topics --list --bootstrap-server localhost:9092
For those who installed without homebrew:
Open terminal with root directory where you installed kafka
./bin/zookeeper-server-start.sh /usr/local/etc/kafka/zookeeper.properties
./bin/kafka-server-start.sh /usr/local/etc/kafka/server.properties
After this the commands for kafka will run without the error
java.net.UnknownHostException: aictjt
For example
./bin/kafka-topics.sh --list --bootstrap-server localhost:9092
The reason for local docker container throwing "java.net.UnknownHostException" is, it is not able to connect to the kafka broker. This is connectivity issue.
It would work if you follow below steps,
In kafka/config/server.properties, there is "listener" line which would be commented by default. i.e. listeners=PLAINTEXT://localhost:9092. uncomment this line.
Later restart both kafka broker and zookeeper.
Note: In non local environment, if you are setting up stream for first time, and facing similar issue to connect to broker and observing UnknownHostException. Try restarting broker and zookeeper.

Cannot connect to Kafka in local Docker container (Windows)

I'm trying to locally start Kafka in a Docker container, but don't seem to get the combination of options right.
I'm running on Windows 10, Docker ce version 2.0.0.3 (31259).
What I'm doing
run Zookeeper in Docker container
docker run -d --name=zookeeper1 --network=host --env-file=zookeeper_options confluentinc/cp-zookeeper
I'll leave out the environment file since zookeeper runs fine.
run Kafka in Docker container
docker run -d --network=host --name=kafka --env-file=kafka_options confluentinc/cp-kafka
with the kafka_options file containing
KAFKA_ZOOKEEPER_CONNECT=zookeeper1:2181
KAFKA_LISTENERS=PLAINTEXT://localhost:9092
KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:29092
Try to get the metadata list
kafkacat -b localhost:29092 -L
(this I do in Windows 10 Ubuntu subsytem, the others I ran from PowerShell. I also have a small Java Kafka application which exhibits the same behavior though)
The result is
% ERROR: Failed to acquire metadata: Local: Broker transport failure
What I've read
Obviously the Quickstart with Docker documentation which uses docker-compose which I don't want; as well as the Docker section of the documentation.
Aside from that, most notably this post by Robin explaining the advertised listeners concept, but I still can't see what I do wrong.
I also found this issue about a difference in Windows preventing you to use the official Quickstart steps in Windows; this lead me try the alternative run with a separate network.
Separate network
Following the steps in the issue:
docker network create confluent
docker run -d --name=zookeeper1 --network=confluent -p 22181:2181 --env-file=zookeeper_options confluentinc/cp-zookeeper
docker run -d --network=confluent --name=kafka -p 29092:9092 --env-file=kafka_options confluentinc/cp-kafka
kafkacat -b localhost:29092 -L
That does change the outcome to
% ERROR: Failed to acquire metadata: Local: Timed out
So it looks like at least it connects, but that doesn't help much in the end.
The question is what am I doing wrong? Is it the Kafka configuration options, or is it a Docker issue I'm not aware of?
EDIT:
It does work with the sample docker-compose.yml here, but shouldn't we be able to start the containers separately?

Unable to list kafka topics in openwhisk setup

Setup details:
I am setting up openwhisk on my local ubuntu(16.04) vm. in this setup kafka is running in one docker and zookeeper in another docker.
I connect to the the kafka docker using cmd
sudo docker exec -it <container id> sh
once connected i execute the following command to get the list of topics
bin/kafka-topics.sh --list --zookeeper localhost:2181
which gives me an exception
Error: Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: 7203; nested exception is:
java.net.BindException: Address already in use
i am unable to understand why is it trying to use 7203 port?
docker ps output
83eba3961247 ches/kafka:0.10.0.1 "/start.sh"
11 days ago Up 23 hours 7203/tcp, 0.0.0.0:9092->9092/tcp
kafka
947fa689a7ef zookeeper:3.4 "/docker-
entrypoin..." 11 days ago Up 23 hours 2888/tcp,
0.0.0.0:2181->2181/tcp, 3888/tcp zookeeper
The Kafka container OpenWhisk is using sets a JMX_PORT by default. That's the 7203 port you're seeing. To get your script to work you need to unset that environment setting:
unset JMX_PORT; bin/kafka-topics.sh --list --zookeeper localhost:2181
Note though, that localhost is not a valid address for your zookeeper instance, as it refers to the localhost of the current container, which is not Zookeeper. If you exchange localhost with the external IP of your VM or the IP of the zookeeper container (get it via docker inspect zookeeper --format {{.NetworkSettings.Networks.bridge.IPAddress}}) your topics should be listed fine.

Resources