Configure Quotas for API Objects

    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 or you can use one of these Kubernetes playgrounds:

    To check the version, enter .

    Create a namespace

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

    Here is the configuration file for a ResourceQuota object:

    admin/resource/quota-objects.yaml

    1. apiVersion: v1
    2. kind: ResourceQuota
    3. metadata:
    4. name: object-quota-demo
    5. spec:
    6. hard:
    7. persistentvolumeclaims: "1"
    8. 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.

    Create a PersistentVolumeClaim

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

    1. apiVersion: v1
    2. metadata:
    3. name: pvc-quota-demo
    4. spec:
    5. storageClassName: manual
    6. accessModes:
    7. - ReadWriteOnce
    8. resources:
    9. requests:
    10. 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:

    Here is the configuration file for a second PersistentVolumeClaim:

    admin/resource/quota-objects-pvc-2.yaml

    1. apiVersion: v1
    2. metadata:
    3. spec:
    4. storageClassName: manual
    5. accessModes:
    6. - ReadWriteOnce
    7. resources:
    8. requests:
    9. 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,

    Notes

    Delete your namespace:

    What’s next

    For app developers