How to send Ajax calls to an app located in a pod in a Kubernetes cluster?

2/13/2019

My app runs perfectly on the localhost of my machine. I make a number of ajax calls to my backend services. The requests I make are like the following example:

http.open("POST","http://127.0.0.1:3000/due",true);

When I put my code in docker containers and run them in my kubernetes cluster, I understand that my ajax calls wont work. What's the proper way to do the calls ?

Is there an IP that I need to send the call to? The calls are made from javascript (front end which also runs in my cluster) to my backend. The only solution I found is to expose my back-end services which I don't want to do.

I am not sure about how to use ClusterIP. Any help is appreciated.

-- Yberrada
ajax
docker
kubernetes

2 Answers

2/13/2019

You can simply create a kubernetes Service with the type ClusterIP targeting the backend pod. But doing so the backend will be only exposed inside the cluster. You can do it as follows.

kind: Service
apiVersion: v1
metadata:
  name: my-backend
spec:
  selector:
    app: MyApp
  ports:
  - protocol: TCP
    port: 3000
    targetPort: 3000
  type: ClusterIP

After doing that you can use http://my-backend:3000/due to do your Ajax call. Make sure that your service in in same namespacse as your pod and to use the selector to point to the backend pods.

-- Hansika Madushan Weerasena
Source: StackOverflow

2/13/2019

Create a backend service that should target the backend pod. You should be hitting the backend pod using backed service dns which would look like backend-service-name. namespace. Svc. Cluster. Local

If your app is running outside the kubernetes cluster then update service type as Node Port. Then Ajax call should be hitting hostname:nodeport

-- P Ekambaram
Source: StackOverflow