在 Kubernetes 中分配和维护连续的 Worker-Number 或 NodeId

Assign and maintain sequential Worker-Number or NodeId in Kubernetes

当 Kubernetes Spring-Boot 应用启动时有 8 个实例,每个节点中的应用 运行 需要获取 pod/container 的序列号。 pods/containers 运行 同一个应用程序不应有重复的数字。假设一个 pod 运行一个容器,一个容器只运行一个应用实例。

应用程序可以从 Kubernetes API 为每个 pod 提取一些唯一标识符,例如:

但是从 API 获取此信息的应用程序无法在 pods [0 - Max Node Count-1] 的指定范围内安全地生成并分配给自己的唯一编号。这些唯一标识符上的任何减速器步骤(按位 &)运行 最终都会重复这些数字。与其他 pods 通信是一种反模式,尽管有些方法采用 consensus/agreement 模式来实现这一点。

我的问题是: 是否有一种简单的方法让 Kubernetes 在创建时为每个 node/container/pod 分配一个序列号 - 可能在 pod 的环境变量中? 数字可以以 0 或1 并且应该达到住宅区的最大数量 pods.

背景信息和一些研究: 执行 UUID.randomUUID().hashCode() & 7 八次将使您重复 0 和 7 之间的数字。参考 article createNodeId() 中的这个错误。 上述减速器步骤实际运行的示例输出。

{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}

我继续 executed a 100 Million runs 上面的代码,发现只有 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

听起来你在请求一个stable Pod identity. If you deploy your Spring Boot app as a StatefulSet instead of as a Deployment,那么这个身份是Kubernetes的“提供的功能”。