kubernetes
[따배쿠] Pod - Pod에 Resource 할당하기 (CPU/memory requests, limits)
bbiyak2da
2024. 12. 3. 10:15
Pod Resource 요청 및 제한
- Resource Requests
- 파드를 실행하기 위한 최소 리소스 양 요청
- Resource Limits
- 파드가 사용할 수 있는 최대 리소스 양 제한
- Memory limit을 초과해서 사용하는 파드는 종료(kill)되며 다시 스케줄링 된다.
- 해당 기능을 통해 파드가 과도한 리소스를 사용하는 것을 방지
- 컨테이너 별로 Resource 설정도 가능하다.
특징
- 리소스 관리
- 요청과 제한을 설정함으로써, 쿠버네티스는 클러스터의 리소스를 효율적으로 관리할 수 있습니다. 요청이 설정된 리소스는 스케줄링 시 고려되며, 제한은 Pod가 과도한 리소스를 사용하지 않도록 합니다.
- 안정성
- 요청과 제한을 통해 클러스터의 안정성을 높일 수 있습니다. 예를 들어, 특정 Pod가 갑자기 많은 리소스를 소모할 경우, 다른 Pod에 영향을 미치지 않도록 제한을 설정할 수 있습니다.
작동 방식
- Pod 생성을 위한 yaml 파일 작성 시, Resource의 request와 limits를 지정한 뒤 Create 합니다.
- 쿠버네티스 스케줄러는 각 노드의 가용 리소스를 확인하고, Pod의 요청량이 해당 노드의 가용 리소스와 일치하는지 평가합니다.
- 요청량이 노드의 가용 리소스보다 작거나 같으면, Pod는 해당 노드에 배치됩니다. 만약 모든 노드가 요청량을 수용할 수 없다면, Pod는 Pending 상태가 됩니다.
예시
1. Resource requests만 설정하기
# pod-nginx-resources.yaml 파일 작성
root@master:~/Getting-Start-Kubernetes/5# vi pod-nginx-resources.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod-env
spec:
containers:
- name: nginx-container
image: nginx:1.14
ports:
- containerPort: 80
protocol: TCP
env:
- name: MYVAR
value: "testvalue"
resources:
requests:
memory: 500Mi
cpu: 200m
nginx-container의 최소 memory가 500mi, cpu는 200m를 가지게 배포해줘 !
# 생성 및 확인
root@master:~/Getting-Start-Kubernetes/5# kubectl create -f pod-nginx-resources.yaml
pod/nginx-pod-env created
root@master:~/Getting-Start-Kubernetes/5# kubectl describe pod nginx-pod-env
Name: nginx-pod-env
Namespace: default
Priority: 0
Service Account: default
Node: node1/10.100.0.101
Start Time: Tue, 03 Dec 2024 01:18:07 +0000
Labels: <none>
Annotations: cni.projectcalico.org/containerID: cf8307f690c0a81e877578af4e4566dd5ace9ead68671e8d73fbe3e77c3fd2ef
cni.projectcalico.org/podIP: 192.168.166.157/32
cni.projectcalico.org/podIPs: 192.168.166.157/32
Status: Running
IP: 192.168.166.157
IPs:
IP: 192.168.166.157
Containers:
nginx-container:
Container ID: containerd://0b417795ebc01ff444332a01c56c14efcf4e43727a3ef3d2aaa4d40a918e8393
Image: nginx:1.14
Image ID: docker.io/library/nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 03 Dec 2024 01:18:08 +0000
Ready: True
Restart Count: 0
Requests:
cpu: 1
memory: 500Mi
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-nhkxh (ro)
Conditions:
Type Status
PodReadyToStartContainers True
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-nhkxh:
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 43s default-scheduler Successfully assigned default/nginx-pod-env to node1
Normal Pulled 42s kubelet Container image "nginx:1.14" already present on machine
Normal Created 42s kubelet Created container nginx-container
Normal Started 42s kubelet Started container nginx-container
2. Resource limits만 설정하기
# pod-nginx-resources.yaml 파일 작성
root@master:~/Getting-Start-Kubernetes/5# vi pod-nginx-resources.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod-env
spec:
containers:
- name: nginx-container
image: nginx:1.14
ports:
- containerPort: 80
protocol: TCP
resources:
limits:
memory: 500Mi
cpu: 1
nginx-container의 최대 memory가 500mi, cpu는 1 코어 개수를 가지게 배포해줘 !
# 생성 및 확인
root@master:~/Getting-Start-Kubernetes/5# kubectl create -f pod-nginx-resources.yaml
pod/nginx-pod-env created
root@master:~/Getting-Start-Kubernetes/5# kubectl describe pod nginx-pod-env
Name: nginx-pod-env
Namespace: default
Priority: 0
Service Account: default
Node: node1/10.100.0.101
Start Time: Tue, 03 Dec 2024 01:22:40 +0000
Labels: <none>
Annotations: cni.projectcalico.org/containerID: acc2c3469785ae28e11327d227ea514ee1a8ec620e65063244e03ac91d013371
cni.projectcalico.org/podIP: 192.168.166.158/32
cni.projectcalico.org/podIPs: 192.168.166.158/32
Status: Running
IP: 192.168.166.158
IPs:
IP: 192.168.166.158
Containers:
nginx-container:
Container ID: containerd://38e51906429a0ad5bca2073572dcd254cbd326baf058ba0ce168553dfa01b949
Image: nginx:1.14
Image ID: docker.io/library/nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 03 Dec 2024 01:22:41 +0000
Ready: True
Restart Count: 0
Limits:
cpu: 1
memory: 500Mi
Requests:
cpu: 1
memory: 500Mi
resource limits만 설정했는데도, requests도 limit 값과 동일하게 생성
3. Pending 상태 확인
# pod-nginx-resources.yaml 파일 작성
root@master:~/Getting-Start-Kubernetes/5# vi pod-nginx-resources.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod-env
spec:
containers:
- name: nginx-container
image: nginx:1.14
ports:
- containerPort: 80
protocol: TCP
resources:
requests:
memory: 500Mi
cpu: 2
# 생성 및 확인
root@master:~/Getting-Start-Kubernetes/5# kubectl create -f pod-nginx-resources.yaml
pod/nginx-pod-env created
root@master:~/Getting-Start-Kubernetes/5# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-pod-env 0/1 Pending 0 9s <none> <none> <none> <none>
정상적으로 생성되지 않고 pending 상태인 것을 확인 가능
즉, 모든 node에서 CPU 2코어만큼 여유가 있지 않고, 그에 따라 모든 노드에 생성할 수가 없다.
(현재 실습 환경인 node1, node2는 모두 2Core로 배포되어있다. 그러나 OS / kubelet 등등이 차지하고 있는 CPU도 있기때문에 CPU가 모자라 배포가 안된다.)
4. Resource requests와 limits 정상적으로 셋팅 후, 배포
# pod-nginx-resources.yaml 파일 작성
root@master:~/Getting-Start-Kubernetes/5# vi pod-nginx-resources.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod-env
spec:
containers:
- name: nginx-container
image: nginx:1.14
ports:
- containerPort: 80
protocol: TCP
resources:
requests:
memory: 500Mi
cpu: 200m
limits:
memory: 1Gi
cpu : 1
# 생성 및 확인
root@master:~/Getting-Start-Kubernetes/5# kubectl create -f pod-nginx-resources.yaml
pod/nginx-pod-env created
pod/nginx-pod-env created
root@master:~/Getting-Start-Kubernetes/5# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-pod-env 1/1 Running 0 6s 192.168.166.159 node1 <none> <none>
root@master:~/Getting-Start-Kubernetes/5# kubectl describe pod nginx-pod-env
Name: nginx-pod-env
Namespace: default
Priority: 0
Service Account: default
Node: node1/10.100.0.101
Start Time: Tue, 03 Dec 2024 01:43:42 +0000
Labels: <none>
Annotations: cni.projectcalico.org/containerID: bf2328273b3450d73d4c471a0457dd726af1f4b94a971b54e627229e6e2bdddf
cni.projectcalico.org/podIP: 192.168.166.159/32
cni.projectcalico.org/podIPs: 192.168.166.159/32
Status: Running
IP: 192.168.166.159
IPs:
IP: 192.168.166.159
Containers:
nginx-container:
Container ID: containerd://44056ce3f9c81f362b84f6825e498dccbc54f4cb5f0fa608c1900d241512c685
Image: nginx:1.14
Image ID: docker.io/library/nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 03 Dec 2024 01:43:43 +0000
Ready: True
Restart Count: 0
Limits:
cpu: 1
memory: 1Gi
Requests:
cpu: 200m
memory: 500Mi
정상적으로 배포된 것을 확인 가능
기타
- CPU는 보통 m(밀리 코어)로 표현하거나, 코어 개수로 표현한다.
- cpu : 200m / cpu : 1
- Memory는 보통 Mi(메비 바이트), Ki(키비 바이트), Gi(기비 바이트) 로 표현한다.
- memory : 250Mi / memory : 250Ki / memory : Gi
- 1MB = 1000KB
- 1Mi(B) = 1024KiB
참고 영상
https://www.youtube.com/watch?v=lxCtyWPsb-0