Configure Quotas for API Objects

This page shows how to configure quotas for API objects, including PersistentVolumeClaims and Services. A quota restricts the number of objects, of a particular type, that can be created 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-object-example

Create a ResourceQuota

Here is the configuration file for a ResourceQuota object:

admin/resource/quota-objects.yaml Configure Quotas for API Objects - 图1

  1. apiVersion: v1
  2. kind: ResourceQuota
  3. metadata:
  4. name: object-quota-demo
  5. spec:
  6. hard:
  7. persistentvolumeclaims: "1"
  8. services.loadbalancers: "2"
  9. services.nodeports: "0"

Create the ResourceQuota:

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

View detailed information about the ResourceQuota:

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

The output shows that in the quota-object-example namespace, there can be at most one PersistentVolumeClaim, at most two Services of type LoadBalancer, and no Services of type NodePort.

  1. status:
  2. hard:
  3. persistentvolumeclaims: "1"
  4. services.loadbalancers: "2"
  5. services.nodeports: "0"
  6. used:
  7. persistentvolumeclaims: "0"
  8. services.loadbalancers: "0"
  9. services.nodeports: "0"

Create a PersistentVolumeClaim

Here is the configuration file for a PersistentVolumeClaim object:

admin/resource/quota-objects-pvc.yaml Configure Quotas for API Objects - 图2

  1. apiVersion: v1
  2. kind: PersistentVolumeClaim
  3. metadata:
  4. name: pvc-quota-demo
  5. spec:
  6. storageClassName: manual
  7. accessModes:
  8. - ReadWriteOnce
  9. resources:
  10. requests:
  11. storage: 3Gi

Create the PersistentVolumeClaim:

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

Verify that the PersistentVolumeClaim was created:

  1. kubectl get persistentvolumeclaims --namespace=quota-object-example

The output shows that the PersistentVolumeClaim exists and has status Pending:

  1. NAME STATUS
  2. pvc-quota-demo Pending

Attempt to create a second PersistentVolumeClaim

Here is the configuration file for a second PersistentVolumeClaim:

admin/resource/quota-objects-pvc-2.yaml Configure Quotas for API Objects - 图3

  1. apiVersion: v1
  2. kind: PersistentVolumeClaim
  3. metadata:
  4. name: pvc-quota-demo-2
  5. spec:
  6. storageClassName: manual
  7. accessModes:
  8. - ReadWriteOnce
  9. resources:
  10. requests:
  11. storage: 4Gi

Attempt to create the second PersistentVolumeClaim:

  1. kubectl apply -f https://k8s.io/examples/admin/resource/quota-objects-pvc-2.yaml --namespace=quota-object-example

The output shows that the second PersistentVolumeClaim was not created, because it would have exceeded the quota for the namespace.

  1. persistentvolumeclaims "pvc-quota-demo-2" is forbidden:
  2. exceeded quota: object-quota-demo, requested: persistentvolumeclaims=1,
  3. used: persistentvolumeclaims=1, limited: persistentvolumeclaims=1

Notes

These are the strings used to identify API resources that can be constrained by quotas:

StringAPI Object
“pods”Pod
“services”Service
“replicationcontrollers”ReplicationController
“resourcequotas”ResourceQuota
“secrets”Secret
“configmaps”ConfigMap
“persistentvolumeclaims”PersistentVolumeClaim
“services.nodeports”Service of type NodePort
“services.loadbalancers”Service of type LoadBalancer

Clean up

Delete your namespace:

  1. kubectl delete namespace quota-object-example

What’s next

For cluster administrators

For app developers