Docker for Mac Kubernetes load balancer NoHttpResponseException

1/13/2019

I'm trying to run a simple load balancing server connecting to a Deployment pod.
I've installed Docker for Mac edge version.
The problem is that when I try to make a GET request to the exposed load balancer url http://localhost:8081/api/v1/posts/health, the error appearing is:

org.apache.http.NoHttpResponseException: localhost:8081 failed to respond

When doing:

k get services 

I get:

enter image description here

enter image description here

Clearly, the service is running, but localhost:8081 fails to respond, no idea why, I keep struggling with this.

My service resource:

---
apiVersion: v1
kind: Service
metadata:
  name: posts-api-svc
  # namespace: nginx-ingress
  labels:
    app: posts-api
    rel: beta
    env: dev
spec:
  type: LoadBalancer
  selector:
    app: posts-api
    rel: beta
    env: dev
  ports:
    - protocol: TCP
      port: 8081

My deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: posts-api-deployment
  # namespace: nginx-ingress
  labels:
    app: posts-api
    rel: beta
    env: dev
spec:
  replicas: 1
  selector:
    matchLabels:
      app: posts-api
      env: dev
      rel: beta
  template:
    metadata:
      labels:
        app: posts-api
        env: dev
        rel: beta
    spec:
      containers:
        - name: posts-api
          image: kimgysen/posts-api:latest
          ports:
          - containerPort: 8083
          livenessProbe:
            httpGet:
              path: /api/v1/posts/health
              port: 8083
            initialDelaySeconds: 120
            timeoutSeconds: 1

Should be a basic setup!

My deployment pod does not show any restarts, everything looks good:

enter image description here

Any advice welcome!

Note:

enter image description here

Edit

When using port 31082, I get the error:

org.apache.http.conn.HttpHostConnectException: Connect to localhost:31082 [localhost/127.0.0.1] failed: Connection refused (Connection refused)

There is no specific reason why I used port 8083.
It is because I tried nodeport first (with multiple services), now Load Balancer.
Next step will be ingress, but it didn't really work out for me the first time, and so I try to go step by step.

I used port 8081 instead of port 80 because I read somewhere that on Mac OSX port 80 is only to be used by root user.

-- Trace
docker-for-mac
kubernetes

1 Answer

1/21/2019

The Service port had to correspond to the Deployment containerPort.
I can now access the api on localhost:8083.

-- Trace
Source: StackOverflow