您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)更加便捷地完成云服務(wù)器的釋放以及彈性設(shè)置的方法的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧。
彈性釋放 ECS 實(shí)例
本文將涉及到幾個(gè)重要功能和相關(guān)API:
釋放按量付費(fèi)的云服務(wù)器
設(shè)置按量付費(fèi)實(shí)例的自動(dòng)釋放時(shí)間
停止服務(wù)器
查詢實(shí)例列表
釋放后,實(shí)例所使用的物理資源將被回收,包括磁盤(pán)及快照,相關(guān)數(shù)據(jù)將全部丟失且永久不可恢復(fù)。如果您還想繼續(xù)使用相關(guān)的數(shù)據(jù),建議您釋放云服務(wù)器之前一定要對(duì)磁盤(pán)數(shù)據(jù)做快照,下次創(chuàng)建 ECS 時(shí)可以直接通過(guò)快照創(chuàng)建資源。
釋放云服務(wù)器
釋放服務(wù)器,首先要求您的服務(wù)器處于停止?fàn)顟B(tài)。當(dāng)服務(wù)器停止后,若影響到應(yīng)用,您可以將服務(wù)器重新啟動(dòng)。
停止云服務(wù)器
停止服務(wù)器的指令非常簡(jiǎn)單,且對(duì)于按量付費(fèi)和包年包月都是一樣的。停止云服務(wù)器的一個(gè)參數(shù)是 ForceStop,若屬性設(shè)置為 true,它將類(lèi)似于斷電,直接停止服務(wù)器,但不承諾數(shù)據(jù)能寫(xiě)到磁盤(pán)中。如果僅僅為了釋放服務(wù)器,這個(gè)可以設(shè)置為 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)
釋放云服務(wù)器
如果您沒(méi)有停止服務(wù)器直接執(zhí)行釋放,可能會(huì)有如下報(bào)錯(cuò):
{"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."}
當(dāng)服務(wù)器處于Stopped狀態(tài)時(shí),您可以執(zhí)行釋放服務(wù)器。釋放服務(wù)器的方法比較簡(jiǎn)單,參數(shù)如下:
InstanceId: 實(shí)例的 ID
force: 如果將這個(gè)參數(shù)設(shè)置為 true,將會(huì)執(zhí)行強(qiáng)制釋放。即使云服務(wù)器不是Stopped狀態(tài)也可以釋放。執(zhí)行的時(shí)候請(qǐng)務(wù)必小心,以防錯(cuò)誤釋放影響您的業(yè)務(wù)。
釋放云服務(wù)器的Request如下:
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)
釋放云服務(wù)器成功的 Response 如下:
{"RequestId":"689E5813-D150-4664-AF6F-2A27BB4986A3"}
設(shè)置云服務(wù)器的自動(dòng)釋放時(shí)間
為了更加簡(jiǎn)化對(duì)云服務(wù)器的管理,您可以自定義云服務(wù)器的釋放時(shí)間。當(dāng)定時(shí)時(shí)間到后,阿里云將自動(dòng)為您完成服務(wù)器的釋放, 無(wú)需手動(dòng)執(zhí)行釋放。
注意:自動(dòng)釋放時(shí)間按照 ISO8601 標(biāo)準(zhǔn)表示,并需要使用 UTC 時(shí)間。 格式為:yyyy-MM-ddTHH:mm:ssZ。 如果秒不是 00,則自動(dòng)取為當(dāng)前分鐘開(kāi)始時(shí)。自動(dòng)釋放的時(shí)間范圍:當(dāng)前時(shí)間后 30 分鐘 ~ 當(dāng)前時(shí)間起 3 年。
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)
執(zhí)行 set_instance_auto_release_time(‘i-1111’, ‘2017-01-30T00:00:00Z’) 后完成設(shè)置。
執(zhí)行設(shè)置成功后,您可以通過(guò)DescribeInstances來(lái)查詢自動(dòng)釋放的時(shí)間設(shè)置。
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
取消自動(dòng)釋放設(shè)置
如果您的業(yè)務(wù)有變化,需要取消自動(dòng)釋放設(shè)置。只需執(zhí)行命令將自動(dòng)釋放時(shí)間設(shè)置為空即可。
set_instance_auto_release_time('i-1111')
完整代碼如下:
注意:釋放云服務(wù)器需謹(jǐn)慎。
# 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 check import 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)
感謝各位的閱讀!關(guān)于更加便捷地完成云服務(wù)器的釋放以及彈性設(shè)置的方法就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。