当前位置:网站首页>Resourcequota for detailed explanation of kubernetes resource constraints

Resourcequota for detailed explanation of kubernetes resource constraints

2022-06-24 05:26:00 Honest1y

1 brief introduction

Kubernetes There are two ways to limit resources :ResourceQuota and LimitRange.

among ResourceQuota Is aimed at namespace Do resource constraints , and LimitRange Is aimed at namespace Resource constraints for each component in .

When more than one namespace When sharing the same cluster, there may be one namespace The resource quota used exceeds its fair quota , Lead to other namespace Of resources are occupied . At this time, we can work for each namespace Create a ResourceQuota,

The user is in namespace When creating resources in ,quota The quota system tracks usage , To make sure it doesn't exceed ResourceQuota The limit value of . If creating or updating resources violates quota constraints , be HTTP The status code will cause the request to fail 403 FORBIDDEN. The change of resource quota will not affect the pod.

apiserver The startup parameters for are usually kubernetes Enabled by default ResourceQuota, stay apiserver Start parameter of –enable-admission-plugins= If any ResourceQuota Start .

2 Use

Create a test using NS

[~] kubectl create ns testquota
namespace/testquota created
[~] kubectl get ns | grep quota
testquota         Active   3m41s

Create a ResourceQuota

[yaml] cat resourcequota.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
  name: testquota-resources
  namespace: testquota
spec:
  hard:
    pods: "4"
    requests.cpu: "1"
    requests.memory: 1Gi
    limits.cpu: "2"
    limits.memory: 2Gi
[yaml] kubectl apply -f resourcequota.yaml
resourcequota/testquota-resources created
[yaml] kubectl describe resourcequotas -n testquota testquota-resources
Name:            testquota-resources
Namespace:       testquota
Resource         Used  Hard
--------         ----  ----
limits.cpu       0     2
limits.memory    0     2Gi
pods             0     4
requests.cpu     0     1
requests.memory  0     1Gi

Create a Deployment And limit resources

[yaml] cat quota-deploy.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: testquota
  labels:
    app: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        resources:
          requests:
            memory: "100Mi"
            cpu: "100m"
          limits:
            memory: "200Mi"
            cpu: "500m"
[yaml] kubectl apply -f quota-deploy.yaml
deployment.apps/nginx-deployment created
[yaml] kubectl get po -n testquota
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-7c6bbc77d8-mfxnl   1/1     Running   0          9s

modify deployment replications , Make the total resources used exceed ResourceQuota Resources defined in

First, check the current resource usage

[yaml] kubectl describe resourcequotas -n testquota testquota-resources
Name:            testquota-resources
Namespace:       testquota
Resource         Used   Hard
--------         ----   ----
limits.cpu       500m   2
limits.memory    200Mi  2Gi
pods             1      4
requests.cpu     100m   1
requests.memory  100Mi  1Gi

Number of modified copies

[yaml] kubectl scale deployment -n testquota nginx-deployment --replicas=4
deployment.apps/nginx-deployment scaled
[yaml] kubectl get po -n testquota
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-7c6bbc77d8-5mbc6   1/1     Running   0          7s
nginx-deployment-7c6bbc77d8-ld69h   1/1     Running   0          7s
nginx-deployment-7c6bbc77d8-mfxnl   1/1     Running   0          5m18s
nginx-deployment-7c6bbc77d8-sdcxb   1/1     Running   0          7s

Current resource usage

[yaml] kubectl describe resourcequotas -n testquota testquota-resources
Name:            testquota-resources
Namespace:       testquota
Resource         Used   Hard
--------         ----   ----
limits.cpu       2      2
limits.memory    800Mi  2Gi
pods             4      4
requests.cpu     400m   1
requests.memory  400Mi  1Gi

Create another deployment

[yaml] kubectl apply -f quota-deploy-2.yaml
deployment.apps/nginx2-deployment created
[yaml] kubectl get deployment -n testquota
NAME                READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment    4/4     4            4           7m48s
nginx2-deployment   0/1     0            0           34s

You can see , although deployment Created successfully , But the corresponding pod, You can see deployment Report errors

[yaml] kubectl describe deployments -n testquota nginx2-deployment
Name:                   nginx2-deployment
Namespace:              testquota
...
Replicas:               1 desired | 0 updated | 0 total | 0 available | 1 unavailable
NewReplicaSet:     nginx2-deployment-7c6bbc77d8 (0/1 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  98s   deployment-controller  Scaled up replica set nginx2-deployment-7c6bbc77d8 to 1

It can be seen that this is due to the pod The sum of has exceeded namespace Cannot create due to the total resource limit pod.

3 Common resource types

原网站

版权声明
本文为[Honest1y]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/08/20210812234516325J.html