Kubernetes内部重定向

huangapple go评论59阅读模式
英文:

Kubernetes internal redirects

问题

我正在尝试实现基于无头服务的自定义负载均衡器。目前有效的设置如下:

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

客户端调用负载均衡器,负载均衡器调用无头服务以检查可用的 Pod。负载均衡器选择一个 Pod 并调用它。

这是可以的,但这意味着 LB 也将充当代理,这是我想避免的。我尝试过重定向,但然后 IP:端口地址被返回给客户端 - 显然无法从集群外部访问 Pod(我也尝试了简单的 Ingress)。

您有什么提示、技巧或诀窍来实现这样的功能吗?

谢谢!
Mateusz

英文:

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

答案1

得分: 0

在使用Kubernetes更多时间后,我成功地使用了无头服务(headless services)和自定义代理,尽管并未完全由我自己实现 Kubernetes内部重定向

对于我们的自定义负载均衡/代理逻辑,我们决定并检查了API Gateway项目(Spring),它会为我们执行代理和转发!我们唯一需要配置的是动态路由。示例:

@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();
}

请注意,上述示例中的代码片段是Java Spring项目中用于配置路由的部分。

英文:

After some more time with Kubernetes I managed to make use of headless services and custom proxy but not fully implemented by myself Kubernetes内部重定向

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();
    }

huangapple
  • 本文由 发表于 2023年7月27日 20:39:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76779827.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定