pod의 spec.containers[].resources 에 아래와 같은 4개 정보를 설정해야 한다.
requests와 limits 이라는 항목이 있으며, 각각 cpu, memory 값을 설정할 수 있다.
예시
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
name: nginx-pod
spec:
containers:
- name: nginx-pod
image: nginx:latest
resources:
limits:
cpu: "1"
memory: "512Mi"
requests:
cpu: "500m"
memory: "256Mi"
해석 : 이 컨테이너는 최소 1 CPU 및 512Mi의 메모리를 요청하며, 최대 500m의 CPU 및 256Mi의 메모리를 사용할 수 있다.
참고
memory 단위 Mi는 MiB를 의미하며, cpu 단위인 m은 1/1000 core 을 의미한다 (250m=0.25). MiB와 MB의 차이점은 링크를 참고하자.
Pod의 resources부분을 작성하다 보면 Request와 Limit가 존재하는 것을 알 수 있다.
위의 그림은 Memory를 Request:500Mb, Limit:750Mb를 지정한 Pod를 예시로 들었다.
그림에서 Pod는 기본적으로 Memory를 500Mb 사용을 보장하되 더 많은 Memory를 필요로 할 경우에는 최대 750Mb까지 사용할 수 있는 것을 나타낸다.
Request는 적어도 이 만큼의 자원은 컨테이너에게 보장해야 한다는 의미이다.
Limit는 유휴 자원이 있다면 최대 이 만큼의 자원까지 컨테이너가 사용할 수 있다는 것을 의미한다.
# Pod 확인
$ kubectl get pod nginx-pod
NAME READY STATUS RESTARTS AGE
nginx-pod 1/1 Running 0 44s
# Pod 정보 확인
$ kubectl describe pod nginx-pod
Name: nginx-pod
Namespace: default
Priority: 0
Service Account: default
Node: w2-k8s/192.168.1.102
Start Time: Mon, 06 May 2024 16:02:30 +0900
Labels: name=nginx-pod
Annotations: cni.projectcalico.org/podIP: 172.16.103.139/32
cni.projectcalico.org/podIPs: 172.16.103.139/32
Status: Running
IP: 172.16.103.139
IPs:
IP: 172.16.103.139
Containers:
nginx-pod:
Container ID: containerd://fa7d17e5fefa7624120a55d0aa65651abcf25c69fc23716fea814ba53ba6d5fa
Image: nginx:latest
Image ID: docker.io/library/nginx@sha256:ed6d2c43c8fbcd3eaa44c9dab6d94cb346234476230dc1681227aa72d07181ee
Port: <none>
Host Port: <none>
State: Running
Started: Mon, 06 May 2024 16:02:32 +0900
Ready: True
Restart Count: 0
Limits:
cpu: 1
memory: 512Mi
Requests:
cpu: 500m
memory: 256Mi
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-t4v65 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-t4v65:
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 53s default-scheduler Successfully assigned default/nginx-pod to w2-k8s
Normal Pulling 53s kubelet Pulling image "nginx:latest"
Normal Pulled 51s kubelet Successfully pulled image "nginx:latest" in 1.516445662s
Normal Created 51s kubelet Created container nginx-pod
Normal Started 51s kubelet Started container nginx-pod
아래와 같이 Limits, Requests 에 대한 리소스 정보를 확인할 수 있다.
Limits:
cpu: 1
memory: 512Mi
Requests:
cpu: 500m
memory: 256Mi
# pod 에 대한 정보 확인
$ kubectl get pod -o yaml [pod]
# 모든 pod 에 대한 값을 확인
$ kubectl get po -o custom-columns="Name:metadata.name,CPU-Requet:spec.containers[*].resources.requests.cpu,CPU-limit:spec.containers[*].resources.limits.cpu,MEM-Request:spec.containers[*].resources.requests.memory,MEM-limit:spec.containers[*].resources.limits.memory"