Dynamic route to a specific StateFulSet POD by hostname

4/3/2020

I have a StatefulSet that has 2 replicas. I want to create an endpoint to be able to reach any of this replica, passing it hostname id, and in a way that if I scale it to more replicas, the new pods need to be reachable.

I can do this creating an Ingress like this:

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

With this, a GET on appscode.example.com/0 will be routed to web-0 pod. But, how can I do this in a dynamic way? If I change the replicas to 3, I will need to manually create a new path route to the pod web-2 to be reachable.

-- fabriciols
kubernetes
kubernetes-ingress
kubernetes-statefulset

1 Answer

4/4/2020

You need a program (operator) listening to the Kubernetes API, and patching the ingress resource every time teh number of pods in the statefull set.

Using go:

-- Kartoch
Source: StackOverflow