kubernetes资源限制Container pod namespace

1.对单个容器cpu和mem的资源限制

#apiVersion: extensions/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
  name: limit-test-deployment
  namespace: test
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: "1.2" #对请求使用最大cpu的限制为1.2核
            memory: "512Mi" #对请求使用最大的内存为512Mi
          requests:
            memory: "100Mi" #启动pod使用请求使用最小的内存大小为100Mi
            cpu: "500m" #启动pod使用请求使用最小的cpu大小为0.5核
        #command: ["stress"]
        args: ["--vm", "2", "--vm-bytes", "256M"] #启用两个vm 都用256m的内存
      #nodeSelector:
      #  env: group1

2.对整个namespace的资源限制

apiVersion: v1
kind: LimitRange
metadata:
  name: limitrange-test
  namespace: test #限制所有ns名为test里面的所有的定义的下列资源类型
spec:
  limits:
  - type: Container       #限制的资源类型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  #限制的资源类型Pod
    max:
      cpu: "4"            #限制单个Pod的最大CPU
      memory: "4Gi"       #限制单个Pod最大内存
  - type: PersistentVolumeClaim #限制的资源类型PVC
    max:
      storage: 50Gi        #限制PVC最大的requests.storage
    min:
      storage: 30Gi        #限制PVC最小的requests.storage

[root@master-etcd1-234-31 namespaces]# kubectl describe limitranges -n test
Name:                  limitrange-test
Namespace:             test
Type                   Resource  Min    Max   Default Request  Default Limit  Max Limit/Request Ratio
----                   --------  ---    ---   ---------------  -------------  -----------------------
Container              cpu       500m   2     500m             500m           2
Container              memory    512Mi  2Gi   512Mi            512Mi          2
Pod                    memory    -      4Gi   -                -              -
Pod                    cpu       -      4     -                -              -
PersistentVolumeClaim  storage   30Gi   50Gi  -                -              -

3.项目限制 namespace

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