<p>I want to config the ingress to work with my domain name.</p>
<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: app-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  tls:
  - hosts:
    - example.com
    secretName: app-tls
  rules:
  - host: example.com
    http:
      paths:
      - path: /my-api1(/|$)(.*)
        backend:
          serviceName: app1
          servicePort: 80
      - path: /my-api2
        backend:
          serviceName: app2
          servicePort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: my-api
spec:
  selector:
    app: my-api
  ports:
  - name: app1
    port: 3000
    targetPort: 80
  - name: app2
    port: 4000
    targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: user-api
spec:
  selector:
    matchLabels:
      app: user-api
  template:
    metadata:
      labels:
        app: user-api
    spec:
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      containers:
      - name: app1
        image: XXX
        ports:
        - name: app1
          containerPort: 3000
      - name: app2
        image: XXX
        ports:
        - name: app2
          containerPort: 4000
</code></pre>
<p>I can reach the app1 service by serverIP:3000(example. <code>172.16.111.211:3000/my-api1</code>). But remotely it always return the 503 status code(<code>curl https://example.com/my-api1</code>).</p>
<pre><code># kubectl describe ingress app-ingress
Name:             app-ingress
Namespace:        default
Address:          serverIP
Default backend:  default-http-backend:80 (&lt;error: endpoints &quot;default-http-backend&quot; not found&gt;)
TLS:
  app-tls terminates example.com
Rules:
  Host                Path  Backends
  ----                ----  --------
  example.com
                      /my-api1(/|$)(.*)   app1:80 (&lt;error: endpoints &quot;app1&quot; not found&gt;)
                      /my-api2            app2:80 (&lt;error: endpoints &quot;app2&quot; not found&gt;)
</code></pre>

ccd
<ol>
<li>First thing is your service name is not matching, you have created a service with name my-api but in ingress you have referred it as app1 and app2 which is not available.</li>
<li>Second error is selector label between your deployment and service are not matching. Deployment created with label user-api but in service selector is mentioned as my-api.</li>
<li>If you need 80 port for both the application then you have to create two different services and refer that in your ingress.</li>
</ol>
<p>Below should work for your requirement.</p>
<pre><code>apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: app-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
spec:
  tls:
  - hosts:
    - example.com
    secretName: app-tls
  rules:
  - host: example.com
    http:
      paths:
      - path: /my-api1(/|$)(.*)
        backend:
          serviceName: app1
          servicePort: 80
      - path: /my-api2
        backend:
          serviceName: app2
          servicePort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: app1
spec:
  selector:
    app: user-api
  ports:
  - name: app1
    port: 3000
    targetPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: app2
spec:
  selector:
    app: user-api
  ports:
  - name: app2
    port: 4000
    targetPort: 80
----

apiVersion: apps/v1
kind: Deployment
metadata:
  name: user-api
spec:
  selector:
    matchLabels:
      app: user-api
  template:
    metadata:
      labels:
        app: user-api
    spec:
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      containers:
      - name: app1
        image: XXX
        ports:
        - name: app1
          containerPort: 3000
      - name: app2
        image: XXX
        ports:
        - name: app2
          containerPort: 4000 
</code></pre>
