简体   繁体   English

在 Kubernetes 中分配和维护顺序 Worker-Number 或 NodeId

[英]Assign and maintain sequential Worker-Number or NodeId in Kubernetes

When a Kubernetes Spring-Boot app is launched with 8 instances, the app running in each node needs to fetch sequence number of the pod/container.当 Kubernetes Spring-Boot 应用以 8 个实例启动时,每个节点中运行的应用需要获取 pod/容器的序列号。 There should be no repeating numbers for the pods/containers running the same app.运行相同应用程序的 pods/容器不应有重复的数字。 Assume that a pod runs a single container, and a container runs only one instance of the app.假设一个 pod 运行一个容器,一个容器只运行一个应用程序实例。

There are a few unique identifiers the app can pull from Kubernetes API for each pod such as:应用程序可以从 Kubernetes API 为每个 pod 提取一些唯一标识符,例如:

  • MAC address ( networkInterface.getHardwareAddress() ) MAC 地址( networkInterface.getHardwareAddress()
  • Hostname主机名
  • nodeName ( aks-default-12345677-3 aks-default-12345677-3aks-default-12345677-3
  • targetRef.name ( my-sample-service-sandbox-54k47696e9-abcde ) targetRef.name ( my-sample-service-sandbox-54k47696e9-abcde )
  • targetRef.uid ( aa7k6278-abcd-11ef-e531-kdk8jjkkllmm ) targetRef.uid ( aa7k6278-abcd-11ef-e531-kdk8jjkkllmm )
  • IP address ( 12.34.56.78 ) IP 地址 ( 12.34.56.78 )

But the app getting this information from the API cannot safely generate and assign a unique number to itself within the specified range of pods [0 - Max Node Count-1].但是从 API 获取此信息的应用程序无法在指定的 pod 范围内安全地生成和分配唯一编号 [0 - Max Node Count-1]。 Any reducer step (bitwise &) running over these unique identifiers will eventually repeat the numbers.在这些唯一标识符上运行的任何减速器步骤(按位 &)最终都会重复这些数字。 And communicating with the other pods is an anti-pattern although there are approaches which take a consensus/agreement patterns to accomplish this.与其他 Pod 通信是一种反模式,尽管有些方法采用共识/协议模式来实现这一点。

My Question is: Is there a simple way for Kubernetes to assign a sequential number for each node/container/pod when it's created - possibly in an environment variable in the pod?我的问题是: Kubernetes 是否有一种简单的方法可以在创建时为每个节点/容器/pod 分配一个序列号 - 可能在 pod 的环境变量中? The numbers can to begin with 0 or 1 and should reach uptown the max count of the number of pods.数字可以从 0 或 1 开始,并且应该到达住宅区的最大数量的豆荚数。

Background info and some research: Executing UUID.randomUUID().hashCode() & 7 eight times will get you repeats of numbers between 0 & 7. Ref article with this mistake in createNodeId() .背景信息和一些研究:执行UUID.randomUUID().hashCode() & 7八次将使您重复 0 和 7 之间的数字。参考文章createNodeId()有此错误。 Sample outputs on actual runs of reducer step above.上述减速器步骤实际运行的示例输出。

{0=2, 1=1, 2=0, 3=3, 4=0, 5=1, 6=1, 7=0}
{0=1, 1=0, 2=0, 3=1, 4=3, 5=0, 6=2, 7=1}
{0=1, 1=0, 2=2, 3=1, 4=1, 5=2, 6=0, 7=1}

I've went ahead and executed a 100 Million runs of the above code and found that only 0.24% of the cases has even distribution.我继续执行上述代码的 1 亿次运行,发现只有 0.24% 的案例具有均匀分布。

Uneven Reducers: 99760174 | Even Reducers: 239826

app is launched with 8 instances, the app running in each node needs to fetch sequence number of the pod app以8个实例启动,每个节点运行的app都需要获取pod的序列号

It sounds like you are requesting a stable Pod identity .听起来您正在请求一个稳定的 Pod 身份 If you deploy your Spring Boot app as a StatefulSet instead of as a Deployment , then this identity is a "provided feature" from Kubernetes.如果您将 Spring Boot 应用程序部署为StatefulSet而不是Deployment ,则此身份是 Kubernetes 的“提供的功能”。

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

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