How to make HTTP call to pod running in Docker Desktop (Kubernetes)

5/22/2019

So I am having an issue where I get ERR_CONNECTION_REFUSED when I try to make a call from my frontend which is setup as an Ingress. My backend express server is running on port 5000 and is setup as ClusterIP. I've read the docker for mac local cluster ip is the same as localhost so thats how I have it set.

I am confident I have the files and networking correctly. I am just not sure where to aim the API call to the backend. Is it also localhost? I will include the files below.

Frontend deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend-deployment
spec:
  selector:
    matchLabels:
      app: frontend
  template:
    metadata:
      labels:
        app: frontend
    spec:
      containers:
      - name: frontend-container
        image: mrich6347/vue-frontend:5
        resources:
          requests:
            memory: "32Mi"
            cpu: "100m"
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 8080

Frontend service

apiVersion: v1
    kind: Service
    metadata:
      name: frontend-service
    spec:
      type: ClusterIP
      selector:
        app: frontend
      ports:
      - port: 8080
        targetPort: 8080

Express server deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: express-server-deployment
spec:
  selector:
    matchLabels:
      app: express-server
  template:
    metadata:
      labels:
        app: express-server
    spec:
      containers:
      - name: express-server
        image: mrich6347/express-server:5
        resources:
          requests:
            memory: "32Mi"
            cpu: "100m"
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
        - containerPort: 5000

Express server service

apiVersion: v1
kind: Service
metadata:
  name: express-server-service
spec:
  type: ClusterIP
  selector:
    app: express-server
  ports:
  - port: 5000
    targetPort: 5000

Here is my api call from vue

export default {
  name: 'app',
  components: {
    HelloWorld
  },
  created: async () => {
    console.log("SERVER IS UP")
    let res = await axios.get("http://127.0.0.1:5000/test");
    console.log(res)
  }
}

And finally my simple express API

const app = require('express')();
var cors = require('cors')

app.use(cors())

app.get("/test", (req, res) => {
    res.send("WORKING!")
})


app.listen(5000, () => {
    console.log("Server is up on 5000")
})

Ingress Controller

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: traffic-router
  annotations:
    ingress.kubernetes.io/rewrite-target: /


spec:
  rules:
    - http:
        paths:
          - path: /
            backend:
              serviceName: frontend-service
              servicePort: 8080
-- matt
devops
docker
kubernetes

1 Answer

5/23/2019

I've read the docker for mac local cluster ip is the same as localhost so thats how I have it set

Services inside Docker for Mac Kubernetes are not directly available to your desktop. There are many Cluster IP's and only one localhost so if they were all exposed you would run into port conflicts on localhost pretty quickly.

Docker for Mac will automagically expose services with type LoadBalancer on localhost. The concept is similar to running docker run -p 80:30843 .... See the EXTERNAL-IP on this ingress controller.

$ kubectl get service m-nginx-nginx-ingress-controller
NAME                               TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)                      AGE
m-nginx-nginx-ingress-controller   LoadBalancer   10.108.201.47   localhost     80:30843/TCP,443:31419/TCP   22h
$ curl http://localhost:80
default backend - 404

As it sounds like you already have an ingress controller for your front end service, it's easiest to reuse that ingress and map another host name or URI path through to your backend service.

-- Matt
Source: StackOverflow