我想安排GCP的SQL实例。如何自动触发启动/停止SQL实例?
我已经成功地调度了计算引擎VM,但仍停留在SQL实例调度中。
为了实现这一点,您可以使用云函数调用云SQL管理API来启动和停止您的云SQL实例(您需要2个云函数(
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
你可以看到我关于如何使用云函数来启动云SQL实例和停止云SQL实例的代码
创建云功能后,您可以配置云调度程序以触发每个云功能的HTTP地址,也可以按照本指南的建议方法,使用pub/sub 触发功能