<p>Not able to resolve an API hosted as a ClusterIP service on Minikube when calling from the React JS frontend.</p>

<p>The basic architecture of my application is as follows
React --> .NET core API</p>

<p>Both these components are hosted as ClusterIP services. I have created an ingress service with http paths pointing to React component and the .NET core API.</p>

<p>However when I try calling it from the browser, react application renders, but the call to the API fails with 
net::ERR_NAME_NOT_RESOLVED</p>

<p>Below are the .yml files for</p>

<hr>

<h2>1. React application</h2>

<pre><code>apiVersion: v1
kind: Service
metadata:
    name: frontend-clusterip
spec:
    type: ClusterIP
    ports:
    - port: 59000
        targetPort: 3000
    selector:
    app: frontend
</code></pre>

<hr>

<h2>2. .NET core API</h2>

<pre><code>apiVersion: v1
kind: Service
metadata:
    name: backend-svc-nodeport
spec:
    type: ClusterIP
    selector:
    app: backend-svc
    ports:
    - port: 5901
        targetPort: 59001
</code></pre>

<hr>

<h2>3. ingress service</h2>

<pre><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata: 
    name: ingress-service
    annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
    rules:
    - http:
        paths:
        - path: /?(.*)
            backend: 
            serviceName: frontend-clusterip
            servicePort: 59000
        - path: /api/?(.*)
            backend:
            serviceName: backend-svc-nodeport
            servicePort: 5901
</code></pre>

<hr>

<h2>4. frontend deployment</h2>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
    name: frontend
spec:
    selector:
    matchLabels:
        app: frontend
    replicas: 1
    template:
        metadata:
        labels:
        app: frontend
    spec:
        containers:
        - name: frontend
        image: upendra409/tasks_tasks.frontend
        ports:
        - containerPort: 3000
        env:
        - name: "REACT_APP_ENVIRONMENT"
            value: "Kubernetes"
        - name: "REACT_APP_BACKEND"
            value: "http://backend-svc-nodeport"
        - name: "REACT_APP_BACKENDPORT"
            value: "5901"
</code></pre>

<hr>

<p>This is the error I get in the browser:</p>

<pre><code>xhr.js:166 GET 
http://backend-svc-nodeport:5901/api/tasks net::ERR_NAME_NOT_RESOLVED
</code></pre>

<p>I installed curl in the frontend container to get in the frontend pod to try to connect the backend API using the above URL, but the command doesn't work</p>

<pre><code>C:\test\tasks [develop ≡ +1 ~6 -0 !]&gt; kubectl exec -it frontend-655776bc6d-nlj7z --curl http://backend-svc-nodeport:5901/api/tasks

Error: unknown flag: --curl
</code></pre>

<p>You are getting this error from local machine because <code>ClusterIP</code> service is wrong type for accessing from outside of the cluster. As mentioned in kubernetes <a href="https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types" rel="nofollow noreferrer">documentation</a> <code>ClusterIP</code> is only reachable from within the cluster.</p>

<blockquote>
  <h2>Publishing Services (ServiceTypes)<a href="https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types" rel="nofollow noreferrer"></a></h2>
  
  <p>For some parts of your application (for example, frontends) you may
  want to expose a Service onto an external IP address, that’s outside
  of your cluster.</p>
  
  <p>Kubernetes  <code>ServiceTypes</code>  allow you to specify what kind of Service
  you want. The default is  <code>ClusterIP</code>.</p>
  
  <p><code>Type</code>  values and their behaviors are:</p>
  
  <ul>
  <li><code>ClusterIP</code>: Exposes the Service on a cluster-internal IP. Choosing this value makes the Service only reachable from within the
  cluster. This is the default  <code>ServiceType</code>.</li>
  <li><a href="https://kubernetes.io/docs/concepts/services-networking/service/#nodeport" rel="nofollow noreferrer"><code>NodePort</code></a>:
  Exposes the Service on each Node’s IP at a static port (the 
  <code>NodePort</code>). A  <code>ClusterIP</code>  Service, to which the  <code>NodePort</code> 
  Service routes, is automatically created. You’ll be able to contact
  the  <code>NodePort</code>  Service, from outside the cluster, by requesting 
  <code>&lt;NodeIP&gt;:&lt;NodePort&gt;</code>.</li>
  <li><a href="https://kubernetes.io/docs/concepts/services-networking/service/#loadbalancer" rel="nofollow noreferrer"><code>LoadBalancer</code></a>:
  Exposes the Service externally using a cloud provider’s load balancer.
  <code>NodePort</code>  and  <code>ClusterIP</code>  Services, to which the external load
  balancer routes, are automatically created.</li>
  <li><p><a href="https://kubernetes.io/docs/concepts/services-networking/service/#externalname" rel="nofollow noreferrer"><code>ExternalName</code></a>:
  Maps the Service to the contents of the  <code>externalName</code>  field (e.g. 
  <code>foo.bar.example.com</code>), by returning a  <code>CNAME</code>  record</p>
  
  <p>with its value. No proxying of any kind is set up.</p>
  
  <blockquote>
    <p><strong>Note:</strong>  You need CoreDNS version 1.7 or higher to use the  <code>ExternalName</code>  type.</p>
  </blockquote></li>
  </ul>
</blockquote>

<p><strong>I suggest using <code>NodePort</code> or <code>LoadBalancer</code> service type instead.</strong></p>

<p>Refer to above documentation links for examples.</p>
