Google Cloud 运行 或 one-time-task-job 的 Cloud Functions
Google Cloud Run or Cloud Function for one-time-task-job
我们有启动脚本,可以使用 Google API 创建监控警报。
为此,我们创建了一个邮递员 collection,我们将所有这些调用捆绑在一起,然后我们希望 运行 一个 shell 脚本,我们将在其中使用服务帐户 json 归档并呼叫邮递员[=38=]
export GOOGLE_APPLICATION_CREDENTIALS=/home/abc/sac-5f63.json
export TOKEN=$(gcloud auth application-default print-access-token)
echo $TOKEN
echo "Running postman collection start"
newman run /home/abc/API.postman_collection.json
我们为此任务创建了一个 docker 文件,其中包含所需的依赖项和 gcloud sdk 作为基础映像。
现在,我们还想为用户提供在 Google 云中执行这项一次性工作的条件。为此,我们正在考虑 Cloud 运行、Cloud Function、App Engine 等选项。但是,所有这些服务都会公开一个端点,然后必须调用它并且服务将执行操作。
我们的要求是一次性 activity 所以我们不想一直保持服务 运行ning。此外,这不是这些服务支持的节点 js,java,go 应用程序。
它更像是一项一次性工作任务,应该让触发器完成它的工作然后停止。
google 云是否支持这样的用例。基本的 hello world docker 是我们拥有的这种要求的一个例子 -
https://hub.docker.com/_/hello-world
我们尝试了云 运行,但它希望存在一个端口号,否则它会失败
Deployment failed
ERROR: (gcloud.run.deploy) Cloud Run error: Container failed to start.
Failed to start and then listen on the port defined by the PORT
environment variable. Logs for this revision might contain more
information.
请分享一些想法。
谢谢,
阿卡什
您想要触发脚本的“事件”是什么?
您列出的服务通常由某些用户触发的 HTTP 方法调用触发,但情况不一定如此,您可以使用例如Eventarc to trigger a Cloud Run services and various events|triggers 用于云函数。
可能 Cloud Scheduler 满足了您的需求。这为作业提供了一个 cron 机制。
NOTE IIUC, you're postman script makes multiple REST API calls against Cloud Platform services. This is one way to address the problem but, as you've found, it is aided by incorporating the Cloud SDK (gcloud
) to obtain credentials and hopefully (!?) includes some error handling if any of the calls fail. You may wish to consider leveraging one of Google's SDKs instead of scripting the REST APIs directly. The SDKs incorporate auth, logging, provide abstractions and encourage better error handling.
hello-world 容器未在任何端口侦听。这解释了你得到的错误。您的容器必须正在侦听传入的 HTTP 请求,如 Container runtime contract 中所述。默认情况下,Cloud 运行 请求发送到 8080,但您可以配置 Cloud 运行 将请求发送到您选择的端口。
因此,为了能够使用 Cloud 运行 作为解决方案,请将 Web 服务器添加到您的容器中。
最后,请记住云 运行 的最大超时为 1 小时。
我推荐你使用GKE Autopilot。您构建容器并 运行 它作为集群中的 pod 作业。无需公开端口或网络服务器。
第一个集群在您的结算帐户上是免费的。您只需为 CPU 时间和 运行ning pod 的记忆时间付费。
一个肮脏的解决方法是使用 Cloud Build 来 运行 您的容器,它是无服务器的,无需配置。这还没有完成,这是一个 hack,但它确实有效!
因为您的服务可能不支持云功能(检查functions-framework)或原生应用引擎标准,并且还希望实例缩减为零,我认为云 运行 适合您.
问题变为“如何调用云 运行 服务”
您可以通过事件(firebase、gcs、pubsub...)或 HTTP 请求调用。
如果您将该服务部署为 HTTP 服务,则无论您的程序语言是什么(即使是 shell),您都应该监听 PORT 环境。
我们有启动脚本,可以使用 Google API 创建监控警报。
为此,我们创建了一个邮递员 collection,我们将所有这些调用捆绑在一起,然后我们希望 运行 一个 shell 脚本,我们将在其中使用服务帐户 json 归档并呼叫邮递员[=38=]
export GOOGLE_APPLICATION_CREDENTIALS=/home/abc/sac-5f63.json
export TOKEN=$(gcloud auth application-default print-access-token)
echo $TOKEN
echo "Running postman collection start"
newman run /home/abc/API.postman_collection.json
我们为此任务创建了一个 docker 文件,其中包含所需的依赖项和 gcloud sdk 作为基础映像。
现在,我们还想为用户提供在 Google 云中执行这项一次性工作的条件。为此,我们正在考虑 Cloud 运行、Cloud Function、App Engine 等选项。但是,所有这些服务都会公开一个端点,然后必须调用它并且服务将执行操作。
我们的要求是一次性 activity 所以我们不想一直保持服务 运行ning。此外,这不是这些服务支持的节点 js,java,go 应用程序。
它更像是一项一次性工作任务,应该让触发器完成它的工作然后停止。
google 云是否支持这样的用例。基本的 hello world docker 是我们拥有的这种要求的一个例子 - https://hub.docker.com/_/hello-world
我们尝试了云 运行,但它希望存在一个端口号,否则它会失败
Deployment failed
ERROR: (gcloud.run.deploy) Cloud Run error: Container failed to start. Failed to start and then listen on the port defined by the PORT environment variable. Logs for this revision might contain more information.
请分享一些想法。
谢谢, 阿卡什
您想要触发脚本的“事件”是什么?
您列出的服务通常由某些用户触发的 HTTP 方法调用触发,但情况不一定如此,您可以使用例如Eventarc to trigger a Cloud Run services and various events|triggers 用于云函数。
可能 Cloud Scheduler 满足了您的需求。这为作业提供了一个 cron 机制。
NOTE IIUC, you're postman script makes multiple REST API calls against Cloud Platform services. This is one way to address the problem but, as you've found, it is aided by incorporating the Cloud SDK (
gcloud
) to obtain credentials and hopefully (!?) includes some error handling if any of the calls fail. You may wish to consider leveraging one of Google's SDKs instead of scripting the REST APIs directly. The SDKs incorporate auth, logging, provide abstractions and encourage better error handling.
hello-world 容器未在任何端口侦听。这解释了你得到的错误。您的容器必须正在侦听传入的 HTTP 请求,如 Container runtime contract 中所述。默认情况下,Cloud 运行 请求发送到 8080,但您可以配置 Cloud 运行 将请求发送到您选择的端口。
因此,为了能够使用 Cloud 运行 作为解决方案,请将 Web 服务器添加到您的容器中。
最后,请记住云 运行 的最大超时为 1 小时。
我推荐你使用GKE Autopilot。您构建容器并 运行 它作为集群中的 pod 作业。无需公开端口或网络服务器。
第一个集群在您的结算帐户上是免费的。您只需为 CPU 时间和 运行ning pod 的记忆时间付费。
一个肮脏的解决方法是使用 Cloud Build 来 运行 您的容器,它是无服务器的,无需配置。这还没有完成,这是一个 hack,但它确实有效!
因为您的服务可能不支持云功能(检查functions-framework)或原生应用引擎标准,并且还希望实例缩减为零,我认为云 运行 适合您.
问题变为“如何调用云 运行 服务”
您可以通过事件(firebase、gcs、pubsub...)或 HTTP 请求调用。 如果您将该服务部署为 HTTP 服务,则无论您的程序语言是什么(即使是 shell),您都应该监听 PORT 环境。