如何在 Nextflow 的 Kubernetes 执行器中禁用特权容器执行?

2024-06-20

我正在共享集群中使用 Kubernetes 执行器运行 Nextflow 工作流程,出于安全原因,该集群不允许特权容器。

当我运行 nextflow 工作流程时,我收到错误,清楚地表明它正在尝试在特权模式下运行我的作业:

job-controller  Error creating: admission webhook "validation.gatekeeper.sh" denied the request: 
[psp-privileged-container] Privileged container is not allowed: nf-f48d29c6300b8f61af05447de0072d69, 
securityContext: {"privileged": true}

Nextflow 文档建议我可以设置privileged=false in the k8s.securityContext。我尝试了两个不同的值来设置特权false(一个基于 Nextflow 文档,privileged,另一个基于 Kubernetes 文档,allowPrivilegeEscalation)。两者仍然产生相同的错误。

// Kubernetes config
process.executor = 'k8s'
k8s.namespace = 'mynamespace'
k8s.computeResourceType = 'Job'
k8s.securityContext.privileged = false                // nextflow doc format
k8s.securityContext.allowPrivilegeEscalation = false  // k8s doc format

我看到以下相关讨论似乎表明这应该有效,但我很难遵循这些线程,或者至少这些线程似乎表明我的nextflow.config是正确的。

  • [错误controller-runtime.controller - 不允许使用特权容器spec.containers[1].securityContext.privileged #792][1]

  • [feat: 支持任意设置运行容器的privileged: true #1383][2]


Update:

当 nextflow 提交我的作业时,Kubernetes 收到的 yaml 如下所示。看起来作业安全上下文是空的,但 pod 模板位于spec.template.spec.containers[0].securityContext.privileged = true.

注意到我也尝试设置k8s.containers.securityContext.privileged = false in nextflow.config但这是一个错误的猜测,我不知道如何索引到 pod 模板设置。

看来我只需要弄清楚如何引用 pod 模板securityContext from nextflow.config.

$ kubectl get job nf-878eb722258681cf0031aeeabe2fb132 -n mynamespace -o yaml
apiVersion: batch/v1
kind: Job
metadata:
  annotations:
    batch.kubernetes.io/job-tracking: ""
  creationTimestamp: "2023-09-12T17:16:58Z"
  generation: 1
  labels:
    nextflow.io/app: nextflow
    nextflow.io/processName: helloWorld1
    nextflow.io/runName: focused_pesquet
    nextflow.io/sessionId: uuid-2a634652-ba40-4905-a9ca-ad0b948df4f0
    nextflow.io/taskName: helloWorld1
  name: nf-878eb722258681cf0031aeeabe2fb132
  namespace: mynamespace
  resourceVersion: "6387865826"
  uid: 6a4ce939-cc01-4298-9d94-84219d750e84
spec:
  backoffLimit: 0
  completionMode: NonIndexed
  completions: 1
  parallelism: 1
  selector:
    matchLabels:
      controller-uid: 6a4ce939-cc01-4298-9d94-84219d750e84
  suspend: false
  template:
    metadata:
      creationTimestamp: null
      labels:
        controller-uid: 6a4ce939-cc01-4298-9d94-84219d750e84
        job-name: nf-878eb722258681cf0031aeeabe2fb132
    spec:
      containers:
      - args:
        - /usr/bin/fusion
        - bash
        - /fusion/s3/mybucket/nextflow/87/8eb722258681cf0031aeeabe2fb132/.command.run
        env:
        - name: FUSION_WORK
          value: /fusion/s3/mybucket/nextflow/87/8eb722258681cf0031aeeabe2fb132
        - name: AWS_S3_ENDPOINT
          value: custom.ceph.endpoint
        - name: FUSION_TAGS
          value: '[.command.*|.exitcode|.fusion.*](nextflow.io/metadata=true),[*](nextflow.io/temporary=true)'
        image: wave.seqera.io/wt/8602f8b269fe/library/ubuntu:latest
        imagePullPolicy: Always
        name: nf-878eb722258681cf0031aeeabe2fb132
        resources:
          limits:
            ephemeral-storage: 1Gi
            memory: 1Gi
          requests:
            cpu: "1"
            ephemeral-storage: 1Gi
            memory: 1Gi
        securityContext:
          privileged: true
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Never
      schedulerName: default-scheduler
      securityContext: {}
      serviceAccount: default
      serviceAccountName: default
      terminationGracePeriodSeconds: 30
  ttlSecondsAfterFinished: 604800
status:
  ready: 0
  startTime: "2023-09-12T17:16:58Z"
  uncountedTerminatedPods: {}
``


  [1]: https://github.com/actions/actions-runner-controller/issues/792
  [2]: https://github.com/actions/actions-runner-controller/pull/1383

In the Nextflow 的 Kubernetes 执行器 https://www.nextflow.io/docs/latest/executor.html#k8s-executor,您可以控制所创建 Pod 的安全上下文。

您需要正确指定安全上下文,使其适用于 Kubernetes 作业规范中的正确级别。看起来 Nextflow 是not应用securityContext根据您的设置到 Pod 中的各个容器nextflow.config.

您可以尝试并利用pod指示 https://www.nextflow.io/docs/latest/process.html#process-pod在 Nextflow 流程定义中指定自定义 Pod 设置。您可以使用 Groovy 语法在 pod 指令中指定安全上下文,以创建表示正确 Kubernetes 配置的 YAML 片段。

process {
    ...
    
    pod = [
        'spec': [
            'containers': [[
                'name': 'main',
                'securityContext': [
                    'privileged': false
                ]
            ]]
        ]
    ]
}

您可以看到pod指令,用于定义自定义 pod 配置,使用配置映射(而不是 YAML 多行字符串)来表示 Kubernetes pod 的 YAML 配置。我们指定一个securityContext在 Pod 级别runAsUser: 1000指示进程将运行的用户 ID(替换1000具有适合您的环境的用户 ID)。

containers列表中,我们指定一个名为“main”的容器并设置privileged: false in its securityContext禁用该容器的特权模式。

该配置应覆盖 Nextflow 应用的默认设置,并允许您的作业在没有特权模式的情况下运行。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Nextflow 的 Kubernetes 执行器中禁用特权容器执行? 的相关文章

随机推荐