pod/container name resolution does not work outside containers

3/14/2019

I am able to resolve pod names to ip addresses inside the corresponding containers, e.g. if I have a pod called elasticsearch-0 and I enter the container via kubectl exec -it elasticsearch-0 bash I can do the following:

curl elasticsearch-0:9200

and get the response:

{
  "name" : "OUvCoFL",
  "cluster_name" : "docker-cluster",
  "cluster_uuid" : "1Vhf2-pGR0m3e4ezDRAZ0Q",
  "version" : {
    "number" : "6.1.0",
    "build_hash" : "c0c1ba0",
    "build_date" : "2017-12-12T12:32:54.550Z",
    "build_snapshot" : false,
    "lucene_version" : "7.1.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

However, if I try to do this from within a different container, e.g. kibana-0, it fails with:

curl: (6) Could not resolve host: elasticsearch-0; Name or service not known

Does anyone know what I am missing?

Thanks

-- João Matos
kubernetes

1 Answer

3/14/2019

A service is what you are looking for. Read this

Pods can be configured to talk to the Service, and know that communication to the Service will be automatically load-balanced out to some pod that is a member of the Service.

Create the service:

apiVersion: v1
kind: Service
metadata:
  name: elasticsearch
  labels:
    app: elasticsearch
spec:
  ports:
  - port: 9200
    protocol: TCP
  selector:
    app: elasticsearch

And access the service from any pod

curl http://elasticsearch:9200

-- Amityo
Source: StackOverflow