暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Kubernetes 配置命名空间的最小和最大内存约束

潍鲸 2020-07-30
221

在命名空间中运行的容器使用的内存的最小值和最大值。可以在 LimitRange对象中指定最小和最大内存值。如果 Pod 不满足 LimitRange 施加的约束,则无法在命名空间中创建它。


创建命名空间


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

    kubectl create namespace constraints-mem-example


    创建 LimitRange 和 Pod


    下面是 LimitRange 的配置文件:

      admin/resource/memory-constraints.yaml


      apiVersion: v1
      kind: LimitRange
      metadata:
      name: mem-min-max-demo-lr
      spec:
      limits:
      - max:
      memory: 1Gi
      min:
      memory: 500Mi
          type: Container


      创建 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 施加的最小和最大内存约束。


              admin/resource/memory-constraints-pod.yaml


              apiVersion: v1
              kind: Pod
              metadata:
              name: constraints-mem-demo
              spec:
              containers:
              - name: constraints-mem-demo-ctr
              image: nginx
              resources:
              limits:
              memory: "800Mi"
              requests:
                      memory: "600Mi"


              创建 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 的内存限制。

                          admin/resource/memory-constraints-pod-2.yaml


                          apiVersion: v1
                          kind: Pod
                          metadata:
                          name: constraints-mem-demo-2
                          spec:
                          containers:
                          - name: constraints-mem-demo-2-ctr
                          image: nginx
                          resources:
                          limits:
                          memory: "1.5Gi"
                          requests:
                                  memory: "800Mi"


                          尝试创建 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 的内存限制。

                                admin/resource/memory-constraints-pod-3.yaml


                                apiVersion: v1
                                kind: Pod
                                metadata:
                                name: constraints-mem-demo-3
                                spec:
                                containers:
                                - name: constraints-mem-demo-3-ctr
                                image: nginx
                                resources:
                                limits:
                                memory: "800Mi"
                                requests:
                                        memory: "100Mi"


                                尝试创建 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 的配置文件。容器没有声明内存请求,也没有声明内存限制。

                                      admin/resource/memory-constraints-pod-4.yaml


                                      apiVersion: v1
                                      kind: Pod
                                      metadata:
                                      name: constraints-mem-demo-4
                                      spec:
                                      containers:
                                      - name: constraints-mem-demo-4-ctr
                                      image: nginx



                                      创建 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
                                                文章转载自潍鲸,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                                                评论