---
title: Kubernetesでコンテナ単位でリソース制限してデプロイする
tags: kubernetes
author: yuta_vamdemic
slide: false
---
## ポイント
- `Request` は使用するリソースの下限を指定する
- 空いているノードにRequestsで指定した量のリソースが足りない場合はスケジューリングされずに、`pending`となる
- `Linits`は使用するリソースの上限を指定する
- ノードにLimitsで指定したリソースが残っていなくてもスケジューリングはされる
- Requests < Limits で指定することになる
- RequestsとLimitsの差がお起きする切る場合、いっきに負荷が上がった場合に実際のリソースが足らなくなってしまう
- よく見かけるのはLimitsはRequestsの1.5倍〜2倍くらい
|リソース |単位 |
|---|---|---|
|CPU |1 = 1000m = 1vcpu |
|メモリ |1G = 1000M (1Gi = 1024Mi) |
## テスト用のyamlファイル
- Resource,Limitsを各Podに指定する例
```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: resource-test
spec:
replicas: 3
selector:
matchLabels:
app: sample-app
template:
metadata:
labels:
app: sample-app
spec:
containers:
- name: nginx-container
image: nginx:1.16
resources:
requests:
memory: 1024Mi
cpu: 500m
limits:
memory: 2048Mi
cpu: 1000m
```
## Nodeのリソース状態を確認する方法
```
kubectl describe node <node名>
```
この部分をチェック
```
Non-terminated Pods: (5 in total)
Namespace Name CPU Requests CPU Limits Memory Requests Memory Limits AGE
--------- ---- ------------ ---------- --------------- ------------- ---
default external-dns-cf685b4c7-8c75v 0 (0%) 0 (0%) 0 (0%) 0 (0%) 15d
default resource-test-fc447dcc-nxfhr 0 (0%) 0 (0%) 0 (0%) 0 (0%) 10m
kube-system alb-ingress-controller-7559fd56d4-lm9rc 0 (0%) 0 (0%) 0 (0%) 0 (0%) 15d
kube-system aws-node-q5zgn 10m (0%) 0 (0%) 0 (0%) 0 (0%) 15d
kube-system kube-proxy-5qthx 100m (5%) 0 (0%) 0 (0%) 0 (0%) 15d
Allocated resources:
(Total limits may be over 100 percent, i.e., overcommitted.)
Resource Requests Limits
-------- -------- ------
cpu 110m (5%) 0 (0%)
memory 0 (0%) 0 (0%)
ephemeral-storage 0 (0%) 0 (0%)
hugepages-1Gi 0 (0%) 0 (0%)
hugepages-2Mi 0 (0%) 0 (0%)
attachable-volumes-aws-ebs 0 0
```
## リソースが足りない状況を作り出す
- 上のyamlのreplicaを10でデプロイ
### 特定のpodが`Pending`になっている
```
yuta:~ $ kubectl get pod
NAME READY STATUS RESTARTS AGE
cent8 1/1 Running 0 10d
external-dns-cf685b4c7-8c75v 1/1 Running 0 15d
nginx 1/1 Running 0 10d
resource-test-8694845b69-2ght7 0/1 Pending 0 24s
resource-test-8694845b69-6b8vx 1/1 Running 0 24s
resource-test-8694845b69-6kvz7 1/1 Running 0 55s
resource-test-8694845b69-7mhtc 1/1 Running 0 24s
resource-test-8694845b69-fhqj5 1/1 Running 0 24s
resource-test-8694845b69-fl4dj 1/1 Running 0 56s
resource-test-8694845b69-hqd2t 1/1 Running 0 24s
resource-test-8694845b69-jzmsb 1/1 Running 0 24s
resource-test-8694845b69-rsmk4 1/1 Running 0 52s
resource-test-8694845b69-xf5h2 1/1 Running 0 24s
```
### 原因を確認
```
kubectl describe pod resource-test-8694845b69-2ght7
```
CPUのLimitを割り当てていて、これ以上のリソースを割り当てることができないためにpendingとなっている
```
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling 45s default-scheduler 0/3 nodes are available: 3 Insufficient cpu
```
### Nodeに割り当てられた状態
- t3.large(2vcpu,8GBメモリ)でcpuで500mのRequestを受けきることができない
- limitsは合計3000mの155%で100%以上を割り当てることができている
```
Allocated resources:
(Total limits may be over 100 percent, i.e., overcommitted.)
Resource Requests Limits
-------- -------- ------
cpu 1810m (93%) 3 (155%)
memory 3212Mi (45%) 6484Mi (90%)
ephemeral-storage 0 (0%) 0 (0%)
hugepages-1Gi 0 (0%) 0 (0%)
hugepages-2Mi 0 (0%) 0 (0%)
attachable-volumes-aws-ebs 0 0
```