0

How can my containerized app determine its own current resource utilization, as well as the maximum limit allocated by Kubernetes? Is there an API to get this info from cAdvisor and/or Kubelet?

For example, my container is allowed to use maximum 1 core, and it's currently consuming 800 millicores. In this situation, I want to drop/reject all incoming requests that are marked as "low priority".

-How can I see my resource utilization & limit from within my container?

Note that this assumes auto-scaling is not available, e.g. when cluster resources are exhausted, or our app is not allowed to auto-scale (further).

HannesM
  • 55
  • 1
  • 11
  • You want your service not to overload when the request volume is high. Why not use horizontal pod autoscaler based on CPU usage? Why specifically want the container to stop handling low priority requests when the system is overloaded. The horizontal pod autoscaling is provided by kubernetes out of the box. – Malathi May 28 '19 at 14:53
  • As long as auto-scaling is in effect, we're fine. It's when we have hit the auto-scale limit(s) that we will overload, and need to start rejecting e.g. low-prio requests. Thanks for asking, I should have clarified this in my question above. – HannesM May 28 '19 at 15:55

1 Answers1

1

You can use the Kubernetes Downward Api to fetch the limits and requests. The syntax is:

volumes:
    - name: podinfo
      downwardAPI:
        items:
          - path: "cpu_limit"
            resourceFieldRef:
              containerName: client-container
              resource: limits.cpu
              divisor: 1m
Alassane Ndiaye
  • 4,427
  • 1
  • 10
  • 19
  • Thanks! Also, how do I get my _current_ CPU usage? (which combined with my limit should give my CPU %) – HannesM May 29 '19 at 05:54
  • 1
    I don't know any straightforward way to get the info. The metrics you are looking for are collected by the cadvisor and forwarded to the metrics-server. To get that information from inside a container, I would fetch it using the metrics.k8s.io API. Here is some [documentation](https://kubernetes.io/docs/tasks/debug-application-cluster/resource-usage-monitoring/) for it. – Alassane Ndiaye May 29 '19 at 13:13
  • Thanks a lot for this tip. I will follow up on that and see how it goes. – HannesM Jun 03 '19 at 06:43