Prakashsinha Bayas
Prakashsinha Bayas

Reputation: 552

How to access pod by it's hostname from within other pod of the same namespace?

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?

Upvotes: 7

Views: 10984

Answers (2)

Shlomi
Shlomi

Reputation: 51

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

Upvotes: 0

FakeAlcohol
FakeAlcohol

Reputation: 1010

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.

Upvotes: 5

Related Questions