5

I have deployed a mongo db, Spring Boot BE, Angular app within GKE. My FE service is a load balancer, it needs to connect with my BE to get data but I'm getting an console error in my browser: GET http://contactbe.default.svc.cluster.local/contacts net::ERR_NAME_NOT_RESOLVED. My FE needs to consume /contacts endpoint to get data. I'm using DNS from my BE service (contactbe.default.svc.cluster.local) within my Angular app. It is my yml file that I used to create my deployment:

apiVersion: v1
kind: Service
metadata:
  name: mongo
  labels:
    run: mongo
spec:
  type: NodePort
  ports:
  - port: 27017
    targetPort: 27017
    protocol: TCP
  selector:
    run: mongo
---
apiVersion: apps/v1beta1
kind: Deployment
metadata: 
  name: mongo
spec:
  template:
    metadata:
      labels:
        run: mongo
    spec:
      containers:
      - name: mongo
        image: mongo
        ports:
        - containerPort: 27017
---        
apiVersion: v1
kind: Service
metadata:
  name: contactbe
  labels:
    app: contactbe
spec:
  type: NodePort 
  ports:
  - port: 8181
    targetPort: 8181
    protocol: TCP
  selector:
    app: contactbe
---
apiVersion: apps/v1beta1
kind: Deployment
metadata: 
  name: contactbe
spec:
  template:
    metadata:
      labels:
        app: contactbe
    spec:
      containers:
      - name: contactbe
        image: glgelopfalcon/k8s_contactbe:latest
        ports:
        - containerPort: 8181
---
apiVersion: apps/v1beta1 # for versions before 1.9.0 use apps/v1beta2 
kind: Deployment 
metadata: 
  name: angular-deployment 
spec: 
  selector: 
    matchLabels: 
      app: angular 
  replicas: 2 # tells deployment to run 2 pods matching the template 
  template: 
    metadata: 
      labels: 
        app: angular 
    spec: 
      containers: 
      - name: angular 
        image: glgelopfalcon/my-angular-app:latest 
        ports: 
        - containerPort: 80
--- 
# https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service  
kind: Service 
apiVersion: v1 
metadata: 
  name: angular-service 
spec: 
  selector: 
    app: angular 
  ports: 
  - port: 80
    targetPort: 80
    protocol: TCP
  type: LoadBalancer 

I googled a lot but I still have not found how to solve this problem. I would appreciate if someone could give a hand.

Console error

Patrick W
  • 4,603
  • 1
  • 12
  • 26
user3380193
  • 51
  • 1
  • 4
  • If I understand this right, the call is being made from the angular-service app to the contactbe app through the service, correct? The error implies a DNS issue. Connect to one of the angular pods and run the same request: kubectl exec -it [angular_pod_name] -- curl http://contactbe.default.svc.cluster.local:8181/contact. You can use wget instead if angular does not have curl installed – Patrick W Feb 27 '19 at 16:58
  • Yes, It connects via curl request when we hit from front it give an error. – Feezan Khattak Apr 14 '22 at 11:19

3 Answers3

1

check for your load balancer having open port 27017 as your sending request to port 27017

otherwise you can change service node port to 80 and target port will be same

Harsh Manvar
  • 27,020
  • 6
  • 48
  • 102
  • Hello Harsh, I didn't understand your suggestion. Could you please give more details or code snippets about that? I'm a newbie in K8S. Thank you in advance. – user3380193 Feb 25 '19 at 14:05
  • check open port on loadbalancer – Harsh Manvar Feb 25 '19 at 14:36
  • Mongo Db is listening to 27017 port. My load balancer is using port 80. In addition my load balancer has Port Node Port Target Port Protocol 80 31011 80 TCP – user3380193 Feb 25 '19 at 16:47
0

port# is missing in the URL. try below

contactbe.default.svc.cluster.local:8181/contacts
P Ekambaram
  • 15,499
  • 7
  • 34
  • 59
  • Same error appears: *contactbe.default.svc.cluster.local:8181/contacts:1 Failed to load resource: net::ERR_NAME_NOT_RESOLVED* – user3380193 Feb 25 '19 at 14:17
  • can you hit the url directly in contactbe pod like below and confirm the result. curl localhost:8181/contacts – P Ekambaram Feb 25 '19 at 14:33
  • I'm getting a result successfully. curl http://localhost:8181/contacts [{"id":"01","name":"Gerardo","email":"gag@gmail.com","phone":"1111","lastName":"Lopez"}]root@contactbe-54d479d774-zf5b8:/usr/app# . Mmmm, I'm confused. – user3380193 Feb 25 '19 at 15:43
  • What does it mean? The pod is working but why can not I get it from my front-end? – user3380193 Feb 25 '19 at 16:12
  • Appears like the pods are not bound to the service. Share output from 'kubectl get ep' – P Ekambaram Feb 25 '19 at 17:07
  • It is result from `kubectl get ep` : angular-service: 10.48.1.6:80,10.48.2.9:80 . contactbe: 10.48.1.4:8181 – user3380193 Feb 25 '19 at 17:11
  • Can you hit host ip address and the nodeport in browser and share the output. Append the result to your question – P Ekambaram Feb 25 '19 at 18:28
0

I found a solution. I changed the kind of service from my Backend. Instead of ClusterIP I'm using a Load balancer, it works as expected

user3380193
  • 51
  • 1
  • 4
  • why? Usually we dont need to expose backend services to external world, and should be kept kind: ClusterIP only. There might be other solution for this. – MechaCode Jun 07 '21 at 14:58
  • Could you resolve it ? I got the same issue. – nihar Jul 06 '21 at 15:05