Configure a Pod Quota for a Namespace

Restrict how many Pods you can create within a namespace.

This page shows how to set a quota for the total number of Pods that can run in a Namespace. You specify quotas in a ResourceQuota object.

Before you begin

You need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. It is recommended to run this tutorial on a cluster with at least two nodes that are not acting as control plane hosts. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

You must have access to create namespaces in your cluster.

Create a namespace

Create a namespace so that the resources you create in this exercise are isolated from the rest of your cluster.

  1. kubectl create namespace quota-pod-example

Create a ResourceQuota

Here is an example manifest for a ResourceQuota:

admin/resource/quota-pod.yaml Configure a Pod Quota for a Namespace - 图1

  1. apiVersion: v1
  2. kind: ResourceQuota
  3. metadata:
  4. name: pod-demo
  5. spec:
  6. hard:
  7. pods: "2"

Create the ResourceQuota:

  1. kubectl apply -f https://k8s.io/examples/admin/resource/quota-pod.yaml --namespace=quota-pod-example

View detailed information about the ResourceQuota:

  1. kubectl get resourcequota pod-demo --namespace=quota-pod-example --output=yaml

The output shows that the namespace has a quota of two Pods, and that currently there are no Pods; that is, none of the quota is used.

  1. spec:
  2. hard:
  3. pods: "2"
  4. status:
  5. hard:
  6. pods: "2"
  7. used:
  8. pods: "0"

Here is an example manifest for a Deployment:

admin/resource/quota-pod-deployment.yaml Configure a Pod Quota for a Namespace - 图2

  1. apiVersion: apps/v1
  2. kind: Deployment
  3. metadata:
  4. name: pod-quota-demo
  5. spec:
  6. selector:
  7. matchLabels:
  8. purpose: quota-demo
  9. replicas: 3
  10. template:
  11. metadata:
  12. labels:
  13. purpose: quota-demo
  14. spec:
  15. containers:
  16. - name: pod-quota-demo
  17. image: nginx

In that manifest, replicas: 3 tells Kubernetes to attempt to create three new Pods, all running the same application.

Create the Deployment:

  1. kubectl apply -f https://k8s.io/examples/admin/resource/quota-pod-deployment.yaml --namespace=quota-pod-example

View detailed information about the Deployment:

  1. kubectl get deployment pod-quota-demo --namespace=quota-pod-example --output=yaml

The output shows that even though the Deployment specifies three replicas, only two Pods were created because of the quota you defined earlier:

  1. spec:
  2. ...
  3. replicas: 3
  4. ...
  5. status:
  6. availableReplicas: 2
  7. ...
  8. lastUpdateTime: 2021-04-02T20:57:05Z
  9. message: 'unable to create pods: pods "pod-quota-demo-1650323038-" is forbidden:
  10. exceeded quota: pod-demo, requested: pods=1, used: pods=2, limited: pods=2'

Choice of resource

In this task you have defined a ResourceQuota that limited the total number of Pods, but you could also limit the total number of other kinds of object. For example, you might decide to limit how many CronJobs that can live in a single namespace.

Clean up

Delete your namespace:

  1. kubectl delete namespace quota-pod-example

What’s next

For cluster administrators

For app developers