DevOps/Kubernetes

Kubernetes - pod resource 요청 및 제한

yscho03 2021. 11. 18. 23:00
728x90
반응형

pod resource 요청 및 제한

Resource Requests

  • 파드를 실행하기 위한 최소 리소스 양을 요청

Resource Limits

  • 파드가 사용할 수 있는 최대 리소스 양을 제한
  • Memory limit을 초과해서 사용되는 파드는 종료 (OOM Kill)되며 다시 스케줄링 된다.

 

pod 실행방법

실행방법

root@master:~# kubectl get pods
No resources found in default namespace.
root@master:~# cat > nginx.yaml
apiVersion: v1
kind: Pod 
metadata:
  name: nginx-pod
spec:
  containers:
  - name: nginx-container
    image: nginx:1.14
    ports:
    - containerPort: 80
      protocol: TCP 
    resources:
      requests:
        cpu: 200m
        memory: 250Mi
      limits:
        cpu: 1
        memory: 500Mi
root@master:~# kubectl create -f nginx.yaml 
pod/nginx-pod created
root@master:~# kubectl get pods
NAME         READY   STATUS    RESTARTS   AGE
nginx-pod   1/1     Running   0          11s

Pending 상태 만들기

노드에 있는 서버 자원의 양보다 높게 설정하거나 리소스가 부족한 경우 pod는 생성되지 않고 pending 상태가 된다.

현재 노드가 2core밖에 없으니 4core로 requests를 설정하여본다.

root@master:~# kubectl get pods
No resources found in default namespace.
root@master:~# cat > nginx-overflow.yaml
apiVersion: v1
kind: Pod 
metadata:
  name: nginx-pod
spec:
  containers:
  - name: nginx-container
    image: nginx:1.14
    ports:
    - containerPort: 80
      protocol: TCP 
    resources:
      requests:
        cpu: 4
        memory: 250Mi
      limits:
        cpu: 4
        memory: 500Mi
root@master:~# kubectl create -f nginx-overflow.yaml 
pod/nginx-pod created
root@master:~# kubectl get pods -o wide
NAME         READY   STATUS    RESTARTS   AGE   IP       NODE     NOMINATED NODE   READINESS GATES
nginx-pod    0/1     Pending   0          6s    <none>   <none>   <none>           <none>

Requests 옵션만 있을 경우

describe 명령어를 실행해서 조회해보면 limits 옵션없이 requests 옵션만 설정된 것을 볼 수 있다.

그러면 limits만 설정해보면 어떻게 될까?

root@master:~# cat > nginx-requests.yaml
apiVersion: v1
kind: Pod 
metadata:
  name: nginx-pod
spec:
  containers:
  - name: nginx-container
    image: nginx:1.14
    ports:
    - containerPort: 80
      protocol: TCP 
    resources:
      requests:
        cpu: 1
        memory: 250Mi
root@master:~# kubectl create -f nginx-requests.yaml 
pod/nginx-pod created
root@master:~# kubectl get pods
NAME        READY   STATUS    RESTARTS   AGE
nginx-pod   1/1     Running   0          9s
root@master:~# kubectl describe pod nginx-pod 
Name:         nginx-pod
Namespace:    default
Priority:     0
Node:         node1.example.com/10.100.0.101
Start Time:   Thu, 18 Nov 2021 23:06:13 +0900
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           10.36.0.1
IPs:
  IP:  10.36.0.1
Containers:
  nginx-container:
    Container ID:   docker://e2f60e57c2248ee65c310b822b4114e104a95a177c14d32f435ccd73522aa594
    Image:          nginx:1.14
    Image ID:       docker-pullable://nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Thu, 18 Nov 2021 23:06:15 +0900
    Ready:          True
    Restart Count:  0
    Requests:
      cpu:        1
      memory:     250Mi
    Environment:  <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-72j2q (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-72j2q:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   Burstable
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  17s   default-scheduler  Successfully assigned default/nginx-pod to node1.example.com
  Normal  Pulled     15s   kubelet            Container image "nginx:1.14" already present on machine
  Normal  Created    15s   kubelet            Created container nginx-container
  Normal  Started    15s   kubelet            Started container nginx-container

limits 옵션만 있을 경우

yaml 파일에 requests 옵션없이 limits 옵션만 설정한 경우에 생성된 pod를 조회해보면 limits에 설정한 옵션값들이 requests에도 같이 설정된 것을 볼 수 있다. 

root@master:~# cat > nginx-limits.yaml
apiVersion: v1
kind: Pod 
metadata:
  name: nginx-pod
spec:
  containers:
  - name: nginx-container
    image: nginx:1.14
    ports:
    - containerPort: 80
      protocol: TCP 
    resources:
      limits:
        cpu: 1
        memory: 250Mi
root@master:~# kubectl get pods
No resources found in default namespace.
root@master:~# kubectl create -f nginx-limits.yaml 
pod/nginx-pod created
root@master:~# kubectl get pods
NAME        READY   STATUS    RESTARTS   AGE
nginx-pod   1/1     Running   0          3s
root@master:~# kubectl describe pods nginx-pod 
Name:         nginx-pod
Namespace:    default
Priority:     0
Node:         node1.example.com/10.100.0.101
Start Time:   Thu, 18 Nov 2021 23:09:05 +0900
Labels:       <none>
Annotations:  <none>
Status:       Running
IP:           10.36.0.1
IPs:
  IP:  10.36.0.1
Containers:
  nginx-container:
    Container ID:   docker://47db9902bf4cec2cb1a96c70f427ca975b464a957ba59da4d3d27a198876a71a
    Image:          nginx:1.14
    Image ID:       docker-pullable://nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Thu, 18 Nov 2021 23:09:06 +0900
    Ready:          True
    Restart Count:  0
    Limits:
      cpu:     1
      memory:  250Mi
    Requests:
      cpu:        1
      memory:     250Mi
    Environment:  <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-x9mtw (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-x9mtw:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   Guaranteed
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  12s   default-scheduler  Successfully assigned default/nginx-pod to node1.example.com
  Normal  Pulled     11s   kubelet            Container image "nginx:1.14" already present on machine
  Normal  Created    11s   kubelet            Created container nginx-container
  Normal  Started    11s   kubelet            Started container nginx-container
728x90
반응형