Search code examples
kubernetesminikubecoredns

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


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?


Solution

  • 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:.

    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.)