UnknownHostException kafka
Asked Answered
T

4

25

I am trying to setup a Kafka cluster (the first node in the cluster actually).

I have a single node zookeeper cluster setup. I am setting up kafka on a separate node.

Both running CentOS 6.4, running IPV6 which is a bit of a PITA. I verified that the machines can talk to each other using netcat.

When I startup kafka, I am getting the following exception (which causes kafka to shut down). EDIT: I got kafka starting, I had to set the host.name property in the server.config file.

I was able to create a test topic and send messages just fine from the kafka server.

However, I get the same error when trying to consume the messages.

Any help, suggestions?

bin/kafka-console-consumer.sh --zookeeper zk1:2181 --topic test --from-beginning
Exception in thread "main" java.net.UnknownHostException: kafka: kafka: Name or service not known
    at java.net.InetAddress.getLocalHost(InetAddress.java:1473)
    at kafka.consumer.ZookeeperConsumerConnector.<init>(ZookeeperConsumerConnector.scala:107)
    at kafka.consumer.ZookeeperConsumerConnector.<init>(ZookeeperConsumerConnector.scala:128)
    at kafka.consumer.Consumer$.create(ConsumerConnector.scala:89)
    at kafka.consumer.ConsoleConsumer$.main(ConsoleConsumer.scala:178)
    at kafka.consumer.ConsoleConsumer.main(ConsoleConsumer.scala)
Caused by: java.net.UnknownHostException: kafka: Name or service not known
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)
    at java.net.InetAddress$1.lookupAllHostAddr(InetAddress.java:901)
    at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1293)
    at java.net.InetAddress.getLocalHost(InetAddress.java:1469)
    ... 5 more
Terrarium answered 26/8, 2014 at 2:52 Comment(8)
Internet connection, URL are correct?Sonar
Well, I know that I can communicate from the KAFKA server to ZOOKEEPER because a leader was elected, I created a topic, and posted a sample message. It is consuming messages that isn't working. There is some configuration someplace that isn't right here, but I am not clear on what that is.Terrarium
so I am assuming that config/server.properties does have the host.name property set properly .. what is your /etc/hosts file shows?Nigercongo
host.name=kafka1; zookeeper.connect=zk1:2181. I have entries in the /etc/hosts file for both "kafka1" and "zk1" with the correct IPV6 addresses. Can confirm that both servers can talk to each other by using PING and "nc -z -w 1 kafka1 9092" and "nc -z -w 1 zk1 2181"Terrarium
I am thinking there is something on the zookeeper side that I didn't setup correctly? When I see "UnknownHostException: kafka", it looks like "kafka" is a default setup someplace, and of course that is not right.Terrarium
In that case in my opinion it would throw something like org.I0Itec.zkclient.exception.ZkException: Unable to connect to zk1:2181 & Caused by: java.net.UnknownHostException: zk1Nigercongo
Looked in the kafka source code, turns out that "java.net.InetAddress.getLocalHost" is not resolving on the VM, this is used to construct a default consumer.id if one is not set in the Consumer properties.Terrarium
I am facing same error can you please tell me how did you resolved this ? java.net.UnknownHostException: QN_STG1Turaco
T
32

When you run > bin/kafka-console-consumer.sh command kafka loads a ConsoleConsumer, which will attempt to create a consumer with an auto generated consumer id. The way Kafka generates the consumer id is to concatenate the name of the local host to it. So, in the problem was the fact that java could not resolve the ip address for local host on the Open Stack VM I am working with.

So the answer was that the Open Stack VM was resolving the local host name to kafka, which is the name of the VM. I had everything setup in the Kafka and Zookeeper instances as kafka1.

So, when java was calling getLocalHost, it was trying to find the IP Address for kafka, which I did not have in my /etc/hosts file.

I simply added an entry for kafka in my /etc/hosts file and everything started working wonderfully!!!

I would have thought it would resolve to localhost, but it did not, it resolved to the name of the vm, kafka.

Terrarium answered 26/8, 2014 at 17:4 Comment(4)
So how you kafka entry look like now?Sprig
how does your entry look like ?Bract
127.0.0.1 localhost 127.0.0.1 ip-172-31-0-252Halfcock
Editing the hosts file is only a workaround - the actual fix is to set advertised.listener correctly - see rmoff.net/2018/08/02/kafka-listeners-explainedAmoebaean
T
7

As noplay pointed out the issue was that Kafka wasn't able to resolve the correct IP, this may happen for example on you EC2 instances running in private subnets without assignment of public IP. The solution summarized:

hostname

Which will show you the host name, something like ip-10-180-128-217. Then just update your /etc/hosts

sudo nano /etc/hosts

edit, e.g.

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 ip-10-180-128-217
Thicket answered 10/1, 2018 at 9:34 Comment(0)
F
2

Under your kafka_folder/config (sudo vim /etc/hosts), your file should look like that:

127.0.0.1 kafka localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain <localhost_OR_DNS_OR_ip_address>
<localhost_OR_DNS_OR_ip_address> kafka

note that kafka is my desired hostname name.

Then, under

kafka_folder/config/server.properties

there's a field "listeners=".

It looks like that

# The address the socket server listens on. If not configured, the host name will be equal to the value of
# java.net.InetAddress.getCanonicalHostName(), with PLAINTEXT listener name, and port 9092.
#   FORMAT:
#     listeners = listener_name://host_name:port
#   EXAMPLE:
#     listeners = PLAINTEXT://your.host.name:9092

Simply uncomment that field by removing the "#" before it. Should look like that:

listeners=PLAINTEXT://<localhost_OR_DNS_OR_ip_address>:9092

Fiducial answered 17/9, 2022 at 18:43 Comment(0)
K
0
  • if you get dns server configured kafka will consider your dns domain name so tape this command on your server

    hostname the result will be your localdomain, now copie your localdomain then open hosts file

    nano /etc/hosts add your localdomain.

    127.0.0.1 localhost localhost.localdomain "your localdomain"

    ::1 localhost localhost.localdomain "your localdomain"

Kentonkentucky answered 19/3, 2022 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.