使用 Python Kubernetes API 使用 Secret 挂载卷

Mount volumes with Secrets using Python Kubernetes API

我正在使用 KubernetesPodOperator 编写 Airflow DAG。容器中的 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. Using Python API for Kubernetes 我试图将它们作为一个卷安装但没有成功。 credentials/ 目录存在于容器中,但它是空的。我应该怎么做才能使文件 jira_crendentials.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

根据 this 示例,Secret 是一个特殊的 class,它将处理自动创建卷装载。查看您的代码,您自己的挂载 /credentials 卷似乎覆盖了 Secret 创建的 /credentials 挂载,并且因为您提供了空的 configs={},该挂载也是空的.

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