管理资源使用


Red Hat OpenShift Pipelines 1.11

在 OpenShift Pipelines 中管理资源消耗

Red Hat OpenShift Documentation Team

摘要

本文档提供有关在 OpenShift Pipelines 中管理资源消耗的信息。

第 1 章 减少 OpenShift Pipelines 的资源消耗

如果在多租户环境中使用集群,您必须控制各个项目和 Kubernetes 对象的 CPU、内存和存储资源的消耗。这有助于防止一个应用程序消耗太多资源并影响其他应用程序。

要定义在生成的 pod 上设置的最终资源限值,Red Hat OpenShift Pipelines 使用资源配额限制和/或限制执行项目的范围。

要限制项目中的资源消耗,您可以:

1.1. 了解管道中的资源消耗

每个任务都由 Task 资源中 steps 字段中定义的特定顺序执行的一些所需步骤组成。每个任务都作为 pod 运行,每个步骤都作为该 pod 中的容器运行。

每次都会执行一个步骤。执行此任务的 Pod 仅请求足够的资源,以一次在任务中运行单个容器镜像(步骤),因此不会为任务中的所有步骤存储资源。

steps spec 中的 Resources 字段指定资源消耗的限值。默认情况下,CPU、内存和临时存储的资源请求设置为 BestEffort (零)值,或通过该项目中限制范围设置的最小值。

步骤的资源请求和限值配置示例

spec:
  steps:
  - name: <step_name>
    computeResources:
      requests:
        memory: 2Gi
        cpu: 600m
      limits:
        memory: 4Gi
        cpu: 900m
Copy to Clipboard Toggle word wrap

当在执行管道和任务运行的项目中指定 LimitRange 参数和容器资源请求的最小值时,Red Hat OpenShift Pipelines 会查看项目中的所有 LimitRange 值,并使用最小值而不是零。

在项目级别配置限制范围参数示例

apiVersion: v1
kind: LimitRange
metadata:
  name: <limit_container_resource>
spec:
  limits:
  - max:
      cpu: "600m"
      memory: "2Gi"
    min:
      cpu: "200m"
      memory: "100Mi"
    default:
      cpu: "500m"
      memory: "800Mi"
    defaultRequest:
      cpu: "100m"
      memory: "100Mi"
    type: Container
...
Copy to Clipboard Toggle word wrap

1.2. 缓解管道中的额外资源消耗

当您在 pod 中的容器上设置了资源限制时,OpenShift Container Platform 会将请求的资源限值作为同时运行的所有容器的总和。

为了消耗调用任务一次执行一个步骤所需的最小资源,Red Hat OpenShift Pipelines 请求最大 CPU、内存和临时存储,这在需要最多资源的步骤中指定。这样可确保满足所有步骤的资源要求。最大值以外的请求设置为零。

但是,这种行为的资源使用量会超过要求。如果使用资源配额,这也可能导致无法调度的 pod。

例如,有一个任务,它带有两个使用脚本的步骤,且没有定义任何资源限值和请求。生成的 pod 有两个 init 容器(一个用于入口点复制,另一个用于编写脚本)和两个容器(每个步骤一个)。

OpenShift Container Platform 使用为项目设置的限制范围来计算所需资源请求和限值。在本例中,在项目中设置以下限制范围:

apiVersion: v1
kind: LimitRange
metadata:
  name: mem-min-max-demo-lr
spec:
  limits:
  - max:
      memory: 1Gi
    min:
      memory: 500Mi
    type: Container
Copy to Clipboard Toggle word wrap

在这种情况下,每个 init 容器使用 1Gi 请求内存(限制范围的最大限制),每个容器都使用 500Mi 请求内存。因此,pod 的内存请求总数为 2Gi。

如果对一个有十个步骤的任务使用相同的限制范围,则最终内存请求为 5Gi,高于每个步骤实际需要的 500Mi(因为每个步骤均在另一个步骤之后运行)。

因此,为了减少资源消耗,您可以:

  • 通过将不同的步骤分组到一个较大的步骤,使用脚本功能和同一镜像来减少给定任务中的步骤数量。这可减少最低请求资源。
  • 分发步骤,它们相互独立,可以对于多个任务而不是单个任务运行。这样可减少每个任务中的步骤数量,使每个任务的请求更小,那么调度程序可在资源可用时运行它们。

Red Hat OpenShift Pipelines 中的 ResourceQuota 对象控制每个命名空间的总资源消耗。您可以使用它来限制命名空间中创建的对象数量,具体取决于对象的类型。另外,您可以指定计算资源配额来限制命名空间中消耗的计算资源总量。

但是,您可能希望限制管道运行生成的 Pod 所消耗的计算资源数量,而不是为整个命名空间设置配额。目前,Red Hat OpenShift Pipelines 不会让您直接为管道指定计算资源配额。

要获得对管道使用计算资源的一定程度的控制,请考虑以下备选方法:

  • 为任务中的每个步骤设置资源请求和限值。

    示例:为任务中的每个步骤设置资源请求和限值。

    ...
    spec:
      steps:
        - name: step-with-limts
          computeResources:
            requests:
              memory: 1Gi
              cpu: 500m
            limits:
              memory: 2Gi
              cpu: 800m
    ...
    Copy to Clipboard Toggle word wrap

  • 通过为 LimitRange 对象指定值来设置资源限值。如需有关 LimitRange 的更多信息,请参阅限制范围限制资源消耗
  • 减少管道资源消耗
  • 设置和管理 每个项目的资源配额
  • 理想情况下,管道的计算资源配额应与管道运行中同时运行的 Pod 所消耗的计算资源总量相同。不过,运行任务的容器集会根据用例消耗计算资源。例如,Maven 构建任务可能需要为它构建的不同应用提供不同的计算资源。因此,您无法为通用管道中的任务预先确定计算资源配额。要提高计算资源使用率的可预测性,并对计算资源的使用进行控制,请将自定义管道用于不同的应用程序。
注意

在配置了 ResourceQuota 对象的命名空间中使用 Red Hat OpenShift Pipelines 时,任务运行和管道运行的 pod 可能会失败,并显示错误,如:failed quota: <quota name> must specify cpu, memory

要避免这个错误,请执行以下操作之一:

  • (推荐)为命名空间指定一个限制范围。
  • 为所有容器明确定义请求和限值。

如需更多信息,请参阅问题解决方案

如果您的用例没有通过这些方法解决,您可以使用优先级类的资源配额来实施临时解决方案。

2.2. 使用优先级类指定管道资源配额

PriorityClass 对象将优先级类名称映射到表示其相对优先级的整数值。数值越高,类的优先级越高。创建优先级类后,您可以创建 pod,在其规格中指定优先级类名称。另外,您可以根据 pod 优先级控制 pod 对系统资源的消耗。

为管道指定资源配额类似于为管道运行创建的 pod 子集设置资源配额。以下步骤通过根据优先级类指定资源配额来提供临时解决方案示例。

流程

  1. 为管道创建优先级类。

    示例:管道的优先级类

    apiVersion: scheduling.k8s.io/v1
    kind: PriorityClass
    metadata:
      name: pipeline1-pc
    value: 1000000
    description: "Priority class for pipeline1"
    Copy to Clipboard Toggle word wrap

  2. 为管道创建资源配额。

    示例:管道的资源配额

    apiVersion: v1
    kind: ResourceQuota
    metadata:
      name: pipeline1-rq
    spec:
      hard:
        cpu: "1000"
        memory: 200Gi
        pods: "10"
      scopeSelector:
        matchExpressions:
        - operator : In
          scopeName: PriorityClass
          values: ["pipeline1-pc"]
    Copy to Clipboard Toggle word wrap

  3. 验证管道的资源配额使用量。

    示例:验证管道的资源配额使用情况

    $ oc describe quota
    Copy to Clipboard Toggle word wrap

    输出示例

    Name:       pipeline1-rq
    Namespace:  default
    Resource    Used  Hard
    --------    ----  ----
    cpu         0     1k
    memory      0     200Gi
    pods        0     10
    Copy to Clipboard Toggle word wrap

    由于容器集没有运行,因此配额没有被使用。

  4. 创建管道和任务。

    示例:管道的 YAML

    apiVersion: tekton.dev/v1
    kind: Pipeline
    metadata:
      name: maven-build
    spec:
      params:
      - name: GIT_URL
      workspaces:
      - name: local-maven-repo
      - name: source
      tasks:
      - name: git-clone
        taskRef:
          kind: ClusterTask
          name: git-clone
        params:
          - name: url
            value: $(params.GIT_URL)
        workspaces:
        - name: output
          workspace: source
      - name: build
        taskRef:
          name: mvn
        runAfter: ["git-clone"]
        params:
        - name: GOALS
          value: ["package"]
        workspaces:
        - name: maven-repo
          workspace: local-maven-repo
        - name: source
          workspace: source
      - name: int-test
        taskRef:
          name: mvn
        runAfter: ["build"]
        params:
        - name: GOALS
          value: ["verify"]
        workspaces:
        - name: maven-repo
          workspace: local-maven-repo
        - name: source
          workspace: source
      - name: gen-report
        taskRef:
          name: mvn
        runAfter: ["build"]
        params:
        - name: GOALS
          value: ["site"]
        workspaces:
        - name: maven-repo
          workspace: local-maven-repo
        - name: source
          workspace: source
    Copy to Clipboard Toggle word wrap

    示例:管道中任务的 YAML

    apiVersion: tekton.dev/v1
    kind: Task
    metadata:
      name: mvn
    spec:
      workspaces:
      - name: maven-repo
      - name: source
      params:
      - name: GOALS
        description: The Maven goals to run
        type: array
        default: ["package"]
      steps:
        - name: mvn
          image: gcr.io/cloud-builders/mvn
          workingDir: $(workspaces.source.path)
          command: ["/usr/bin/mvn"]
          args:
            - -Dmaven.repo.local=$(workspaces.maven-repo.path)
            - "$(params.GOALS)"
    Copy to Clipboard Toggle word wrap

  5. 创建并启动管道运行。

    示例:管道运行的 YAML

    apiVersion: tekton.dev/v1
    kind: PipelineRun
    metadata:
      generateName: petclinic-run-
    spec:
      pipelineRef:
        name: maven-build
      params:
      - name: GIT_URL
        value: https://github.com/spring-projects/spring-petclinic
      taskRunTemplate:
        podTemplate:
          priorityClassName: pipeline1-pc
      workspaces:
      - name: local-maven-repo
        emptyDir: {}
      - name: source
        volumeClaimTemplate:
          spec:
            accessModes:
              - ReadWriteOnce
            resources:
              requests:
                storage: 200M
    Copy to Clipboard Toggle word wrap

    注意

    管道运行可能会失败,并显示错误:failed quota: <quota name> must specify cpu, memory

    要避免这个错误,请为命名空间设置一个限制范围,其中 LimitRange 对象中的默认值应用到构建过程中创建的 pod。

    有关设置限制范围的更多信息,请参阅附加资源部分中的限制范围限制资源消耗

  6. 创建 pod 后,验证管道运行的资源配额使用量。

    示例:验证管道的资源配额使用情况

    $ oc describe quota
    Copy to Clipboard Toggle word wrap

    输出示例

    Name:       pipeline1-rq
    Namespace:  default
    Resource    Used  Hard
    --------    ----  ----
    cpu         500m  1k
    memory      10Gi  200Gi
    pods        1     10
    Copy to Clipboard Toggle word wrap

    输出表明,您可以通过指定每个优先级类的资源配额,为属于优先级类的所有并发运行 pod 管理组合资源配额。

法律通告

Copyright © 2024 Red Hat, Inc.
The text of and illustrations in this document are licensed by Red Hat under a Creative Commons Attribution–Share Alike 3.0 Unported license ("CC-BY-SA"). An explanation of CC-BY-SA is available at http://creativecommons.org/licenses/by-sa/3.0/. In accordance with CC-BY-SA, if you distribute this document or an adaptation of it, you must provide the URL for the original version.
Red Hat, as the licensor of this document, waives the right to enforce, and agrees not to assert, Section 4d of CC-BY-SA to the fullest extent permitted by applicable law.
Red Hat, Red Hat Enterprise Linux, the Shadowman logo, the Red Hat logo, JBoss, OpenShift, Fedora, the Infinity logo, and RHCE are trademarks of Red Hat, Inc., registered in the United States and other countries.
Linux® is the registered trademark of Linus Torvalds in the United States and other countries.
Java® is a registered trademark of Oracle and/or its affiliates.
XFS® is a trademark of Silicon Graphics International Corp. or its subsidiaries in the United States and/or other countries.
MySQL® is a registered trademark of MySQL AB in the United States, the European Union and other countries.
Node.js® is an official trademark of Joyent. Red Hat is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.
The OpenStack® Word Mark and OpenStack logo are either registered trademarks/service marks or trademarks/service marks of the OpenStack Foundation, in the United States and other countries and are used with the OpenStack Foundation's permission. We are not affiliated with, endorsed or sponsored by the OpenStack Foundation, or the OpenStack community.
All other trademarks are the property of their respective owners.
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat