Pod container resource quota

Configure container quotas

The processes in the container will occupy the host's resources, such as CPU and memory usage. If there are no resource restrictions for some containers in the production environment, it may eat up a large amount of the host's resources, causing other containers to fail to run normally. For this In this case, kubernetes provides a quota mechanism for cpu and memory.

[root@k8s-master ~]# kubectl explain pod.spec.containers.resources
KIND:     Pod
VERSION:  v1
RESOURCE: resources <Object>
FIELDS:
limits	<map[string]string>			//最大限制	
requests	<map[string]string>			//最小限制

limits: Used to limit the maximum resources occupied by the running container. When the resources occupied by the container exceed the limits setting, it will be terminated and restarted.

requests: used to set the minimum resources of the container. If the environment resources are not enough, the container cannot be started. imits is the maximum number of resources allocated to the container, and requests is the minimum number of resources allocated to the container. If the smallest resource node of request cannot be supplied, then the container will not be able to Start, if the resources used by the container exceed the limit, the container will be terminated and restarted. Currently, pod resources can only limit the CPU and memory resources used by the container. The unit of CPU is the number of cores, which can be integer or decimal memory units. It is in the form of Gi, Mi, G, M, etc.

Configure the node with sufficient resources for the container

1.编写 yaml 文件
apiVersion: v1
kind: Pod
metadata:
  name: pod-resources
  namespace: dev
  labels:
    app: base
spec:
  containers:
  - name: nginx-port
    image: nginx:1.18
    resources:						#定义资源配置
      limits:						#最大资源限制
        cpu: "2"						#cpu限制在2核
        memory: "10Gi"					 #内存限制在10G
      requests: 					#最小资源限制
        cpu: "1"						#cpu限制在1核
        memory: "10Mi"					 #内存限制在10M
        
2.创建pod
[root@k8s-master ~]# kubectl create -f pod-resources.yaml
pod/pod-resources created

3.查看pod是否创建成功
[root@k8s-master ~]# kubectl get pod/pod-resources -n dev
NAME            READY   STATUS    RESTARTS   AGE
pod-resources   1/1     Running   0          2m54s

4.查看资源限制是否生效
[root@k8s-master ~]# kubectl describe pod/pod-resources -n dev

Kubernetes configures POD resource quota (7)_nginx

Allocate resources beyond the node

1.修改最小限制为10G,即至少分配给容器10G内存
[root@k8s-master ~]# vim pod-resources.yaml

apiVersion: v1
kind: Pod
metadata:
  name: pod-resources
  namespace: dev
  labels:
    app: base
spec:
  containers:
  - name: nginx-port
    image: nginx:1.18
    resources:
      limits:
        cpu: "2"
        memory: "10Gi"
      requests:
        cpu: "1"
        memory: "10Gi"						#最少分配给容器10G内存


2.创建资源
[root@k8s-master ~]# kubectl apply  -f pod-resources.yaml
pod/pod-resources created

3.已经是启动不起来了
[root@k8s-master ~]# kubectl get pod/pod-resources -n dev
NAME            READY   STATUS    RESTARTS   AGE
pod-resources   0/1     Pending   0          66s
#内存设置的过高也会出现OutOfmemory
[root@k8s-master ~]# kubectl get pod/pod-resources -n dev
NAME                READY   STATUS        RESTARTS   AGE
pod-resources   0/1     OutOfmemory   0          7s

4.查看详细输出信息
[root@k8s-master ~]# kubectl describe pod/pod-resources -n dev

Kubernetes configures POD resource quota (7)_Pod_02