How to automatically exit/stop the running instance

时光总嘲笑我的痴心妄想 提交于 2019-12-28 12:42:47

问题


I have managed to create an instance and ssh into it. However, I have couple of questions regarding the Google Compute Engine.

  1. I understand that I will be charged for the time my instance is running. That is till I exit out of the instance. Is my understanding correct?
  2. I wish to run some batch job (java program) on my instance. How do I make my instance stop automatically after the job is complete (so that I don't get charged for the additional time it may run)
  3. If I start the job and disconnect my PC, will the job continue to run on the instance?

Regards, Asim


回答1:


Correct, instances are charged for the time they are running. (to the minute, minimum 10 minutes). Instances run from the time they are started via the API until they are stopped via the API. It doesn't matter if any user is logged in via SSH or not. For most automated use cases users never log in - programs are installed and started via start up scripts.

You can view your running instances via the Cloud Console, to confirm if any are currently running.

If you want to stop your instance from inside the instance, the easiest way is to start the instance with the compute-rw Service Account Scope and use gcutil.

For example, to start your instance from the command line with the compute-rw scope:

$ gcutil --project=<project-id> addinstance <instance name> --service_account_scopes=compute-rw

(this is the default when manually creating an instance via the Cloud Console)

Later, after your batch job completes, you can remove the instance from inside the instance:

$ gcutil deleteinstance -f <instance name>



回答2:


You can put halt command at the end of your batch script (assuming that you output your results on persistent disk). After halt the instance will have a state of TERMINATED and you will not be charged. See https://developers.google.com/compute/docs/pricing scroll downn to "instance uptime"




回答3:


You can auto shutdown instance after model training. Just run few extra lines of code after the model training is complete.

from googleapiclient import discovery
from oauth2client.client import GoogleCredentials

credentials = GoogleCredentials.get_application_default()

service = discovery.build('compute', 'v1', credentials=credentials)

# Project ID for this request.
project = 'xyz'  # Project ID
# The name of the zone for this request.
zone = 'xyz'  # Zone information

# Name of the instance resource to stop.
instance = 'xyz'  # instance id

request = service.instances().stop(project=project, zone=zone, instance=instance)
response = request.execute()

add this to your model training script. When the training is complete GCP instance automatically shuts down. More info on official website: https://cloud.google.com/compute/docs/reference/rest/v1/instances/stop



来源:https://stackoverflow.com/questions/16608594/how-to-automatically-exit-stop-the-running-instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!