Peter
Peter

Reputation: 493

Kubernates pod memory limit using metrics

I am trying to fetch the actual and total amount of memory allocated to a pod using API. While I am able to fetch actual memory consumption using metrics server api. How can I fetch total memory assigned to pod using metrics server API?

I am developing a dashboard in which I need to showcase pod memory and cpu.The ui graph has input actual and total amout. I can fetch actual memory used by command kubectl get --raw /apis/metrics.k8s.io/v1beta1/namespaces/default/pods/ but how can I get total memory of pod?

Upvotes: 0

Views: 594

Answers (2)

Nick
Nick

Reputation: 1948

All the available APIs are described at Kubernetes API Guide. It is available for the different K8s versions (just pay attention to URL).

Nick, How can I fetch this value using API ?

At a first glance even /api/v1/namespaces/<namespace>/pods/ will do the job.

Please see my example (I have decided to tet it myself).

$ cat pod.yaml 

apiVersion: v1
kind: Pod
metadata:
  name: server-go-lim 
  ...
spec:
  containers:
  - image: nkolchenko/enea:server_go_latest
    resources:
      ...
      limits:
        memory: 1024Mi   # Here is the Limit
    ...

$ kubectl create -f pod.yaml
pod/server-go-lim created


$ kubectl get --raw /api/v1/namespaces/default/pods/server-go-lim | json_pp 
{
   ...
   "spec" : {
      "containers" : [
         {
            "resources" :  
               "limits" : {
                  "memory" : "1Gi"
               }

As we can see, API returns Limits for pod. Let me know if that's the one you've been looking for.

Upvotes: 1

mandopaloooza
mandopaloooza

Reputation: 149

You simply run kubectl describe pod <pod> and look under .Containers.<container>.Limits

The total amount of memory allocated to a pod is bounded by the memory limit you assign to the pod's containers. https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/#specify-a-memory-request-and-a-memory-limit

To specify a memory request for a Container, include the resources:requests field in the Container's resource manifest. To specify a memory limit, include resources:limits.

If you don't specify a memory limit, the container has no upper bound. https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/#if-you-do-not-specify-a-memory-limit

If you do not specify a memory limit for a Container, one of the following situations applies:

• The Container has no upper bound on the amount of memory it uses. The Container could use all of the memory available on the Node where it is running which in turn could invoke the OOM Killer. Further, in case of an OOM Kill, a container with no resource limits will have a greater chance of being killed.

• The Container is running in a namespace that has a default memory limit, and the Container is automatically assigned the default limit. Cluster administrators can use a LimitRange to specify a default value for the memory limit.

Upvotes: 0

Related Questions