计划GCP SQL实例上的启动/停止 [英] Schedule Start/stop on GCP SQL Instance

查看:78
本文介绍了计划GCP SQL实例上的启动/停止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想安排我的GCP的SQL实例.如何自动触发启动/停止SQL实例?

I want to schedule my SQL Instance of GCP. How to Trigger start/ stop SQL Instance automatically?

我已经成功计划了计算引擎VM,但陷入了SQL实例计划中.

I already successfully scheduled the compute Engine VM but stuck in SQL Instance scheduling.

推荐答案

为实现此目的,您可以使用云功能以调用

In order to achieve this you can use a Cloud Function to make a call to the Cloud SQL Admin API to start and stop your Cloud SQL instance (you will need 2 Cloud functions)

def hello_world(request):

instance = 'test'  # TODO: Update placeholder value.
request = service.instances().get(project=project, instance=instance)
response = request.execute()
j = response["settings"]
settingsVersion = int(j["settingsVersion"])

dbinstancebody = {
   "settings": {
       "settingsVersion": settingsVersion,
       "tier": "db-n1-standard-1",
       "activationPolicy": "Always"
   }
}

request = service.instances().update(
   project=project,
   instance=instance,
   body=dbinstancebody)
response = request.execute()
pprint(response)

request_json = request.get_json()

if request.args and 'message' in request.args:
    return request.args.get('message')
elif request_json and 'message' in request_json:
    return request_json['message']
else:
    return f"Hello World!"


requirements.txt

google-api-python-client==1.7.8
google-auth-httplib2==0.0.3
google-auth==1.6.2
oauth2client==4.1.3

您可以看到我的代码,了解如何使用Cloud Function来

You can see my code on how to use a Cloud Function to start a Cloud SQL instance and stop a Cloud SQL instance

创建云功能后,您可以配置Cloud Scheduler 来触发HTTP每个Cloud函数的地址,或者您可以按照此

After creating your Cloud Function you can configure the Cloud Scheduler to trigger the HTTP address of each Cloud function or you can follow the recommended approach of this guide and trigger the functions with pub/sub

这篇关于计划GCP SQL实例上的启动/停止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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