<p><strong>What I'm trying to solve :</strong> Have a Java microservice be aware of total number of Replicas. This replica count is dynamic in nature</p>
<p><strong>Problem:</strong> Kubernetes downward API has limited metadata that doesnt include this information. Is there a way to qausi-query a kubectl-like command natively from a container?</p>
<p><strong>Why am I trying to do this:</strong> In relation to Kafka, new replica will cause a rebalance. Looking to mitigate rebalancing when new containers come online/offline with additional business logic.</p>
<p><strong>Goal:</strong> Create an arbiter java-service that detects replica count for deployment <em>xyz</em> and orchestrates corresponding pods to yield on Kafka connection until criteria is met</p>
<p><em>also if this is crazy, I wont take offense. In that case I'm asking for a friend</em></p>

<blockquote>
<p>Kubernetes downward API has limited metadata that doesnt include this information. Is there a way to qausi-query a kubectl-like command natively from a container?</p>
</blockquote>
<p>You need to query the Kubernetes API server for info about the number of replicas for a specific Deployment. You can do this with <a href="https://github.com/kubernetes-client/java" rel="nofollow noreferrer">kubernetes-client java</a>.</p>
<blockquote>
<p>Why am I trying to do this: In relation to Kafka, new replica will cause a rebalance. Looking to mitigate rebalancing when new containers come online/offline with additional business logic.</p>
</blockquote>
<p>Sounds like you want <em>consistent</em> number of Pods all the time, e.g. <strong>avoiding Rolling Deployment</strong>? In your Deployment, you can set <code>strategy:</code> to <code>type: Recreate</code> - 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).</p>
<h2>StatefulSet</h2>
<p>When you want <strong>at most X number of replicas</strong> you should consider using <code>StatefulSet</code> as its behavior differ from <code>Deployment</code> when e.g. a Node becomes unreachable. <code>Deployment</code> has the behavior, <strong>at least X number of replicas</strong>.</p>
