Airflow 中的 KubernetesPodOperator 特权 security_context

Nit*_*lli 5 kubernetes google-kubernetes-engine airflow google-cloud-composer

我在 Google 的 Cloud Composer 上运行 Airflow。我现在用的是KubernetesPodOperator并想通过一个谷歌存储桶安装到目录荚gcsfuse。似乎要做到这一点,我需要提供此处指定的 k8s 特权安全上下文。似乎气流最近向 KubernetesPodOperator 添加了 security_context 参数。我在操作符中指定的安全上下文是:

security_context = {
  'securityContext': {
    'privileged': True,
    'capabilities':
      {'add': ['SYS_ADMIN']}
  }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试airflow test dag_id task_id date在气流工作器中运行时,pod 启动,当代码尝试通过 gcsfuse 挂载存储桶时,它会引发错误"fusermount: fuse device not found, try 'modprobe fuse' first"。这使得它看起来好像 security_context 不起作用(例如)。

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

hex*_*ide 5

KubernetesPodOperator 的kwargsecurity_context设置 pod 的安全上下文,而不是 pod 中的特定容器,因此它仅支持 中概述的选项PodSecurityContext。由于您指定的参数对于 Pod 的安全上下文无效,因此它们将被丢弃。

和属性仅作为容器的一部分有效privileged,这意味着您需要以某种方式在 pod 的容器规范上设置它们。您可以通过自己定义整个 Pod 规范来实现这一点(而不是让操作员为您生成它)。使用 KubernetesPodOperator,您可以设置或指定 Kubernetes API Python 对象或对象 YAML 的路径,然后用于生成 pod。使用前者的示例:capabilitiesSecurityContextfull_pod_specpod_template_file

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)
Run Code Online (Sandbox Code Playgroud)

如果您想与 Cloud Composer 一起使用,您可以将 pod YAML 上传到 GCS 并将其设置为映射的存储路径pod_template_file之一(例如,如果将其放在 DAGs 目录中)。/home/airflow/gcs/dags/my-pod.yaml

通读airflow.providers.google.cloudKubernetesPodOperator 的版本,可能会full_pod_spec在新版本的操作器中出现问题。但是,它应该适用于旧的 contrib 版本。

  • 我浏览了 Airflow 1.10.6 的代码,还发现“full_pod_spec”不会在那里得到处理,并且如果将其放入“V1Pod”定义,容器的 security_context 也不会被设置。但看起来在 1.10.12(这是 us-central1-b 上 Cloud Composer 的最新版本)中,他们已经修复了这个问题并且可以使用,但我自己没有尝试过。关于答案的另一件事是,Airflow 在此类 pod 规范定义上抛出空容器错误,您应该传递像这样的容器列表“k8s.V1PodSpec(containers[container])”。 (2认同)