5

Is there a way to access a pod by its hostname? I have a pod with hostname: my-pod-1 that need to connect to another pod with hostname: my-pod-2.

What is the best way to achieve this without services?

Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73
  • 2
    Possible duplicate of [How to access pods without services in Kubernetes](https://stackoverflow.com/questions/53517633/how-to-access-pods-without-services-in-kubernetes) – Ivan Aracki Jul 16 '19 at 11:01
  • https://github.com/kubernetes/kubernetes/issues/49270 – Malathi Jul 16 '19 at 11:06
  • not sure how you have this configured, but I would recommend using StatefulSets so that there is still a replication controller involved and a service which allows for name resolution (podname.servicename.namespace.svc.cluster.local). With cluster first DNS, you'll only need to specify "podname.servicename.namespace" and kube-dns will handle the rest. – Patrick W Jul 16 '19 at 15:05

2 Answers2

5

Through your description, Headless-Service is you want to find. You can access pod by accessing podName.svc with headless service.

OR access pod by pod ip address.

FakeAlcohol
  • 860
  • 7
  • 28
0

In order to connect from one pod to another by name (and not by IP), replace the other pod's IP with the service name that points on it.

for example, If my-pod-1 (172.17.0.2) is running rabbitmq, And my-pod-2 (172.17.0.4) is running a rabbitmq consumer (let's say in python).

In my-pod-2 instead of running:

spec:
  containers:
  - name: consumer-container
    image:  shlomimn/rabbitmq_consumer:latest
    args: ["/app/consumer.py","-p","5672","-s","172.17.0.2"]

Use:

spec:
  containers:
  - name: consumer-container
    image:  shlomimn/rabbitmq_consumer:latest
    args: ["/app/consumer.py","-p","5672","-s","rabbitmq-svc"]

Where rabbitmq_service.yaml is,

apiVersion: v1
kind: Service
metadata:
  name: rabbitmq-svc
  namespace: rabbitmq-ns
spec:
  selector: 
    app:  rabbitmq
  ports:
  - name: rabbit-main
    protocol: TCP
    port: 5672
    targetPort: 5672

Shlomi

Shlomi
  • 33
  • 7