Home > Net >  Kubernetes pod memory usage programatically
Kubernetes pod memory usage programatically

Time:10-26

I have a use case where in my jax-rs rest api exposed will do a memory intensive operation like generating a pdf and sending it as response. I want to check pod memory usage before start doing the business logic and if the current memory usage is > 50% , i would like to send a error response to user asking him to try again later. how do i check kubernetes pod memory usage inside my rest api. Is it even possible?

My current code somewhat looks like below.

@Path("/doSomeMemoryStuff")
@Produces("text/plain")
public Response doStuff(){

     int memoryUsage = getPodCurrentMemoryUsage();  //Get pod current memory usage
     if( memoryUsage <= 50 ) {

          //do Some memory intensive operation
      }
      else{
           //memory usage is more than 50 % , return error
           return Response.ok("Try again later.").build();
          }
  }         

How do I find out the current memory usage of pod from my rest api. Thanks in advance.

CodePudding user response:

Strictly, we could paraphrase your statement as "Kubernetes pod memory usage automatically" in the sense that if you have processes that, from time to time, consume a lot of resources that need to be freed, you can use Vertical Pod Autoscaler.

Some interesting references:

  1. https://github.com/kubernetes/autoscaler/tree/master/vertical-pod-autoscaler
  2. https://docs.aws.amazon.com/eks/latest/userguide/vertical-pod-autoscaler.html
  3. https://cloud.google.com/kubernetes-engine/docs/concepts/verticalpodautoscaler

The use of VPA is appropriate when resource requirements are very different for one process or another.

If this is not the case (more or less all requests will use a known amount of resources) or even if you use VPA, it is recommended that you limit your services to not accept requests if they are working on an expensive operation, Kubernetes will automatically increase or decrease the number of Pods depending on the load and your users will receive a 503 error which is precisely to indicate that they cannot be served now and should try again later.

That is to say:

  1. do not use VPA if not strictly necessary.
  2. configure your deployment with an adequate number of pods.
  3. restrict your services within the pods to a single concurrent request (or as many as fit in your resource configuration).
  4. don't do anything special, if your system has reached the limit you have set, just let the users receive a 503 (your user interface will translate the error as "Try again later").
  • Related