英文:
Kubernetes internal redirects
问题
我正在尝试实现基于无头服务的自定义负载均衡器。目前有效的设置如下:
- worker
- worker-service
- load-balancer
- load-balancer-service
- 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:
- worker
- worker-service
- load-balancer
- load-balancer-service
- 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)和自定义代理,尽管并未完全由我自己实现
对于我们的自定义负载均衡/代理逻辑,我们决定并检查了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
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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论