Kubernetes Communication between services

8/19/2018

For learning purposes, i have two services in a cluster on google cloud:

API Service with the following k8 config:

deployment.yaml

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: myapp-api
  labels:
    app: myapp-api
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: myapp-api
    spec:
      containers:
        - image: gcr.io/prefab-basis-213412/myapp-api:0.0.1
          name: myapp-api
          ports:
            - containerPort: 3000

service.yaml

kind: Service
apiVersion: v1
metadata:
  name: myapp-api
spec:
  selector:
    app: myapp-api
  ports:
  - protocol: TCP
    port: 80
    targetPort: 3000

And a second service, called frontend, that's publicly exposed:

deployment.yaml

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: myappfront-deployment
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: myappfront
    spec:
      containers:
        - image: gcr.io/prefab-basis-213412/myappfront:0.0.11
          name: myappfront-deployment
          ports:
          - containerPort: 3000

service.yaml

apiVersion: v1
kind: Service
metadata:
  name: myappfront-service
spec:
  type: LoadBalancer
  selector:
    app: myappfront
  ports:  
    - port: 80
      targetPort: 3000

The front service is basically a nodejs app that only does a rest call to the api service like so axios.get('http://myapp-api').

The issue is that the call is failing and it's unable to find the requested endpoint. Is there any additional config that i'm currently missing for the API service to be discovered?

P.S. Both services are running and I can connect to them by proxying from localhost.

-- vladsuciu
google-kubernetes-engine
kubernetes

1 Answer

8/19/2018

Since you're able to hit the services when proxying it sounds like you've already gone through most of the debugging steps for in-cluster issues (https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/). That suggests the problem might not be within the cluster. Something to watch out for with frontends is where the http call takes place. It could be in the server with node but given you're seeing this issue I'd suggest that it's in the browser. (If you see can see the call in the browser console then it is.) If the frontend's call is being made in the browser then it doesn't have access to the Kubernetes dns and the cluster-internal service name won't resolve. To handle this you could make the backend service LoadBalancer and pass the external name into the frontend.

-- Ryan Dawson
Source: StackOverflow