Kubernetes routing to specific pod in function of a param in url

10/23/2018

The need I have just looks like this stuff :

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: http
spec:
  serviceName: "nginx-set"
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: gcr.io/google_containers/nginx-slim:0.8
        ports:
        - containerPort: 80
          name: http
----
apiVersion: v1
kind: Service
metadata:
  name: nginx-set
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    name: http
  clusterIP: None
  selector:
    app: nginx

Here is the interesting part :

apiVersion: voyager.appscode.com/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: default
spec:
  rules:
  - host: appscode.example.com
    http:
      paths:
      - path: '/testPath'
        backend:
          hostNames:
          - web-0
          serviceName: nginx-set #! There is no extra service. This
          servicePort: '80'      # is the Statefulset's Headless Service

I'm able to target a specific pod because of setting the hostName in function of the url.

Now I'd like to know if it's possible in kubernetes to create a rule like that

apiVersion: voyager.appscode.com/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: default
spec:
  rules:
  - host: appscode.example.com
    http:
      paths:
      - path: '/connect/(\d+)'
        backend:
          hostNames:
          - web-(result of regex match with \d+)
          serviceName: nginx-set #! There is no extra service. This
          servicePort: '80'      # is the Statefulset's Headless Service

or if I have to wrote a rule for each pod ?

-- Quentin
kubernetes
kubernetes-ingress

1 Answer

10/23/2018

Sorry that isn't possible, the best solution is to create multiple paths, each one referencing one pod:

apiVersion: voyager.appscode.com/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  namespace: default
spec:
  rules:
  - host: appscode.example.com
    http:
      paths:
      - path: '/connect/0'
        backend:
          hostNames:
          - web-0
          serviceName: nginx-set
          servicePort: '80'
      - path: '/connect/1'
        backend:
          hostNames:
          - web-1
          serviceName: nginx-set
          servicePort: '80'
-- Ignacio Millán
Source: StackOverflow