<p>According to <a href="https://kubernetes.github.io/ingress-nginx/examples/rewrite/" rel="nofollow noreferrer">the documentation</a>, if you want to use regular-expression rewriting in your paths, you you be using <code>pathType: ImplementationSpecific</code> rather than <code>pathType: Prefix</code>. In fact, if I submit your manifests locally (against k8s 1.27), I get the warning:</p>
<pre><code>Warning: path /(.*)/api cannot be used with pathType Prefix
Warning: path /(.*)/novnc cannot be used with pathType Prefix
</code></pre>
<p>You want:</p>
<pre><code>spec:
  rules:
  - host: &quot;&quot;
    http:
      paths:
      - backend:
          service:
            name: app-service
            port:
              number: 3033
        path: /(.*)/api
        pathType: ImplementationSpecific
      - backend:
          service:
            name: app-service
            port:
              number: 6080
        path: /(.*)/novnc
        pathType: ImplementationSpecific
</code></pre>
<p>With that one change, the Ingress appears to work as expected. Here are my test manifests (you can find these online at <a href="https://github.com/larsks/devops-example-18709" rel="nofollow noreferrer">https://github.com/larsks/devops-example-18709</a>):</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  labels:
    app: so-example
  name: app-service
spec:
  ports:
  - name: port-6080
    port: 6080
    protocol: TCP
    targetPort: 6080
  - name: port-3033
    port: 3033
    protocol: TCP
    targetPort: 3033
  selector:
    app: so-example
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: so-example
  name: app-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: so-example
  template:
    metadata:
      labels:
        app: so-example
    spec:
      containers:
      - env:
        - name: WHOAMI_PORT_NUMBER
          value: &quot;6080&quot;
        - name: WHOAMI_NAME
          value: novnc
        image: docker.io/traefik/whoami
        name: app-novnc
        ports:
        - containerPort: 6080
          name: novnc
      - env:
        - name: WHOAMI_PORT_NUMBER
          value: &quot;3033&quot;
        - name: WHOAMI_NAME
          value: api
        image: docker.io/traefik/whoami
        name: app-api
        ports:
        - containerPort: 3033
          name: api
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
  labels:
    app: so-example
  name: my-ingress
spec:
  rules:
  - host: &quot;&quot;
    http:
      paths:
      - backend:
          service:
            name: app-service
            port:
              number: 3033
        path: /(.*)/api
        pathType: ImplementationSpecific
      - backend:
          service:
            name: app-service
            port:
              number: 6080
        path: /(.*)/novnc
        pathType: ImplementationSpecific
</code></pre>
<p>With these deployed in my local environment, <a href="http://cluster.example.com/something/api" rel="nofollow noreferrer">http://cluster.example.com/something/api</a> connects to port 3033 in the <code>app-deployment</code> pod, and <a href="http://cluster.example.com/something/novnc" rel="nofollow noreferrer">http://cluster.example.com/something/novnc</a> connects to port 6080.</p>

CC BY-SA 4.0
2023-12-31T01:17:26.640Z
2023-12-31T01:17:26.640Z
larsks
<p>I have a Docker container that is used in Kubernetes, but I've encountered a task to implement routing using the pod name. Here are my Deployment and Service. There's also an Ingress, but it's not working. Pay attention to the replicas. I need each replica (pod) to have individual access like this: http://IP_INGRESS/POD_NAME/api -&gt; PROXY_PASS -&gt; POD_IP:3303 and http://IP_INGRESS/POD_NAME/novnc -&gt; PROXY_PASS -&gt; POD_IP:8060.</p>
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: app-service
  labels:
    app: app-pod
spec:
  selector:
    app: app-pod
  ports:
    - protocol: TCP
      name: port-6080
      port: 6080
      targetPort: 6080
    - protocol: TCP
      name: port-3033
      port: 3033
      targetPort: 3033
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-deployment
  labels:
    app: app-pod
spec:
  replicas: 2
  selector:
    matchLabels:
      app: app-pod
  template:
    metadata:
      labels:
        app: app-pod
    spec:
      containers:
        - name: app-container
          image: xxx/xxxx
          resources:
            limits:
              memory: &quot;2Gi&quot;
          ports:
            - containerPort: 6080
            name: novnc
            - containerPort: 3033
            name: api
      imagePullSecrets:
        - name: dockerhub-secret
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/use-regex: &quot;true&quot;
    kubernetes.io/ingress.class: &quot;nginx&quot;
spec:
  rules:
    - host: &quot;&quot;
      http:
        paths:
          - path: /(.*)/api
            pathType: Prefix
            backend:
              service:
                name: app-service
                port:
                  number: 3033
          - path: /(.*)/novnc
            pathType: Prefix
            backend:
              service:
                name: app-service
                port:
                  number: 6080
</code></pre>

CC BY-SA 4.0
2023-12-30T23:02:46.510Z
This is probably a copy/paste error, but note that your Deployment contains invalid YAML (`name` in each of your `ports` is mis-indented).
larsks
<kubernetes>
Kubernetes Ingress route path pod name
user2812527
# Kubernetes Ingress route path pod name

(Asked by: username_0 on 2023-12-28)

I have a Docker container that is used in Kubernetes, but I've encountered a task to implement routing using the pod name. Here are my Deployment and Service. There's also an Ingress, but it's not working. Pay attention to the replicas. I need each replica (pod) to have individual access like this: http://IP_INGRESS/POD_NAME/api -> PROXY_PASS -> POD_IP:3303 and http://IP_INGRESS/POD_NAME/novnc -> PROXY_PASS -> POD_IP:8060.
apiVersion: v1
kind: Service
metadata:
  name: app-service
  labels:
    app: app-pod
spec:
  selector:
    app: app-pod
  ports:
    - protocol: TCP
      name: port-6080
      port: 6080
      targetPort: 6080
    - protocol: TCP
      name: port-3033
      port: 3033
      targetPort: 3033
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: app-deployment
  labels:
    app: app-pod
spec:
  replicas: 2
  selector:
    matchLabels:
      app: app-pod
  template:
    metadata:
      labels:
        app: app-pod
    spec:
      containers:
        - name: app-container
          image: xxx/xxxx
          resources:
            limits:
              memory: "2Gi"
          ports:
            - containerPort: 6080
            name: novnc
            - containerPort: 3033
            name: api
      imagePullSecrets:
        - name: dockerhub-secret
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: my-ingress
  namespace: default
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/use-regex: "true"
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
    - host: ""
      http:
        paths:
          - path: /(.*)/api
            pathType: Prefix
            backend:
              service:
                name: app-service
                port:
                  number: 3033
          - path: /(.*)/novnc
            pathType: Prefix
            backend:
              service:
                name: app-service
                port:
                  number: 6080

**Tags:** kubernetes

> **Comments:**
> **username_1**: This is probably a copy/paste error, but note that your Deployment contains invalid YAML (`name` in each of your `ports` is mis-indented).  

---

## 1 answers

### Answer by username_1 on 2023-12-31. Score: 0

According to the documentation, if you want to use regular-expression rewriting in your paths, you you be using pathType: ImplementationSpecific rather than pathType: Prefix. In fact, if I submit your manifests locally (against k8s 1.27), I get the warning:
Warning: path /(.*)/api cannot be used with pathType Prefix
Warning: path /(.*)/novnc cannot be used with pathType Prefix

You want:
spec:
  rules:
  - host: ""
    http:
      paths:
      - backend:
          service:
            name: app-service
            port:
              number: 3033
        path: /(.*)/api
        pathType: ImplementationSpecific
      - backend:
          service:
            name: app-service
            port:
              number: 6080
        path: /(.*)/novnc
        pathType: ImplementationSpecific

With that one change, the Ingress appears to work as expected. Here are my test manifests (you can find these online at https://github.com/username_1/devops-example-18709):
apiVersion: v1
kind: Service
metadata:
  labels:
    app: so-example
  name: app-service
spec:
  ports:
  - name: port-6080
    port: 6080
    protocol: TCP
    targetPort: 6080
  - name: port-3033
    port: 3033
    protocol: TCP
    targetPort: 3033
  selector:
    app: so-example
  type: NodePort
---
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: so-example
  name: app-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: so-example
  template:
    metadata:
      labels:
        app: so-example
    spec:
      containers:
      - env:
        - name: WHOAMI_PORT_NUMBER
          value: "6080"
        - name: WHOAMI_NAME
          value: novnc
        image: docker.io/traefik/whoami
        name: app-novnc
        ports:
        - containerPort: 6080
          name: novnc
      - env:
        - name: WHOAMI_PORT_NUMBER
          value: "3033"
        - name: WHOAMI_NAME
          value: api
        image: docker.io/traefik/whoami
        name: app-api
        ports:
        - containerPort: 3033
          name: api
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/rewrite-target: /$1
    nginx.ingress.kubernetes.io/use-regex: "true"
  labels:
    app: so-example
  name: my-ingress
spec:
  rules:
  - host: ""
    http:
      paths:
      - backend:
          service:
            name: app-service
            port:
              number: 3033
        path: /(.*)/api
        pathType: ImplementationSpecific
      - backend:
          service:
            name: app-service
            port:
              number: 6080
        path: /(.*)/novnc
        pathType: ImplementationSpecific

With these deployed in my local environment, http://cluster.example.com/something/api connects to port 3033 in the app-deployment pod, and http://cluster.example.com/something/novnc connects to port 6080.


---

