本教程介绍如何使用 Cloud Scheduler 和 Cloud Run functions 安排 Filestore 实例的备份。
如需了解如何使用代管式服务调度器创建备份,请参阅 Filestore 增强型备份。
目标
- 为 Cloud Scheduler 创建客户端服务帐号,该账号具有调用 Cloud Run functions 函数所需的凭据。
- 为 Cloud Run functions 创建客户端服务帐号,该账号具有调用 Filestore 端点的凭据。
- 创建一个 Cloud Run functions 函数,用于创建 Filestore 实例的备份。
- 创建一个 Cloud Run functions 函数,用于删除 Filestore 实例的备份。
- 创建一个 Cloud Scheduler 作业,用于定期运行任一函数。
费用
在本文档中,您将使用的以下收费组件: Google Cloud
- Artifact Registry API
- Cloud Build API
- Filestore API
- Cloud Functions API
- Cloud Logging API
- Pub/Sub API
- Cloud Run Admin API
- Cloud Scheduler API
您可使用 价格计算器 根据您的预计使用情况来估算费用。
准备工作
- 登录您的 Google Cloud 账号。如果您是 Google Cloud的新用户, 请创建一个账号,以评估我们的产品在 实际场景中的表现。新客户还可获享 $300 赠金,用于 运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry, Cloud Build, Filestore, Cloud Run functions, Cloud Logging, Pub/Sub, Cloud Run, and Cloud Scheduler APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init -
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Artifact Registry, Cloud Build, Filestore, Cloud Run functions, Cloud Logging, Pub/Sub, Cloud Run, and Cloud Scheduler APIs.
Roles required to enable APIs
To enable APIs, you need the
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
安装 Google Cloud CLI。
-
如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI。
-
如需初始化 gcloud CLI,请运行以下命令:
gcloud init - 如果您的项目中没有 Filestore 实例,则必须先创建一个。
为 Cloud Scheduler 和 Cloud Run functions 创建客户端服务账号
如果您尚未执行此操作,请在 Google Cloud 控制台中点击 激活 Cloud Shell。
创建一个 Cloud Scheduler 用于调用 Cloud Run functions 函数的客户端服务帐号运行身份。在此示例中,使用
iam service-accounts create命令将账号命名为schedulerunner,并将显示名设置为 “FS Backups-Scheduler 的服务账号”:gcloud iam service-accounts create schedulerunner \ --display-name="Service Account for FS Backups-Scheduler"创建一个 Cloud Run functions 运行身份用以调用 Filestore 端点的客户端服务帐号。在此示例中,我们将账号命名为
backupagent,并将显示名设置为“FS Backups-GCF 的服务账号”:gcloud iam service-accounts create backupagent \ --display-name="Service Account for FS Backups-GCF"您可以通过运行
iam service-accounts list命令来检查是否已创建服务帐号:gcloud iam service-accounts list该命令会返回如下内容:
NAME EMAIL DISABLED Service Account for FS Backups-GCF backupagent@$PROJECT_ID.iam.gserviceaccount.com False Service Account for FS Backups-Scheduler schedulerunner@$PROJECT_ID.iam.gserviceaccount.com False
设置环境变量
在本地环境中设置以下环境变量:
Google Cloud 项目 ID 和项目:
export PROJECT_ID=`gcloud config get-value core/project` export PROJECT_NUMBER=`gcloud projects describe $PROJECT_ID --format="value(projectNumber)"`Cloud Scheduler 服务代理以及 Cloud Scheduler 和 Cloud Run functions 的客户端 服务账号 :
export SCHEDULER_SA=service-$PROJECT_NUMBER@gcp-sa-cloudscheduler.iam.gserviceaccount.com export SCHEDULER_CLIENT_SA=schedulerunner@$PROJECT_ID.iam.gserviceaccount.com export GCF_CLIENT_SA=backupagent@$PROJECT_ID.iam.gserviceaccount.com您的 Filestore 实例:
export SOURCE_INSTANCE_LOCATION=fs-location export SOURCE_INSTANCE_NAME=instance-id export SHARE_NAME=file-share-name替换以下内容:
- fs-location,替换为来源 Filestore 实例所在的可用区或区域。
- instance-id,替换为来源 Filestore 实例的实例 ID。
- file-share-name,替换为您为从实例提供的 NFS 文件共享 所指定的名称。
为 Filestore 备份设置环境变量:
export BACKUP_REGION=backup-region将 backup-region 替换为要存储备份的区域。
创建用于创建备份的函数
在 Google Cloud 控制台中,前往 Cloud Run functions 页面。
点击编写函数 并按如下所示配置函数:
- 配置:
- 服务名称:在此示例中,我们将函数命名为
fsbackup。 - 区域:在此示例中,选择
us-central1。 - 运行时:从菜单中选择 Cloud Run functions 完全支持的任何受支持的 Python 3 运行时。
- 服务名称:在此示例中,我们将函数命名为
- 触发器:
- 在此示例中,无需设置触发器。
- 身份验证:选择
Require authentication。 - 入站流量:选择
All。 - 容器、卷、网络、安全性
- 前往 Security 标签页,然后从菜单中选择
Service Account for FS Backups-GCF(backupagent@$PROJECT_ID.iam.gserviceaccount.com)。
- 前往 Security 标签页,然后从菜单中选择
- 配置:
点击创建 ,然后继续进行如下配置:
- 函数入口点:输入
create_backup。 将以下依赖项添加到
requirements.txt文件中:functions-framework==3.* google-auth==2.29.0
- 函数入口点:输入