website/content/zh/docs/tasks/administer-cluster/manage-resources/memory-constraint-namespace.md

13 KiB
Raw Blame History


title: 配置命名空间的最小和最大内存约束 content_template: templates/task weight: 30

{{% capture overview %}}

此页面介绍如何设置在命名空间中运行的容器使用的内存的最小值和最大值。 您可以在 [LimitRange](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#limitrange-v1-core)对象中指定最小和最大内存值。 如果 Pod 不满足 LimitRange 施加的约束,则无法在命名空间中创建它。

{{% /capture %}}

{{% capture prerequisites %}}

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

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

{{% /capture %}}

{{% capture steps %}}

创建命名空间

创建一个命名空间,以便在此练习中创建的资源与群集的其余资源隔离。

kubectl create namespace constraints-mem-example

创建 LimitRange 和 Pod

下面是 LimitRange 的配置文件:

{{< codenew file="admin/resource/memory-constraints.yaml" >}}

创建 LimitRange:

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

查看 LimitRange 的详情:

kubectl get limitrange mem-min-max-demo-lr --namespace=constraints-mem-example --output=yaml

输出显示预期的最小和最大内存约束。 但请注意,即使您没有在 LimitRange 的配置文件中指定默认值,也会自动创建它们。

  limits:
  - default:
      memory: 1Gi
    defaultRequest:
      memory: 1Gi
    max:
      memory: 1Gi
    min:
      memory: 500Mi
    type: Container

现在,只要在 constraints-mem-example 命名空间中创建容器Kubernetes 就会执行下面的步骤:

  • 如果 Container 未指定自己的内存请求和限制,将为它指定默认的内存请求和限制。

  • 验证 Container 的内存请求是否大于或等于500 MiB。

  • 验证 Container 的内存限制是否小于或等于1 GiB。

这里给出了包含一个 Container 的 Pod 配置文件。Container 声明了600 MiB 的内存请求和800 MiB 的内存限制, 这些满足了 LimitRange 施加的最小和最大内存约束。

{{< codenew file="admin/resource/memory-constraints-pod.yaml" >}}

创建 Pod

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

确认下 Pod 中的容器在运行:

kubectl get pod constraints-mem-demo --namespace=constraints-mem-example

查看 Pod 详情:

kubectl get pod constraints-mem-demo --output=yaml --namespace=constraints-mem-example

输出结果显示容器的内存请求为600 MiB内存限制为800 MiB。这些满足了 LimitRange 设定的限制范围。

resources:
  limits:
     memory: 800Mi
  requests:
    memory: 600Mi

删除你创建的 Pod

kubectl delete pod constraints-mem-demo --namespace=constraints-mem-example

尝试创建一个超过最大内存限制的 Pod

这里给出了包含一个容器的 Pod 的配置文件。容器声明了800 MiB 的内存请求和1.5 GiB 的内存限制。

{{< codenew file="admin/resource/memory-constraints-pod-2.yaml" >}}

尝试创建 Pod:

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

输出结果显示 Pod 没有创建成功,因为容器声明的内存限制太大了:

Error from server (Forbidden): error when creating "examples/admin/resource/memory-constraints-pod-2.yaml":
pods "constraints-mem-demo-2" is forbidden: maximum memory usage per Container is 1Gi, but limit is 1536Mi.

尝试创建一个不满足最小内存请求的 Pod

这里给出了包含一个容器的 Pod 的配置文件。容器声明了100 MiB 的内存请求和800 MiB 的内存限制。

{{< codenew file="admin/resource/memory-constraints-pod-3.yaml" >}}

尝试创建 Pod

kubectl create -f https://k8s.io/examples/admin/resource/memory-constraints-pod-3.yaml --namespace=constraints-mem-example

输出结果显示 Pod 没有创建成功,因为容器声明的内存请求太小了:

Error from server (Forbidden): error when creating "examples/admin/resource/memory-constraints-pod-3.yaml":
pods "constraints-mem-demo-3" is forbidden: minimum memory usage per Container is 500Mi, but request is 100Mi.

创建一个没有声明内存请求和限制的 Pod

这里给出了包含一个容器的 Pod 的配置文件。容器没有声明内存请求,也没有声明内存限制。

{{< codenew file="admin/resource/memory-constraints-pod-4.yaml" >}}

创建 Pod

kubectl create -f https://k8s.io/examples/admin/resource/memory-constraints-pod-4.yaml --namespace=constraints-mem-example

查看 Pod 详情:

kubectl get pod constraints-mem-demo-4 --namespace=constraints-mem-example --output=yaml

输出结果显示 Pod 的内存请求为1 GiB内存限制为1 GiB。容器怎样获得哪些数值呢

resources:
  limits:
    memory: 1Gi
  requests:
    memory: 1Gi

因为你的容器没有声明自己的内存请求和限制,它从 LimitRange 那里获得了默认的内存请求和限制

此时你的容器可能运行起来也可能没有运行起来。回想一下我们本次任务的先决条件是你的每个节点都至少有1 GiB 的内存。如果你的每个节点都只有1 GiB 的内存那将没有一个节点拥有足够的可分配内存来满足1 GiB 的内存请求。

删除你的 Pod

kubectl delete pod constraints-mem-demo-4 --namespace=constraints-mem-example

强制执行内存最小和最大限制

LimitRange 为命名空间设定的最小和最大内存限制只有在 Pod 创建和更新时才会强制执行。如果你更新 LimitRange它不会影响此前创建的 Pod。

设置内存最小和最大限制的动因

做为集群管理员,你可能想规定 Pod 可以使用的内存总量限制。例如:

  • 集群的每个节点有2 GB 内存。你不想接受任何请求超过2 GB 的 Pod因为集群中没有节点可以满足。

数据清理

删除你的命名空间:

kubectl delete namespace constraints-mem-example

{{% /capture %}}

{{% capture whatsnext %}}

集群管理员参考

应用开发者参考

{{% /capture %}}