kubernetes 资源管理策略 LimitRange

网友投稿 282 2022-08-23

kubernetes 资源管理策略 LimitRange

kubernetes 资源管理策略 LimitRange

tags: LimitRange

文章目录

​​kubernetes 资源管理策略 LimitRange​​

​​1. LimitRange 为 namespace 配置默认的内存请求和限制​​

​​2. LimitRange 配置 CPU 最小和最大约束​​

​​3. LimitRange为配置 namespace 内存最小和最大约束​​

1. LimitRange 为 namespace 配置默认的内存请求和限制

1.1 LimitRange 限制 namespace 资源

创建 namespace

kubectl create namespace default-mem-example

创建 LimitRange 和 Pod

apiVersion: v1kind: LimitRangemetadata: name: mem-limit-rangespec: limits: - default: memory: 512Mi defaultRequest: memory: 256Mi type: Container

在 default-mem-example namespace创建限制范围:

kubectl apply -f --namespace=default-mem-example

下面是具有一个容器的 Pod 的配置文件。 容器未指定内存请求和限制。

apiVersion: v1kind: Podmetadata: name: default-mem-demospec: containers: - name: default-mem-demo-ctr image: nginx

kubectl apply -f --namespace=default-mem-example

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

输出内容显示该 Pod 的容器有 256 MiB 的内存请求和 512 MiB 的内存限制。 这些都是 LimitRange 设置的默认值。

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

apiVersion: v1kind: Podmetadata: name: default-mem-demo-2spec: containers: - name: default-mem-demo-2-ctr image: nginx resources: limits: memory: "1Gi"

kubectl apply -f --namespace=default-mem-example

查看 Pod 的详情:

kubectl get pod default-mem-demo-2 --output=yaml --namespace=default-mem-example

输出结果显示容器的内存请求被设置为它的内存限制相同的值。注意该容器没有被指定默认的内存请求值 256MiB。

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

apiVersion: v1kind: Podmetadata: name: default-mem-demo-3spec: containers: - name: default-mem-demo-3-ctr image: nginx resources: requests: memory: "128Mi"

kubectl apply -f --namespace=default-mem-example

kubectl get pod default-mem-demo-3 --output=yaml --namespace=default-mem-example

resources: limits: memory: 512Mi requests: memory: 128Mi

2. LimitRange 配置 CPU 最小和最大约束

创建 namespace

kubectl create namespace constraints-cpu-example

创建 LimitRange 和 Pod

apiVersion: v1kind: LimitRangemetadata: name: cpu-min-max-demo-lrspec: limits: - max: cpu: "800m" min: cpu: "200m"

kubectl apply -f --namespace=constraints-cpu-example

查看 LimitRange 详情:

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

limits:- default: cpu: 800m defaultRequest: cpu: 800m max: cpu: 800m min: cpu: 200m type: Container

2.1 创建满足配置 CPU 最小和最大约束的 pod

apiVersion: v1kind: Podmetadata: name: constraints-cpu-demospec: containers: - name: constraints-cpu-demo-ctr image: nginx resources: limits: cpu: "800m" requests: cpu: "500m"

kubectl apply -f --namespace=constraints-cpu-examplekubectl get pod constraints-cpu-demo --namespace=constraints-cpu-example

查看 Pod 的详情:

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

输出结果表明容器的 CPU 请求为 500 millicpu,CPU 限制为 800 millicpu。 这些参数满足 LimitRange 规定的限制范围。

resources: limits: cpu: 800m requests: cpu: 500m

2.2 创建超过最大 CPU 限制的 Pod

apiVersion: v1kind: Podmetadata: name: constraints-cpu-demo-2spec: containers: - name: constraints-cpu-demo-2-ctr image: nginx resources: limits: cpu: "1.5" requests: cpu: "500m"

kubectl apply -f --namespace=constraints-cpu-example

Error from server (Forbidden): error when creating "examples/admin/resource/cpu-constraints-pod-2.yaml":pods "constraints-cpu-demo-2"

2.3 创建不满足最小 CPU 请求的 Pod

apiVersion: v1kind: Podmetadata: name: constraints-cpu-demo-3spec: containers: - name: constraints-cpu-demo-3-ctr image: nginx resources: limits: cpu: "800m" requests: cpu: "100m"

kubectl apply -f --namespace=constraints-cpu-example

Error from server (Forbidden): error when creating "examples/admin/resource/cpu-constraints-pod-3.yaml":pods "constraints-cpu-demo-4"

apiVersion: v1kind: Podmetadata: name: constraints-cpu-demo-4spec: containers: - name: constraints-cpu-demo-4-ctr image: vish/stress

kubectl apply -f --namespace=constraints-cpu-example

查看 Pod 的详情:

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

输出结果显示 Pod 的容器有个 800 millicpu 的 CPU 请求和 800 millicpu 的 CPU 限制。 容器是怎样得到那些值的呢?

resources: limits: cpu: 800m requests: cpu: 800m

3. LimitRange为配置 namespace 内存最小和最大约束

在 LimitRange 对象中指定最小和最大内存值。如果 Pod 不满足 LimitRange 施加的约束,则无法在 namespace 中创建它.

3.1 LimitRange 配置最大最小限制值

创建 namespace

kubectl create namespace constraints-mem-example

创建 LimitRange 和 Pod

apiVersion: v1kind: LimitRangemetadata: name: mem-min-max-demo-lrspec: limits: - max: memory: 1Gi min: memory: 500Mi type: Container

kubectl apply -f --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

apiVersion: v1kind: Podmetadata: name: constraints-mem-demospec: containers: - name: constraints-mem-demo-ctr image: nginx resources: limits: memory: "800Mi" requests: memory: "600Mi"

kubectl apply -f --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

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

apiVersion: v1kind: Podmetadata: name: constraints-mem-demo-2spec: containers: - name: constraints-mem-demo-2-ctr image: nginx resources: limits: memory: "1.5Gi" requests: memory: "800Mi"

kubectl apply -f --namespace=constraints-mem-example

Error from server (Forbidden): error when creating "examples/admin/resource/memory-constraints-pod-2.yaml":pods "constraints-mem-demo-2"

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

apiVersion: v1kind: Podmetadata: name: constraints-mem-demo-3spec: containers: - name: constraints-mem-demo-3-ctr image: nginx resources: limits: memory: "800Mi" requests: memory: "100Mi"

kubectl apply -f --namespace=constraints-mem-example

Error from server (Forbidden): error when creating "examples/admin/resource/memory-constraints-pod-3.yaml":pods "constraints-mem-demo-3"

apiVersion: v1kind: Podmetadata: name: constraints-mem-demo-4spec: containers: - name: constraints-mem-demo-4-ctr image: nginx

kubectl apply -f --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

参考:

​​kubernetes Limit Ranges​​​​kubernetes资源调度之LimitRange​​​​Restrict resource consumption with limit ranges​​​​Limit Ranges in Kubernetes​​

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:【Kafka消息队列】生产者发送消息流程
下一篇:Python小知识: List的赋值方法,不能直接等于
相关文章

 发表评论

暂时没有评论,来抢沙发吧~