Closed YanivKunda closed 4 weeks ago
I actually went ahead and tried to implement my idea - but when I added a test to try using withAdvertisedListener, it didn't work - Using kcat to access the advertised listener just hung.
Hi, are you trying to connect to redpanda from another container? Then, this should work.
FTR there is a test for what you just described. See https://github.com/testcontainers/testcontainers-java/blob/main/modules/redpanda/src/test/java/org/testcontainers/redpanda/RedpandaContainerTest.java#L114-L141
If there is more information that you can provide, that would be helpful.
I know, I based my test on this one -
RedpandaContainer redpanda = new RedpandaContainer("docker.redpanda.com/redpandadata/redpanda:v23.1.7")
.withNetworkAliases("redpanda")
.withAdvertisedListener(() -> "redpanda:9092")
.withNetwork(network);
but as I said, it failed. Trying to debug it (using the same command the test issues on kcat) I got the following:
sh-4.4$ kcat -b redpanda:9092 -t msgs -P -l /data/msgs.txt
%3|1724958737.983|FAIL|rdkafka#producer-1| [thrd:localhost:63177/0]: localhost:63177/0: Connect to ipv4#127.0.0.1:63177 failed: Connection refused (after 1ms in state CONNECT)
% ERROR: Local: Broker transport failure: localhost:63177/0: Connect to ipv4#127.0.0.1:63177 failed: Connection refused (after 1ms in state CONNECT)
Which recreates the issue I have in my app (container -> RP TC) - in this case RP returns the mapped port advertised listener. But I think at least one of the causes is that I'm trying to create an advertised listener on the same standard 9092 port.
If I use another port (like in the existing test), I still get an error, but from a different listener:
sh-4.4$ kcat -b redpanda:19092 -t msgs -P -l /data/msgs.txt
%3|1724959011.160|FAIL|rdkafka#producer-1| [thrd:redpanda:19092/bootstrap]: redpanda:19092/bootstrap: Connect to ipv4#172.18.0.2:19092 failed: Connection refused (after 1ms in state CONNECT)
% ERROR: Local: Broker transport failure: redpanda:19092/bootstrap: Connect to ipv4#172.18.0.2:19092 failed: Connection refused (after 1ms in state CONNECT)
% ERROR: Local: All broker connections are down: 1/1 brokers are down: terminating
sh-4.4$
Here RP resolves the correct IP from the Docker DNS, but there is no listener on that port, so it doesn't work.
The root cause is probably that the broker returns the first matching advertised listener corresponding to the listener the client connected to - which in the case of 9092 is the first (default) one.
The existing test actually provides an easy workaround for my problem, so I'm not sure this ticket needs the solution I suggested.
you mean withListener
instead of withAdvertisedListener
, right?
Well, I'd like to clarify things first. What you just described in What happened?
section should work when you are connecting from other container in the same network.
Trying to debug it (using the same command the test issues on kcat) I got the following:
from inside the container or from your host machine? If you are trying from your host machine then it will not work.
I've been adding some improvements to KafkaContainer in order to work around external clients. See KafkaContainer but those haven't been moved to RedpandaContainer yet.
Yes, I meant using the existing withListener
as a workaround - but only when using a port different from 9092.
And what I described happened from another container (for the example I've used the confluentinc/cp-kcat
image used in the tests) -
but I think it didn't work because when using withListener
with port 9092 (which already has a listener on 0.0.0.0) the broker can't create the new listener on a different IP because of 0.0.0.0's special role.
Hi @YanivKunda, I think the issue is that you are trying to use an existing port 9092
which is being used. Have you tried with another port like in the examples?
@eddumelendez exactly! That's what I wrote in my previous comment - and indeed I used another port for a workaround.
Can two listeners be declared in the same port? Sorry, but I'm not a kafka expert, so, any documentation that you can point me would be helpful to fix it.
Generally in networks you can start two sockets on the same port, but of course in different IPs - Obviously, if one of them is 0.0.0.0 that other wouldn't be able to bind. I don't think Kafka adds any restriction of its own, but I think I'm just going to close this issue since the workaround is more than enough.
Module
Redpanda
Testcontainers version
1.20.1
Using the latest Testcontainers version?
Yes
Host OS
Mac
Host Arch
ARM
Docker version
What happened?
I tried to use
withListener(() -> "mykafka:9092")
to add a listener using the network alias added viawithNetworkAliases("mykafka")
, but container startup failed due to the fact thatwithListener
adds the given argument both as an advertised listener and an actual listener - the latter with the 0.0.0.0 network address, which conflicts with the default kafka_api listener bound on 0.0.0.0:9092Relevant log output
Additional Information
It looks like the problem is that both the RedpandaContainer API only supports adding "Listeners" and the
redpanda.yaml.ftl
freemarker template interprets it as both the listeners and advertised listeners - I think the solution would be:advertisedListenersValueSupplier
set to RedpandaContainer, in addition to the existinglistenersValueSupplier
setwithAdvertisedListener
method to RedpandaContainer to add an advertised listener just toadvertisedListenersValueSupplier
withListener
to add a listener to both lists (to maintain backward compatibility)advertised_kafka_api
from the new set