简体   繁体   English

如何在 kubernetes 中划分 n 个 pod 之间的空间

[英]How to partitions space between n pods in kubernetes

We are using Kubernetes and we need to do "Smart partitioning" of data.我们正在使用 Kubernetes,我们需要对数据进行“智能分区”。 We want to split the space between 1 to 1000 between n running Pods, And each pod should know which part of the space is his to handle (for pooling partitioned tasks).我们希望在 n 个正在运行的 Pod 之间分割 1 到 1000 之间的空间,并且每个 pod 都应该知道空间的哪一部分是他要处理的(用于池化分区任务)。

So for example, if we have 1 pod he will handle the whole space from 1-1000.例如,如果我们有 1 个 pod,他将处理 1-1000 的整个空间。

When we scale out to 3 pods, each of them will get the same share.当我们扩展到 3 个 Pod 时,它们每个都将获得相同的份额。

Pod 1 - will handle 1-333 Pod 1 - 将处理 1-333

Pod 2 - 334-667吊舱 2 - 334-667

Pod 3 667-1000吊舱 3 667-1000

Right now the best way that we find to handle this issue is to create a Stateful-set, that pooling the number of running pods and his instance number and decide which part of the space he needs to handle.现在我们发现处理这个问题的最好方法是创建一个有状态集,它汇集了正在运行的 pod 的数量和他的实例数,并决定他需要处理哪一部分空间。 Is there a smarter/built-in way in Kubernetes to partition the space between nodes in this manner? Kubernetes 中是否有更智能/内置的方式来以这种方式划分节点之间的空间?

Service fabric has this feature built-in. Service Fabric 内置了此功能。

There are NO native tools for scaling at the partition level in K8s yet.K8s中还没有用于分区级别扩展的原生工具。

Only custom solutions similar to what you have came up with in your original post.只有与您在原始帖子中提出的类似的自定义解决方案。

Provide another customized way for doing this for your reference.提供另一种定制的方法供您参考。 Based on this tech blog of Airbnb基于Airbnb 的这个技术博客

Given the list of pods and their names, each pod is able to deterministically calculate a list of partitions that it should work on.给定 pod 列表及其名称,每个 pod 都能够确定性地计算它应该处理的分区列表。 When we add or remove pods from the ReplicaSet, the pods will simply pick up the change, and work on the new set of partitions instead当我们从 ReplicaSet 添加或删除 Pod 时,Pod 将简单地获取更改,并改为处理新的分区集

How do they do is based on the their repo .他们如何做取决于他们的回购 I summarized the key components here (Note: the repo is written in Java).我在这里总结了关键组件(注意:repo 是用 Java 编写的)。

  1. Get how many pods running in the k8s namespace, and sort by pod name ( code ).获取在 k8s 命名空间中运行的 pod 数量,并按 pod 名称( 代码)排序。 Code snippet代码片段
String podName = System.getenv("K8S_POD_NAME");
String namespace = System.getenv("K8S_NAMESPACE");
NamespacedKubernetesClient namespacedClient = kubernetesClient.inNamespace(namespace);
ReplicaSet replicaSet;

// see above code link to know how to get activePods, remove it here because it is too long

int podIndex = activePods.indexOf(podName);
int numPods = activePods.size();

  1. Every time you call the above code, you will have deterministic list of podIndex and numPods .每次调用上述代码时,都会得到podIndexnumPods的确定性列表。 Then, using this information to calculate the range this pod is responsible for然后,使用这些信息来计算这个 pod 负责的范围
List<Integer> partitions = new ArrayList<>();
int split = spaceRange / numPods;
int start = podIndex * split;
int end = (podIndex == numPods - 1) ? spaceRange - 1 : ((podIndex + 1) * split) - 1;
for (int i = start; i <= end; i++) {
  partitions.add(i);
}
  1. Since the number of pods will be changed anytime, you may need a executorService.scheduleWithFixedDelay to periodically update the list as here由于 pod 的数量会随时更改,您可能需要一个executorService.scheduleWithFixedDelay来定期更新列表,如下所示
executorService.scheduleWithFixedDelay(this::updatePartitions, 0, 30, TimeUnit.SECONDS);

This approach is not the best, since if you set scheduleWithFixedDelay with 30 seconds, any pod change won't be captured within 30 seconds.这种方法不是最好的,因为如果您将 scheduleWithFixedDelay 设置为 30 秒,则不会在 30 秒内捕获任何 pod 更改。 Also, it is possible in a short period of time, two pods may be responsible for the same space, and you need to handle this special case in your business logics as Airbnb tech blog does.此外,有可能在短时间内,两个 Pod 可能负责同一个空间,您需要像 Airbnb 技术博客那样在业务逻辑中处理这种特殊情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM