convert kubernetes state metrics curl response units to megabytes

4/17/2020

i need to get kube state metrics with Mi, it default comes with Ki. can any one please help me

[root@dte-dev-1-bizsvck8s-mst harsha]# curl http://<server IP>:8088/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/hello-kubernetes-65bc74d4b9-qp9dc
{
  "kind": "PodMetrics",
  "apiVersion": "metrics.k8s.io/v1beta1",
  "metadata": {
    "name": "hello-kubernetes-65bc74d4b9-qp9dc",
    "namespace": "default",
    "selfLink": "/apis/metrics.k8s.io/v1beta1/namespaces/default/pods/hello-kubernetes-65bc74d4b9-qp9dc",
    "creationTimestamp": "2020-04-17T12:31:59Z"
  },
  "timestamp": "2020-04-17T12:31:26Z",
  "window": "30s",
  "containers": [
    {
      "name": "hello-kubernetes",
      "usage": {
        "cpu": "0",
        "memory": "20552Ki"
      }
    }
  ]

i want to get memory usage from Mi (megabytes) not the Ki. Please help me!

-- harsha rachith
curl
devops
kubernetes
linux

1 Answer

4/20/2020

This unit is hardcoded in official kube-state-metrics code which shouldn't be changed. For example node metrics - especially memory usage is in Megabytes unit not Kilobytes.

To get memory usage of specific pod in Megabytes units simply execute:

kubectl top pod --namespace example-app

NAME                              CPU(cores)   MEMORY(bytes)   
app-deployment-76bf4969df-65wmd     12m           1Mi             
app-deployment-76bf4969df-mmqvt     16m           1Mi    

The kubectl top command returns current CPU and memory usage for a cluster’s pods or nodes, or for a particular pod or node if specified.

You can also convert received value:

1 KB = 0.001 MB (in decimal),
1 KB = 0.0009765625 MB (in binary)

Take a look: kube-state-metrics-monitoring.

-- MaggieO
Source: StackOverflow