I setup a single node Kafka Docker container on my local machine like it is described in the Confluent documentation (steps 2-3).
In addition, I also exposed Zookeep
tl;dr - At the end of the day, it's all the same Apache Kafka running in a container. You're just dependent on how it is configured. And which variables make it so.
The following uses
confluentinc
docker images, notwurstmeister/kafka
, although there is a similar configuration, I have not tried it. If using that image, read their Connectivity wiki.
Nothing against the
wurstmeister
image, but it's community maintained, not built in an automated CI/CD release... Bitnami ones are similarly minimalistic and run in multiple cloud providers. Forbitnami
Kafka images, refer their README
debezium/kafka
docs on it are mentioned here. Note: advertised host and port settings are deprecated. Advertised listeners covers both
spotify/kafka
is deprecated and outdated.fast-data-dev
is great for an all in one solution, but it is bloated
For supplemental reading, a fully-functional
docker-compose
, and network diagrams, see this blog by @rmoff
The Confluent quickstart (Docker) document assumes all produce and consume requests will be within the Docker network.
You could fix the problem by running your Kafka client code within its own container, but otherwise you'll need to add some more environment variables for exposing the container externally, while still having it work within the Docker network.
First add a protocol mapping of PLAINTEXT_HOST:PLAINTEXT
that will map the listener protocol to a Kafka protocol
Key: KAFKA_LISTENER_SECURITY_PROTOCOL_MAP
Value: PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT
Then setup two advertised listeners on different ports. (kafka:9092
here refers to the docker container name). Notice the protocols match the right side values of the mappings above
Key: KAFKA_ADVERTISED_LISTENERS
Value: PLAINTEXT://kafka:9092,PLAINTEXT_HOST://localhost:29092
When running the container, add -p 29092:29092
for the host port mapping
tl;dr (with the above settings)
When running any Kafka Client outside the Docker network (including CLI tools you might have installed locally), use localhost:29092
for bootstrap servers and localhost:2181
for Zookeeper
If trying to connect from an external server, you'll need to advertise the external hostname/ip of the host as well/in place of localhost
When running an app in the Docker network, use kafka:9092
for bootstrap servers and zookeeper:2181
for Zookeeper, just like any other Docker service communication
See the example Compose file for the full Confluent stack
For anyone interested in Kubernetes deployments: https://operatorhub.io/?keyword=Kafka
before zookeeper
after kafka
in kafka consumer and producer config
@Bean
public ProducerFactory<String, String> producerFactory() {
Map<String, Object> configProps = new HashMap<>();
configProps.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.8.128:9092");
configProps.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
configProps.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return new DefaultKafkaProducerFactory<>(configProps);
}
@Bean
public ConsumerFactory<String, String> consumerFactory() {
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.8.128:9092");
props.put(ConsumerConfig.GROUP_ID_CONFIG, "group_id");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
return new DefaultKafkaConsumerFactory<>(props);
}
I run my project with these regulations. Good luck dude.
When you first connect to a kafka node, it will give you back all the kafka node and the url where to connect. Then your application will try to connect to every kafka directly.
Issue is always what is the kafka will give you as url ? It's why there is the KAFKA_ADVERTISED_LISTENERS
which will be used by kafka to tell the world how it can be accessed.
Now for your use-case, there is multiple small stuff to think about:
Let say you set plaintext://kafka:9092
kafka
that is resolvable through the docker network. kafka
name cannot be resolved.==> To fix this, you need to have a specific DNS server like a service discovery one, but it is big trouble for small stuff. Or you set manually the kafka
name to the container ip in each /etc/hosts
If you set plaintext://localhost:9092
==> If you have this and wish to use a kafka client in another container, one way to fix this is to share the network for both container (same ip)
Last option : set an IP in the name: plaintext://x.y.z.a:9092
This will be ok for everybody... BUT how can you get the x.y.z.a name ?
The only way is to hardcode this ip when you launch the container: docker run .... --net confluent --ip 10.x.y.z ...
. Note that you need to adapt the ip to one valid ip in the confluent
subnet.