首页 > 代码库 > 阿里云云中沙箱自助实验-使用OpenApi弹性释放和设置云服务器ECS释放
阿里云云中沙箱自助实验-使用OpenApi弹性释放和设置云服务器ECS释放
云服务器ECS的一个重要特性就是按需创建资源。您可以在业务高峰期按需弹性的自定义规则进行资源创建,在完成业务计算的时候释放资源。本篇将提供几个Tips帮助您更加容易和自动化的完成云服务器的释放和弹性设置。
本文将涉及到几个重要的功能和相关API:
释放按量付费的云服务器
设置按量付费实例的自动释放时间
停止服务器
查询实例列表
释放后实例所使用的物理资源都被回收,包括磁盘及快照,相关数据全部丢失且永久不可恢复。如果您还想继续使用相关的手,建议您释放云服务器之前一定要对磁盘数据做快照,这样您可以下次创建ECS的时候直接通过快照来快速的创建资源。
释放云服务器
释放服务器,首先要求您的服务器处于停止状态,这样当服务器停止以后,如果影响到您的应用,你就可以将服务器重新启动。
停止云服务器
停止服务器的指令非常简单,这个对于按量付费和包年包月都是一样的。停止云服务器有一个参数是ForceStop,如果您将这个属性设置为true,它将类似于断电,直接停止服务器,不承诺数据能写到磁盘中,但是如果您是为了释放服务器,这个可以设置为true。
def stop_instance(instance_id, force_stop=False): ‘‘‘ stop one ecs instance. :param instance_id: instance id of the ecs instance, like ‘i-***‘. :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)
释放云服务器
如果您没有停止服务器直接执行释放,可能会报错下面的内容
{"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."}
当服务器处于Stopped
的状态的时候您可以执行释放服务器。释放服务器的方法也比较简单的。参数包括:
InstanceId: 实例的id
force: 如果将这个参数设置为true,将会执行强制释放,即使云服务器不是
Stopped
状态也可以释放,所以执行的时候务必小心,以防错误释放影响您的业务。
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 ‘i-***‘. :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)
释放云服务器成功的Response比较简单:
{"RequestId":"689E5813-D150-4664-AF6F-2A27BB4986A3"}
设置云服务器的自动释放时间
为了更加简化您对云服务器的管理,您可以自定义云服务器的释放时间,当定时任务时间到了以后,阿里云将自动为您完成服务器的释放, 您可以不用自己来执行释放。
请注意自动释放时间按照 ISO8601 标准表示,并需要使用 UTC 时间。 格式为:yyyy-MM-ddTHH:mm:ssZ。 如果秒不是 00,则自动取为当前分钟开始时。最少在当前时间之后半小时;最多不能超过当前时间起三年。
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-***‘. :param time_to_release: if the property is setting, such as ‘2017-01-30T00:00:00Z‘ 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)
执行 set_instance_auto_release_time(‘i-1111‘, ‘2017-01-30T00:00:00Z‘)
就会执行设置。
执行设置成功之后您可以通过熟悉的DescribeInstances
来查询自动释放的时间设置。
def describe_instance_detail(instance_id): ‘‘‘ describe instance detail :param instance_id: instance id of the ecs instance, like ‘i-***‘. :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
取消自动释放设置
如果您的业务有变化,需要取消自动释放设置。只需要执行命令将自动释放时间设置为空即可。
set_instance_auto_release_time(‘i-1111‘)
完整代码如下
释放云服务器需谨慎^_^。
# 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 json import logging from aliyunsdkcore import client from aliyunsdkecs.request.v20140526.DeleteInstanceRequest import DeleteInstanceRequest from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest from aliyunsdkecs.request.v20140526.ModifyInstanceAutoReleaseTimeRequest import ModifyInstanceAutoReleaseTimeRequest from 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)s‘, datefmt=‘%a, %d %b %Y %H:%M:%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 ‘i-***‘. :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 ‘i-***‘. :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 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 ‘i-***‘. :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 ‘i-***‘. :param time_to_release: if the property is setting, such as ‘2017-01-30T00:00:00Z‘ 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 as e: logging.error(e)if __name__ == ‘__main__‘: logging.info("Release ecs instance by Aliyun OpenApi!") set_instance_auto_release_time(‘i-1111‘, ‘2017-01-28T06:00:00Z‘) # set_instance_auto_release_time(‘i-1111‘) # stop_instance(‘i-1111‘) # release_instance(‘i-1111‘) # release_instance(‘i-1111‘, True)
云中沙箱为阿里云官方实验平台。
相关实验:ECS的磁盘挂载、快照及自定义镜像
本文出自 “12554681” 博客,请务必保留此出处http://12564681.blog.51cto.com/12554681/1895571
阿里云云中沙箱自助实验-使用OpenApi弹性释放和设置云服务器ECS释放