0

我正在使用KubernetesPodOperator. 在容器中运行的 Python 进程必须打开包含敏感数据的文件:

with open('credentials/jira_credentials.json', 'r') as f:
    creds = json.load(f)

并且必须对 CloudStorage 客户端进行身份验证:

os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "credentials/cloud_storage_credentials.json"

根据最佳安全实践,我不会将容器的图像与敏感数据打包在一起。相反,我使用Kubernetes Secrets。使用适用于 Kubernetes 的 Python API我正在尝试将它们安装为一个卷,但没有成功。该credentials/目录存在于容器中,但它是空的。我应该怎么做才能使文件jira_credentials.jsoncloud_storage_credentials.json在容器中可访问?

我的 DAG 代码:

from airflow import DAG
from datetime import datetime, timedelta
from airflow.contrib.operators.kubernetes_pod_operator import KubernetesPodOperator
from airflow.kubernetes.secret import Secret
from airflow.kubernetes.volume import Volume
from airflow.kubernetes.volume_mount import VolumeMount
from airflow.operators.dummy_operator import DummyOperator
from kubernetes.client import models as k8s

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': datetime.utcnow(),
    'email': ['airflow@example.com'],
    'email_on_failure': False,
    'email_on_retry': False,
    'retry_delay': timedelta(minutes=5)
}

volume = Volume(name="volume-credentials", configs={})
volume_mnt = VolumeMount(mount_path="/credentials", name="volume-credentials", sub_path="", read_only=True)

secret_jira_user = Secret(deploy_type="volume",
                          deploy_target="/credentials",
                          secret="jira-user-secret",
                          key="jira_credentials.json")
secret_storage_credentials = Secret(deploy_type="volume",
                                    deploy_target="/credentials",
                                    secret="jira-trans-projects-cloud-storage-creds",
                                    key="cloud_storage_credentials.json")



dag = DAG(
    dag_id="jira_translations_project",
    schedule_interval="0 1 * * MON",
    start_date=datetime(2021, 9, 5, 0, 0, 0),
    max_active_runs=1,
    default_args=default_args
)

start = DummyOperator(task_id='START', dag=dag)

passing = KubernetesPodOperator(namespace='default',
                                image="eu.gcr.io/data-engineering/jira_downloader:v0.18",
                                cmds=["/usr/local/bin/run_process.sh"],
                                name="jira-translation-projects-01",
                                task_id="jira-translation-projects-01",
                                get_logs=True,
                                dag=dag,
                                volumes=[volume],
                                volume_mounts=[volume_mnt],
                                secrets=[
                                    secret_jira_user,
                                    secret_storage_credentials],
                                env_vars={'MIGRATION_DATETIME': '2021-01-02T03:04:05'}, 
                                )

start >> passing
4

1 回答 1

2

根据这个例子,Secret是一个特殊的类,它将自动处理创建卷挂载。查看您的代码,您自己的带有 mount 的卷似乎/credentials覆盖/credentials了由 创建的 mount Secret,并且因为您提供了 empty configs={},所以该 mount 也是空的。

尝试仅提供secrets=[secret_jira_user,secret_storage_credentials]并删除手册volume_mounts


于 2021-09-15T14:35:14.173 回答