4.13. 在特权安全上下文中使用 pod

如果 Pod 由管道运行或任务运行,则 OpenShift Pipelines 1.3.x 及更新版本的默认配置不允许使用特权安全上下文运行 Pod。对于这样的容器集,默认服务帐户为 pipeline,与 pipelines 服务帐户关联的安全性上下文约束(SCC)是 pipelines-sccpipelines-scc SCC 与 anyuid SCC 类似,但存在细微差别,如管道 SCC 的 YAML 文件中所定义:

SecurityContextConstraints 对象示例

apiVersion: security.openshift.io/v1
kind: SecurityContextConstraints
...
fsGroup:
  type: MustRunAs
...

另外,Buildah 集群任务作为 OpenShift Pipelines 的一部分提供,使用 vfs 作为默认存储驱动程序。

4.13.1. 运行管道运行和任务运行带有特权安全上下文的 Pod

流程

要使用 特权 安全上下文运行 pod(从管道运行或任务运行中),请执行以下修改:

  • 将关联的用户帐户或服务帐户配置为具有显式 SCC。您可以使用以下任一方法执行配置:

    • 运行以下命令:

      $ oc adm policy add-scc-to-user <scc-name> -z <service-account-name>
    • 或者,修改 RoleBindingRoleClusterRole 的 YAML 文件:

      RoleBinding 对象示例

      apiVersion: rbac.authorization.k8s.io/v1
      kind: RoleBinding
      metadata:
        name: service-account-name 1
        namespace: default
      roleRef:
        apiGroup: rbac.authorization.k8s.io
        kind: ClusterRole
        name: pipelines-scc-clusterrole 2
      subjects:
      - kind: ServiceAccount
        name: pipeline
        namespace: default

      1
      使用适当的服务帐户名称替换。
      2
      根据您使用的角色绑定,使用适当的集群角色替换。

      ClusterRole 对象示例

      apiVersion: rbac.authorization.k8s.io/v1
      kind: ClusterRole
      metadata:
        name: pipelines-scc-clusterrole 1
      rules:
      - apiGroups:
        - security.openshift.io
        resourceNames:
        - nonroot
        resources:
        - securitycontextconstraints
        verbs:
        - use

      1
      根据您使用的角色绑定,使用适当的集群角色替换。
    注意

    作为最佳实践,请创建默认 YAML 文件的副本并在重复文件中进行更改。

  • 如果您不使用 vfs 存储驱动程序,请将与任务运行或管道运行关联的服务帐户配置为具有特权 SCC,并将安全上下文设置为 privileged: true

4.13.2. 使用自定义 SCC 和自定义服务帐户运行管道运行和任务

使用与默认 pipelines 服务帐户关联的 pipelines-scc 安全性上下文约束(SCC)时,管道运行和任务运行 pod 可能会面临超时问题。这是因为在默认的 pipelines-scc SCC 中,fsGroup.type 参数设置为 MustRunAs

注意

有关 pod 超时的更多信息,请参阅 BZ#1995779.

为避免 pod 超时,您可以创建一个自定义 SCC,并将 fsGroup.type 参数设置为 RunAsAny,并将它与自定义服务帐户关联。

注意

作为最佳实践,使用自定义 SCC 和自定义服务帐户来运行管道运行和任务运行。这种方法具有更大的灵活性,在升级过程中修改默认值时不会中断运行。

流程

  1. 定义自定义 SCC,并将 fsGroup.type 参数设置为 RunAsAny

    示例:自定义 SCC

    apiVersion: security.openshift.io/v1
    kind: SecurityContextConstraints
    metadata:
      annotations:
        kubernetes.io/description: my-scc is a close replica of anyuid scc. pipelines-scc has fsGroup - RunAsAny.
      name: my-scc
    allowHostDirVolumePlugin: false
    allowHostIPC: false
    allowHostNetwork: false
    allowHostPID: false
    allowHostPorts: false
    allowPrivilegeEscalation: true
    allowPrivilegedContainer: false
    allowedCapabilities: null
    defaultAddCapabilities: null
    fsGroup:
      type: RunAsAny
    groups:
    - system:cluster-admins
    priority: 10
    readOnlyRootFilesystem: false
    requiredDropCapabilities:
    - MKNOD
    runAsUser:
      type: RunAsAny
    seLinuxContext:
      type: MustRunAs
    supplementalGroups:
      type: RunAsAny
    volumes:
    - configMap
    - downwardAPI
    - emptyDir
    - persistentVolumeClaim
    - projected
    - secret

  2. 创建自定义 SCC:

    示例:创建 my-scc SCC

    $ oc create -f my-scc.yaml

  3. 创建自定义服务帐户:

    示例:创建一个 fsgroup-runasany 服务帐户

    $ oc create serviceaccount fsgroup-runasany

  4. 将自定义 SCC 与自定义服务帐户关联:

    示例:将 my-scc SCC 与 fsgroup-runasany 服务帐户关联

    $ oc adm policy add-scc-to-user my-scc -z fsgroup-runasany

    如果要将自定义服务帐户用于特权任务,您可以通过运行以下命令将 privileged SCC 与自定义服务帐户关联:

    示例:将 privileged SCC 与 fsgroup-runasany 服务帐户关联

    $ oc adm policy add-scc-to-user privileged -z fsgroup-runasany

  5. 在管道运行和任务运行中使用自定义服务帐户:

    示例:Pipeline 使用 fsgroup-runasany 自定义服务帐户运行 YAML

    apiVersion: tekton.dev/v1beta1
    kind: PipelineRun
    metadata:
      name: <pipeline-run-name>
    spec:
      pipelineRef:
        name: <pipeline-cluster-task-name>
      serviceAccountName: 'fsgroup-runasany'

    示例:任务使用 fsgroup-runasany 自定义服务帐户运行 YAML

    apiVersion: tekton.dev/v1beta1
    kind: TaskRun
    metadata:
      name: <task-run-name>
    spec:
      taskRef:
        name: <cluster-task-name>
      serviceAccountName: 'fsgroup-runasany'

4.13.3. 其他资源