【云原生|kubernetes】10-namespace的cpu和内存的请求与限制
文章目录
为命名空间配置默认的内存请求和限制
一个 kubernetes 集群可被划分为多个命名空间。 如果你在具有默认内存限制的命名空间内尝试创建一个 pod,并且这个 pod 中的容器没有声明自己的内存资源限制, 那么控制面会为该容器设定默认的内存限制。
创建namespace
[root@master ~]# kubectl create namespace default-mem-example
namespace/default-mem-example created
[root@master pod]# kubectl get namespace default-mem-example
name status age
default-mem-example active 14m
[root@master pod]#
- 为该
namespace
创建内存limitrange
apiversion: v1
kind: limitrange
metadata:
name: mem-limit-range
namespace: default-mem-example
spec:
limits:
- default:
memory: 512mi
defaultrequest:
memory: 256mi
type: container
- 查看
namespace
详细信息
[root@master pod]# kubectl apply -f memory-defaults.yaml
limitrange/mem-limit-range created
[root@master pod]#
[root@master pod]# kubectl describe namespace default-mem-example
name: default-mem-example
labels: kubernetes.io/metadata.name=default-mem-example
annotations: <none>
status: active
no resource quota.
resource limits
type resource min max default request default limit max limit/request ratio
---- -------- --- --- --------------- ------------- -----------------------
container memory - - 256mi 512mi -
[root@master pod]#
- 在 default-mem-example 命名空间中创建一个 pod, 并且该 pod 中所有容器都没有声明自己的内存请求和内存限制, 控制面 会将内存的默认请求值 256mib 和默认限制值 512mib 应用到 pod 上。
apiversion: v1
kind: pod
metadata:
name: default-mem-demo
namespace: default-mem-example
spec:
containers:
- name: default-mem-demo-ctr
image: nginx
- 运行pod,查看详细信息
kubectl describe -n default-mem-example pods default-mem-demo
## 输出内容显示该 pod 的容器有 256 mib 的内存请求和 512 mib 的内存限制。 这些都是 limitrange 设置的默认值。
containers:
- image: nginx
imagepullpolicy: always
name: default-mem-demo-ctr
resources:
limits:
memory: 512mi
requests:
memory: 256mi
声明容器的内存限制而不声明它的请求
- 容器的内存请求被设置为它的内存限制相同的值。注意该容器没有被指定默认的内存请求值。
声明容器的内存请求而不声明内存限制
- 容器的内存请求为 pod 清单中声明的值。 然而同一容器的内存限制被设置为 该命名空间的默认内存限制值。
设置默认内存限制和请求的动机
如果你的命名空间设置了内存 资源配额, 那么为内存限制设置一个默认值会很有帮助。 以下是内存资源配额对命名空间的施加的三条限制:
- 命名空间中运行的每个 pod 中的容器都必须有内存限制。 (如果为 pod 中的每个容器声明了内存限制, kubernetes 可以通过将其容器的内存限制相加推断出 pod 级别的内存限制)。
- 内存限制用来在 pod 被调度到的节点上执行资源预留。 预留给命名空间中所有 pod 使用的内存总量不能超过规定的限制。
- 命名空间中所有 pod 实际使用的内存总量也不能超过规定的限制。
当你添加 limitrange
时:
如果该命名空间中的任何 pod 的容器未指定内存限制, 控制面将默认内存限制应用于该容器, 这样 pod 可以在受到内存 resourcequota
限制的命名空间中运行。
为命名空间配置默认的 cpu 请求和限制
- 创建namespace
[root@master ~]# kubectl create namespace default-cpu-example
namespace/default-cpu-example created
[root@master pod]# kubectl get namespace default-cpu-example
name status age
default-cpu-example active 14m
[root@master pod]#
- 为该
namespace
创建cpu的limitrange
apiversion: v1
kind: limitrange
metadata:
name: cpu-limit-range
namespace: default-cpu-example
spec:
limits:
- default:
cpu: 1
defaultrequest:
cpu: 0.5
type: container
- 查看
namespace
详细信息
kubectl get pod default-cpu-demo --output=yaml --namespace=default-cpu-example
## 输出显示该 pod 的唯一的容器有 500m cpu 的 cpu 请求和 1 cpu 的 cpu 限制。 这些是 limitrange 声明的默认值。
containers:
- image: nginx
imagepullpolicy: always
name: default-cpu-demo-ctr
resources:
limits:
cpu: "1"
requests:
cpu: 500m
只声明容器的cpu限制,而不声明请求
- 容器的 cpu 请求和 cpu 限制设置相同
只声明容器的cpu请求,而不声明它的限制
- 容器的 cpu 请求为 pod 清单中声明的值。 然而同一容器的 cpu 限制被设置为命名空间的默认 cpu 限制值。
默认 cpu 限制和请求的动机
如果你的命名空间设置了 cpu 资源配额, 为 cpu 限制设置一个默认值会很有帮助。 以下是 cpu 资源配额对命名空间的施加的两条限制:
- 命名空间中运行的每个 pod 中的容器都必须有 cpu 限制。
- cpu 限制用来在 pod 被调度到的节点上执行资源预留。
预留给命名空间中所有 pod 使用的 cpu 总量不能超过规定的限制。
当你添加limitrange
时:
如果该命名空间中的任何 pod 的容器未指定 cpu 限制, 控制面将默认 cpu 限制应用于该容器, 这样 pod 可以在受到 cpu resourcequota
限制的命名空间中运行。
配置命名空间的最小和最大内存约束
- 创建一个
namespace
kubectl create namespace default-mem-example
- 资源清单定义最大,最小的内存限制
apiversion: v1
kind: limitrange
metadata:
name: mem-limit-range
namespace: default-mem-example
spec:
limits:
- default:
memory: 512mi
defaultrequest:
memory: 256mi
max:
memory: 1gi
min:
memory: 200mi
type: container
- 查看资源详情
[root@master pod]# kubectl describe namespace default-mem-example
name: default-mem-example
labels: kubernetes.io/metadata.name=default-mem-example
annotations: <none>
status: active
no resource quota.
resource limits
type resource min max default request default limit max limit/request ratio
---- -------- --- --- --------------- ------------- -----------------------
container memory 200mi 1gi 256mi 512mi -
创建一个超过最大内存限制的 pod
- pod不会创建成功,因为它定义了一个容器的内存请求超过了允许的值。
创建一个不满足最小内存请求的 pod
- pod 不会创建成功,因为它定义了一个容器的内存请求小于强制要求的最小值。
创建一个没有声明内存请求和限制的 pod
- pod 没有为容器声明任何内存请求和限制,集群会从
limitrange
获取默认的内存请求和限制 应用于容器
为命名空间配置 cpu 最小和最大约束
- 创建一个
namespace
kubectl create namespace default-mem-example
- 资源清单定义最大,最小的内存限制
apiversion: v1
kind: limitrange
metadata:
name: mem-limit-range
namespace: default-mem-example
spec:
limits:
- default:
cpu: 1
defaultrequest:
cpu: 0.8
max:
cpu: 2
min:
cpu: 0.6
type: container
- 查看资源详情
[root@master pod]# kubectl describe namespace default-mem-example
name: default-mem-example
labels: kubernetes.io/metadata.name=default-mem-example
annotations: <none>
status: active
no resource quota.
resource limits
type resource min max default request default limit max limit/request ratio
---- -------- --- --- --------------- ------------- -----------------------
container cpu 600m 2 800m 1 -
[root@master pod]#
创建一个超过最大 cpu 限制的 pod
- pod 不会创建成功,因为其中定义了一个无法被接受的容器。 该容器之所以无法被接受是因为其中设定了过高的 cpu 限制值
创建一个不满足最小 cpu 请求的 pod
- pod 不会创建成功,因为其中定义了一个无法被接受的容器。 该容器无法被接受的原因是其中所设置的 cpu 请求小于最小值的限制
创建一个没有声明 cpu 请求和 cpu 限制的 pod
- 容器没有声明自己的 cpu 请求和限制, 控制面会根据命名空间中配置 limitrange 设置默认的 cpu 请求和限制。
为命名空间配置内存和 cpu 配额
- 创建命名空间
kubectl create namespace quota-mem-cpu-example
resourcequota
的示例清单
apiversion: v1
kind: resourcequota
metadata:
name: mem-cpu-demo
spec:
hard:
requests.cpu: "1"
requests.memory: 1gi
limits.cpu: "2"
limits.memory: 2gi
配置命名空间下 pod 配额
apiversion: v1
kind: resourcequota
metadata:
name: pod-demo
spec:
hard:
pods: "2"
总结
apiversion: v1
kind: limitrange
metadata:
name: mem-limit-range
namespace: default-mem-example
spec:
limits:
- default:
memory: 512mi
cpu: 1
defaultrequest:
memory: 256mi
cpu: 0.8
max:
memory: 1gi
cpu: 2
min:
memory: 200mi
cpu: 0.6
type: container
- 显示如下
[root@master pod]# kubectl describe namespace default-mem-example
name: default-mem-example
labels: kubernetes.io/metadata.name=default-mem-example
annotations: <none>
status: active
no resource quota.
resource limits
type resource min max default request default limit max limit/request ratio
---- -------- --- --- --------------- ------------- -----------------------
container cpu 600m 2 800m 1 -
container memory 200mi 1gi 256mi 512mi -
[root@master pod]#
发表评论