Wednesday 29 April 2020

Macam mana nak set resource quota di ns K8s

Salam geng, hari nie saya nak kongsi macam mana nak set resource quota di namespace (ns) dalam Kubernetes( K8s ). Ini untuk kawal supaya user tak boleh guna sesuka hati semua CPU dan Memory kat dalam sebuat cluster K8s.

1. Set context ke ns yang nak di quotakan. Pada contoh ni, saya nak ke ns sandbox
kubectl config set-context --current --namespace=sandbox

2. Tetapkan quota untuk CPU dan Memory. Unit CPU dan Memory adalah seperti berikut

CPU = 1 ( 1 CPU ), 100m ( 0.1 CPU )
Memory = 1Gi ( 1 Gigabyte ), 1Mi ( 1 Megabyte )

Apply manifest resourcequota untuk set quota di ns ini.
kubectl apply -f quota.yml

quota.yml
apiVersion: v1
kind: ResourceQuota
metadata:
  name: ns-quota
spec:
  hard:
    limits.cpu: "1"
    limits.memory: 4Gi


3. Untuk semak quota dah ada ke belum buat arahan yang berikut:-
S54034@VBCTRUKBXJH300 quota]$ kubectl describe quota
Name:          ns-quota
Namespace:     sandbox
Resource       Used  Hard
--------       ----  ----
limits.cpu     0     1
limits.memory  0     4Gi


4. Jom deploy 3 nginx pod yang masing-masing gunak 0.5 CPU dan 2GB memory. Apa yang akan terjadi ialah

i.   nginx pertama akan pakai separuh dari quota.
ii.  nginx kedua akan pakai semua quota
iii. nginx ketiga akan gagal kerana quota dah penuh.

Ini manifest untuk nginx pertama. Untuk nginx kedua kena edit nginx01 ke nginx02. Untuk nginx ketiga kena ubah ke nginx03

#Test pod
apiVersion: v1
kind: Pod
metadata:
  name: nginx01
spec:
  containers:
  - name: nginx01
    image: harbor.dev.kubix.tm.com.my/library/nginx:latest
    resources:
      limits:
        memory: "2Gi"
        cpu: "500m" 
      requests:
        memory: "2Gi"
        cpu: "500m" 




Mari kita tengok contoh quota ini :

$ kubectl apply -f quota.yml 
resourcequota/ns-quota created
$ kubectl describe quota
Name:          ns-quota
Namespace:     sandbox
Resource       Used  Hard
--------       ----  ----
limits.cpu     0     1
limits.memory  0     4Gi

$ kubectl apply -f nginx-0.5vcpu-2gb-01.yml 
pod/nginx01 created
$ kubectl describe quota
Name:          ns-quota
Namespace:     sandbox
Resource       Used  Hard
--------       ----  ----
limits.cpu     500m  1
limits.memory  2Gi   4Gi
$ kubectl apply -f nginx-0.5vcpu-2gb-02.yml 
pod/nginx02 created
$ kubectl describe quota
Name:          ns-quota
Namespace:     sandbox
Resource       Used  Hard
--------       ----  ----
limits.cpu     1     1
limits.memory  4Gi   4Gi
$ kubectl apply -f nginx-0.5vcpu-2gb-03.yml 
Error from server (Forbidden): error when creating "nginx-0.5vcpu-2gb-03.yml": pods "nginx03" is forbidden: exceeded quota: ns-quota, requested: limits.cpu=500m,limits.memory=2Gi, used: limits.cpu=1,limits.memory=4Gi, limited: limits.cpu=1,limits.memory=4Gi

No comments:

Post a Comment

Tatacara klon pelayan di AWS lightsail

  Kadang-kala kita diminta untuk buat satu sistem baru untuk tujuan ujikaji atau percubaan fungsi baru. Jika di AWS lightsail kita boleh bua...