
k8s中配置namespace的最小和最大内存
k8s中配置namespace的最小和最大内存
·
命名空间中运行的容器使用的内存的最小值和最大值。 你可以在 LimitRange 对象中指定最小和最大内存值。如果 Pod 不满足 LimitRange 施加的约束,则无法在命名空间中创建它。
-
创建namespace
kubectl create namespace test
-
编写LimitRange 对象文件
apiVersion: v1 kind: LimitRange metadata: name: mem-min-demo namespace: test spec: limits: - max: memory: 1Gi min: memory: 500Mi type: Container
可以看到内存的最大限制是1GB,最小请求500MB
注:如果不知道apiVersion版本,可以使用一下命令查看 kubectl explain limitrange
[root@master-2-4 kubernetes]# kubectl explain limitrange
KIND: LimitRange
VERSION: v1
DESCRIPTION:
LimitRange sets resource usage limits for each kind of resource in a
Namespace.
FIELDS:
apiVersion <string>
APIVersion defines the versioned schema of this representation of an
object. Servers should convert recognized schemas to the latest internal
value, and may reject unrecognized values. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#resources
kind <string>
Kind is a string value representing the REST resource this object
represents. Servers may infer this from the endpoint the client submits
requests to. Cannot be updated. In CamelCase. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#types-kinds
metadata <Object>
Standard object's metadata. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#metadata
spec <Object>
Spec defines the limits enforced. More info:
https://git.k8s.io/community/contributors/devel/sig-architecture/api-conventions.md#spec-and-status
- 使用pod测试
- 尝试创建一个超出内存限制的pod
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: test
spec:
containers:
- name: nginx
image: nginx:1.20
imagePullPolicy: IfNotPresent
resources:
limits:
memory: 1800Mi
requests:
memory: 500Mi
[root@master-2-4 kubernetes]# kubectl apply -f nginx.yaml
Error from server (Forbidden): error when creating "nginx.yaml": pods "nginx" is forbidden: maximum memory usage per Container is 1Gi, but limit is 1800Mi
输出结果显示 Pod 没有创建成功,因为容器声明的内存限制太大了
2. 尝试创建一个小于内存限制的pod
apiVersion: v1
kind: Pod
metadata:
name: nginx
namespace: test
spec:
containers:
- name: nginx
image: nginx:1.20
imagePullPolicy: IfNotPresent
resources:
limits:
memory: 600Mi
requests:
memory: 300Mi
[root@master-2-4 kubernetes]# kubectl apply -f nginx.yaml
Error from server (Forbidden): error when creating "nginx.yaml": pods "nginx" is forbidden: minimum memory usage per Container is 500Mi, but request is 300Mi
输出结果显示 Pod 没有创建成功,因为容器声明的内存请求太小了
设置内存最小和最大限制的动因
作为集群管理员,你可能想规定 Pod 可以使用的内存总量限制。例如:
- 集群的每个节点有 2 GB 内存。你不想接受任何请求超过 2 GB 的 Pod,因为集群中没有节点可以满足。
- 集群由生产部门和开发部门共享。允许产品部门的负载最多耗用 8 GB 内存, 但是开发部门的负载最多可使用 512 MiB。 这时,可以为产品部门和开发部门分别创建名字空间,并为各个名字空间设置内存约束。
更多推荐
所有评论(0)