简体   繁体   English

Google Cloud Run 或 Cloud Function 用于一次性任务

[英]Google Cloud Run or Cloud Function for one-time-task-job

We have startup script that will create monitoring alerts using Google APIs.我们有启动脚本,可以使用 Google API 创建监控警报。

For this we created a postman collection where we have bundled all these calls and then we wish to run a shell script where we will use service account json file and call the postman collection为此,我们创建了一个 postman 集合,其中捆绑了所有这些调用,然后我们希望运行一个 shell 脚本,我们将在其中使用服务帐户 json 文件并调用 postman 集合

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

We created a docker file for this task with required dependencies and gcloud sdk as base image.我们为此任务创建了一个 docker 文件,其中包含所需的依赖项和 gcloud sdk 作为基础映像。

Now, We also want to give provision to execute this one time job in Google Cloud to users.现在,我们还想为用户提供在谷歌云中执行这项一次性工作的条件。 For this we are considering options such as Cloud Run, Cloud Function, App Engine.为此,我们正在考虑 Cloud Run、Cloud Function、App Engine 等选项。 But, all these services expose an endpoint and then it has to be called and the the service will perform the action.但是,所有这些服务都公开了一个端点,然后必须调用它并且服务将执行操作。

Our requirement is a one time activity so we don't want to keep a service running all the time.我们的要求是一次性活动,因此我们不想让服务一直运行。 Also, this is not a node.js, java, go application that these services support.此外,这不是这些服务支持的 node.js、java、go 应用程序。

It is more like a one time job task, that should get trigger do it's job and then stop.它更像是一项一次性工作任务,应该触发它完成它的工作然后停止。

Does google cloud support such a use case.谷歌云是否支持这样的用例。 Basic hello world docker is an example of this kind of requirement that we have - https://hub.docker.com/_/hello-world基本的 hello world docker 是我们拥有的这种需求的示例 - https://hub.docker.com/_/hello-world

We tried cloud run but it expects a portNo to be present with out which it fails我们尝试了云运行,但它希望存在一个端口号,否则它会失败

Deployment failed部署失败
ERROR: (gcloud.run.deploy) Cloud Run error: Container failed to start.错误:(gcloud.run.deploy) Cloud Run 错误:容器无法启动。 Failed to start and then listen on the port defined by the PORT environment variable.无法启动然后侦听 PORT 环境变量定义的端口。 Logs for this revision might contain more information.此修订的日志可能包含更多信息。

Please share some thoughts.请分享一些想法。

thanks, Aakash谢谢,阿卡什

I recommend you to use GKE Autopilot .我推荐你使用GKE Autopilot You build your container and you run it, as pod job in your cluster.您构建容器并运行它,作为集群中的 pod 作业。 No need to expose a port or a webserver.无需公开端口或网络服务器。

The first cluster is free on your billing account.第一个集群在您的计费帐户上是免费的。 You pay only for the CPU time and the Memory time of your running pod.您只需为运行的 pod 的 CPU 时间和 Memory 时间付费。


A dirty workaround is to use Cloud Build to run your container also, it's serverless, nothing to provision.一个肮脏的解决方法是也使用 Cloud Build 来运行您的容器,它是无服务器的,无需提供任何东西。 It's not done for, it's a hack, but it works!它没有完成,它是一个黑客,但它有效!

What is the "event" that you have that you want to trigger your script?您想要触发脚本的“事件”是什么?

The services that you list are often triggered by some user-triggered HTTP method call but that's not necessarily the case and you can use eg Eventarc to trigger a Cloud Run services and various events|triggers for Cloud Functions.您列出的服务通常由某些用户触发的 HTTP 方法调用触发,但情况不一定如此,您可以使用例如Eventarc来触发 Cloud Run 服务和 Cloud Functions 的各种事件|触发器

It may be that Cloud Scheduler addresses your needs. Cloud Scheduler可能会满足您的需求。 This provides a cron mechanism for jobs.这为作业提供了一个 cron 机制。

NOTE IIUC, you're postman script makes multiple REST API calls against Cloud Platform services.注意IIUC,您的 postman 脚本对 Cloud Platform 服务进行了多次 REST API 调用。 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.这是解决问题的一种方法,但正如您所发现的,它通过合并云 SDK ( gcloud ) 来获取凭据,并希望 (?.) 在任何调用失败时包含一些错误处理。 You may wish to consider leveraging one of Google's SDKs instead of scripting the REST APIs directly, The SDKs incorporate auth, logging.您可能希望考虑利用 Google 的 SDK 之一,而不是直接编写 REST API 脚本,SDK 包含身份验证、日志记录。 provide abstractions and encourage better error handling.提供抽象并鼓励更好的错误处理。

The hello-world container is not listening at any port port. hello-world 容器没有在任何端口监听。 That explains the error you got.这解释了你得到的错误。 Your container, must be listening for incoming HTTP requests as stated in the Container runtime contract .您的容器必须按照 容器运行时合同中的规定侦听传入的 HTTP 请求。 By default, Cloud Run requests are sent to 8080, but you can configure Cloud Run to send requests to the port of your choice.默认情况下,Cloud Run 请求发送到 8080,但您可以将 Cloud Run 配置为将请求发送到您选择的端口。

So to be able to use Cloud Run as a solution, add a web server to your container.因此,为了能够使用 Cloud Run 作为解决方案,请将 web 服务器添加到您的容器中。

Finally, keep in mind that the max timeout with cloud run is 1h.最后,请记住,云运行的最大超时为 1 小时。

Because your service may not support by cloud functions (check functions-framework ) or app engine standard natively, and also want the instances to scale down to zero, I think Cloud Run is for you.因为您的服务可能不支持云功能(检查功能框架)或应用引擎标准本机,并且还希望实例缩减为零,我认为 Cloud Run 适合您。

The question becomes "how to invoke the Cloud Run service"问题变成了“如何调用 Cloud Run 服务”

You can invoke either by events (firebase, gcs, pubsub...) or HTTP requests.您可以通过事件(firebase、gcs、pubsub...)或 HTTP 请求调用。 If you deploy the service as a HTTP service, you should listen to PORT environment, no matter what your program language (even in shell ).如果将该服务部署为 HTTP 服务,则无论您的程序语言是什么(即使是shell ),您都应该监听 PORT 环境。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Google Cloud SDK 创建 Cloud Run Job - Google Cloud SDK to create a Cloud Run Job 如何将参数传递给 Google Cloud Run Job - How to pass parameters to Google Cloud Run Job Google Cloud Run - 创建服务任务永远加载 - Google Cloud Run - create service task is loadingforever 从云端在 GKE 中运行作业 function - Run Job in GKE from a cloud function Google Cloud 中的 EventArc 一次多次请求 Cloud Run Service 触发器 - EventArc in Google Cloud Request the Cloud Run Service more than Once in One Time Trigger Google Cloud Function 并行运行具有不同用户参数的相同训练作业 - Google Cloud Function to run same training job with different user args in paralell 了解求职面试所需的 Google Cloud Platform 任务 - Understanding Google Cloud Platform task required for Job Interview 如何通过任务名称获取Google Cloud Task的预定时间? - How to get the scheduled time of Google Cloud Task by task name? 如何使用自定义 Docker 图像运行 Python Google Cloud Dataflow 作业? - How to run a Python Google Cloud Dataflow job with a custom Docker image? 如何在谷歌云运行中更改时区 - How to change time zone in google Cloud Run
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM