cantdocpp
cantdocpp

Reputation: 1116

Kubernetes service can't communicate with each other

I'm new with Kubernetes and I tried to create two services that can communicate with each other. The first service is a simple static web (HTML, js) with type LoadBalancer and the second service is a express.js backend with the type ClusterIP. Here's my service.yaml file:

apiVersion: v1
kind: Service
metadata:
  name: number-generator
spec:
  selector:
    app: number-generator
  ports:
  - port: 3000
    targetPort: 3000
  type: ClusterIP
---
apiVersion: v1
kind: Service
metadata:
  name: number-web
spec:
  selector:
    app: number-web
  ports:
  - port: 80
    targetPort: 80
  type: LoadBalancer

And here's my deployment.yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: number-generator
spec:
  selector:
    matchLabels:
      app: number-generator
  template:
    metadata:
      labels:
        app: number-generator
    spec:
      containers:
      - name: number-generator
        image: cantdocpp/number-generator
        ports:
        - containerPort: 3000
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: number-web
spec:
  selector:
    matchLabels:
      app: number-web
  template:
    metadata:
      labels:
        app: number-web
    spec:
      containers:
      - name: number-web
        image: cantdocpp/number-web:v5
        ports:
        - containerPort: 80

So inside the number-web service which can be accessed in localhost:80, should access the number-generator API to get a random number. I tried to access it using the http://number-generator/ and http://number-generator:3000/, but both of them return a network error like this:

GET http://number-generator/ net::ERR_NAME_NOT_RESOLVED

The error should be more or less the same wether I call http://number-generator/ or http://number-generator:3000/. So how can I make this two services talks to each others ?

Upvotes: 1

Views: 338

Answers (1)

cantdocpp
cantdocpp

Reputation: 1116

I just found the problem.

So, because I use a static site for the frontend, when I call the backend service it actually calls it from the browser. The browser is not our kubernetes cluster, so when I tried to access the number-generator service using ajax, it sends those error.

When I tried to change my static frontend into express app, it can access the backend number-generator service, because it's included in the cluster.

Upvotes: 1

Related Questions