Kubernetes Resource Quotas

Resource Quota adalah mekanisme pembagian sebuah resource didalam kubernetes cluster. Konsep ini sangat berhubungan dengan namespace karena pembagian resource cluster berdasarkan namespacenya. Resource Quota dapat dicek dengan

ubuntu@master:~$ kubectl describe ns default
Name:         default
Labels:       <none>
Annotations:  <none>
Status:       Active

No resource quota.

No resource limits.

Resource Quota berguna jika didalam kubernetes cluster terdapat banyak team (dengan namespace) dan resource cluster harus dibagi agar adil.

Jenis Quota

  • Compute Resource (limits.cpu, limits.memory, requests.cpu, requests.memory)
  • Storage Resource
  • Object Count (configmaps, persistentvolumeclaims, pods, resourcequotas, services, secrets)

Object Count Quota

berikut adalah manifest yaml yang digunakan untuk melimit jumlah pod maksimal hanya 2.

apiVersion: v1
kind: ResourceQuota
metadata:
  name: pod-limit
spec:
  hard:
    pods: "2"

lalu eksekusi didalam namespace tertentu (e.g. development)

kubectl create -f rq-pod.yaml --namespace=development

lalu cek namespace tersebut

ubuntu@master:~$ kubectl create -f rq-pod.yaml --namespace=development
resourcequota "pod-limit" created
ubuntu@master:~$ kubectl describe namespace development
Name:         development
Labels:       <none>
Annotations:  <none>
Status:       Active

Resource Quotas
 Name:     pod-limit
 Resource  Used  Hard
 --------  ---   ---
 pods      0     2

No resource limits.
ubuntu@master:~$ 

lalu coba buat sebuah development yang mendeploy 2 pod :

kind:                          Deployment
apiVersion:                    extensions/v1beta1
metadata:
  name:                        nginx
spec:
  replicas:                    2
  template:
    metadata:
      labels:
        app:                   nginx
    spec:
      containers:
      - name:                  webserver
        image:                 nginx
        ports:
        - containerPort:       80
ubuntu@master:~$ kubectl create -f rq-deployment.yaml --namespace=development
deployment.extensions "nginx" created
ubuntu@master:~$ kubectl describe namespace development
Name:         development
Labels:       <none>
Annotations:  <none>
Status:       Active

Resource Quotas
 Name:     pod-limit
 Resource  Used  Hard
 --------  ---   ---
 pods      2     2

lalu coba deploy lagi 1 deployment :

ubuntu@master:~$ kubectl run postgres --image=postgres --namespace=development
deployment.apps "postgres" created
ubuntu@master:~$ kubectl get deploy --namespace=development
NAME       DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
nginx      2         2         2            2           2m
postgres   1         0         0            0           21s
ubuntu@master:~$ kubectl get pod --namespace=development
NAME                     READY     STATUS    RESTARTS   AGE
nginx-5757dfbb6f-c7d5s   1/1       Running   0          2m
nginx-5757dfbb6f-tchmp   1/1       Running   0          2m
ubuntu@master:~$ kubectl get deploy postgres --namespace=development --output=yaml | grep message
    message: Created new replica set "postgres-66b497686b"
    message: Deployment has minimum availability.
    message: 'pods "postgres-66b497686b-r94l8" is forbidden: exceeded quota: pod-limit,

Berikut contoh lain dari resourcequota

apiVersion: v1
kind: ResourceQuota
metadata:
  name: pod-limit
spec:
  hard:
    pods: "2"
    configmaps: "2"
    secrets: "2"
    services: "2"
ubuntu@master:~$ kubectl create -f rq.yaml --namespace=production
resourcequota "pod-limit" created
ubuntu@master:~$ kubectl describe namespace production
Name:         production
Labels:       <none>
Annotations:  <none>
Status:       Active

Resource Quotas
 Name:       pod-limit
 Resource    Used  Hard
 --------    ---   ---
 configmaps  0     2
 pods        0     2
 secrets     1     2
 services    0     2

No resource limits.

CPU & Memory Quota

membuat konfigurasi default limit dan request untuk cpu & memory dan konfigurasi minimum dan maksimum memory dan cpu yang dapat digunakan container. jika sebuah container tidak dikonfigurasi, maka otomatis akan menggunakan default konfigurasi ini :

apiVersion: v1
kind: LimitRange
metadata:
  name: default-min-max-range
spec:
  limits:
  - default:
      cpu: 0.5
      memory: 512Mi
    defaultRequest:
      cpu: 0.25
      memory: 256Mi
    max:
      cpu: 1
      memory: 1024Mi
    min:
      cpu: 0.1
      memory: 128Mi
    type: Container

dari konfigurasi diatas :

  • jika sebuah container tidak menyertakan spesifikasi, gunakan default request (0.25 cpu & 256Mi) dan default limit (0.5 cpu & 512Mi)
  • pastikan bahwa container tidak melebihi limit maksimum atau limit minimum.
  • type: Container, berlaku untuk container, ganti dengan pod agar berlalu untuk pod (ingat didalam pod dapat memiliki banyak container)

membuat konfigurasi maksimal resource yang dapat digunakan didalam sebuah namespace

apiVersion: v1
kind: ResourceQuota
metadata:
  name: mem-cpu-rq
spec:
  hard:
    requests.cpu: "2"
    requests.memory: 4Gi
    limits.cpu: "4"
    limits.memory: 8Gi

dari konfigurasi namespace diatas :

  • maksimal request dari seluruh resource yang digunakan sebuah namespace tersebut adalah (2 cpu & 4Gi) dan limit (4 cpu & 8Gi).
ubuntu@master:~/rq$ kubectl create -f limitrange.yaml 
limitrange "default-min-max-range" created 
ubuntu@master:~/rq$ kubectl create -f rq-namespace.yaml 
resourcequota "mem-cpu-rq" created
ubuntu@master:~/rq$ kubectl describe namespace default
Name:         default
Labels:       <none>
Annotations:  <none>
Status:       Active

Resource Quotas
 Name:            mem-cpu-rq
 Resource         Used  Hard
 --------         ---   ---
 limits.cpu       0     4
 limits.memory    0     8Gi
 requests.cpu     0     2
 requests.memory  0     4Gi

Resource Limits
 Type       Resource  Min    Max  Default Request  Default Limit  Max Limit/Request Ratio
 ----       --------  ---    ---  ---------------  -------------  -----------------------
 Container  cpu       100m   1    250m             500m           -
 Container  memory    128Mi  1Gi  256Mi            512Mi          -

deploy deployment tanpa konfigurasi resource:

ubuntu@master:~/rq$ kubectl run nginx --image=nginx
deployment.apps "nginx" created
ubuntu@master:~/rq$ kubectl get pod
NAME                     READY     STATUS    RESTARTS   AGE
nginx-65899c769f-w2vwd   1/1       Running   0          1m
ubuntu@master:~/rq$ kubectl get pod nginx-65899c769f-w2vwd -o yaml
.
.
.
spec:
  containers:
  - image: nginx
    imagePullPolicy: Always
    name: nginx
    resources:
      limits:
        cpu: 500m
        memory: 512Mi
      requests:
        cpu: 250m
        memory: 256Mi
ubuntu@master:~/rq$ kubectl describe namespace default
Name:         default
Labels:       <none>
Annotations:  <none>
Status:       Active

Resource Quotas
 Name:            mem-cpu-rq
 Resource         Used   Hard
 --------         ---    ---
 limits.cpu       500m   4
 limits.memory    512Mi  8Gi
 requests.cpu     250m   2
 requests.memory  256Mi  4Gi

Resource Limits
 Type       Resource  Min    Max  Default Request  Default Limit  Max Limit/Request Ratio
 ----       --------  ---    ---  ---------------  -------------  -----------------------
 Container  cpu       100m   1    250m             500m           -
 Container  memory    128Mi  1Gi  256Mi            512Mi          -

Comments are closed.