Calling the pods using pod's name instead of host and port

7/21/2020

I'm running set of micro-services inside of a local minikube cluster (using helm charts) which communicate each other. Each and every service's host and port passed via value-dev.yaml to the other services and communication works fine. Now I need go bit of further, and alter connection calling from http://helm-chart-name:PORT/ to http://helm-chart-name/ or http://service-pod-name/. I tried to do this but it didn't work. Is there way to achieve this?

-- Govinda Malavipathirana
coredns
kubernetes
minikube

2 Answers

7/21/2020

If you need to access with pod, The DNS resolution is as follows: pod-ip-address.deployment-name.my-namespace.svc.cluster-domain.example. Given the ephemeral nature of pods and likelihood of running more than one pod of same kind, I would recommend using Service abstraction for intra-cluster communication like what you require. Services resolve to DNS of this format my-svc.my-namespace.svc.cluster-domain.example. You can also have headless Service and resolve to Pod with specific (host)name. Refer to DNS Reolution details here

-- Vanitha Kumar
Source: StackOverflow

7/21/2020

In your Services (specifically) set the port: number to 80. This is the default TCP port number for HTTP, so it's the port number that will get used if there's not a ...:12345 port number in a URL. The targetPort: needs to match whatever port the pod is listening on; it doesn't need to match the port:.

<!-- language: lang-yaml -->
apiVersion: v1
kind: Service
metadata:
  name: {{ include "chart.fullname" . }}
spec:
  selector:
    {{- include "chart.selectorLabels" . | nindent 4 }}
  ports:
    - name: http
      protocol: TCP
      port: 80          # default HTTP port
      targetPort: 3000  # port number the matching Pod uses

Now other services can call http://helm-chart-name/ without explicitly giving a port number.

(You pretty much always need to use a Service to accept connections into a pod; you don't generally communicate directly to a pod, and aside from some specialized circumstances it's tricky to do so.)

-- David Maze
Source: StackOverflow