Pod unable to curl localhost

10/6/2020

There is an elastic search container running on localhost on port 9200, but from a pod on the same host, I'm unable to curl the localhost port 9200

[root@jenkins ~]# netstat -tupln | grep 9200
tcp6       0      0 :::9200                 :::*                    LISTEN      4148/docker-proxy


[jenkins@kb-s-9xttg agent]$ curl http://localhost:9200
curl: (7) Failed to connect to ::1: Network is unreachable

/etc/hosts

# Kubernetes-managed hosts file.
127.0.0.1       localhost
::1     localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
fe00::0 ip6-mcastprefix
fe00::1 ip6-allnodes
fe00::2 ip6-allrouters
192.168.255.23  kbs-9xttg

I am able to curl public_host_ip:9200

elastic-search container is not managed by Kubernetes but running on the same host.

Why is the pod unable to talk to localhost:9200 or 127.0.0.1:9200 ?

-- Sanjay M. P.
elasticsearch
kubernetes

1 Answer

10/7/2020

Summary from the comments:

If you're talking to localhost from within a Pod, you're only talking to the containers inside that Pod.

Containers in different Pods have distinct IP addresses and can not communicate by using localhost. They might however be exposing their own port on your local host network, similar to what your Docker container is doing (which is why you can communicate from your local node using localhost).

Inside your cluster you can use the Pod IPs, but if you want to talk to your host you need to use host networking for your Pod

spec:
   hostNetwork: true

or the external IP of your host.

More on Kubernetes Networking in the docs and this blog post.

-- char
Source: StackOverflow