how to give service name and port in configmap yaml?

3/12/2019

I have a service (CusterIP) like following which is exposing ports of backend POD.

apiVersion: v1
kind: Service
metadata:
  name: fsimulator
  namespace: myns
spec:
  type: ClusterIP
  selector:
    application: oms
  ports:
    - name: s-port
      port: 9780
    - name: b-port
      port: 8780

Front end POD should be able to connect to Backend POD using service. Should we replace hostname with service name to connect from Frontend POD to Backend POD ?

I have to supply the service name and port through environment variables to Frontend POD container. The enviroment variables are set using configMap.

Is it enough to give service name fsimulator as hostname to connect to ? How to give service if is created inside namespace ?

Thanks

-- Chandu
configmap
kubernetes

1 Answer

3/12/2019

Check out this documentation. The internal service PORT / IP pairs for active services are indeed passed into the containers by default.

As the documentation also says, it is possible (recommended) to use a DNS cluster add-on for service discovery. Accessing service.namespace from outside / inside a service will resolve to the correct service route (or just service from inside the namespace). This is usually the right path to take.

Built-in service discovery is a huge perk of using Kubernetes, use the available tools if at all possible!

-- Monkeyanator
Source: StackOverflow