简体   繁体   English

Kubernetes:如何从 Java 微服务中检测总副本

[英]Kubernetes : How to detect total replicas from within a Java micro service

What I'm trying to solve: Have a Java microservice be aware of total number of Replicas.我要解决的问题:让 Java 微服务了解副本的总数。 This replica count is dynamic in nature此副本计数本质上是动态的

Problem: Kubernetes downward API has limited metadata that doesnt include this information.问题: Kubernetes 向下 API 具有不包含此信息的有限元数据。 Is there a way to qausi-query a kubectl-like command natively from a container?有没有办法从容器中本地查询类似 kubectl 的命令?

Why am I trying to do this: In relation to Kafka, new replica will cause a rebalance.我为什么要这样做:对于 Kafka,新副本会导致重新平衡。 Looking to mitigate rebalancing when new containers come online/offline with additional business logic.希望在新容器上线/下线时通过额外的业务逻辑来减轻重新平衡。

Goal: Create an arbiter java-service that detects replica count for deployment xyz and orchestrates corresponding pods to yield on Kafka connection until criteria is met目标:创建一个仲裁器 java 服务,检测部署xyz的副本计数并编排相应的 pod 以在 Kafka 连接上产生,直到满足条件

also if this is crazy, I wont take offense.如果这很疯狂,我也不会生气。 In that case I'm asking for a friend在那种情况下,我要找一个朋友

Kubernetes downward API has limited metadata that doesnt include this information. Kubernetes 向下 API 具有不包含此信息的有限元数据。 Is there a way to qausi-query a kubectl-like command natively from a container?有没有办法从容器中本地查询类似 kubectl 的命令?

You need to query the Kubernetes API server for info about the number of replicas for a specific Deployment.您需要查询 Kubernetes API 服务器以获取有关特定部署的副本数量的信息。 You can do this with kubernetes-client java .您可以使用kubernetes-client java来做到这一点。

Why am I trying to do this: In relation to Kafka, new replica will cause a rebalance.我为什么要这样做:对于 Kafka,新副本会导致重新平衡。 Looking to mitigate rebalancing when new containers come online/offline with additional business logic.希望在新容器上线/下线时通过额外的业务逻辑来减轻重新平衡。

Sounds like you want consistent number of Pods all the time, eg avoiding Rolling Deployment ?听起来您一直想要一致数量的 Pod,例如避免滚动部署 In your Deployment, you can set strategy: to type: Recreate - then the current Pod will be removed first, and then the new will be created - so at most 1 is running at the same time (or the same number as replicas).在您的 Deployment 中,您可以设置strategy: to type: Recreate - 然后将首先删除当前 Pod,然后创建新 Pod - 因此最多同时运行 1 个(或与副本相同的数量)。

StatefulSet有状态集

When you want at most X number of replicas you should consider using StatefulSet as its behavior differ from Deployment when eg a Node becomes unreachable.当您最多需要 X 个副本时,您应该考虑使用StatefulSet ,因为它的行为与Deployment不同,例如当节点变得无法访问时。 Deployment has the behavior, at least X number of replicas . Deployment具有行为,至少 X number of replicas

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

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