Configure a Pod Quota for 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. If you do not already have a cluster, you can create one by using minikube or you can use one of these Kubernetes playgrounds:

To check the version, enter kubectl version.

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 the configuration file for a ResourceQuota object:

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 the configuration file 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 the configuration file, replicas: 3 tells Kubernetes to attempt to create three 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.

  1. spec:
  2. ...
  3. replicas: 3
  4. ...
  5. status:
  6. availableReplicas: 2
  7. ...
  8. lastUpdateTime: 2017-07-07T20: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'

Clean up

Delete your namespace:

  1. kubectl delete namespace quota-pod-example

What’s next

For cluster administrators

For app developers