...
spec:
steps:
- name: step-with-limts
computeResources:
requests:
memory: 1Gi
cpu: 500m
limits:
memory: 2Gi
cpu: 800m
...
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
...
通过为LimitRange
对象指定值来设置资源限制。有关LimitRange
的更多信息,请参阅 使用限制范围限制资源消耗。
设置和管理 每个项目的资源配额。
理想情况下,管道的计算资源配额应与管道运行中并发运行的 Pod 消耗的计算资源总量相同。但是,运行任务的 Pod 根据用例消耗计算资源。例如,Maven 构建任务可能需要为其构建的不同应用程序提供不同的计算资源。因此,您无法预先确定通用管道中任务的计算资源配额。为了更好地预测和控制资源使用情况,请为不同的应用程序使用自定义管道。
在使用配置了 为避免此错误,请执行以下任一操作:
|
如果这些方法没有解决您的用例,您可以通过使用优先级类的资源配额来实现变通方法。
PriorityClass
对象将优先级类名称映射到表示其相对优先级的整数值。值越高,类的优先级越高。创建优先级类后,您可以创建在规范中指定优先级类名称的 Pod。此外,您可以根据 Pod 的优先级控制 Pod 对系统资源的消耗。
为流水线指定资源配额类似于为流水线运行创建的 Pod 子集设置资源配额。以下步骤提供了一个基于优先级类指定资源配额的变通方法示例。
为流水线创建一个优先级类。
apiVersion: scheduling.k8s.io/v1
kind: PriorityClass
metadata:
name: pipeline1-pc
value: 1000000
description: "Priority class for pipeline1"
为流水线创建一个资源配额。
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"]
验证流水线的资源配额使用情况。
$ oc describe quota
Name: pipeline1-rq Namespace: default Resource Used Hard -------- ---- ---- cpu 0 1k memory 0 200Gi pods 0 10
由于 Pod 没有运行,因此配额未使用。
创建流水线和任务。
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:
resolver: cluster
params:
- name: kind
value: task
- name: name
value: git-clone
- name: namespace
value: openshift-pipelines
workspaces:
- name: output
workspace: source
params:
- name: URL
value: $(params.GIT_URL)
- 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
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)"
创建并启动流水线运行。
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
流水线运行可能会失败并显示错误: 为避免此错误,请为命名空间设置限制范围,其中 有关设置限制范围的更多信息,请参阅“附加资源”部分中的“使用限制范围限制资源消耗”。 |
创建 Pod 后,验证流水线运行的资源配额使用情况。
$ oc describe quota
Name: pipeline1-rq Namespace: default Resource Used Hard -------- ---- ---- cpu 500m 1k memory 10Gi 200Gi pods 1 10
输出表明,您可以通过为每个优先级类指定资源配额来管理属于某个优先级类的所有并发运行 Pod 的组合资源配额。