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

4
我正在使用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文档建议我可以在k8s.securityContext中设置privileged=false。我尝试了两个不同的值来将privileged设置为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是正确的。
  • [ERROR controller-runtime.controller - 不允许特权容器 spec.containers[1].securityContext.privileged #792][1]

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


更新:
当Nextflow提交我的作业时,这是Kubernetes接收到的yaml的样子。看起来作业的安全上下文是空的,但是Pod模板中的spec.template.spec.containers[0].securityContext.privileged = true
我还注意到我尝试在nextflow.config中设置k8s.containers.securityContext.privileged = false,但这是一个错误的猜测,我不知道如何索引到Pod模板的设置。
看起来我只需要弄清楚如何从nextflow.config引用Pod模板的securityContext
$ 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
2个回答

3
Nextflow的Kubernetes执行器中,您可以控制创建的Pod的安全上下文。
您需要正确指定安全上下文,以便它适用于Kubernetes作业规范中的正确级别。根据您的nextflow.config中的设置,Nextflow似乎securityContext应用于Pod内的各个容器。
您可以尝试在Nextflow流程定义中使用pod指令来指定自定义Pod设置。您可以使用Groovy语法在pod指令中指定安全上下文,以创建表示正确Kubernetes配置的YAML片段。
process {
    ...
    
    pod = [
        'spec': [
            'containers': [[
                'name': 'main',
                'securityContext': [
                    'privileged': false
                ]
            ]]
        ]
    ]
}

你可以看到pod指令,用于定义自定义的pod配置,使用一个配置映射(而不是YAML多行字符串)来表示Kubernetes pod的YAML配置。我们在pod级别上指定了一个securityContext,其中runAsUser: 1000指定了进程将以哪个用户ID运行(请将1000替换为适合您环境的用户ID)。
containers列表中,我们指定了一个名为"main"的容器,并在其securityContext中设置privileged: false,以禁用该容器的特权模式。
该配置应该覆盖Nextflow应用的默认设置,并允许您的作业在非特权模式下运行。

这看起来是正确的,谢谢!我只需要弄清楚正确的语法。这里出现了一个错误 无法将对象 <YAML-STRING> 从类 'java.lang.String' 转换为类 'java.util.List'。我正在尝试弄清楚它对 pod 期望的是什么。除非这里有人知道,否则我可能不得不开始深入代码,因为文档没有涉及到这个层面。 - undefined
@DavidParks 我已经编辑了答案,提出了一种可能与预期相兼容的替代语法。 - undefined
未知的Pod选项:[spec:[containers:[[name:main, securityContext:[privileged:false]]]]]。我正在提出一个关于如何正确构建Pod安全上下文的新问题。我认为你在这里主要回答了我想要的答案,语法问题应该分开讨论,我可以稍后更新这个问题。https://stackoverflow.com/questions/77092888/how-to-correctly-format-the-pod-securitycontext-directive-in-a-nextflow-workfl - undefined
1
@DavidParks 说得好。我已经编辑了答案,提出了在地图结构中修复的建议,但如果这个方法行不通,你的新问题应该能解决问题。 - undefined

0
请记住,不使用特权可能会导致权限错误(例如文件权限错误,您的错误消息中可能包含.sh文件)。 在您的Nextflow配置中添加以下片段之一:
// 1-cloud executors
    
process.containerOptions = "--user 0:0"

// 2-Kubernetes

k8s.securityContext = [
"runAsUser": 0,
"runAsGroup": 0
        ]

更多信息请参考[K8s官方文档](link1)。

我已经配置了Nextflow在S3存储桶文件系统上运行,所以我不希望在非特权模式下出现任何文件权限问题。我并不试图在容器中设置用户(这似乎是你在这里做的),我试图禁用特权模式执行,因为在我的环境中是不允许的。此外,k8s.securityContext.privileged没有任何效果,可能是因为改变了作业的安全上下文而不是Pod模板的安全上下文。 - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接