Leonardo
Leonardo

Reputation: 11387

Kubernetes -> Memory consumption per namespace

Is there a way to get the memory consumption per namespace on Kubernetes?

Upvotes: 4

Views: 4707

Answers (3)

adamency
adamency

Reputation: 1500

No need for a ResourceQuota to get this metric. However it is not provided by an ad-hoc kubectl command. We need to compute the sum ourselves.

Here is a one-liner to get total CPU & RAM usage for a given namespace:

kubectl top pods -n <namespace> | awk 'BEGIN {mem=0; cpu=0} {mem += int($3); cpu += int($2);} END {print "Mem
ory: " mem "Mi" "\n" "Cpu: " cpu "m"}'

Example output:

Memory: 717Mi
Cpu: 257m

Upvotes: 3

Daniel Marques
Daniel Marques

Reputation: 1405

It's possible creating a resourcequota object like this:

apiVersion: v1
kind: ResourceQuota
metadata:
  name: mem-cpu-demo
spec:
  hard:
    requests.cpu: "1"
    requests.memory: 1Gi
    limits.cpu: "2"
    limits.memory: 2Gi

However there are some pre requisites in order to check the pods consumption:

  1. Every Container must have a memory request, memory limit, cpu request, and cpu limit.
  2. The memory request total for all Containers must not exceed 1 GiB.
  3. The memory limit total for all Containers must not exceed 2 GiB.
  4. The CPU request total for all Containers must not exceed 1 cpu.
  5. The CPU limit total for all Containers must not exceed 2 cpu.

Pod example template

apiVersion: v1
kind: Pod
metadata:
  name: quota-mem-cpu-demo
spec:
  containers:
  - name: quota-mem-cpu-demo-ctr
    image: nginx
    resources:
      limits:
        memory: "800Mi"
        cpu: "800m" 
      requests:
        memory: "600Mi"
        cpu: "400m"

To check the resource consumption use the following command:

kubectl --context <cluster_context> describe resourcequota -n my-namespace

Source: https://kubernetes.io/docs/tasks/administer-cluster/manage-resources/quota-memory-cpu-namespace/

Upvotes: 0

DT.
DT.

Reputation: 3551

On high level we can get this from kubectl

$ kubectl describe resourcequota -n my-namespace

Name:            compute-resources
Namespace:       default
Resource         Used    Hard
--------         ----    ----
limits.cpu       12      48
limits.memory    1024M   120Gi
requests.cpu     250m    24
requests.memory  512M    60Gi

Note : will work only if your create resourcequota.

Upvotes: 3

Related Questions