> For the complete documentation index, see [llms.txt](https://bohans.gitbook.io/devops/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bohans.gitbook.io/devops/kubernetes/ji-chu-zhi-shi/kubernetes-zi-yuan-guan-li/resourcequota.md).

# ResourceQuota

<mark style="color:blue;">**资源配额**</mark>，通过 ResourceQuota 对象来定义，<mark style="color:blue;">**对每个命名空间的资源消耗总量提供限制**</mark>。

* 它可以限制命名空间中**某种类型的对象的总数目**上限；
* 也可以限制命名空间中的 **Pod 可以使用的计算资源的总上限**。

{% hint style="warning" %}

## <mark style="color:orange;">注意：</mark>

<mark style="color:orange;">**如果命名空间下的计算资源 （如 cpu 和 memory）的配额被启用， 则用户必须为这些资源设定请求值（request）和约束值（limit），否则配额系统将拒绝 Pod 的创建。**</mark>&#x20;

### <mark style="color:blue;">**提示：可使用 LimitRanger 准入控制器来为没有设置计算资源需求的 Pod 设置默认值。**</mark>

{% endhint %}

## 定义一个ResourceQuota

**资源配额可以通过一个YAML文件进行创建**，比如定义一个比较常用的ResourceQuota如下：

<details>

<summary>resource-quota.yaml</summary>

```yaml
apiVersion: v1
kind: ResourceQuota
metadata:
  name: resources-quota
spec:
  hard:
    requests.cpu: "1"
    requests.memory: 1Gi
    limits.cpu: "2"
    limits.memory: 2Gi
    requests.nvidia.com/gpu: 4
    configmaps: "10"
    persistentvolumeclaims: "4"
    pods: "4"
    replicationcontrollers: "20"
    secrets: "10"
    services: "10"
    services.loadbalancers: "2"
```

</details>

## ResourceQuota的使用

首先创建一个用于测试的Namespace：

```properties
$ kubectl create ns quota-example
namespace/quota-example created
```

创建ResourceQuota：

```properties
$ kubectl create -f resource-quota.yaml -n quota-example
resourcequota/resources-quota created
```

查看创建的ResourceQuota状态：

```properties
$ kubectl describe quota resources-quota -n quota-example
Name:                    resources-quota
Namespace:               quota-example
Resource                 Used  Hard
--------                 ----  ----
configmaps               1     10
limits.cpu               0     2
limits.memory            0     2Gi
persistentvolumeclaims   0     4
pods                     0     4
replicationcontrollers   0     20
requests.cpu             0     1
requests.memory          0     1Gi
requests.nvidia.com/gpu  0     4
secrets                  0     10
services                 0     10
services.loadbalancers   0     2
```
