Kubernetes 为 Namespace 配置Pod配额

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

对于集群管理员

对于应用开发者

译者:xingzhou / 原文链接

K8S中文社区微信公众号

《Kubernetes 为 Namespace 配置Pod配额》有2个想法

  1. 创建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

  2. 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

发表评论

电子邮件地址不会被公开。

Time limit exceeded. Please complete the captcha once again.