29. 资源限制
29.1 资源限制概括
1.如果运行的容器没有定义资源(memory、CPU)等限制,但是在namespace定义了LimitRange限制,那么该容器会继承LimitRange中的默认限制
2.如果namespace没有定义LimitRange限制,那么该容器可以只要宿主机的最大可用资源,直到无资源可用而触发宿主机(OOM Killer)
CPU 以核心为单位进行限制,单位可以是整核,浮点核心数或豪核(m/milli)
2=2核心=200% 0.5=500m=50% 1.2=1200m=120%
memory 以字节为单位,单位可以是E、P、T、G、M、K、Gi、Mi
1536Mi=1.5Gi
requests(请求)为kubernetes scheduler执行pod调度时node节点至少需要拥有的资源
limits(限制)为pod运行成功后最多可以使用的资源上限
29.2 容器资源限制
root@k8s-master1:~/20220821/magedu-limit-case# cat case2-pod-memory-and-cpu-limit.yml
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
name: limit-test-deployment
namespace: demo
spec:
replicas: 1
selector:
matchLabels: #rs or deployment
app: limit-test-pod
template:
metadata:
labels:
app: limit-test-pod
spec:
containers:
- name: limit-test-container
image: lorel/docker-stress-ng
resources:
limits:
cpu: "2"
memory: "512Mi"
requests:
memory: "512M"
cpu: "2"
#command: ["stress"]
args: ["--vm", "3", "--vm-bytes", "256M"]
29.3 Pod资源限制
root@k8s-master1:~/20220821/magedu-limit-case# cat case3-LimitRange.yaml
apiVersion: v1
kind: LimitRange
metadata:
name: limitrange-magedu
namespace: demo
spec:
limits:
- type: Container #限制的资源类型
max:
cpu: "2" #限制单个容器的最大CPU
memory: "2Gi" #限制单个容器的最大内存
min:
cpu: "500m" #限制单个容器的最小CPU
memory: "512Mi" #限制单个容器的最小内存
default:
cpu: "500m" #默认单个容器的CPU限制
memory: "512Mi" #默认单个容器的内存限制
defaultRequest:
cpu: "500m" #默认单个容器的CPU创建请求
memory: "512Mi" #默认单个容器的内存创建请求
maxLimitRequestRatio:
cpu: 2 #限制POD CPU limit/request比值最大为2
memory: 2 #限制内存POD limit/request比值最大为1.5
- type: Pod
max:
cpu: "4" #限制单个Pod的最大CPU
memory: "4Gi" #限制单个Pod最大内存
- type: PersistentVolumeClaim
max:
storage: 50Gi #限制PVC最大的requests.storage
min:
storage: 30Gi #限制PVC最小的requests.storage
29.4 namespace资源限制
root@k8s-master1:~/20220821/magedu-limit-case# cat case6-ResourceQuota-demo.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
name: quota-demo
namespace: demo
spec:
hard:
requests.cpu: "8"
limits.cpu: "8"
requests.memory: 8Gi
limits.memory: 8Gi
requests.nvidia.com/gpu: 4
pods: "6"
services: "6"