Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. This replica count is dynamic in nature

Problem: 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?

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.

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

also if this is crazy, I wont take offense. In that case I'm asking for a friend

like image 848
stackoverflow Avatar asked Dec 13 '25 12:12

stackoverflow


1 Answers

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?

You need to query the Kubernetes API server for info about the number of replicas for a specific Deployment. You can do this with kubernetes-client java.

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.

Sounds like you want consistent number of Pods all the time, e.g. avoiding Rolling Deployment? 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).

StatefulSet

When you want at most X number of replicas you should consider using StatefulSet as its behavior differ from Deployment when e.g. a Node becomes unreachable. Deployment has the behavior, at least X number of replicas.

like image 85
Jonas Avatar answered Dec 15 '25 09:12

Jonas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!