Search code examples
kuberneteshttp-redirectload-balancingkubernetes-ingress

Kubernetes internal redirects


I'm trying to implement a custom load balancer based on headless service. What I found working (for now) was the following setup:

  1. worker
  2. worker-service
  3. load-balancer
  4. load-balancer-service
  5. headless-service

Client calls load balancer which calls headless service to check the available pods. Load balancer picks one pod and calls it.

This is fine, but it means that the LB would be a proxy as well, which I'd like to avoid. I've tried redirect but then the ip:port address is returned back to the client - and the pod is obviously not accessible outside from cluster (I've tried simple ingress as well).

Do you Folks have any hints & tips & tricks how to do such thing?

Thanks! Mateusz


Solution

  • After some more time with Kubernetes I managed to make use of headless services and custom proxy but not fully implemented by myself :)

    For our custom load balancing / proxy logic we decided and checked API Gateway project (Spring) which happens do the proxy and forwarding for us! Only thing we configure is the dynamic routes. Example:

    @Bean
        public RouteLocator workerRoutingService(RouteLocatorBuilder builder) {
            var routes = builder.routes();
    
            routes.route(r -> r
                    .path("/api/worker/{resourceId}")
                    .filters(f -> f.changeRequestUri(exchange -> {
                        String resourceId = ServerWebExchangeUtils.getUriTemplateVariables(exchange).get("resourceId");
                        return workerService.getWorkerEndpointByResource(resourceId);
                    }))
                    .uri("http://fallback/")
            );
    
    
            return routes.build();
        }