Airflow 中的 KubernetesPodOperator 特权 security_context

2023-12-23

我在 Google 的 Cloud Composer 上运行 Airflow。我正在使用KubernetesPodOperator https://airflow.apache.org/_api/airflow/contrib/operators/kubernetes_pod_operator/index.html#airflow.contrib.operators.kubernetes_pod_operator.KubernetesPodOperator并希望通过以下方式将 google 存储桶挂载到 pod 中的目录gcsfuse https://github.com/GoogleCloudPlatform/gcsfuse。看来要做到这一点我需要给 k8s 指定的特权安全上下文here https://github.com/maciekrb/gcs-fuse-sample。看来 Airflow 最近向 KubernetesPodOperator 添加了 security_context 参数。我在运算符中指定的安全上下文是:

security_context = {
  'securityContext': {
    'privileged': True,
    'capabilities':
      {'add': ['SYS_ADMIN']}
  }
}

当我尝试跑步时airflow test dag_id task_id date在 Airflow Worker 中,Pod 启动,当代码尝试通过 gcsfuse 安装存储桶时,它会抛出错误"fusermount: fuse device not found, try 'modprobe fuse' first"。这使得 security_context 看起来不起作用(ex. https://serverfault.com/questions/924793/gcsfuse-in-google-app-engine-flexible-custom-runtime).

我是否误解了运算符中的 security_context 参数和/或我的 securityContext 字典定义无效?


The security_contextKubernetesPodOperator 的 kwarg 设置 pod 的安全上下文,而不是 pod 中的特定容器,因此它仅支持中概述的选项PodSecurityContext https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#podsecuritycontext-v1-core。由于您指定的参数对于 Pod 的安全上下文无效,因此它们将被丢弃。

The privileged and capabilities属性仅作为容器的一部分有效SecurityContext https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.19/#securitycontext-v1-core,这意味着您需要以某种方式将它们设置在 Pod 上容器规格您可以通过自己定义整个 Pod 规范来实现这一点(而不是让操作员为您生成它)。使用 KubernetesPodOperator,您可以设置full_pod_spec or pod_template_file指定 Kubernetes API Python 对象或对象 YAML 的路径,然后用于生成 pod。使用前者的示例:

from airflow.contrib.operators.kubernetes_pod_operator import KubernetesPodOperator
import kubernetes.client.models as k8s

pod = k8s.V1Pod()
pod.spec = k8s.V1PodSpec()
pod.spec.containers = [
    k8s.V1Container(
      ...,
      security_context={
          'privileged': True,
          'capabilities': {'add': ['SYS_ADMIN']}
      }
    )
]

# Equivalent to setting security_context from the operator
pod.spec.security_context = {}

t1 = KubernetesPodOperator(..., full_pod_spec=pod)

如果你想使用pod_template_file使用 Cloud Composer,您可以将 pod YAML 上传到 GCS 并将其设置为其中之一映射存储路径 https://cloud.google.com/composer/docs/concepts/cloud-storage (e.g. /home/airflow/gcs/dags/my-pod.yaml如果你把它放在DAGs目录中。

阅读通过airflow.providers.google.cloudKubernetesPodOperator 的版本,有可能full_pod_spec在新版本的运算符中已损坏。但是,它应该适用于旧的 contrib 版本。

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

Airflow 中的 KubernetesPodOperator 特权 security_context 的相关文章

随机推荐