Accessing services internally in k8s cluster in addition to exposing them outside through ingress

12/7/2018

I have configured two services, both as NodePort.

Service 1

kind: Service
apiVersion: v1
metadata:
  name: bitcoinrpc-dev-service
  namespace: dev
spec:
  selector:
    app: bitcoin-node
  ports:
  - name: bitcoin-mainnet
    protocol: TCP
    port: 80
    targetPort: 8332
  - name: bitcoin-testnet
    protocol: TCP
    port: 8080
    targetPort: 18332  
  type: NodePort

Service 2

kind: Service
apiVersion: v1
metadata:
  name: counterpartyrpc-dev-service
  namespace: dev
spec:
  selector:
    app: counterparty-node
  ports:
  - name: mainnet
    protocol: TCP
    port: 80
    targetPort: 4000
  - name: testnet
    protocol: TCP
    port: 8080
    targetPort: 14000  
  type: NodePort

I can access both these services outside the cluster through an ingress. However, when I try to connect to service1(bitcoin from service2(counterparty), it keeps losing connection. I am referring the service as bitcoinrpc-dev-service.dev.svc.cluster.local:80

However, if I refer this service as hostname configured in ingress, the connection is much more stable. e.g. when I refer service1 by bitcoin.mydomain.com:80

Since, this would mean routing the traffice outside the cluster when both the services that need to communicate to each other are in the same cluster, it seems slightly inefficient.

I tried changing the servicetype to clusterIP, however then I can't access them through ingress.

What should be the correct configuration?

-- kosta
google-kubernetes-engine
kubernetes

1 Answer

12/8/2018

The reason behind that is only k8s cluster with kube-dns add ons can translate the domain name bitcoinrpc-dev-service.dev.svc.cluster.local:80 to its corresponding IP address.

Have a look at this great answer which shows the solutions around this issue:

How to access a service in a kubernetes cluster using the service name .

-- Prafull Ladha
Source: StackOverflow