如何为 Cloud Build 用于 Cloud Run 部署的 Cloud Storage 存储分区指定区域? [英] How can I specify a region for the Cloud Storage buckets used by Cloud Build for a Cloud Run deployment?

查看:18
本文介绍了如何为 Cloud Build 用于 Cloud Run 部署的 Cloud Storage 存储分区指定区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将 docker 容器映像部署到 Cloud Run 时,我可以选择一个区域,这很好.Cloud Run 将构建委托给 Cloud Build,它显然创建了两个存储桶来实现这一点.意外行为是存储分区不是在 Cloud Run 部署的区域中创建的,而是默认为美国多区域.

如何将区域指定为us-east1",以便永远免费"层吸收存储成本?(显然美国多区域存储桶将数据存储在外部区域免费套餐限制的一部分,这导致了意外账单 - 我正在努力避免该账单.)

如果重要的话,我也在这个项目中使用 Firebase.我在 us-east1 区域创建了 Firebase 默认存储桶,希望它也可以成为其他桶的默认存储桶,但事实并非如此.最终的存储桶列表如下所示,您可以在其中看到使用不需要的多区域设置自动创建的两个存储桶.

这是我用来构建和部署的 shell 脚本:

#!/bin/shproject_id=$1service_id=$2如果 [ -z "$project_id" ];然后echo "第一个参数必须是 Google Cloud 项目 ID" >&2出口 1菲如果 [ -z "$service_id" ];然后echo "第二个参数必须是 Cloud Run 应用程序名称" >&2出口 1菲echo "将 $service_id 部署到 $project_id"tag="gcr.io/$project_id/$service_id"gcloud 构建提交 --project "$project_id" --tag "$tag" &&gcloud 运行部署$service_id"--project "$project_id" --image "$tag" --平台管理--update-env-vars "GOOGLE_CLOUD_PROJECT=$project_id" --region us-central1 --允许未经身份验证

解决方案

正如您提到的,Cloud Build 创建了一个或多个具有多区域的存储桶,因为在 Cloud Run 中创建服务时,只添加了所需的标志和参数到部署服务.

文档 对于命令 gcloud builds submit 提到以下标志 --gcs-source-staging-dir:

<块引用>

--gcs-source-staging-dir=GCS_SOURCE_STAGING_DIR

Google Cloud Storage 中的一个目录,用于复制用于暂存构建的源.如果指定的存储桶不存在,Cloud Build 将创建一个.如果不设置此字段,则使用 gs://[PROJECT_ID]_cloudbuild/source.

由于没有设置这个标志,bucket 是在 multi-regionus 中创建的.此行为也适用于标志 --gcs-log-dir.

现在,在您想要的双区域、区域或多区域中使用存储桶的必要步骤是使用 cloudbuild.yaml 并使用标志 --gcs-source-暂存目录.您可以执行以下操作:

  1. 在您可能需要的区域、双区域或多区域中创建一个存储桶.例如,我创建了一个名为example-bucket"的存储桶;在 australia-southeast1.
  2. 创建一个 cloudbuild.yaml 文件.这是将构建的工件存储在您想要的存储桶中所必需的,如前所述 此处.一个例子如下:

步骤:- 名称:'gcr.io/cloud-builders/gcloud'参数:- '跑'- '部署'- '云运行服务'- ' - 图片'- 'gcr.io/PROJECT_ID/IMAGE'- ' - 地区'- 'REGION_TO_DEPLOY'- ' - 平台'- '管理'- '--allow-unauthenticated'文物:对象:位置:'gs://example-bucket'路径:['*']

  1. 最后,您可以运行以下命令:

gcloud builds submit --gcs-source-staging-dir=gs://example-bucket/cloudbuild-custom"--config cloudbuild.yaml

前面提到的步骤可以适应您的脚本.请试一试:),您会看到,即使 Cloud Run 服务部署在亚洲、欧洲或美国,之前指定的存储桶也可以在其他位置.

When deploying a docker container image to Cloud Run, I can choose a region, which is fine. Cloud Run delegates the build to Cloud Build, which apparently creates two buckets to make this happen. The unexpected behavior is that buckets aren't created in the region of the Cloud Run deployment, and instead default to multi-region US.

How do I specify the region as "us-east1" so the cost of storage is absorbed by the "always free" tier? (Apparently US multi-region storage buckets store data in regions outside of the free tier limits, which resulted in a surprise bill - I am trying to avoid that bill.)

If it matters, I am also using Firebase in this project. I created the Firebase default storage bucket in the us-east1 region with the hopes that it might also become the default for other buckets, but this is not so. The final bucket list looks like this, where you can see the two buckets created automatically with the undesirable multi-region setting.

This is the shell script I'm using to build and deploy:

#!/bin/sh

project_id=$1
service_id=$2

if [ -z "$project_id" ]; then
    echo "First argument must be the Google Cloud project ID" >&2
    exit 1
fi

if [ -z "$service_id" ]; then
    echo "Second argument must be the Cloud Run app name" >&2
    exit 1
fi

echo "Deploying $service_id to $project_id"

tag="gcr.io/$project_id/$service_id"

gcloud builds submit 
    --project "$project_id" 
    --tag "$tag" 
&& 
gcloud run deploy "$service_id" 
    --project "$project_id" 
    --image "$tag" 
    --platform managed 
    --update-env-vars "GOOGLE_CLOUD_PROJECT=$project_id" 
    --region us-central1 
    --allow-unauthenticated

解决方案

As you mention, Cloud Build creates a bucket or buckets with multi region because when creating the service in Cloud Run, there are only added the needed flags and arguments to deploy the service.

The documentation for the command gcloud builds submit mentions the following for the flag --gcs-source-staging-dir:

--gcs-source-staging-dir=GCS_SOURCE_STAGING_DIR

A directory in Google Cloud Storage to copy the source used for staging the build. If the specified bucket does not exist, Cloud Build will create one. If you don't set this field, gs://[PROJECT_ID]_cloudbuild/source is used.

As this flag is not set, the bucket is created in multi-region and in us. This behavior also applies for the flag --gcs-log-dir.

Now the necessary steps to use the bucket in the dual-region, region or multi-region you want is using a cloudbuild.yaml and using the flag --gcs-source-staging-dir. You can do the following:

  1. Create a bucket in the region, dual-region or multi-region you may want. For example I created a bucket called "example-bucket" in australia-southeast1.
  2. Create a cloudbuild.yaml file. This is necessary to store the artifacts of the build in the bucket you want as mentioned here. An example is as follows:

steps:
- name: 'gcr.io/cloud-builders/gcloud'
    args:
    - 'run'
    - 'deploy'
    - 'cloudrunservice'
    - '--image'
    - 'gcr.io/PROJECT_ID/IMAGE'
    - '--region'
    - 'REGION_TO_DEPLOY'
    - '--platform'
    - 'managed'
    - '--allow-unauthenticated'
artifacts:
    objects:
    location: 'gs://example-bucket'
    paths: ['*']

  1. Finally you could run the following command:

gcloud builds submit --gcs-source-staging-dir="gs://example-bucket/cloudbuild-custom" --config cloudbuild.yaml

The steps mentioned before can adapted to your script. Please give a try :) and you will see that even if the Cloud Run service is deployed in Asia, Europe or US, the bucket specified before can be in another location.

这篇关于如何为 Cloud Build 用于 Cloud Run 部署的 Cloud Storage 存储分区指定区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆