计划GCP SQL实例上的启动/停止

时间:2020-04-08 12:40:21

标签: google-cloud-sql

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

我已经成功地调度了计算引擎VM,但是陷入了SQL实例调度中。

1 个答案:

答案 0 :(得分:1)

为实现此目的,您可以使用Cloud Function调用Cloud SQL Admin API来启动和停止Cloud SQL实例(您将需要2个Cloud函数)

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来start a Cloud SQL instancestop a Cloud SQL instance

的代码。

创建Cloud函数后,您可以configure the Cloud Scheduler触发每个Cloud函数的HTTP地址,也可以按照推荐的方法guide并通过pub / sub触发函数