0

Some time ago, I had a similar problem with docker (the same project, actually) that I posted here, but then solved it by using a localhost link for react to connect to express, and it actually worked. I tried linking containers in that case, but it didn't work. Now I am putting the thing in kubernetes, and while the express pod connects to the database pod with no trouble with the database service being set like this:

---
apiVersion: v1
kind: Service
metadata:
  name: database-service
spec:
  selector:
    app: database
  ports:
    - protocol: TCP
      port: 27017
      targetPort: 27017
  type: ClusterIP

And the backend main container being set like this:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: backend-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: backend
  template:
    metadata:
      labels:
        app: backend
    spec:
      containers:
        - name: backend
          image: cloud-backend
          imagePullPolicy: Never
          ports:
            - containerPort: 5001
          env:
            - name: PORT
              value: "5001"
            - name: DBLINK
              value: "mongodb://database-service:27017/TODO"

This connects express to mongo with absolutely no issues. However, this approach doesn't seem to work for me with connecting frontend to backend:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: frontend-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: frontend
  template:
    metadata:
      labels:
        app: frontend
    spec:
      containers:
        - name: frontend
          image: cloud-frontend
          imagePullPolicy: Never
          ports:
            - containerPort: 3001
          env:
            - name: REACT_APP_API_URL
              value: "http://backend-service:5001"
            - name: PORT
              value: "3001"

backend service:

apiVersion: v1
kind: Service
metadata:
  name: backend-service
spec:
  selector:
    app: backend
  ports:
    - protocol: TCP
      port: 5001
      targetPort: 5001
  type: ClusterIP

You can see, that I input an enviromental variable for a link here. This doesn't work. The only way I was able to make this work is by getting a temporary link from the command: minikibe service backend-service. That worked, but that requires editing the yaml file every time, and that's a terrible approach. How do I link my backend the proper way?

nicvampire
  • 13
  • 2
  • Is the React application actually running in a browser; do you see a DNS error looking up `backend-service` in the browser console? Since it's in the browser it can't access any of the Kubernetes-internal networking. (This is probably the same issue with plain Docker where "change the URL to localhost" works.) You need to create an Ingress object pointing at the backend service, and set the backend URL to the externally-visible Ingress URL. – David Maze Jun 16 '23 at 10:23
  • that's a reasonable explanation. but do you know how one can call their backend without exposing an endpoint (through ingress) while running the service as `clusterIP` without the client (browser) trying to resolve the DNS? – faizan Jun 16 '23 at 11:24
  • Okay, I didn't use ingress bc I am currently learning this whole thing, and didn't know about it, but it is required in the task, so how do I configure it? I tried to play around with it, but no progress – nicvampire Jun 18 '23 at 17:47

0 Answers0