Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kafka server configuration - listeners vs. advertised.listeners

To get Kafka running, you need to set some properties in config/server.properties file. There are two settings I don't understand.

Can somebody explain the difference between listeners and advertised.listeners property?

The documentation says:

listeners: The address the socket server listens on.

and

advertised.listeners: Hostname and port the broker will advertise to producers and consumers.

When do I have to use which setting?

like image 626
CPA Avatar asked Mar 24 '17 11:03

CPA


People also ask

What is listeners in Kafka server properties?

KAFKA_LISTENERS is a comma-separated list of listeners and the host/IP and port to which Kafka binds to for listening. For more complex networking, this might be an IP address associated with a given network interface on a machine. The default is 0.0. 0.0, which means listening on all interfaces.

What is Kafka advertised host?

Learn about the Advertised Host setting of Kafka. Advertised listeners is the most important setting of Kafka and setting it correctly ensures your clients all over your network can successfully connect to every broker in your Kafka cluster.

How does Kafka listener work?

The Kafka Listener is work on the publish and subscribe model. The Apache Kafka is nothing but a massaging protocol. The Kafka broker will receive the number of messages by the Kafka topics. We need to set the listener configuration correctly.

What is 9092 port in Kafka?

Default port By default, the Kafka server is started on port 9092 . Kafka uses ZooKeeper, and hence a ZooKeeper server is also started on port 2181 . If the current default ports don't suit you, you can change either by adding the following in your build.


2 Answers

listeners is what the broker will use to create server sockets.

advertised.listeners is what clients will use to connect to the brokers.

The two settings can be different if you have a "complex" network setup (with things like public and private subnets and routing in between).

like image 169
Thilo Avatar answered Oct 13 '22 00:10

Thilo


Since I cannot comment yet I will post this as an "answer", adding on to M.Situations answer.

Within the same document he links there is this blurb about which listener is used by a KAFKA client (https://cwiki.apache.org/confluence/display/KAFKA/KIP-103%3A+Separation+of+Internal+and+External+traffic):

As stated previously, clients never see listener names and will make metadata requests exactly as before. The difference is that the list of endpoints they get back is restricted to the listener name of the endpoint where they made the request.

This is important as depending on what URL you use in your bootstrap.servers config that will be the URL* that the client will get back if it is mapped in advertised.listeners (do not know what the behavior is if the listener does not exist).

Also note this:

The exception is ZooKeeper-based consumers. These consumers retrieve the broker registration information directly from ZooKeeper and will choose the first listener with PLAINTEXT as the security protocol (the only security protocol they support).

As an example broker config (for all brokers in cluster):

advertised.listeners=EXTERNAL://XXXXX.compute-1.amazonaws.com:9990,INTERNAL://ip-XXXXX.ec2.internal:9993

inter.broker.listener.name=INTERNAL

listener.security.protocol.map=EXTERNAL:SSL,INTERNAL:PLAINTEXT

If the client uses XXXXX.compute-1.amazonaws.com:9990 to connect, the metadata fetch will go to that broker. However, the returning URL to use with the Group Coordinator or Leader could be 123.compute-1.amazonaws.com:9990* (a different machine!). This means that the match is done on the listener name as advertised by KIP-103 irrespective of the actual URL (node).

Since the protocol map for EXTERNAL is SSL this would force you to use an SSL keystore to connect.

If on the other hand you are within AWS lets say, you can then issue ip-XXXXX.ec2.internal:9993 and the corresponding connection would be plaintext as per the protocol map.

This is especially needed in IaaS where in my case brokers and consumers live on AWS, whereas my producer lives on a client site, thus needing different security protocols and listeners.

EDIT: Also adding Inbound Rules is much easier now that you have different ports for different clients (brokers, producers, consumers).

EDIT2: This article is a great in depth guide if the above is still not clear: https://rmoff.net/2018/08/02/kafka-listeners-explained/

like image 48
PragmaticProgrammer Avatar answered Oct 13 '22 01:10

PragmaticProgrammer