2

My app is

      React Front-end <------> Nodes js back-end <------> mongodb

Source code can be downloaded from here

You can deploy above ./setup.sh command at minikube kubernetes

1) mongoDB deployment with clusterIP Type service resource

2) Backend node js server with clusterIP Type service resource

3) front-end React build with nginx and LoadBalancer type service resource

Accessing monogdb pod from node js pod using mongodb service FQDN is working fine as mongodb service is cluster IP type and accessing from nodejs pod is working smoothly.

I am having trouble in having communication using axios from react (build) using FQDN of back-end service resource. It is saying:

**POST http://todo-backend-service:5000/init net::ERR_NAME_NOT_RESOLVED**

I have even tried with cluster IP with 5000 port instead of FQDN, not working.

This seems to be a problem after making build or something else?

Solution to it would be appreciated.

Spazzy757
  • 889
  • 4
  • 20
A-P
  • 198
  • 1
  • 12

1 Answers1

2

So the issue here is that a frontend application makes requests from your browser (it is client-side, not server-side) meaning essentially what you need to do is expose your node.js backend

Example: if you are using Minikube you could do something as simple: Change your service type for you node.js to type Loadbalancer:

apiVersion: v1
kind: Service
metadata:
  name: todo-backend-service
spec:
  ports:
  - port: 5000
    targetPort: 5000
  selector:
    app: todo-server-app
  type: LoadBalancer

You would then need to run:

minikube service todo-backend-service --url
# OUTPUT
http://192.168.99.113:30048

This IP address and port is what your frontend should use to connect to the node.js backend:

curl -X POST http://192.168.99.113:30048/todo/list
# OUTPUT
{"error":"Please login first."}

Just a note here, when listing items generally you should use a GET request

The reasoning behind the example:

A client-side application loads in your browser, therefore any request made to your backend service will need to be via an external endpoint as your browser will not be on the same network as your Kubernetes Pods

Spazzy757
  • 889
  • 4
  • 20
  • Thanks for such a detailed answer. I was correct React JS data when make build goes to client side and thats out of cluster so ClusterIP is out of question. LB type service as you also sent the output, working fine but is there any way to have FQDN or alternate to absolute IP and port (I know port can be made fixed but I believe not a good option to hard code it) of a back-end service. As every deployment need to go and check backend svc IP and port and then change absolute IP and port at front-end Dockerfile and make new build again to deploy and that's need little extra work for deployer. – A-P Oct 02 '19 at 07:29
  • regarding POST, I have read somewhere that POST are better when sending auth token, password etc in request. Correct me if I am still wrong. – A-P Oct 02 '19 at 07:31
  • 1
    So the way to specify this URL should not be done in your Dockerfile, you should be able to specify this via an environment variable i.e BACKEND_URL= (have a look at 12-factor apps). There is a way to setup FQDN but you would need to set up an ingress of some type, which is a bit more difficult on Minikube than on a Cloud Provider. You would then need to setup DNS to route to that IP. But I believe this would have to be another question and is different from this problem – Spazzy757 Oct 02 '19 at 09:35