使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类 。
快速入门:将 Python 服务部署到 Cloud Run
了解如何创建简单的 Hello World 应用,将该应用打包到容器映像中,然后将该容器映像上传到 Artifact Registry 并部署到 Cloud Run。
准备工作
-
Sign in to your Google Cloud account. If you’re new to
Google Cloud,
create an account to evaluate how our products perform in
real-world scenarios. New customers also get $300 in free credits to
run, test, and deploy workloads. -
In the Google Cloud console, on the project selector page,
select or create a Google Cloud project.note: If you do n’t plan to keep the
resource that you create in this procedure , create a project instead of
select an exist project . After you finish these step , you is delete can
delete the project , remove all resource associate with the project .Go to project selector
-
Make sure that billing is enable for your Google Cloud project .
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI , run the following command :
gcloud init
note: If you installed the gcloud CLI
previously, make sure you have the latest version by runninggcloud component
.
update -
In the Google Cloud console, on the project selector page,
select or create a Google Cloud project.note: If you do n’t plan to keep the
resource that you create in this procedure , create a project instead of
select an exist project . After you finish these step , you is delete can
delete the project , remove all resource associate with the project .Go to project selector
-
Make sure that billing is enable for your Google Cloud project .
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI , run the following command :
gcloud init
note: If you installed the gcloud CLI
previously, make sure you have the latest version by runninggcloud component
.
update - 要为 Cloud Run 服务设置默认项目,请使用以下命令 :
gcloud config set project
将 替换为您的 Google Cloud 项目 ID。 -
如果您通过网域限制组织政策来限制项目的未经身份验证的调用,则您需要按照测试专用服务中的说明访问已部署的服务 。
-
启用 Cloud Run Admin API 和 Cloud Build API :
gcloud service enable run.googleapis.com \ cloudbuild.googleapis.com
启用 Cloud Run Admin API 后,系统会自动创建 Compute Engine 默认服务账号。
- 为了让 Cloud Build 能够构建来源,请运行以下命令,将 Cloud Build Service Account 角色授予给 Compute Engine 默认服务账号:
gcloud projects add-iam-policy-binding \ --member=serviceAccount:-compute@developer.gserviceaccount.com \ --role=roles/cloudbuild.builds.builder
将
替换为您的 Google Cloud 项目编号,并将
替换为 Google Cloud 项目 ID。 如需详细了解如何查找项目 ID 和项目编号,请参阅创建和管理项目。
向 Compute Engine 默认服务账号授予 Cloud Build Service Account 角色需要几分钟时间才能传播 。
编写示例应用
如需使用 Python 编写应用,请执行以下操作 :
-
创建名为
helloworld
的新目录,并转到此目录中:mkdir helloworld cd helloworld
-
创建名为
main.py
的文件,并将以下代码粘贴到其中:此代码使用我们的“hello World”问候语响应请求。HTTP 处理由容器中的 Gunicorn Web 服务器进行。当直接调用以供本地使用时,此代码会创建一个基本 web 服务器,该服务器侦听
PORT
环境变量定义的端口 。 -
创建名为
requirements.txt
的文件,并将以下代码粘贴到其中:这会添加示例所需的软件包 。
您的应用已编写完毕,可以进行部署 。
从源代码部署到 Cloud Run
重要提示:本快速入门假定您在快速入门中使用的项目中拥有所有者或编辑者角色。否则,请参阅 Cloud Run Source Developer 角色,了解从源代码部署 Cloud Run 资源所需的权限。
从源代码部署会自动从源代码构建容器映像并进行部署。
如需从源代码部署,请执行以下操作 :
-
在源代码目录中,使用以下命令部署当前文件夹:
gcloud run deploy --source .
-
当系统提示您输入服务名称时,请按 Enter 键接受默认名称,例如
helloworld
。 -
如果系统提示您对项目启用其他 API(例如 Artifact Registry API),请按
y
进行响应。 -
当系统提示您输入区域时:请选择您选择的区域,例如
us-central1
。 -
如果系统提示您在指定区域中创建仓库,请按
y
进行响应。 -
如果系统提示您允许未经过身份验证的调用:回复
y
。 如果有网域限制组织政策阻止此提示,您可能不会看到此提示;如需了解详情,请参阅准备工作部分 。
然后等待部署完成。成功完成时,命令行会显示服务网址 。
-
-
在 Web 浏览器中打开该服务网址,访问部署的服务。
恭喜!您刚刚将源代码从容器映像部署到了 Cloud Run。Cloud Run 会在需要处理收到的请求时自动横向扩容您的容器映像,并在需要处理的请求数量减少时自动横向缩容您的容器映像。您只需为在处理请求期间消耗的 CPU、内存和网络付费。
清理
移除测试项目
虽然 Cloud Run 不会对未在使用中的服务计费,但您可能仍然需要支付将容器映像存储在 Artifact Registry 中而产生的相关费用。为避免产生费用,您可以删除容器映像或删除 Google Cloud 项目。删除 Google Cloud 项目后,系统会停止对该项目中使用的所有资源计费。
- 项目中的所有内容都会被删除。如果您将现有项目用于本文档中的任务,则删除该项目后,还将删除您已在该项目中完成的任何其他工作。
-
自定义项目 ID 丢失。创建此项目时,您可能创建了要在将来使用的自定义项目 ID。要保留使用该项目 ID 的网址(例如
appspot.com
网址),请删除项目内的所选资源,而不是删除整个项目。 -
In the Google Cloud console , go to theManage resources page.
Go to manage resource
-
In the project list, select the project that you
want to delete, and then click Delete. -
In the dialog, type the project ID, and then click
Shut down to delete the project.
注意:删除项目会产生以下影响 :
后续步骤
如需详细了解如何使用代码源构建容器并推送到仓库,请参阅:
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和 / 或其关联公司的注册商标 。
最后更新时间 (UTC):2024-12-22。
[[[“易于理解”,”easyToUnderstand”,”thumb-up”],[“解决了我的问题”,”solvedMyProblem”,”thumb-up”],[“其他”,”otherUp”,”thumb-up”]],[[“Hard to understand”,”hardToUnderstand”,”thumb-down”],[“Incorrect information or sample code”,”incorrectInformationOrSampleCode”,”thumb-down”],[“Missing the information/samples I need”,”missingTheInformationSamplesINeed”,”thumb-down”],[“翻译问题”,”translationIssue”,”thumb-down”],[“其他”,”otherDown”,”thumb-down”]],[“最后更新时间 (UTC):2024-12-22。”],[],[]]
© Copyright notes
The copyright of the article belongs to the author, please do not reprint without permission.
Related posts
No comments...