Managed Airflow(第 3 代) | Managed Airflow(第 2 代) | Managed Airflow(旧版第 1 代)
本页面介绍了如何为 Managed Airflow 环境配置 SMTP 服务。
准备工作
如果您的 Managed Airflow 环境配置为使用专用 IP,请确保该环境能够连接到外部电子邮件服务的 API 端点(例如
https://api.sendgrid.com或您首选的 SMTP 服务器)。
配置 SendGrid 电子邮件服务
若想接收通知,请配置相应环境变量,以通过 SendGrid 电子邮件服务发送电子邮件。
注册 SendGrid
如果您尚未注册,请在 Google Cloud 控制台中注册 SendGrid,点击“在提供商处管理”以转到 SendGrid 网域,然后点击“设置”以检索您的用户名并创建 API 密钥。作为 Google Cloud 开发者,您每月可以免费发送 12,000 封电子邮件。
配置变量
SendGrid 使用 API 密钥和发件人邮箱发送邮件。您可以通过以下任一选项提供此信息:
在 Secret Manager 中存储值
如需在 Secret Manager 中存储值,请执行以下操作:
为您的环境配置 Secret Manager。请务必为 Secret 后端设置权限和 Airflow 配置选项。
替换以下 Airflow 配置选项:
部分 键 值 emailemail_conn_idsendgrid_defaultemailemail_backendairflow.providers.sendgrid.utils.emailer.send_email为名为
airflow-connections-sendgrid_default的 SendGrid 连接创建 Secret。将密钥的值设置为连接 URI。例如:sendgrid://<username>:<sendgrid_api_key>@smtp.sendgrid.net:587如需详细了解如何在 Secret Manager 中存储连接,请参阅管理 Airflow 连接。
为 SendGrid 配置邮箱。无法通过 Secret 设置邮箱。而是替换以下 Airflow 配置选项:
部分 键 值 emailfrom_email发件人邮箱,例如 noreply@example.com。
在 Airflow 中存储值
替换以下 Airflow 配置选项:
部分 键 值 emailemail_conn_idsendgrid_defaultemailemail_backendairflow.providers.sendgrid.utils.emailer.send_email在 Airflow 中,配置 名为
sendgrid_default的连接。在连接 URI 中指定 Sendgrid API 密钥。例如:gcloud composer environments run ENVIRONMENT_NAME \ --location LOCATION \ connections add -- \ --conn-uri "sendgrid://USERNAME:SENDGRID_API_KEY@smtp.sendgrid.net:587" \ sendgrid_default替换以下内容:
ENVIRONMENT_NAME:您的环境的名称。LOCATION:环境所在的区域。USERNAME:SendGrid 用户名。SENDGRID_API_KEY:SendGrid API 密钥。
为 SendGrid 配置邮箱。无法通过 Secret 设置邮箱。而是替换以下 Airflow 配置选项:
部分 键 值 emailfrom_email发件人邮箱,例如 noreply@example.com。
测试 SendGrid 配置
如需测试 SendGrid 配置,请执行以下操作:
- 创建一个使用
EmailOperator的测试 DAG。例如:
- 将 DAG 上传到您的环境,并检查任务是否成功。
- 使用您的 SendGrid 凭据登录 SendGrid。
- 在 SendGrid 界面中,转到“活动”页面。
- 在列表中搜索该电子邮件。您应该会看到 SendGrid 已处理并发送了该电子邮件。
如果该电子邮件未被处理和递送,请执行以下操作:
- 检查您的 SendGrid 配置。
- 验证您是否已启用 Secret Manager 后端。确保您已授予额外权限并为 Airflow 配置选项设置了替换。
- 检查您的电子邮件客户端的垃圾邮件过滤器。
配置第三方 SMTP 服务
如需通过第三方 SMTP 服务发送电子邮件,请替换 email_backend Airflow 配置选项并配置其他与 SMTP 相关的参数。
如需配置第三方 SMTP 服务,请替换以下 Airflow 配置选项:
| 部分 | 键 | 值 |
|---|---|---|
email |
email_backend |
airflow.utils.email.send_email_smtp |
smtp |
smtp_host |
SMTP 服务器的主机名。 |
smtp |
smtp_user |
SMTP 服务器上的用户名。 |
smtp |
smtp_port |
SMTP 服务器的端口。端口 25 不可用。您可以使用其他端口,例如标准 SMTP 端口 465 和 587。 |
smtp |
smtp_password |
不支持通过 smtp_password 设置密码。要设置 SMTP 密码,请按照配置 SMTP 密码中的说明操作。 |
smtp |
smtp_mail_from |
发件人邮箱,例如 noreply-composer@。 |
smtp |
smtp_starttls |
为增强安全性,请将其设置为 True。 |
smtp |
smtp_ssl |
为增强安全性,请将其设置为 True。 |
为第三方 SMTP 服务配置 SMTP 密码
将 SMTP 密码以纯文本形式保存在 Airflow 配置文件中是一种不好的安全做法。因此,Managed Airflow 不支持此方法。您可以使用其他两种方法来配置 SMTP 密码。
使用命令检索 SMTP 密码
无法在 Managed Airflow(旧版第 1 代)中使用命令检索 SMTP 密码。在 Airflow 界面中访问管理 > 配置时,通过命令提供密码会触发错误。
使用存储在 Secret Manager 中的密文来检索 SMTP 密码
您可以将 Secret Manager 配置为 Airflow 密钥后端。
为 Composer 环境配置 Secret Manager 后,您可以将 SMTP 密码存储在 Secret Manager 中:
创建新 Secret :
echo -n "SMTP_PASSWORD" | gcloud beta secrets create \ airflow-config-smtp-password \ --data-file=- \ --replication-policy=automatic将
SMTP_PASSWORD替换为您的 SMTP 密码。配置 Airflow 以从 Secret Manager 获取 SMTP 密码。为此,请替换以下 Airflow 配置选项。当 Airflow 从 Secret Manager 检索
airflow-config-smtp-passwordSecret 时,会自动将airflow-config-前缀附加到您为smtp_password_secret指定的值。部分 键 值 smtpsmtp_password_secretsmtp-password