如何为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?

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

问题描述

将docker容器映像部署到Cloud Run时,我可以选择一个区域,这很好. Cloud Run将构建委托给Cloud Build,Cloud Build显然创建了两个存储桶以实现此目的.意外的行为是,没有在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.

如何将区域指定为"us-east1",以使存储成本由始终免费"层承担?(显然,美国多区域存储桶将数据存储在外部区域的免费套餐限制,导致产生了意外费用-我正努力避免该费用.)

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.)

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

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.

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

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

推荐答案

如前所述,Cloud Build会创建一个具有多个区域的存储桶,因为在Cloud Run中创建服务时,仅向其中添加了所需的标志和参数.部署服务.

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.

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

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

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

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.

由于未设置此标志,因此将在multi-regionus中创建存储桶.此行为也适用于标志 --gcs-log-dir .

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.

现在要在双区域,区域或多区域中使用存储桶的必要步骤是使用cloudbuild.yaml和标记--gcs-source-staging-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. 在您可能需要的区域,双区域或多区域中创建存储桶.例如,我创建了一个名为"example-bucket"的存储桶,在australia-southeast1中.

创建一个cloudbuild.yaml文件.必须将构建的工件存储在所需的存储桶中,如

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: ['*']

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

  • Finally you could run the following command:

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

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

    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天全站免登陆