<p>For local development I have a working minikube. There we have different services deployed. Now I want to connect the Frontend with the Backend.</p>

<p>The Frontend is a angular application and lives in its own service.
The Backend is a node.js application also using a separate service and uses DNS to connect to other internal services like mongodb. </p>

<p>Now I want to communicate from the Frontend with the Backend. DNS is not working because the Frontend does not know how to resolve the named route. The problem is to tell the frontend which backend URL and port it should use to send requests to?</p>

<p>The only working state was approached when I first started the Backend service with type NodePort and copied the url and port to the Frontends target URL. I think this is very unclean to me. Is there another approach to get the url for backend requests into the Frontend?</p>

<p>I know when we deploy a service on a production system with type="LoadBalancer" that the service is exposed by an external IP and I can access the service then from there. And that the external IP will be permanent at pod updates and so on. The problem I also see is that the backend IP needs to be injected into the docker container by an additional commit.</p>

<p>Edit(1): The backend service</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: backend
  labels:
    app: some-app
    tier: backend
spec:
  type: NodePort
  ports:
  - port: 3000
  selector:
    app: some-app
    tier: backend
</code></pre>

<p>Edit(2): I also get this response when I request from the client with a fqn:</p>

<pre><code>OPTIONS http://backend.default.svc.cluster.local:3000/signup/ net::ERR_NAME_NOT_RESOLVED
</code></pre>

<p>First I will try to address your specific questions</p>

<blockquote>
  <p>The only working state was approached when I first started the Backend
  service with type NodePort and copied the url and port to the
  Frontends target URL. I think this is very unclean to me. Is there
  another approach to get the url for backend requests into the
  Frontend?</p>
</blockquote>

<p>You have couple of options here 1) As you said, use type="LoadBalancer". OR  2) Proxy all your backend calls through your front end server   </p>

<blockquote>
  <p>I know when we deploy a service on a production system with
  type="LoadBalancer" that the service is exposed by an external IP and
  I can access the service then from there. And that the external IP
  will be permanent at pod updates and so on. The problem I also see is
  that the backend IP needs to be injected into the docker container by
  an additional commit.</p>
</blockquote>

<ol>
<li>Make it a 12-factor app (or 1 step closer to a 12-factor app :)) by moving the config out from your code to platform (let's say to k8s configmap or an external KV registry like consul/eureka)</li>
<li>Even if it's left in code, as you said, the external IP will be referable and it's not going to change unless you do so. I don't see why you need another deployment</li>
</ol>

<h1>Proxy all your backend calls through your front end server</h1>

<p>If you are routing (or willing to route) all your microservices/backend call thru the server side of your front end and if are deploying both your front end and backend in the same k8s cluster in the same namespace, then you can use KubeDNS add-on (If it is not available in your k8s cluster yet, you can check with the k8s admin) to resolve the backend service name to it's IP. From your front end <strong>server</strong>, Your backend service will always be resolvable by it's name.</p>

<p>Since you have kubeDNS in your k8s cluster, and both frontend and backend services resides in same k8s cluster and same namespace, we can make use of k8s' inbuilt service discovery mechanism. Backend service and frontend service will be discoverable each other by it's name. That means, you can simply use the DNS name "backend" to reach your backend service from your frontend <strong>pods</strong>. So, just proxy all the backend request through your front end nginx to your upstream backend service. In the frontend nginx <strong>pods</strong>, backend service's IP will resolvable for the domain name "backend". This will save you the CORS headache too. This setup is portable, meaning, it doesn't matter whether you are deploying in dev or stage or prod, name "backend" will always resolve to the corresponding backend.</p>

<p>A potential pitfall of this approach is, your backend may not be able to scale independent of frontend; Which is not a big deal in my humble opinion; In a k8s environment, it is just a matter of spinning up more pods if needed.</p>

<p>Just curious- What is serving your front end (which server technology is delivering your index.html to user's browser)? is it static servers like nginx or apache httpd or are you using nodejs here?</p>
