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. 本任务展示了如何为某一名字空间(namespace)设置Pod配额以限制可以在名字空间中运行的Pod数量。 配额通过ResourceQuota对象设置。
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.
创建名字空间
创建一个单独的名字空间,以便于隔离您在本练习中创建的资源与集群的其他资源。
kubectl create namespace quota-pod-example
创建ResourceQuota对象
以下展示了ResourceQuota对象的配置文件内容:
quota-pod.yaml |
---|
apiVersion: v1 kind: ResourceQuota metadata: name: pod-demo spec: hard: pods: "2" |
下面,首先创建ResourceQuota对象
kubectl create -f https://k8s.io/docs/tasks/administer-cluster/quota-pod.yaml --namespace=quota-pod-example
然后可以通过以下命令查看ResourceQuota对象的详细信息:
kubectl get resourcequota pod-demo --namespace=quota-pod-example --output=yaml
命令输出显示了这个名字空间的Pod配额是2,由于目前没有Pod运行,所有配额并没有被使用。
spec: hard: pods: "2" status: hard: pods: "2" used: pods: "0"
下面展示的是一个Deployment的配置文件:
quota-pod-deployment.yaml |
---|
apiVersion: apps/v1beta1 kind: Deployment metadata: name: pod-quota-demo spec: replicas: 3 template: metadata: labels: purpose: quota-demo spec: containers: - name: pod-quota-demo image: nginx |
从配置文件可以看到,replicas: 3将令Kubernetes尝试创建3个Pod,所有的Pod实例都将运行同样的应用程序。
接下来尝试创建这个Deployment:
kubectl create -f https://k8s.io/docs/tasks/administer-cluster/quota-pod-deployment.yaml --namespace=quota-pod-example
并通过以下命令查看Deployment的详细信息:
kubectl get deployment pod-quota-demo --namespace=quota-pod-example --output=yaml
从命令输出可以看到尽管在Deployment中我们设置了需要启动3个Pod实例,但由于配额的存在,只有两个Pod被成功创建。
spec: ... replicas: 3 ... status: availableReplicas: 2 ... lastUpdateTime: 2017-07-07T20:57:05Z message: 'unable to create pods: pods "pod-quota-demo-1650323038-" is forbidden: exceeded quota: pod-demo, requested: pods=1, used: pods=2, limited: pods=2'
练习环境的清理
通过删除名字空间即可完成环境的清理:
kubectl delete namespace quota-pod-example
What’s next
对于集群管理员
- 为 Namespace 设置最小和最大内存限制
- 为 Namespace 配置默认内存请求和限制
- 为 Namespace 配置默认 CPU 请求和限制
- 为 Namespace 配置最小和最大 CPU 限制
- 为 Namespace 配置内存和 CPU 配额
- 为 Namespace 配置 Pod 配额
- 为 API 对象配置配额
创建quota-pod-deployment.yaml失败了
kubectl apply -f quota-pod-deployment.yaml –namespace=quota-pod-example
error: error validating “quota-pod-deployment.yaml”: error validating data: ValidationError(Deployment.spec): missing required field “selector” in io.k8s.api.apps.v1.DeploymentSpec; if you choose to ignore these errors, turn validation off with –validate=false
apiVersion: apps/v1
kind: Deployment
metadata:
name: pod-quota-demo
spec:
selector:
matchLabels:
purpose: quota-demo
replicas: 3
template:
metadata:
labels:
purpose: quota-demo
spec:
containers:
– name: pod-quota-demo
image: nginx