kubernetes - route ingress traffic to specific pod for some paths

12/4/2020

I have multiple pods, that scale up and down automatically.

I am using an ingress as entry point. I need to route external traffic to a specific pod base on some conditions (lets say path). At the point the request is made I am sure the specific pod is up.

For example lets say I have domain someTest.com, that normally routes traffic to pod 1, 2 and 3 (lets say I identify them by internal ips - 192.168.1.10, 192.168.1.11 and 192.168.1.13).

When I call someTest.com/specialRequest/12, I need to route the traffic to 192.168.1.12, when I call someTest.com/specialRequest/13, I want to route traffic to 192.168.1.13. For normal cases (someTest.com/normalRequest) I just want to do the lb do his epic job normally.

If pods scale up and 192.168.1.14 appears, I need to be able to call someTest.com/specialRequest/14 and be routed to the mentioned pod.

Is there anyway I can achieve this?

-- zozo
kubernetes
kubernetes-ingress
nginx-ingress
routes

3 Answers

12/16/2020

would it be feasible to create another application that can get the path and target the pod directly via a pattern in the naming convention? for example

${podnamePrefix+param}.${service name}.${namespace}.svc.cluster.local

-- Jay Garzon
Source: StackOverflow

12/4/2020

Yes, you can easily achieve this using Kubernetes Ingress. Here is a sample code that might help:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: app-ingress
  spec:
    rules:
    - host: YourHostName.com
      http:
        paths:
        - path: /
          backend:
            serviceName: Service1
            servicePort: 8000
        - path: /api
          backend:
            serviceName: Service2
            servicePort: 8080
        - path: /admin
          backend:
            serviceName: Service3
            servicePort: 80

Please not that the ingress rules have serviceNames and not pod names, so you will have to create services for your pods. Here is an example for a service which exposes nginx as a service in Kubernetes:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  labels:
    io.kompose.service: nginx
spec:
  ports:
  - name: "80"
    port: 80
    targetPort: 80
  selector:
    io.kompose.service: nginx
-- Vikrant
Source: StackOverflow

12/13/2020

I am not aware of built-in functionality to implement this (if this is what your really want). You can achieve this by building your own operator for Kubernetes. Your operator may provision a Pod+Ingress combo which will do exactly what you want - forward your traffic to a single pod, or you can provision 2 pods and 1 ingress to achive HA setup.

Depending on the Ingress you are using, it also may be possible to group multiple ingress resources under the same load balancer.

Here is a brief diagram of how this could look like. example operator

-- gp42
Source: StackOverflow