简体   繁体   English

分布式系统的唯一标识符:Kuberbetes stateful set

[英]Unique identifiers for distributed systems: Kuberbetes stateful set

I am using springboot microservices and Kube.netes for our application development and deployments.我正在使用 springboot 微服务和 Kube.netes 进行应用程序开发和部署。 We are using stateful sets for deployments.我们正在使用有状态集进行部署。 We have a need to generate unique identifiers that can be used in the services across pods deployed on nodes across clusters.我们需要生成唯一标识符,这些标识符可用于跨集群部署在节点上的跨 Pod 的服务。

I am using twitter snowflake format for the generation of unique Id with K8s stateful sets.我正在使用 twitter 雪花格式来生成具有 K8s 状态集的唯一 ID。 Format: 64 bit java long: 1 bit reserved + 41 bit timestamp + 10 bits nodeId + 12 bits counter格式:64 bit java long:1 bit reserved + 41 bit timestamp + 10 bits nodeId + 12 bits counter

Its a simple problem to solve if we are talking about say 5 replicas deployed on the same cluster (across different nodes).如果我们谈论的是部署在同一个集群(跨不同节点)上的 5 个副本,这是一个很容易解决的问题。 I could easily generate unique orderId that will serve as nodeId in the format above.我可以轻松生成唯一的 orderId,它将作为上述格式的 nodeId。 But the moment I deploy the pods on multiple clusters, it duplicates the nodeIds since it generated similar orders across the clusters.但是当我在多个集群上部署 pod 时,它会复制 nodeIds,因为它在集群中生成了相似的订单。

example: 5 replicas in a single cluster create pods like: service-0, service-1, service-2, service-3 service-4示例:单个集群中的 5 个副本创建如下 Pod:service-0、service-1、service-2、service-3 service-4

I could get the unique pod numbers (0,1,2,3..) that will serve as nodeId in my uniqueId generator and hence I could derive a unique identifier.我可以获得唯一的 pod 编号 (0,1,2,3..),它们将在我的 uniqueId 生成器中用作 nodeId,因此我可以派生一个唯一的标识符。 But if I deploy the application (via stateful set) to 2 clusters, it will generate similar services in the second cluster too:但是如果我将应用程序(通过有状态集)部署到 2 个集群,它也会在第二个集群中生成类似的服务:

service-0, service-1, service-2, service-3 service-4服务 0、服务 1、服务 2、服务 3 服务 4

now if my GLB routes my calls randomly across the 10 pods in the 2 cluster, the probability of generating duplicate Unique Id (for calls made during the same milisecond) is pretty high and hence my solution might not work.现在,如果我的 GLB 将我的呼叫随机路由到 2 集群中的 10 个 pod,则生成重复的唯一 ID(对于在同一毫秒内进行的呼叫)的可能性非常高,因此我的解决方案可能不起作用。

Need inputs on how to solve the problem.需要关于如何解决问题的意见。 Any help will be highly appreciated任何帮助将不胜感激

Hi WhizKid I can give a work around for this try to include your cluster name included in the naming convention (eg: cluster1-service-0) instead of just going with nodeID.您好WhizKid我可以尝试将您的集群名称包括在命名约定中(例如:cluster1-service-0),而不是仅仅使用 nodeID。 Since we have generated unique IDs for each cluster now even if they lie on the same nodes or same servers it won't become a problem.由于我们现在已经为每个集群生成了唯一的 ID,即使它们位于相同的节点或相同的服务器上,也不会成为问题。 Hope this helps you.希望这对你有帮助。

Thanks for all the suggestions.感谢所有的建议。 We figured out a way to retrieve the cluster Id from the code.我们想出了一种从代码中检索集群 ID 的方法。 We are setting the cluster Id in the Jules file (since using jules for deployment to Kube.netes), and then using the cluster variable in the deployment.我们在 Jules 文件中设置集群 ID(因为使用 jules 部署到 Kube.netes),然后在部署中使用集群变量。 That way we are able to retrieve the cluster name in the code.这样我们就可以在代码中检索集群名称。 something like this:是这样的:

Deployment.yml:
......
          env:
            - name: INFO_KUBE_ENV
              value: ${environment}
            - name: INFO_KUBE_CLUSTER
              value: ${cluster}

and then using the env variable INFO_KUBE_CLUSTER in the code like this:然后在代码中使用环境变量 INFO_KUBE_CLUSTER,如下所示:

System.getenv("INFO_KUBE_CLUSTER")

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

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