如何更加便捷地完成云服务器的释放以及弹性设置

本文在介绍如何更加便捷地完成云服务器的释放以及弹性设置的基础上,重点探讨了其具体步骤,本文内容紧凑,希望大家可以有所收获。

弹性释放 ECS 实例

本文将涉及到几个重要功能和相关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状态也可以释放。执行的时候请务必小心,以防错误释放影响您的业务。

释放云服务器的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)

释放云服务器成功的 Response 如下:

{"RequestId":"689E5813-D150-4664-AF6F-2A27BB4986A3"}

设置云服务器的自动释放时间

为了更加简化对云服务器的管理,您可以自定义云服务器的释放时间。当定时时间到后,阿里云将自动为您完成服务器的释放, 无需手动执行释放。

注意:自动释放时间按照 ISO8601 标准表示,并需要使用 UTC 时间。 格式为:yyyy-MM-ddTHH:mm:ssZ。 如果秒不是 00,则自动取为当前分钟开始时。自动释放的时间范围:当前时间后 30 分钟 ~ 当前时间起 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)

执行 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 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)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_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 '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)

以上就是如何更加便捷地完成云服务器的释放以及弹性设置的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/176156.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
深圳市1月卖得最好的20款车:极氪001力压小米SU7夺冠
上一篇 2026年8月31日 18:27:28
夸克浏览器网页版在线进入 夸克浏览器官网地址直接进入
下一篇 2026年8月31日 18:31:34

相关推荐

  • VSCode安装C/C++代码格式化 专业VSCode开发环境配置

    配置VSCode进行C/C++开发需安装C/C++扩展包和clang-format,设置自动格式化与调试环境,推荐使用CMake Tools、Include Autocomplete等扩展,结合快捷键、代码片段和任务自动化提升效率。 配置VSCode以实现C/C++代码的专业格式化和高效开发环境,核…

    2026年9月22日
    400
  • Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)

    could not find doxygen (missing: doxygen_executable)  使用cmake .. 有时候会遇到如下问题: 代码语言:javascript代码运行次数:0运行复制 $ cmake ..– The CXX compiler identification …

    2026年9月22日
    100
  • MySQL自动化备份如何实现_适合企业级部署吗?

    MySQL自动化备份如何实现_适合企业级部署吗?MySQL自动化备份如何实现_适合企业级部署吗?MySQL自动化备份如何实现_适合企业级部署吗?MySQL自动化备份如何实现_适合企业级部署吗?

    mysql的自动化备份对企业级部署是必要的,且可通过多种方式实现。1. 使用mysqldump+定时任务(crontab)是最基础的方式,操作简单适合中小规模数据库,但备份时可能锁表影响业务;2. 增量备份结合二进制日志(binary log)更高效,适用于频繁变更的数据,支持精确恢复到某时间点;3…

    2026年9月21日 用户投稿
    100
  • Online Config VS Code

    Online Config VS CodeOnline Config VS CodeOnline Config VS CodeOnline Config VS Code

    run vs view Install Code Server Update Code Server Database:It is recommended to create a Docker container for the database. Code Language: JavaScript…

    2026年9月21日 用户投稿
    100
  • 一键PHP环境怎么解决网页空白问题_空白页故障诊断

    答案是开启错误提示并检查文件路径与代码逻辑。先启用PHP错误显示,确认配置正确;再核对网站根目录和入口文件是否存在;接着排查代码致命错误及输出缓冲问题,确保无BOM头且session前无输出。 遇到一键PHP环境安装后出现网页空白或空白页问题,通常不是环境完全失效,而是某些关键环节出了错。这类问题在…

    2026年9月21日
    100
  • Bun 1.3 正式发布

    2025年10月10日,高性能 javascript 运行时 bun 发布了 1.3 版本。这是 bun 项目迄今为止最重大的版本更新,标志着 bun 从单纯的运行时工具演变为一个功能完备的全栈 javascript 开发平台。 从运行时到全栈平台的跨越 Bun 1.3 的核心突破在于将前端开发能力…

    2026年9月21日
    100
  • Linux文件系统mv命令使用详解

    mv命令用于移动或重命名文件和目录,若目标不存在则重命名,存在且为目录则移动,为文件则覆盖;常用选项包括-i(交互确认)、-f(强制覆盖)、-u(更新移动)、-v(显示过程);可重命名如mv oldname.txt newname.txt,或移动如mv file.txt /dir/;建议使用alia…

    2026年9月21日
    100
  • VSCode怎么手动保存代码_VSCode禁用自动保存与手动保存设置教程

    手动保存代码只需按Ctrl + S(Windows/Linux)或Cmd + S(macOS),可通过设置将“files.autoSave”设为“off”来禁用自动保存,并结合版本控制保障代码安全。 VSCode手动保存代码其实很简单,默认情况下,按下 Ctrl + S (Windows/Linux…

    2026年9月21日
    200
  • 如何解决Linux软件包冲突 依赖问题处理方案

    如何解决Linux软件包冲突 依赖问题处理方案如何解决Linux软件包冲突 依赖问题处理方案如何解决Linux软件包冲突 依赖问题处理方案如何解决Linux软件包冲突 依赖问题处理方案

    遇到linux系统中软件包冲突或依赖问题时,应首先理解依赖关系并使用合适工具解决。1. 使用apt或yum的自动修复功能,如debian/ubuntu可用sudo apt –fix-broken install,centos/fedora可用sudo dnf install @syste…

    2026年9月21日 用户投稿
    800
  • MySQL数据备份自动化实施_MySQL定时任务与脚本管理

    MySQL数据备份自动化实施_MySQL定时任务与脚本管理MySQL数据备份自动化实施_MySQL定时任务与脚本管理MySQL数据备份自动化实施_MySQL定时任务与脚本管理MySQL数据备份自动化实施_MySQL定时任务与脚本管理

    mysql数据备份的自动化实施核心在于结合mysqldump等工具与操作系统的定时任务(如linux的cron或windows的task scheduler),通过编写和管理脚本实现定期执行备份。1. 使用mysqldump作为基础工具,编写包含数据库连接信息、时间戳文件名、日志记录、压缩清理等功能…

    2026年9月21日 用户投稿
    200
  • 如何设置Linux软件包更新排除 yum exclude和apt-mark hold

    如何设置Linux软件包更新排除 yum exclude和apt-mark hold如何设置Linux软件包更新排除 yum exclude和apt-mark hold如何设置Linux软件包更新排除 yum exclude和apt-mark hold如何设置Linux软件包更新排除 yum exclude和apt-mark hold

    要阻止linux系统中特定软件包更新,可针对不同发行版使用相应方法。对于rhel/centos系系统,可通过在/etc/yum.conf或.repo文件中添加exclude=包名来排除升级;对于debian/ubuntu系系统,则使用sudo apt-mark hold 包名命令锁定版本。这两种方式…

    2026年9月21日 用户投稿
    500
  • VSCode怎么看效果_VSCode实时预览和调试代码运行效果教程

    VSCode通过实时预览扩展和内置调试器实现代码效果查看。使用Live Server可实时预览前端页面,保存即刷新;Markdown文件支持侧边预览。调试功能需配置launch.json,支持Node.js、Python、浏览器端JavaScript等,通过断点、变量监视、调用堆栈等深入分析代码执行…

    2026年9月21日
    100
  • 如何检测Linux网络接口DMA状态 硬件加速功能验证

    如何检测Linux网络接口DMA状态 硬件加速功能验证如何检测Linux网络接口DMA状态 硬件加速功能验证如何检测Linux网络接口DMA状态 硬件加速功能验证如何检测Linux网络接口DMA状态 硬件加速功能验证

    可通过以下方法检测linux系统中网络接口dma状态和硬件加速是否启用:1.使用 ethtool -i eth0 和 ethtool -k eth0 查看驱动信息及sg、tso、ufo、gso功能是否启用;2.通过 cat /proc/interrupts 和 cat /proc/slabinfo …

    2026年9月21日 用户投稿
    800
  • VSCode整个项目怎么导出_VSCode项目打包与导出为压缩文件的完整教程

    答案:导出VSCode项目可通过手动压缩、终端命令、插件或Git克隆实现,推荐使用终端命令排除node_modules并选择zip格式以兼顾兼容性与效率。 将VSCode整个项目导出,实际上就是将项目文件夹打包成一个压缩文件,方便备份、分享或迁移。下面介绍几种常见的打包导出方法。 解决方案: 手动压…

    2026年9月21日
    200
  • SpringBoot的定时任务

    SpringBoot的定时任务SpringBoot的定时任务SpringBoot的定时任务SpringBoot的定时任务

    大家好,我是你们的老朋友全栈君。我们又见面了。 一、基于注解(@Scheduled)的定时任务 使用SpringBoot的@Scheduled注解来创建定时任务非常简单,只需几行代码就能实现。然而,@Scheduled默认是单线程运行,这意味着当启动多个任务时,一个任务的执行时间可能会影响到下一个任…

    2026年9月21日 用户投稿
    500
  • 使用 EMQ 搭建 MQTT 服务器

    本教程详细介绍了如何使用 emq 搭建 mqtt 服务,适用于设备联网的 mqtt 协议。 预备条件: 一台 Ubuntu 服务器或在虚拟机中安装 Ubuntu 系统emqx-ubuntu18.04-4.3.1-amd64.deb 安装包(安装包链接见文末) 安装步骤: 使用 dpkg 安装 EMQ…

    2026年9月21日
    000
  • 实时即未来:Apache Flink实践(二)

    俗话说,工欲善其事,必先利其器!这句话确实很有道理。因此,今天我们将讨论如何在版本较低的windows电脑上学习 apache flink 知识。 Windows子系统简介:Windows内置了Ubuntu子系统,这是由Microsoft官方发布的,不是虚拟机。其安装方法也非常简单。 微软官方文档对…

    2026年9月21日
    500
  • PHPComposer怎么安装_PHPComposer依赖管理工具安装与使用指南

    PHPComposer是PHP的依赖管理工具,类似npm或pip。需先安装PHP,再下载并验证composer-setup.php,执行安装生成composer.phar,推荐全局安装至/usr/local/bin/composer,运行composer –version验证。使用com…

    2026年9月21日
    100
  • 开源 串口调试助手 BaoYuanSerial 使用教程「建议收藏」

    大家好,很高兴再次与大家见面,我是你们的老朋友全栈君。 简介:本软件采用.Net5与Avalonia技术实现跨平台解决方案,适用于Linux Ubuntu和Windows系统,并已在Ubuntu20.04及Win10 Professional 20H2上成功测试。 官方下载地址: GitHub项目地…

    2026年9月21日
    200
  • 为“架构”再建个模:如何用代码描述软件架构?

    在 archguard 平台中,为了实现对架构的治理,我们需要通过代码和模型来描述所需处理的内容和数据。因此,archguard 引入了代码模型、依赖模型、变更模型等,而架构模型和架构治理模型则是两个核心的部分。其它如构建模型等,将会在后续逐步引入到系统中。 PS:本文中的架构展开是基于自动化分析需…

    2026年9月21日
    000

发表回复

登录后才能评论
关注微信