Kubernetes Container、Pod、Namespace内存及CPU限制

2022/8/27 5:24:42

本文主要是介绍Kubernetes Container、Pod、Namespace内存及CPU限制,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

       如果运行的容器没有定义资源(memory、CPU)等限制,那么该容器可以使用宿主机的最大可用资源,直到无资源可用而触发宿主机(OOM Killer),为保证容器资源合理利用,需要在namespace定义LimitRange等限制,这样容器会继承LimitRange中的默认限制。

1、kubernetes对单个容器的memory实现资源限制

case1-pod-memory-limit.yml

#apiVersion: extensions/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
  name: limit-test-deployment
  namespace: magedu
spec:
  replicas: 1
  selector:
    matchLabels: #rs or deployment
      app: limit-test-pod
#    matchExpressions:
#      - {key: app, operator: In, values: [ng-deploy-80,ng-rs-81]}
  template:
    metadata:
      labels:
        app: limit-test-pod
    spec:
      containers:
      - name: limit-test-container
        image: lorel/docker-stress-ng
        resources:
          limits:
            memory: "1024Mi"
          requests:
            memory: "1024Mi"
        #command: ["stress"]
        args: ["--vm", "3", "--vm-bytes", "256M"]
      #nodeSelector:
      #  env: group1

kubectl apply -f case1-pod-memory-limit.yml

describe 看已生效

 top查看也限制在1G以下

 dashboard查看

2、kubernetes对单个容器的CPU及memory实现资源限制

 cat case2-pod-memory-and-cpu-limit.yml

#apiVersion: extensions/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
  name: limit-test-deployment
  namespace: magedu
spec:
  replicas: 1
  selector:
    matchLabels: #rs or deployment
      app: limit-test-pod
#    matchExpressions:
#      - {key: app, operator: In, values: [ng-deploy-80,ng-rs-81]}
  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"]
      #nodeSelector:
      #  env: group1

top查看pod cpu、memory已经限制在2000毫核、512M范围

 3、通过LimitRange对namespace进行限制

  •  限制namespace中每个Pod或容器的最小与最大计算资源
  •  限制namespace中每个Pod或容器计算资源request、limit之间的比例
  •  限制namespace中每个存储卷声明(PersistentVolumeClaim)可使用的最小与最大存储空间
  •  设置namespace中容器默认计算资源的request、limit,并在运行时自动注入到容器中

cat case3-LimitRange.yaml 

apiVersion: v1
kind: LimitRange
metadata:
  name: limitrange-magedu
  namespace: magedu
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              #限制CPU limit/request比值最大为2  
      memory: 2         #限制内存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

 4、kubernetes对cpu与内存RequestRatio比例限制

因为命名空间做了最大请求资源限制,当创建的pod 请超出最大范围时,pod无法创建成功

cat case4-pod-RequestRatio-limit.yaml

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: magedu-wordpress-deployment-label
  name: magedu-wordpress-deployment
  namespace: magedu
spec:
  replicas: 1
  selector:
    matchLabels:
      app: magedu-wordpress-selector
  template:
    metadata:
      labels:
        app: magedu-wordpress-selector
    spec:
      containers:
      - name: magedu-wordpress-nginx-container
        image: nginx:1.16.1
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          protocol: TCP
          name: http
        env:
        - name: "password"
          value: "123456"
        - name: "age"
          value: "18"
        resources:
          limits:
            cpu: 1.5  #此时1.5超出requests请求数512m 2倍数值正常范围
            memory: 1Gi
          requests:
            cpu: 512m
            memory: 512Mi

      - name: magedu-wordpress-php-container
        image: php:5.6-fpm-alpine 
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          protocol: TCP
          name: http
        env:
        - name: "password"
          value: "123456"
        - name: "age"
          value: "18"
        resources:
          limits:
            cpu: 3 
            #cpu: 2
            memory: 1Gi
          requests:
            cpu: 2000m
            memory: 512Mi
---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: magedu-wordpress-service-label
  name: magedu-wordpress-service
  namespace: magedu
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 8080
    nodePort: 30033
  selector:
    app: magedu-wordpress-selector

创建并观察pod与deployment,此时pod无法被创建,deployment  ready个数为0

通过kubectl get deployments magedu-wordpress-deployment -n magedu -ojson 可以查看相关详情,发现有资源被限制提示~

root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# kubectl get deployment -n magedu magedu-wordpress-deployment   -ojson
{
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
        "annotations": {
            "deployment.kubernetes.io/revision": "2",
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"apps/v1\",\"kind\":\"Deployment\",\"metadata\":{\"annotations\":{},\"labels\":{\"app\":\"magedu-wordpress-deployment-label\"},\"name\":\"magedu-wordpress-deployment\",\"namespace\":\"magedu\"},\"spec\":{\"replicas\":3,\"selector\":{\"matchLabels\":{\"app\":\"magedu-wordpress-selector\"}},\"template\":{\"metadata\":{\"labels\":{\"app\":\"magedu-wordpress-selector\"}},\"spec\":{\"containers\":[{\"env\":[{\"name\":\"password\",\"value\":\"123456\"},{\"name\":\"age\",\"value\":\"18\"}],\"image\":\"nginx:1.16.1\",\"imagePullPolicy\":\"Always\",\"name\":\"magedu-wordpress-nginx-container\",\"ports\":[{\"containerPort\":80,\"name\":\"http\",\"protocol\":\"TCP\"}],\"resources\":{\"limits\":{\"cpu\":1.5,\"memory\":\"1Gi\"},\"requests\":{\"cpu\":\"512m\",\"memory\":\"512Mi\"}}},{\"env\":[{\"name\":\"password\",\"value\":\"123456\"},{\"name\":\"age\",\"value\":\"18\"}],\"image\":\"php:5.6-fpm-alpine\",\"imagePullPolicy\":\"Always\",\"name\":\"magedu-wordpress-php-container\",\"ports\":[{\"containerPort\":80,\"name\":\"http\",\"protocol\":\"TCP\"}],\"resources\":{\"limits\":{\"cpu\":2,\"memory\":\"1Gi\"},\"requests\":{\"cpu\":\"2000m\",\"memory\":\"512Mi\"}}}]}}}}\n"
        },
        "creationTimestamp": "2022-08-25T14:58:59Z",
        "generation": 2,
        "labels": {
            "app": "magedu-wordpress-deployment-label"
        },
        "name": "magedu-wordpress-deployment",
        "namespace": "magedu",
        "resourceVersion": "5424603",
        "uid": "23be6ba8-003e-4454-9490-77e41af3220b"
    },
    "spec": {
        "progressDeadlineSeconds": 600,
        "replicas": 3,
        "revisionHistoryLimit": 10,
        "selector": {
            "matchLabels": {
                "app": "magedu-wordpress-selector"
            }
        },
        "strategy": {
            "rollingUpdate": {
                "maxSurge": "25%",
                "maxUnavailable": "25%"
            },
            "type": "RollingUpdate"
        },
        "template": {
            "metadata": {
                "creationTimestamp": null,
                "labels": {
                    "app": "magedu-wordpress-selector"
                }
            },
            "spec": {
                "containers": [
                    {
                        "env": [
                            {
                                "name": "password",
                                "value": "123456"
                            },
                            {
                                "name": "age",
                                "value": "18"
                            }
                        ],
                        "image": "nginx:1.16.1",
                        "imagePullPolicy": "Always",
                        "name": "magedu-wordpress-nginx-container",
                        "ports": [
                            {
                                "containerPort": 80,
                                "name": "http",
                                "protocol": "TCP"
                            }
                        ],
                        "resources": {
                            "limits": {
                                "cpu": "1500m",
                                "memory": "1Gi"
                            },
                            "requests": {
                                "cpu": "512m",
                                "memory": "512Mi"
                            }
                        },
                        "terminationMessagePath": "/dev/termination-log",
                        "terminationMessagePolicy": "File"
                    },
                    {
                        "env": [
                            {
                                "name": "password",
                                "value": "123456"
                            },
                            {
                                "name": "age",
                                "value": "18"
                            }
                        ],
                        "image": "php:5.6-fpm-alpine",
                        "imagePullPolicy": "Always",
                        "name": "magedu-wordpress-php-container",
                        "ports": [
                            {
                                "containerPort": 80,
                                "name": "http",
                                "protocol": "TCP"
                            }
                        ],
                        "resources": {
                            "limits": {
                                "cpu": "2",
                                "memory": "1Gi"
                            },
                            "requests": {
                                "cpu": "2",
                                "memory": "512Mi"
                            }
                        },
                        "terminationMessagePath": "/dev/termination-log",
                        "terminationMessagePolicy": "File"
                    }
                ],
                "dnsPolicy": "ClusterFirst",
                "restartPolicy": "Always",
                "schedulerName": "default-scheduler",
                "securityContext": {},
                "terminationGracePeriodSeconds": 30
            }
        }
    },
    "status": {
        "conditions": [
            {
                "lastTransitionTime": "2022-08-25T14:58:59Z",
                "lastUpdateTime": "2022-08-25T14:58:59Z",
                "message": "Deployment does not have minimum availability.",
                "reason": "MinimumReplicasUnavailable",
                "status": "False",
                "type": "Available"
            },
            {
                "lastTransitionTime": "2022-08-25T14:58:59Z",
                "lastUpdateTime": "2022-08-25T14:58:59Z",
                "message": "pods \"magedu-wordpress-deployment-564d449cb-vncg9\" is forbidden: [maximum cpu usage per Container is 2, but limit is 2500m, cpu max limit to request ratio per Container is 2, but provided ratio is 3.125000, maximum cpu usage per Container is 2, but limit is 3, maximum cpu usage per Pod is 4, but limit is 5500m]",
                "reason": "FailedCreate",
                "status": "True",
                "type": "ReplicaFailure"
            },
            {
                "lastTransitionTime": "2022-08-25T15:22:42Z",
                "lastUpdateTime": "2022-08-25T15:22:42Z",
                "message": "Created new replica set \"magedu-wordpress-deployment-7f8fd59b68\"",
                "reason": "NewReplicaSetCreated",
                "status": "True",
                "type": "Progressing"
            }
        ],
        "observedGeneration": 2,
        "unavailableReplicas": 4
    }
}
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# 
View Code

当cpu 512m 设置为800m ,2*800m=1600m 大于resource cpu 限制1.5,小于命名空间限制数值2,pod被正常创建

cat case5-pod-cpu-limit.yaml

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: magedu-wordpress-deployment-label
  name: magedu-wordpress-deployment
  namespace: magedu
spec:
  replicas: 1
  selector:
    matchLabels:
      app: magedu-wordpress-selector
  template:
    metadata:
      labels:
        app: magedu-wordpress-selector
    spec:
      containers:
      - name: magedu-wordpress-nginx-container
        image: nginx:1.16.1
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          protocol: TCP
          name: http
        env:
        - name: "password"
          value: "123456"
        - name: "age"
          value: "18"
        resources:
          limits:
            cpu: 1.5
            memory: 1Gi
          requests:
            cpu: 800m
            memory: 512Mi
      - name: magedu-wordpress-php-container
        image: php:5.6-fpm-alpine 
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          protocol: TCP
          name: http
        env:
        - name: "password"
          value: "123456"
        - name: "age"
          value: "18"
        resources:
          limits:
            cpu: 2.8
            #cpu: 2
            memory: 1Gi
          requests:
            cpu: 1.5
            memory: 512Mi
---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: magedu-wordpress-service-label
  name: magedu-wordpress-service
  namespace: magedu
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 8080
    nodePort: 30033
  selector:
    app: magedu-wordpress-selector
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# grep -A 5 limits case4-pod-RequestRatio-limit.yaml
          limits:
            cpu: 1.5
            memory: 1Gi
          requests:
            cpu: 512m
            memory: 512Mi
--
          limits:
            cpu: 2
            #cpu: 2
            memory: 1Gi
          requests:
            cpu: 2000m
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# kubectl get pod -n magedu
NAME                                             READY   STATUS    RESTARTS       AGE
deploy-devops-redis-69f975f65d-nmmdk             1/1     Running   0              5d6h
limit-test-deployment-6b84745dc8-cxbfb           1/1     Running   2 (8h ago)     2d8h
magedu-tomcat-app1-deployment-68b54f67d4-8sfff   2/2     Running   20 (15m ago)   13h
magedu-tomcat-app1-deployment-68b54f67d4-crkdt   2/2     Running   21 (14m ago)   13h
magedu-tomcat-app1-deployment-68b54f67d4-q45fq   2/2     Running   19 (17m ago)   13h
mysql-0                                          2/2     Running   0              2d23h
mysql-1                                          2/2     Running   0              2d23h
mysql-2                                          2/2     Running   0              2d23h
redis-0                                          1/1     Running   0              4d23h
redis-1                                          1/1     Running   0              4d23h
redis-2                                          1/1     Running   0              4d23h
redis-3                                          1/1     Running   0              4d23h
redis-4                                          1/1     Running   0              4d23h
redis-5                                          1/1     Running   0              4d23h
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# kubectl get deployments -n magedu
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deploy-devops-redis             1/1     1            1           5d6h
limit-test-deployment           1/1     1            1           2d8h
magedu-tomcat-app1-deployment   3/3     3            3           13h
magedu-wordpress-deployment     0/3     0            0           29m
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# vi case4-pod-RequestRatio-limit.yaml 
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# grep -A 5 limits case5-pod-cpu-limit.yaml
          limits:
            cpu: 1.5
            memory: 1Gi
          requests:
            cpu: 800m
            memory: 512Mi
--
          limits:
            cpu: 2
            #cpu: 2
            memory: 1Gi
          requests:
            cpu: 2000m
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# kubectl apply -f case5-pod-cpu-limit.yaml
deployment.apps/magedu-wordpress-deployment configured
service/magedu-wordpress-service unchanged
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# kubectl get deployments -n magedu
NAME                            READY   UP-TO-DATE   AVAILABLE   AGE
deploy-devops-redis             1/1     1            1           5d6h
limit-test-deployment           1/1     1            1           2d8h
magedu-tomcat-app1-deployment   3/3     3            3           13h
magedu-wordpress-deployment     3/3     3            3           30m
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# kubectl get pod -n magedu
NAME                                             READY   STATUS    RESTARTS       AGE
deploy-devops-redis-69f975f65d-nmmdk             1/1     Running   0              5d6h
limit-test-deployment-6b84745dc8-cxbfb           1/1     Running   2 (8h ago)     2d8h
magedu-tomcat-app1-deployment-68b54f67d4-8sfff   2/2     Running   20 (17m ago)   13h
magedu-tomcat-app1-deployment-68b54f67d4-crkdt   2/2     Running   21 (16m ago)   13h
magedu-tomcat-app1-deployment-68b54f67d4-q45fq   2/2     Running   19 (19m ago)   13h
magedu-wordpress-deployment-67598dcbc6-7q7zd     2/2     Running   0              26s
magedu-wordpress-deployment-67598dcbc6-bb7d4     2/2     Running   0              34s
magedu-wordpress-deployment-67598dcbc6-qbbn4     2/2     Running   0              45s
mysql-0                                          2/2     Running   0              2d23h
mysql-1                                          2/2     Running   0              2d23h
mysql-2                                          2/2     Running   0              2d23h
redis-0                                          1/1     Running   0              4d23h
redis-1                                          1/1     Running   0              4d23h
redis-2                                          1/1     Running   0              4d23h
redis-3                                          1/1     Running   0              4d23h
redis-4                                          1/1     Running   0              4d23h
redis-5                                          1/1     Running   0              4d23h
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# 

5、限定某个对象类型(pod、service)可创建的总数

清掉之前设置的环境参数,保持一个干净的环境

kubectl delete -f case1-pod-memory-limit.yml -f case2-pod-memory-and-cpu-limit.yml  
kubectl delete -f case4-pod-RequestRatio-limit.yaml  case5-pod-cpu-limit.yaml 

cat case6-ResourceQuota-magedu.yaml  

apiVersion: v1
kind: ResourceQuota
metadata:
  name: quota-magedu
  namespace: magedu
spec:
  hard:
    requests.cpu: "8"
    limits.cpu: "8"
    requests.memory: 8Gi
    limits.memory: 8Gi
    requests.nvidia.com/gpu: 4
    pods: "6"
    services: "6"

针对后期创建pod个数做限制

创建pod测试

cat case7-namespace-pod-limit-test.yaml

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: magedu-nginx-deployment-label
  name: magedu-nginx-deployment
  namespace: magedu
spec:
  replicas: 1
  selector:
    matchLabels:
      app: magedu-nginx-selector
  template:
    metadata:
      labels:
        app: magedu-nginx-selector
    spec:
      containers:
      - name: magedu-nginx-container
        image: nginx:1.16.1
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          protocol: TCP
          name: http
        env:
        - name: "password"
          value: "123456"
        - name: "age"
          value: "18"
        resources:
          limits:
            cpu: 1
            memory: 1Gi
          requests:
            cpu: 500m
            memory: 512Mi

---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: magedu-nginx-service-label
  name: magedu-nginx-service
  namespace: magedu
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 8080
    nodePort: 30033
  selector:
    app: magedu-nginx-selector

此时创建pod失败,提示超出资源限制请求,pod个数已经被占用

修改magedu  namespace资源限制

cat case6-ResourceQuota-magedu.yaml 

apiVersion: v1
kind: ResourceQuota
metadata:
  name: quota-magedu
  namespace: magedu
spec:
  hard:
    requests.cpu: "20"
    limits.cpu: "20"
    requests.memory: 20Gi
    limits.memory: 20Gi
    requests.nvidia.com/gpu: 10
    pods: "20"
    services: "20"
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# vi case6-ResourceQuota-magedu.yaml 
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# 
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# kubectl apply -f case6-ResourceQuota-magedu.yaml 
resourcequota/quota-magedu configured
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# kubectl describe ns -n magedu magedu
Name:         magedu
Labels:       kubernetes.io/metadata.name=magedu
Annotations:  <none>
Status:       Active

Resource Quotas
  Name:                    quota-magedu
  Resource                 Used    Hard
  --------                 ---     ---
  limits.cpu               4500m   20
  limits.memory            3Gi     20Gi
  pods                     13      20
  requests.cpu             7800m   20
  requests.memory          9444Mi  20Gi
  requests.nvidia.com/gpu  0       10
  services                 6       20

No LimitRange resource.
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case#  
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# kubectl apply -f case7-namespace-pod-limit-test.yaml 
deployment.apps/magedu-nginx-deployment configured
service/magedu-nginx-service created
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# kubectl get pod -n magedu
NAME                                             READY   STATUS    RESTARTS         AGE
deploy-devops-redis-69f975f65d-nmmdk             1/1     Running   0                5d7h
magedu-nginx-deployment-658df6889f-qnqm7         1/1     Running   0                2m19s
magedu-nginx-deployment-658df6889f-tntkx         1/1     Running   0                2m19s
magedu-tomcat-app1-deployment-68b54f67d4-8sfff   2/2     Running   21 (2m52s ago)   14h
magedu-tomcat-app1-deployment-68b54f67d4-crkdt   2/2     Running   22 (21m ago)     14h
magedu-tomcat-app1-deployment-68b54f67d4-q45fq   2/2     Running   22 (14m ago)     14h
mysql-0                                          2/2     Running   0                3d
mysql-1                                          2/2     Running   0                3d
mysql-2                                          2/2     Running   0                3d
redis-0                                          1/1     Running   0                5d
redis-1                                          1/1     Running   0                5d
redis-2                                          1/1     Running   0                5d
redis-3                                          1/1     Running   0                5d
redis-4                                          1/1     Running   0                5d
redis-5                                          1/1     Running   0                5d
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# 

新建文件让其超出namespace限制的cpu、内存

cat case8-namespace-cpu-limit-test.yaml

kind: Deployment
apiVersion: apps/v1
metadata:
  labels:
    app: magedu-nginx-deployment-label
  name: magedu-nginx-deployment
  namespace: magedu
spec:
  replicas: 3
  selector:
    matchLabels:
      app: magedu-nginx-selector
  template:
    metadata:
      labels:
        app: magedu-nginx-selector
    spec:
      containers:
      - name: magedu-nginx-container
        image: nginx:1.16.1
        imagePullPolicy: Always
        ports:
        - containerPort: 80
          protocol: TCP
          name: http
        env:
        - name: "password"
          value: "123456"
        - name: "age"
          value: "18"
        resources:
          limits:
            cpu: 8
            memory: 4Gi
          requests:
            cpu: 4
            memory: 4Gi

---
kind: Service
apiVersion: v1
metadata:
  labels:
    app: magedu-nginx-service-label
  name: magedu-nginx-service
  namespace: magedu
spec:
  type: NodePort
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 8080
    nodePort: 30033
  selector:
    app: magedu-nginx-selector
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# kubectl describe ns -n magedu magedu
Name:         magedu
Labels:       kubernetes.io/metadata.name=magedu
Annotations:  <none>
Status:       Active

Resource Quotas
  Name:                    quota-magedu
  Resource                 Used     Hard
  --------                 ---      ---
  limits.cpu               6500m    20
  limits.memory            5Gi      20Gi
  pods                     15       20
  requests.cpu             8800m    20
  requests.memory          10468Mi  20Gi
  requests.nvidia.com/gpu  0        10
  services                 7        20

No LimitRange resource.
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# vi case8-namespace-cpu-limit-test.yaml 
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# 
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# kubectl apply -f case8-namespace-cpu-limit-test.yaml 
deployment.apps/magedu-nginx-deployment configured
service/magedu-nginx-service unchanged
此时pod已经无法在创建了
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# kubectl get pod -n magedu 
NAME                                             READY   STATUS    RESTARTS         AGE
deploy-devops-redis-69f975f65d-nmmdk             1/1     Running   0                5d7h
magedu-nginx-deployment-658df6889f-tntkx         1/1     Running   0                8m41s
magedu-nginx-deployment-7c8f464cfc-frplk         1/1     Running   0                41s
magedu-tomcat-app1-deployment-68b54f67d4-8sfff   2/2     Running   21 (9m14s ago)   14h
magedu-tomcat-app1-deployment-68b54f67d4-crkdt   2/2     Running   22 (28m ago)     14h
magedu-tomcat-app1-deployment-68b54f67d4-q45fq   2/2     Running   23 (43s ago)     14h
mysql-0                                          2/2     Running   0                3d
mysql-1                                          2/2     Running   0                3d
mysql-2                                          2/2     Running   0                3d
redis-0                                          1/1     Running   0                5d
redis-1                                          1/1     Running   0                5d
redis-2                                          1/1     Running   0                5d
redis-3                                          1/1     Running   0                5d
redis-4                                          1/1     Running   0                5d
redis-5                                          1/1     Running   0                5d
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# 

查看deployments 可以提示资源限制

kubectl get deployment -n magedu magedu-nginx-deployment  -o json

root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# kubectl get deployment -n magedu magedu-nginx-deployment  -o json
{
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
        "annotations": {
            "deployment.kubernetes.io/revision": "2",
            "kubectl.kubernetes.io/last-applied-configuration": "{\"apiVersion\":\"apps/v1\",\"kind\":\"Deployment\",\"metadata\":{\"annotations\":{},\"labels\":{\"app\":\"magedu-nginx-deployment-label\"},\"name\":\"magedu-nginx-deployment\",\"namespace\":\"magedu\"},\"spec\":{\"replicas\":3,\"selector\":{\"matchLabels\":{\"app\":\"magedu-nginx-selector\"}},\"template\":{\"metadata\":{\"labels\":{\"app\":\"magedu-nginx-selector\"}},\"spec\":{\"containers\":[{\"env\":[{\"name\":\"password\",\"value\":\"123456\"},{\"name\":\"age\",\"value\":\"18\"}],\"image\":\"nginx:1.16.1\",\"imagePullPolicy\":\"Always\",\"name\":\"magedu-nginx-container\",\"ports\":[{\"containerPort\":80,\"name\":\"http\",\"protocol\":\"TCP\"}],\"resources\":{\"limits\":{\"cpu\":8,\"memory\":\"4Gi\"},\"requests\":{\"cpu\":4,\"memory\":\"4Gi\"}}}]}}}}\n"
        },
        "creationTimestamp": "2022-08-25T16:09:16Z",
        "generation": 4,
        "labels": {
            "app": "magedu-nginx-deployment-label"
        },
        "name": "magedu-nginx-deployment",
        "namespace": "magedu",
        "resourceVersion": "5435774",
        "uid": "7a034e10-8740-4a86-b067-3eba9549c717"
    },
    "spec": {
        "progressDeadlineSeconds": 600,
        "replicas": 3,
        "revisionHistoryLimit": 10,
        "selector": {
            "matchLabels": {
                "app": "magedu-nginx-selector"
            }
        },
        "strategy": {
            "rollingUpdate": {
                "maxSurge": "25%",
                "maxUnavailable": "25%"
            },
            "type": "RollingUpdate"
        },
        "template": {
            "metadata": {
                "creationTimestamp": null,
                "labels": {
                    "app": "magedu-nginx-selector"
                }
            },
            "spec": {
                "containers": [
                    {
                        "env": [
                            {
                                "name": "password",
                                "value": "123456"
                            },
                            {
                                "name": "age",
                                "value": "18"
                            }
                        ],
                        "image": "nginx:1.16.1",
                        "imagePullPolicy": "Always",
                        "name": "magedu-nginx-container",
                        "ports": [
                            {
                                "containerPort": 80,
                                "name": "http",
                                "protocol": "TCP"
                            }
                        ],
                        "resources": {
                            "limits": {
                                "cpu": "8",
                                "memory": "4Gi"
                            },
                            "requests": {
                                "cpu": "4",
                                "memory": "4Gi"
                            }
                        },
                        "terminationMessagePath": "/dev/termination-log",
                        "terminationMessagePolicy": "File"
                    }
                ],
                "dnsPolicy": "ClusterFirst",
                "restartPolicy": "Always",
                "schedulerName": "default-scheduler",
                "securityContext": {},
                "terminationGracePeriodSeconds": 30
            }
        }
    },
    "status": {
        "availableReplicas": 2,
        "conditions": [
            {
                "lastTransitionTime": "2022-08-25T16:20:13Z",
                "lastUpdateTime": "2022-08-25T16:28:19Z",
                "message": "ReplicaSet \"magedu-nginx-deployment-7c8f464cfc\" is progressing.",
                "reason": "ReplicaSetUpdated",
                "status": "True",
                "type": "Progressing"
            },
            {
                "lastTransitionTime": "2022-08-25T16:28:19Z",
                "lastUpdateTime": "2022-08-25T16:28:19Z",
                "message": "pods \"magedu-nginx-deployment-7c8f464cfc-xjl7d\" is forbidden: exceeded quota: quota-magedu, requested: limits.cpu=8, used: limits.cpu=14500m, limited: limits.cpu=20",
                "reason": "FailedCreate",
                "status": "True",
                "type": "ReplicaFailure"
            },
            {
                "lastTransitionTime": "2022-08-25T16:29:23Z",
                "lastUpdateTime": "2022-08-25T16:29:23Z",
                "message": "Deployment does not have minimum availability.",
                "reason": "MinimumReplicasUnavailable",
                "status": "False",
                "type": "Available"
            }
        ],
        "observedGeneration": 4,
        "readyReplicas": 2,
        "replicas": 2,
        "unavailableReplicas": 2,
        "updatedReplicas": 1
    }
}
root@easzlab-deploy:~/20220821-case/20220821/magedu-limit-case# 
View Code



这篇关于Kubernetes Container、Pod、Namespace内存及CPU限制的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程