<p>I am testing kubernetes with minikube on centOS 7, following the follow progress:</p>

<ol>
<li>Build 2 docker images running simple backend with express.js</li>
<li>Running deployment to serve totally 4 pods, 2 of each image respectively</li>
<li>Serving service with the pods</li>
<li>Using ingress to access the services</li>
</ol>

<p>When I gets into the minikube by <code>minikube ssh</code>, the APIs all work properly.
However, outside of it, it always get error.
So guess it must be error in 3 or 4?  But it doesn't get connection error (as it once was before setup properly).  Can it be security settings or what?</p>

<hr>

<p>Settings &amp; results as below:</p>

<ul>
<li>deployment.yaml</li>
</ul>

<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: abc
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: abc
    spec:
      containers:
        - name: nginx
          image: image/abc
          imagePullPolicy: Never
          ports:
            - containerPort: 3000
  selector:
    matchLabels:
      app: abc
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: efg
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: efg
    spec:
      containers:
        - name: nginx
          image: image/efg
          imagePullPolicy: Never
          ports:
            - containerPort: 3000
  selector:
    matchLabels:
      app: efg
</code></pre>

<ul>
<li>service.yaml</li>
</ul>

<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
  name: abc-service
spec:
  type: NodePort
  selector:
    app: abc
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
---
apiVersion: v1
kind: Service
metadata:
  name: efg-service
spec:
  type: NodePort
  selector:
    app: efg
  ports:
    - protocol: TCP
      port: 80
      targetPort: 3000
</code></pre>

<ul>
<li>ingress.yaml</li>
</ul>

<pre class="lang-yaml prettyprint-override"><code>apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: web
spec:
  rules:
    - host: my.host.com
      http:
        paths:
          - path: /abc
            backend:
              serviceName: abc-service
              servicePort: 80
          - path: /efg
            backend:
              serviceName: efg-service
              servicePort: 80
</code></pre>

<ul>
<li><code>kubectl describe</code> result:</li>
</ul>

<pre><code>Name:             web
Namespace:        default
Default backend:  default-http-backend:80 (&lt;error: endpoints "default-http-backend" not found&gt;)
Rules:
  Host         Path    Backends
  ----         ----    --------
  my.host.com
               /abc    abc-service:80 (172.18.0.4:3000,172.18.0.6:3000)
               /efg    efg-service:80 (172.18.0.5:3000,172.18.0.7:3000)
Annotations:   Events
  Type         Reason  Age               From                      Message
  ----         ------  ----              ----                      -------
  Normal       CREATE  24m               nginx-ingress-controller  Ingress default/web
  Normal       UPDATE  3s (x4 over 23m)  nginx-ingress-controller  Ingress default/web
</code></pre>

<ul>
<li><p><code>minikube ip</code> gets <code>172.17.0.4</code></p></li>
<li><p>/etc/hosts</p></li>
</ul>

<pre><code>172.17.0.4    my.host.com
</code></pre>

<ul>
<li>Running <code>curl 172.18.0.4:3000</code> gets the expected result (json) successfully</li>
<li>Running <code>curl my.host.com/abc</code>, it gets</li>
</ul>

<pre class="lang-html prettyprint-override"><code>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
&lt;meta charset="utf-8"&gt;
&lt;title&gt;Error&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;pre&gt;Cannot GET /abc&lt;/pre&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>

Ellie Wu
<p>Finally my kubernetes works.  Here to sum up the progress:</p>

<ol>
<li>As suggestion in comments above, add <code>nginx.ingress.kubernetes.io/rewrite-target: /</code> in annotations of ingress.yaml</li>
<li>But then the new issue was that, every route maps samely to the root api of each service.  Here's the solution: <a href="https://stackoverflow.com/questions/58673051/ingress-routing-api-prefix-issue">ingress routing api prefix issue</a></li>
</ol>

<p>Finally my new ingress.yaml:</p>

<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: web
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - host: my.host.com
      http:
        paths:
          - path: /abc/?(.*)
            backend:
              serviceName: abc-service
              servicePort: 80
          - path: /efg/?(.*)
            backend:
              serviceName: efg-service
              servicePort: 80
</code></pre>

<p>Then I can now access my APIs:</p>

<ul>
<li><code>my.host.com/abc/</code> (one thing to highlight is that the final <code>/</code> is required here, otherwise it gets 404 Error)</li>
<li><code>my.host.com/abc/child</code></li>
<li><code>my.host.com/efg/</code></li>
<li><code>my.host.com/efg/child</code></li>
</ul>

<p>May be somehow helpful for others also new to K8s now or after.</p>
