Kubernetes resource quotas 사용법

Winston Lee·2023년 5월 3일
1

k8s

목록 보기
4/9

리소스 할당량(resource quota)은 애플리케이션이 사용할 수 있는 CPU 또는 메모리 양을 제어하여 리소스 경합 및 '랜드 그랩(토지수탈?)'을 방지합니다.

리소스 할당량(resource quota)이란?

간단히 말해 리소스 할당량(resource quota)은 네임스페이스당 리소스 사용을 제한하는 제약 조건을 제공합니다. 네임스페이스 수준에서만 적용될 수 있으므로 컴퓨팅 리소스에 적용하여 네임스페이스 내의 개체 수를 제한할 수 있다.
쿠버네티스 리소스 할당량은 리소스쿼터(ResourceQuota) 오브젝트에 의해 정의된다. 네임스페이스에 적용하면 CPU 및 메모리와 같은 컴퓨팅 리소스는 물론 다음 오브젝트의 생성을 제한할 수 있다:

  • Pods
  • Services
  • Secrets
  • Persistent Volume Claims (PVCs)
  • ConfigMaps

쿠버네티스는 컴퓨팅 리소스를 관리하기 위해 두 가지 유형의 CPU 및 메모리 쿼터를 지원한다. 이는 리밋레인지 문서에서 설명하는 것처럼 리밋과 요청을 통해 제어된다.
간단히 말해, 요청(request)은 컨테이너에 대해 보장된 CPU 또는 메모리 리소스를 정의하는 반면, 제한(limit)은 다른 컨테이너의 사용량에 따라 컨테이너가 사용할 수 있는 메모리 또는 CPU 임계값을 의미합니다.

아래 이미지는 쿠버네티스 리소스 쿼터에서 요청과 제한의 차이를 보여줍니다.

Kubernetes resource quota에서 request와 limit의 차이

리소스 할당량(resource quota) 예제

CPU quota를 적용할 namespace 생성

quota-test namespace 생성

$ kubectl create namespace quota-test
namespace/quota-test created

cpu-quota.yaml 파일 작성

apiVersion: v1
kind: ResourceQuota
metadata:
  name: test-cpu-quota
**  namespace: quota-test
**spec:
  hard:
    requests.cpu: "100m"  
    limits.cpu: "200m"

cpu-quota.yaml Kubernetes cluster에 적용

$ kubectl apply -f cpu-qouta.yaml 
resourcequota/test-cpu-quota created

kubectl describe command로 확인

$ kubectl describe resourcequota/test-cpu-quota --namespace quota-test
Name:         test-cpu-quota
Namespace:    quota-test
Resource      Used  Hard
--------      ----  ----
limits.cpu    0     200m
requests.cpu  0     100m

Test

이제 할당량을 정의했으니 테스트. 이 예제에서는 동일한 네임스페이스에 서로 다른 세 개의 파드를 배포하여 정의한 제한에 따라 리소스 사용량을 제어할 수 있는지 확인. 세 개의 파드는 다음과 같습니다:

PodA: 가장 먼저 인스턴스화되는 이 파드는 사용 가능한 CPU의 50%를 사용.
PodB: 이 파드는 사용 가능한 CPU의 나머지 50%를 사용하며, 두 번째로 인스턴스화된 파드이다.
파드C: 정의된 할당량으로 인해 이 세 번째 파드가 배포되지 않아야 한다.

poda 생성됨
$ kubectl create -n quota-test -f- <<EOF
> apiVersion: v1
> kind: Pod
> metadata:
>   name: poda
> spec:
>   containers:
>   - name: quota-test
>     image: busybox
>     imagePullPolicy: IfNotPresent
>     command: ['sh', '-c', 'echo Pod is Running ; sleep 5000']
>     resources:
>       requests:
>         cpu: "50m"
>       limits:
>         cpu: "100m"
>   restartPolicy: Never
> EOF
pod/poda created
podb 생성됨
$ kubectl describe resourcequota/test-cpu-quota --namespace quota-test
Name:         test-cpu-quota
Namespace:    quota-test
Resource      Used  Hard
--------      ----  ----
limits.cpu    100m  200m
requests.cpu  50m   100m
[root@k8s-master qouta]# kubectl create -n quota-test -f- <<EOF
> apiVersion: v1
> kind: Pod
> metadata:
>   name: podb
> spec:
>   containers:
>   - name: quota-test
>     image: busybox
>     imagePullPolicy: IfNotPresent
>     command: ['sh', '-c', 'echo Pod is Running ; sleep 5000']
>     resources:
>       requests:
>         cpu: "50m"
>       limits:
>         cpu: "100m"
>   restartPolicy: Never
> EOF
pod/podb created
quota 사용량 확인, Hard Limit까지 사용중
$ kubectl describe resourcequota/test-cpu-quota --namespace quota-test
Name:         test-cpu-quota
Namespace:    quota-test
Resource      Used  Hard
--------      ----  ----
limits.cpu    200m  200m
requests.cpu  100m  100m  
podc 생성요청
$ kubectl describe resourcequota/test-cpu-quota --namespace quota-test
Name:         test-cpu-quota
Namespace:    quota-test
Resource      Used  Hard
--------      ----  ----
limits.cpu    200m  200m
requests.cpu  100m  100m
[root@k8s-master qouta]# kubectl create -n quota-test -f- <<EOF
> apiVersion: v1
> kind: Pod
> metadata:
>   name: podc
> spec:
>   containers:
>   - name: quota-test
>     image: busybox
>     imagePullPolicy: IfNotPresent
>     command: ['sh', '-c', 'echo Pod is Running ; sleep 5000']
>     resources:
>       requests:
>         cpu: "5m"
>       limits:
>         cpu: "10m"
>   restartPolicy: Never
> EOF
Error from server (Forbidden): error when creating "STDIN": pods "podc" is forbidden: exceeded quota: test-cpu-quota, requested: limits.cpu=10m,requests.cpu=5m, used: limits.cpu=200m,requests.cpu=100m, limited: limits.cpu=200m,requests.cpu=100m

🥕Quota를 초과하는 리소스를 사용하려하여 Pod 생성이 안된다는 메시지 출력

Error from server (Forbidden): error when creating "STDIN": pods "podc" is forbidden: exceeded quota: test-cpu-quota, requested: limits.cpu=10m,requests.cpu=5m, used: limits.cpu=200m,requests.cpu=100m, limited: limits.cpu=200m,requests.cpu=100m

Clean up

$ kubectl delete -n quota-test
profile
인프라 엔지니어

0개의 댓글