In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
This article is to share with you about how to more easily complete the release of CVM and how to set up flexibility. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.
Elastic release ECS instance
This article will cover several important features and related API:
Release the pay-per-view CVM
Set the automatic release time for pay-by-usage instances
Stop the server
Query the list of instances
After release, the physical resources used by the instance will be reclaimed, including disks and snapshots, and all related data will be lost and permanently unrecoverable. If you want to continue to use the relevant data, it is recommended that you take a snapshot of the disk data before releasing the CVM. The next time you create an ECS, you can directly create resources through snapshots.
Release the CVM
To release the server, first require your server to be stopped. When the server stops, if the application is affected, you can restart the server.
Stop the CVM
The instructions to stop the server are very simple and are the same for pay-as-you-go and prepaid. One of the parameters for stopping the CVM is ForceStop. If the property is set to true, it will be similar to a power outage and stop the server directly, but it does not guarantee that the data can be written to disk. This can be set to true if only to free the server.
Def stop_instance (instance_id, force_stop=False):''stop one ecs instance. Param instance_id: instance id of the ecs instance, like'imurf. : param force_stop: if force stop is true, it will force stop the server and not ensure the data write to disk correctly. : return:''request = StopInstanceRequest () request.set_InstanceId (instance_id) request.set_ForceStop (force_stop) logging.info ("Stop% s command submit successfully.", instance_id) _ send_request (request)
Release the CVM
If you do not stop the server to perform the release directly, you may have the following error:
{"RequestId": "3C6DEAB4-7207-411F-9A31-6ADE54C268BE", "HostId": "ecs-cn-hangzhou.aliyuncs.com", "Code": "IncorrectInstanceStatus", "Message": "The current status of the resource does not support this operation."}
When the server is in the Stopped state, you can perform releasing the server. The method of releasing the server is relatively simple. The parameters are as follows:
InstanceId: the ID of the instance
Force: if this parameter is set to true, a forced release will be performed. Even if the CVM is not in Stopped status, it can be released. Please be careful when performing so that the error release will not affect your business.
The Request for releasing the CVM is as follows:
Def release_instance (instance_id, force=False):''delete instance according instance id, only support after pay instance. Param instance_id: instance id of the ecs instance, like'imurf. : param force: if force is false, you need to make the ecs instance stopped, you can execute the delete action. If force is true, you can delete the instance even the instance is running. : return:''request = DeleteInstanceRequest (); request.set_InstanceId (instance_id) request.set_Force (force) _ send_request (request)
The successful Response for releasing the CVM is as follows:
{"RequestId": "689E5813-D150-4664-AF6F-2A27BB4986A3"}
Set the automatic release time of the CVM
To simplify the management of the CVM, you can customize the release time of the CVM. When the timing time is up, Aliyun will automatically complete the release of the server for you, without the need to perform the release manually.
Note: the automatic release time is expressed in accordance with the ISO8601 standard, and UTC time is required. The format is yyyy-MM-ddTHH:mm:ssZ. If the second is not 00, it is automatically taken as the start of the current minute. The time range of automatic release: 30 minutes after the current time to 3 years from the current time.
Def set_instance_auto_release_time (instance_id, time_to_release = None):''setting instance auto delete time: param instance_id: instance id of the ecs instance, like'i murmur. Param time_to_release: if the property is setting, such as' 2017-01-30T00 it means setting the instance to be release at that time. If the property is None, it means cancel the auto delete time. : return:''request = ModifyInstanceAutoReleaseTimeRequest () request.set_InstanceId (instance_id) if time_to_release is not None: request.set_AutoReleaseTime (time_to_release) _ send_request (request)
The setup is completed after the execution of set_instance_auto_release_time ('iMui 111111,' 2017-01-30T00VUR 00Z').
After the execution of the settings is successful, you can query the time settings for automatic release through DescribeInstances.
Def describe_instance_detail (instance_id):''describe instance detail: param instance_id: instance id of the ecs instance, like'i murmur. : return:''request = DescribeInstancesRequest () request.set_InstanceIds (json.dumps ([instance_id])) response = _ send_request (request) if response is not None: instance_list = response.get (' Instances'). Get ('Instance') if len (instance_list) > 0: return instance_list [0] def check_auto_release_time_ready (instance_id): Detail = describe_instance_detail (instance_id=instance_id) if detail is not None: release_time = detail.get ('AutoReleaseTime') return release_time
Cancel the automatic release setting
If your business changes, you need to cancel the automatic release setting. Simply execute the command to set the automatic release time to empty.
Set_instance_auto_release_time ('iMur1111')
The complete code is as follows:
Note: you need to be cautious in releasing CVM.
# coding=utf-8# if the python sdk is not install using 'sudo pip install aliyun-python-sdk-ecs'# if the python sdk is install using' sudo pip install-- upgrade aliyun-python-sdk-ecs'# make sure the sdk version is 2.1.2, you can use command 'pip show aliyun-python-sdk-ecs' to checkimport jsonimport loggingfrom aliyunsdkcore import clientfrom aliyunsdkecs.request.v20140526.DeleteInstanceRequest import DeleteInstanceRequestfrom aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequestfrom aliyunsdkecs.request.v20140526.ModifyInstanceAutoReleaseTimeRequest import\ ModifyInstanceAutoReleaseTimeRequestfrom aliyunsdkecs.request.v20140526.StopInstanceRequest import StopInstanceRequest# configuration the log output formatter If you want to save the output to file,# append ", filename='ecs_invoke.log'" after datefmt.logging.basicConfig (level=logging.INFO, format='% (asctime) s% (filename) s [line:% (lineno) d]% (levelname) s% (message) slots, datefmt='%a,% d% b% Y% HRV% MRV% S') clt = client.AcsClient ('Your Access Key Id',' Your Access Key Secrect' 'cn-beijing') def stop_instance (instance_id, force_stop=False):' 'stop one ecs instance. Param instance_id: instance id of the ecs instance, like'imurf. : param force_stop: if force stop is true, it will force stop the server and not ensure the data write to disk correctly. : return:''request = StopInstanceRequest () request.set_InstanceId (instance_id) request.set_ForceStop (force_stop) logging.info ("Stop% s command submit successfully.", instance_id) _ send_request (request) def describe_instance_detail (instance_id):' 'describe instance detail: param instance_id: instance id of the ecs instance, like'iMurray. : return:''request = DescribeInstancesRequest () request.set_InstanceIds (json.dumps ([instance_id])) response = _ send_request (request) if response is not None: instance_list = response.get (' Instances'). Get ('Instance') if len (instance_list) > 0: return instance_list [0] def check_auto_release_time_ready (instance_id): Detail = describe_instance_detail (instance_id=instance_id) if detail is not None: release_time = detail.get ('AutoReleaseTime') return release_timedef release_instance (instance_id Force=False):''delete instance according instance id, only support after pay instance. Param instance_id: instance id of the ecs instance, like'imurf. : param force: if force is false, you need to make the ecs instance stopped, you can execute the delete action. If force is true, you can delete the instance even the instance is running. : return:''request = DeleteInstanceRequest (); request.set_InstanceId (instance_id) request.set_Force (force) _ send_request (request) def set_instance_auto_release_time (instance_id, time_to_release = None):' 'setting instance auto delete time: param instance_id: instance id of the ecs instance, like'imurf. Param time_to_release: if the property is setting, such as' 2017-01-30T00 it means setting the instance to be release at that time. If the property is None, it means cancel the auto delete time. : return:''request = ModifyInstanceAutoReleaseTimeRequest () request.set_InstanceId (instance_id) if time_to_release is not None: request.set_AutoReleaseTime (time_to_release) _ send_request (request) release_time = check_auto_release_time_ready (instance_id) logging.info ("Check instance% s auto release time setting is% s.", instance_id Release_time) def _ send_request (request):''send open api request: param request:: return:' 'request.set_accept_format (' json') try: response_str = clt.do_action (request) logging.info (response_str) response_detail = json.loads (response_str) return response_detail except Exception ase: Logging.error (e) if _ _ name__ ='_ _ main__': logging.info ("Release ecs instance by Aliyun OpenApi!") Set_instance_auto_release_time ('imur111111,' 2017-01-28T06) # set_instance_auto_release_time ('imur1111') # stop_instance (' imur111111') # release_instance ('imur111111') # release_instance ('iMuth1111', True) Thank you for reading! This is the end of the way to more easily complete the release of the CVM and the flexible setting. I hope the above content can be of some help to you, so that you can learn more. If you think the article is good, you can share it and let more people see it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.