Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic route to a specific StateFulSet POD by hostname

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.

like image 560
fabriciols Avatar asked Oct 28 '22 00:10

fabriciols


1 Answers

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:

  • watching a resource: https://medium.com/programming-kubernetes/building-stuff-with-the-kubernetes-api-part-4-using-go-b1d0e3c1c899
  • patching a resource: https://dwmkerr.com/patching-kubernetes-resources-in-golang/
like image 128
Kartoch Avatar answered Nov 15 '22 09:11

Kartoch