Files
Rui Chen 6e094fa640 Merge branch 'master' of git://github.com/kubernetes/website into release-1.12
* 'master' of git://github.com/kubernetes/website: (222 commits)
  Add temporary owners for 1.13 release (#11453)
  fix Minikube 404 error. (#11461)
  Resolve conflicts against dev-1.13 for /ko contents (#11439)
  replace `run` with `create deployment` (#11392)
  Updated list all pods with -o wide comment (#11394)
  fix broken link for KubeletConfiguration (#11423)
  Update on pod-priority-preemption.md (#11418)
  Add guidelines for working with localized content (#11415)
  Update what-is-kubernetes.md (#11399)
  Remove redundant close tags and little bit formatting (#11389)
  Add SysEleven MetaKube as hosted solution (#11393)
  Add rui to sig-docs-zh team (#11391)
  fix Improper translation (#11384)
  Add pigletfly(WangBing) as a sig-docs-zh-reviewer (#11370)
  update link to CloudProvider Interface (#11228)
  Fix the "my-scheduler-as-kube-scheduler" ClusterRoleBinding. (#11112)
  fix non-existing "CloudProvider Interface" link (#10953)
  Updated ingress.md (#11213)
  Further updates to TLS Bootstrapping (#11258)
  Updated 'exec' description (#11365)
  ...
2018-12-03 17:11:43 -05:00

8.3 KiB
Raw Permalink Blame History

title, content_template, weight
title content_template weight
为命名空间配置内存和 CPU 配额 templates/task 50

{{% capture overview %}}

本文介绍怎样为命名空间设置容器可用的内存和 CPU 总量。你可以通过 [ResourceQuota](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#resourcequota-v1-core) 对象设置配额.

{{% /capture %}}

{{% capture prerequisites %}}

{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}

集群中每个节点至少有1 GiB的内存。

{{% /capture %}}

{{% capture steps %}}

创建命名空间

创建一个命名空间,以便本练习中创建的资源和集群的其余部分相隔离。

kubectl create namespace quota-mem-cpu-example

创建 ResourceQuota

这里给出一个 ResourceQuota 对象的配置文件:

{{< codenew file="admin/resource/quota-mem-cpu.yaml" >}}

创建 ResourceQuota

kubectl create -f https://k8s.io/examples/admin/resource/quota-mem-cpu.yaml --namespace=quota-mem-cpu-example

查看 ResourceQuota 详情:

kubectl get resourcequota mem-cpu-demo --namespace=quota-mem-cpu-example --output=yaml

ResourceQuota 在 quota-mem-cpu-example 命名空间中设置了如下要求:

  • 每个容器必须有内存请求和限制,以及 CPU 请求和限制。
  • 所有容器的内存请求总和不能超过1 GiB。
  • 所有容器的内存限制总和不能超过2 GiB。
  • 所有容器的 CPU 请求总和不能超过1 cpu。
  • 所有容器的 CPU 限制总和不能超过2 cpu。

创建 Pod

这里给出 Pod 的配置文件:

{{< codenew file="admin/resource/quota-mem-cpu-pod.yaml" >}}

创建 Pod

kubectl create -f https://k8s.io/examples/admin/resource/quota-mem-cpu-pod.yaml --namespace=quota-mem-cpu-example

检查下 Pod 中的容器在运行:

kubectl get pod quota-mem-cpu-demo --namespace=quota-mem-cpu-example

再查看 ResourceQuota 的详情:

kubectl get resourcequota mem-cpu-demo --namespace=quota-mem-cpu-example --output=yaml

输出结果显示了配额以及有多少配额已经被使用。你可以看到 Pod 的内存和 CPU 请求值及限制值没有超过配额。

status:
  hard:
    limits.cpu: "2"
    limits.memory: 2Gi
    requests.cpu: "1"
    requests.memory: 1Gi
  used:
    limits.cpu: 800m
    limits.memory: 800Mi
    requests.cpu: 400m
    requests.memory: 600Mi

尝试创建第二个 Pod

这里给出了第二个 Pod 的配置文件:

{{< codenew file="admin/resource/quota-mem-cpu-pod-2.yaml" >}}

配置文件中,你可以看到 Pod 的内存请求为700 MiB。请注意新的内存请求与已经使用的内存请求只和超过了内存请求的配额。600 MiB + 700 MiB > 1 GiB。

尝试创建 Pod

kubectl create -f https://k8s.io/examples/admin/resource/quota-mem-cpu-pod-2.yaml --namespace=quota-mem-cpu-example

第二个 Pod 不能被创建成功。输出结果显示创建第二个 Pod 会导致内存请求总量超过内存请求配额。

Error from server (Forbidden): error when creating "examples/admin/resource/quota-mem-cpu-pod-2.yaml":
pods "quota-mem-cpu-demo-2" is forbidden: exceeded quota: mem-cpu-demo,
requested: requests.memory=700Mi,used: requests.memory=600Mi, limited: requests.memory=1Gi

讨论

如你在本练习中所见,你可以用 ResourceQuota 限制命名空间中所有容器的内存请求总量。同样你也可以限制内存限制总量、CPU 请求总量、CPU 限制总量。

如果你想对单个容器而不是所有容器进行限制,就请使用 LimitRange

数据清理

删除你的命名空间:

kubectl delete namespace quota-mem-cpu-example

{{% /capture %}}

{{% capture whatsnext %}}

集群管理员参考

应用开发者参考

{{% /capture %}}