<p>I am trying to deploy my microservice application on Kubernetes using minikube. I have an angular front service and two backend services and I used this configuration to lunch deployment and services </p>

<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: searchservice
  labels:
    app: searchservice
spec:
  selector:
    matchLabels:
      app: searchservice
  template:
    metadata:
      labels:
        app: searchservice
    spec:
      containers:
      - name: searchservice
        image: ayoubdali/searchservice:0.1.9-SNAPSHOT
        ports:
        - containerPort: 8070
---
apiVersion: v1
kind: Service
metadata:
  name: searchservice
spec:
  type: NodePort
  selector:
    app: searchservice
  ports:
  - protocol: TCP
   # Port accessible inside cluster
    port: 8070
   # Port to forward to inside the pod
    targetPort: 8070
    nodePort: 31000

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: searchappfront
  labels:
    app: searchappfront
spec:
  selector:
    matchLabels:
      app: searchappfront
  template:
    metadata:
      labels:
        app: searchappfront
    spec:
      containers:
      - name: searchappfront
        image: ayoubdali/searchappfront:0.6.5
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: searchappfront
spec:
  type: NodePort
  selector:
    app: searchappfront
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 80

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: subscriberservice
  labels:
    app: subscriberservice
spec:
  selector:
    matchLabels:
      app: subscriberservice
  template:
    metadata:
      labels:
        app: subscriberservice
    spec:
      containers:
      - name: subscriberservice
        image: ayoubdali/subscriber-service:0.1.0-SNAPSHOT 
        ports:
        - containerPort: 8090
---
apiVersion: v1
kind: Service
metadata:
  name: subscriberservice
spec:
  type: NodePort
  selector:
    app: subscriberservice
  ports:
  - protocol: TCP
    port: 8090
    #targetPort: 80
    nodePort: 31102

</code></pre>

<p>and this configuration for the ingress service </p>

<pre><code> apiVersion: networking.k8s.io/v1beta1 # for versions before 1.14 use extensions/v1beta1
 kind: Ingress
 metadata:
   name: ingress
   annotations:
     nginx.ingress.kubernetes.io/rewrite-target: /$1
 spec:
   rules:
   - host: app.info
     http:
       paths:
       - path: /
         backend:
           serviceName: searchappfront
           servicePort: 80
       - path: /api
         backend:
           serviceName: subscriberservice
           servicePort: 8090    

</code></pre>

<p>But when I open app.info/ I got a blank page with javascript error like </p>

<pre><code>Failed to load module script: The server responded with a non-JavaScript MIME type of "text/html". Strict MIME type checking is enforced for module scripts per HTML spec.
</code></pre>

<p>I tried deploying the application using docker compose and it works fine. </p>

<p>When you enable  <a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/" rel="nofollow noreferrer"><code>rewrite-target</code></a> it will create a capture group and send it to the appropriate service. </p>

<p>If you set the capture group to <code>$1</code>, everything after the root <code>/</code> will be discarted and the request is forwarded to root <code>path</code>.</p>

<ul>
<li>In order to forward the full request, <strong>remove the <code>rewrite-target</code> line</strong>, your Ingress should be like this:</li>
</ul>

<pre><code>apiVersion: networking.k8s.io/v1beta1
 kind: Ingress
 metadata:
   name: ingress
   annotations:
     kubernetes.io/ingress.class: nginx
 spec:
   rules:
   - host: app.info
     http:
       paths:
       - path: /
         backend:
           serviceName: searchappfront
           servicePort: 80
       - path: /api
         backend:
           serviceName: subscriberservice
           servicePort: 8090    
</code></pre>

<hr>

<p><strong>Example:</strong></p>

<ul>
<li><p>I've deployed two <code>echo-apps</code> that echoes the requests, I've suppressed the output to show the path and the pod handling it on the background).</p>

<ul>
<li><code>echo1-app</code> is simulating <code>searchappfront</code></li>
<li><code>echo2-app</code> is simulating <code>subscriberservice</code></li>
</ul></li>
<li><p>This is the ingress I'm using:</p></li>
</ul>

<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: echo-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
spec:
  rules:
    - host: app.info
      http:
        paths:
          - path: /
            backend:
              serviceName: echo1-svc
              servicePort: 80
          - path: /api
            backend:
              serviceName: echo2-svc
              servicePort: 80
</code></pre>

<ul>
<li>Now let's test the commands:</li>
</ul>

<pre><code>$ kubectl get ingress
NAME           HOSTS      ADDRESS        PORTS   AGE
echo-ingress   app.info   35.188.7.149   80      73m

$ kubectl get pods
NAME                            READY   STATUS    RESTARTS   AGE
echo1-deploy-764d5df7cf-2wx4m   1/1     Running   0          74m
echo2-deploy-7bcb8f8d5f-xlknt   1/1     Running   0          74m

$ tail -n 1 /etc/hosts
35.188.7.149 app.info

$ curl app.info
{"path": "/",
  "os": {"hostname": "echo1-deploy-764d5df7cf-2wx4m"}}

$ curl app.info/foo/bar
{"path": "/foo/bar",
  "os": {"hostname": "echo1-deploy-764d5df7cf-2wx4m"}}

$ curl app.info/api
{"path": "/api",
  "os": {"hostname": "echo2-deploy-7bcb8f8d5f-xlknt"}}

$ curl app.info/api/foo
{"path": "/api/foo",
  "os": {"hostname": "echo2-deploy-7bcb8f8d5f-xlknt"}}

$ curl app.info/api/foo/bar
{"path": "/api/foo/bar",
  "os": {"hostname": "echo2-deploy-7bcb8f8d5f-xlknt"}}
</code></pre>

<p>To summarize:</p>

<ul>
<li>Requests to <strong>app.info/</strong> will be delivered to <code>echo1-app</code> as <strong>/</strong></li>
<li>Requests to <strong>app.info/foo/bar</strong> will be delivered to <code>echo1-app</code> as <strong>/foo/bar</strong></li>
<li>Requests to <strong>app.info/api</strong> will be delivered to <code>echo2-app</code> as <strong>/api</strong></li>
<li>Requests to <strong>app.info/api/foo/bar</strong> will be delivered to <code>echo2-app</code> as <strong>/api/foo/bar</strong></li>
</ul>

<hr>

<p>Considerations about your environment:</p>

<ul>
<li><p>I understand you are using <code>NodePort</code> to test the access, but if you wish to close that access, you can set the services as <code>ClusterIP</code> since it will be Ingress job to handle the incoming traffic.</p></li>
<li><p><a href="https://kubernetes.io/docs/concepts/services-networking/service/#nodeport" rel="nofollow noreferrer">Nodeport</a> port range by default is 30000-32767.</p>

<ul>
<li><code>searchappfront</code> has it set to <code>80</code>, which will be ignored and the correct port will be attributed.</li>
</ul></li>
</ul>

<p>If you have any question let me know in the comments.</p>
