* '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) ...
9.4 KiB
title, content_template, weight
| title | content_template | weight |
|---|---|---|
| 为命名空间配置默认的内存请求和限制 | templates/task | 10 |
{{% capture overview %}}
本文介绍怎样给命名空间配置默认的内存请求和限制。如果在一个有默认内存限制的命名空间创建容器,该容器没有声明自己的内存限制时,将会被指定默认内存限制。Kubernetes 还为某些情况指定了默认的内存请求,本章后面会进行介绍。
{{% /capture %}}
{{% capture prerequisites %}}
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
你的集群中的每个节点必须至少有2 GiB的内存。
{{% /capture %}}
{{% capture steps %}}
创建命名空间
创建一个命名空间,以便本练习中所建的资源与集群的其余资源相隔离。
kubectl create namespace default-mem-example
创建 LimitRange 和 Pod
这里给出了一个限制范围对象的配置文件。该配置声明了一个默认的内存请求和一个默认的内存限制。
{{< codenew file="admin/resource/memory-defaults.yaml" >}}
在 default-mem-example 命名空间创建限制范围:
kubectl create -f https://k8s.io/examples/admin/resource/memory-defaults.yaml --namespace=default-mem-example
现在,如果在 default-mem-example 命名空间创建容器,并且该容器没有声明自己的内存请求和限制值,它将被指定一个默认的内存请求256 MiB和一个默认的内存限制512 Mib。
{{< codenew file="admin/resource/memory-defaults-pod.yaml" >}}
创建 Pod
kubectl create -f https://k8s.io/examples/admin/resource/memory-defaults-pod.yaml --namespace=default-mem-example
查看 Pod 的详情:
kubectl get pod default-mem-demo --output=yaml --namespace=default-mem-example
输出内容显示该Pod的容器有一个256 MiB的内存请求和一个512 MiB的内存限制。这些都是限制范围声明的默认值。
containers:
- image: nginx
imagePullPolicy: Always
name: default-mem-demo-ctr
resources:
limits:
memory: 512Mi
requests:
memory: 256Mi
删除你的 Pod:
kubectl delete pod default-mem-demo --namespace=default-mem-example
声明容器的限制而不声明它的请求会怎么样?
这里给出了包含一个容器的 Pod 的配置文件。该容器声明了内存限制,而没有声明内存请求:
{{< codenew file="admin/resource/memory-defaults-pod-2.yaml" >}}
创建 Pod:
kubectl create -f https://k8s.io/examples/admin/resource/memory-defaults-pod-2.yaml --namespace=default-mem-example
查看 Pod 的详情:
kubectl get pod default-mem-demo-2 --output=yaml --namespace=default-mem-example
输出结果显示容器的内存请求被设置为它的内存限制相同的值。注意该容器没有被指定默认的内存请求值256Mi。
resources:
limits:
memory: 1Gi
requests:
memory: 1Gi
声明容器的内存请求而不声明内存限制会怎么样?
这里给出了一个包含一个容器的 Pod 的配置文件。该容器声明了内存请求,但没有内存限制:
{{< codenew file="admin/resource/memory-defaults-pod-3.yaml" >}}
创建 Pod:
kubectl create -f https://k8s.io/examples/admin/resource/memory-defaults-pod-3.yaml --namespace=default-mem-example
查看 Pod 声明:
kubectl get pod default-mem-demo-3 --output=yaml --namespace=default-mem-example
输出结果显示该容器的内存请求被设置为了容器配置文件中声明的数值。容器的内存限制被设置为512Mi,即命名空间的默认内存限制。
resources:
limits:
memory: 512Mi
requests:
memory: 128Mi
设置默认内存限制和请求的动机
如果你的命名空间有资源配额,那么默认内存限制是很有帮助的。下面是一个例子,通过资源配额为命名空间设置两项约束:
- 运行在命名空间中的每个容器必须有自己的内存限制。
- 命名空间中所有容器的内存使用量之和不能超过声明的限制值。
如果一个容器没有声明自己的内存限制,会被指定默认限制,然后它才会被允许在限定了配额的命名空间中运行。
{{% /capture %}}
{{% capture whatsnext %}}
集群管理员参考
应用开发者参考
{{% /capture %}}