使用CLI便捷管理弹性云服务器ECS
引导式阅读
Others
使用CLI便捷管理弹性云服务器ECS
作者
C***
上架时间
2021-10-09 08:16:47

描述

基于华为云CLI,以shell脚本模板形式集成CLI调用命令,对弹性云服务器进行管理,覆盖从弹性云服务器的创建(购买)到变更规格、再到删除的整个生命周期。

关于华为云CLI更多使用帮助请参考用户指南

场景介绍

本示例以创建(购买)弹性云服务器为主线,通过虚拟私有云建立专属的网络环境,设置子网、安全组,过程中如遇异常将逆向释放相关云资源。同时,提供弹性云服务器相关操作的其他命令。

涉及相关功能如下:

可用区相关:查询可用域列表;

镜像相关:查询镜像列表;

虚拟私有云相关:创建/查询/删除虚拟私有云以及其下子网;

规格相关:查询云服务器规格列表,查询云服务器规格变更支持列表,查询系统中可用的弹性云服务器规格列表,查询云服务器规格详情;

安全组相关:创建/查询/删除安全组及安全组规则,查询指定弹性云服务器的安全组、为弹性云服务器添加一个安全组、移除弹性云服务器中的安全组;

密钥密码相关:查询SSH密钥列表,创建、导入、删除SSH密钥,查询SSH密钥详情,查询是否支持一键重置密码;

磁盘相关:挂载、卸载磁盘,查询弹性云服务器磁盘信息,查询弹性云服务器挂载的磁盘信息,查询弹性云服务器挂载的单个磁盘信息;

网卡相关:添加、删除云服务器网卡,查询云服务器网卡信息,查询指定云服务器网卡信息;

弹性云服务器相关:创建弹性云服务器(按需付费/包年包月)、删除云服务器、修改云服务器名称及描述、变更云服务器规格、修改云服务器销毁时间;

变更服务器状态:关闭/启动/重启弹性云服务器。

前提条件

下载安装HCloud CLI

HCloud CLI的下载安装请参考安装指南

配置(删除配置)HCloud CLI

开发者在配置HCloud CLI前需先获取认证相关的信息。

1.AK/SK 访问密钥

请在华为云控制台“我的凭证-访问密钥”页面上创建和查看您的AK/SK。更多信息请查看访问密钥

2.region 当前可调用的区域

华为云各服务应用区域和各服务的终端节点请查看地区和终端节点

3.project-id 云服务所在项目ID

根据需要操作的项目所属区域选择对应的项目ID 。

配置和删除配置HCloud CLI的命令如下:

hcloud configure set --profile=cliProfile --project-id={project_id} --region={region} --mode=AKSK --access-key={AK} --secret-key={SK} --read-timeout=60 --connect-timeout=30

hcloud configure delete --profile=cliProfile

获取创建弹性云服务器必须的参数:

1.availability_zone 可用区

本模板指定执行hcloud ECS NovaListAvailabilityZones命令获取到的第一个"zoneName"为当前可用区,如:cn-north-4a。您也通过此命令查询到可用区列表后,自行指定可用区。也可参考地区和终端节点自行指定可用区。

2.flavorRef 系统规格ID

本模板指定执行hcloud ECS ListFlavors --availability_zone={availability_zone}命令获取到的第一个"id"为当前待创建云服务器的规格ID,如:s2.medium.2。您也可通过此命令查询到云服务器规格列表后,自行指定规格ID。

3.imageRef 系统镜像ID

本模板指定执行hcloud IMS ListImages --flavor_id={flavor_id}命令获取到的的第一个"id"为当前待创建云服务器的系统镜像ID。您也可通过此命令查询到云服务器系统镜像列表后,自行指定镜像ID。也可通过“华为云控制台-弹性云服务器ECS-镜像服务”页面,选取指定镜像,查看其ID。 如下所示,选取Ubuntu 18.04 server 64bit版本镜像。

4.vpcid 虚拟私有云ID

本模板通过执行hcloud VPC ListVpcs/v2命令查询虚拟私有云。您也可通过“华为云控制台-虚拟私有云VPC”页面,选取指定VPC,查看其ID。如下所示。

5.subnet_id 网络ID

本模板通过执行hcloud VPC ListSubnets/v2 --vpc_id={vpc_id}命令查询指定vpc下的子网。您也可通过“华为云控制台-虚拟私有云VPC”页面,选取指定VPC后,选取指定子网查看其ID。如下所示。

开发说明

本示例从零开始搭建一套可扩展的基础ECS架构,如您需要将待创建的弹性云服务器归属到已有的vpc下,或添加到已有的安全组下,请略过vpc、安全组的创建过程。

本模板默认使用用户名密码方式登录云服务器,您也可将“server.adminPass”参数替换为“key_name”修改登录方式为SSH密钥方式登录。

示例代码

使用如下代码管理VPC及其子网,调用前请根据实际情况修改部分参数内容。

# 查询VPC列表 hcloud VPC ListVpcs/v2 # 查询子网列表 hcloud VPC ListSubnets/v2 --vpc_id={vpc_id} # 创建VPC hcloud VPC CreateVpc/v2 --vpc.name=vpc_$(date "+%Y%m%d%H%M%S") --vpc.cidr="192.168.0.0/16" # 创建子网 hcloud VPC CreateSubnet --subnet.vpc_id={vpc_id} --subnet.name=subnet_$(date "+%Y%m%d%H%M%S") --subnet.cidr="192.168.0.0/16" --subnet.gateway_ip="192.168.0.3" # 删除子网 hcloud VPC DeleteSubnet --vpc_id={vpc_id} --subnet_id={subnet_id} # 删除VPC hcloud VPC DeleteVpc/v2 --vpc_id={vpc_id}

使用如下代码查询规格相关信息,调用前请根据实际情况修改部分参数内容。

# 查询云服务器规格列表 hcloud ECS ListFlavors --availability_zone={availability_zone} # 查询云服务器规格变更支持列表 hcloud ECS ListResizeFlavors --source_flavor_id={flavor_id} --source_flavor_name={flavor_name} --instance_uuid={server_id} # 查询系统中可用的弹性云服务器规格列表 hcloud ECS NovaListFlavors # 查询云服务器规格详情 hcloud ECS NovaShowFlavor --flavor_id={flavor_id}

使用如下代码管理安全组、设置安全组规则,调用前请根据实际情况修改部分参数内容。

# 查询安全组列表 hcloud VPC ListSecurityGroups/v2 --vpc_id={vpc_id} # 查询安全组规则列表 hcloud VPC ListSecurityGroupRules/v2 --security_group_id={security_group_id} # 创建安全组 hcloud VPC CreateSecurityGroup --security_group.vpc_id={vpc_id} --security_group.name=security_group_$(date "+%Y%m%d%H%M%S") # 创建安全组规则 hcloud VPC CreateSecurityGroupRule --security_group_rule.protocol="tcp" --security_group_rule.security_group_id={security_group_id} --security_group_rule.port_range_max=80 --security_group_rule.direction="ingress" --security_group_rule.port_range_min=80 # 删除安全组 hcloud VPC DeleteSecurityGroup --security_group_id={security_group_id} # 删除安全组规则 hcloud VPC DeleteSecurityGroupRule/v2 --security_group_rule_id={securityGroupRule} # 查询指定弹性云服务器的安全组 hcloud ECS NovaListServerSecurityGroups --server_id={server_id} # 为弹性云服务器添加一个安全组 hcloud ECS NovaAssociateSecurityGroup --server_id={server_id} --addSecurityGroup.name={security_group_name} # 移除弹性云服务器中的安全组 hcloud ECS NovaDisassociateSecurityGroup --server_id={server_id} --removeSecurityGroup.name={security_group_name}

使用如下代码管理密钥,调用前请根据实际情况修改部分参数内容。

# 查询SSH密钥列表 hcloud ECS NovaListKeypairs --limit=20 # 导入SSH密钥 hcloud ECS NovaCreateKeypair --keypair.public_key={public_key} --keypair.type="ssh" --keypair.user_id={user ID of the key} --keypair.name={key_name} # 创建SSH密钥 # hcloud ECS NovaCreateKeypair --keypair.name={key_name} # 删除SSH密钥 hcloud ECS NovaDeleteKeypair --keypair_name={key_name} # 查询SSH密钥详情 hcloud ECS NovaShowKeypair --keypair_name={key_name} # 查询是否支持一键重置密码 hcloud ECS ShowResetPasswordFlag --server_id={server_id}

使用如下代码管理磁盘,调用前请根据实际情况修改部分参数内容。

# 挂载磁盘 hcloud ECS AttachServerVolume --server_id={server_id} --volumeAttachment.volumeId={volume_id} --volumeAttachment.device={device} # 卸载磁盘 hcloud ECS DetachServerVolume --volume_id={volume_id} --server_id={server_id} --delete_flag="0" # 查询弹性云服务器磁盘信息 hcloud ECS ListServerVolumeAttachments --server_id={server_id} # 查询弹性云服务器挂载的磁盘信息 hcloud ECS ListServerBlockDevices --server_id={server_id} # 查询弹性云服务器挂载的单个磁盘信息 hcloud ECS ShowServerBlockDevice --volume_id={volume_id} --server_id={server_id}

使用如下代码管理网卡,调用前请根据实际情况修改部分参数内容。

# 批量添加云服务器网卡 hcloud ECS BatchAddServerNics --server_id={server_id} --nics.1.security_groups.1.id={security_group_id} --nics.1.subnet_id={subnet_id} # 批量删除云服务器网卡 hcloud ECS BatchDeleteServerNics --server_id={server_id} --nics.1.id={port_id} # 查询云服务器网卡信息 hcloud ECS ListServerInterfaces --server_id={server_id} # 添加云服务器网卡 hcloud ECS NovaAttachInterface --server_id={server_id} --interfaceAttachment.fixed_ips.1.ip_address={ip_address} --interfaceAttachment.port_id={port_id} # 删除云服务器网卡 hcloud ECS NovaDetachInterface --port_id={port_id} --server_id={server_id} # 查询指定云服务器网卡信息 hcloud ECS ShowServerBlockDevice --volume_id={volume_id} --server_id={server_id}

使用如下代码管理弹性云服务器,调用前请根据实际情况修改部分参数内容。

# 创建云服务器(按需) hcloud ECS CreatePostPaidServers --server.security_groups.1.id=${security_group_id} --server.availability_zone=${availability_zone} --server.vpcid=${vpc_id} --server.count=1 --server.data_volumes.1.volumetype="SSD" --server.data_volumes.1.size=50 --server.name=ecs_$(date "+%Y%m%d%H%M%S") --server.nics.1.subnet_id=${subnet_id} --server.root_volume.volumetype="SSD" --server.root_volume.size=50 --server.flavorRef=${flavor_id} --server.imageRef=${image_id} --server.key_name={key_name} # 创建云服务器 hcloud ECS CreateServers --server.security_groups.1.id=${security_group_id} --server.availability_zone=${availability_zone} --server.vpcid=${vpc_id} --server.count=1 --server.data_volumes.1.volumetype="SSD" --server.data_volumes.1.size=50 --server.name=ecs_$(date "+%Y%m%d%H%M%S") --server.nics.1.subnet_id=${subnet_id} --server.root_volume.volumetype="SSD" --server.root_volume.size=50 --server.flavorRef=${flavor_id} --server.imageRef=${image_id} --server.adminPass=ecs_123456@Q # 查询云服务器详情列表 hcloud ECS NovaListServersDetails # 删除云服务器 hcloud ECS NovaDeleteServer --server_id={server_id} # 修改云服务器名称及描述 hcloud ECS NovaUpdateServer --server_id={server_id} --server.name={name} --server.description={description} # 变更云服务器规格 hcloud ECS ResizeServer --server_id={server_id} --resize.mode="withStopServer" --resize.flavorRef={flavor_id} --resize.extendparam.isAutoPay="true" # 修改云服务器销毁时间 hcloud ECS UpdateServerAutoTerminateTime --server_id={server_id} --auto_terminate_time="2023-09-25T12:05:00Z"

使用如下代码变更服务器状态,调用前请根据实际情况修改部分参数内容。

# 启动云服务器 hcloud ECS NovaStartServer --server_id={server_id} --os-start="" hcloud ECS BatchStartServers --os-start.servers.1.id={server_id1} --os-start.servers.2.id={server_id2} # 关闭云服务器 hcloud ECS NovaStopServer --server_id={server_id} --os-stop.type="SOFT" hcloud ECS BatchStopServers --os-stop.servers.1.id={server_id1} --os-stop.servers.2.id={server_id2} --os-stop.type="SOFT" # 重启云服务器 hcloud ECS NovaRebootServer --server_id={server_id} --reboot.type="SOFT" hcloud ECS BatchRebootServers --reboot.servers.1.id={server_id1} --reboot.servers.2.id={server_id2} --reboot.type="SOFT"

运行示例

运行时首先通过sudo ln -s CLI工具所在目录 /usr/local/bin/命令配置华为云CLI工具环境变量,或将脚本模板放置在华为云CLI工具所在目录,并在hcloud开头的相关命令前加“./”。其次,替换认证参数、根据实际情况修改部分参数内容、或注释无关的命令,最后执行sh create-server.sh命令。

运行结果

脚本执行成功后,控制台打印云服务器详情列表信息。

{ "servers": [ { "tenant_id": "060576xxxxxxxxxxxxxxc01fb35c740d", "metadata": {}, "addresses": {}, "OS-EXT-STS:task_state": "scheduling", "OS-DCF:diskConfig": "MANUAL", "OS-EXT-AZ:availability_zone": "cn-north-4a", "links": [ { "rel": "self", "href": "https://ecs.br-iaas-odin1.huaweicloud.com/v2.1/060576xxxxxxxxxxxxxxc01fb35c740d/servers/1c6c6a7d-ffa3-49ce-b8a8-1b424b8c89ce" }, { "rel": "bookmark", "href": "https://ecs.br-iaas-odin1.huaweicloud.com/060576xxxxxxxxxxxxxxc01fb35c740d/servers/1c6c6a7d-ffa3-49ce-b8a8-1b424b8c89ce" } ], "OS-EXT-STS:power_state": 0, "id": "1c6c6a7d-ffa3-49ce-b8a8-1b424b8c89ce", "os-extended-volumes:volumes_attached": [], "OS-EXT-SRV-ATTR:host": null, "image": { "links": [ { "rel": "bookmark", "href": "https://ecs.br-iaas-odin1.huaweicloud.com/060576xxxxxxxxxxxxxxc01fb35c740d/images/4ffe0dc8-ac16-4d3a-a1e9-5640e4930cea" } ], "id": "4ffe0dc8-ac16-4d3a-a1e9-5640e4930cea" }, "OS-SRV-USG:terminated_at": null, "accessIPv4": "", "accessIPv6": "", "created": "2021-06-29T08:23:18Z", "hostId": "", "OS-EXT-SRV-ATTR:hypervisor_hostname": null, "flavor": { "links": [ { "rel": "bookmark", "href": "https://ecs.br-iaas-odin1.huaweicloud.com/060576xxxxxxxxxxxxxxc01fb35c740d/flavors/2afe1d42-c18b-4147-9672-e0f2ab0821f8" } ], "id": "2afe1d42-c18b-4147-9672-e0f2ab0821f8" }, "key_name": null, "config_drive": "", "OS-EXT-STS:vm_state": "building", "user_id": "a2f37xxxxxxxxxxxxxxxxxxxxx90f42a", "OS-EXT-SRV-ATTR:instance_name": "instance-0017835e", "name": "ecs_20210629162313", "progress": 0, "OS-SRV-USG:launched_at": null, "updated": "2021-06-29T08:23:19Z", "status": "BUILD" } ] }

参考

关于华为云CLI的更多信息请参考:https://support.huaweicloud.com/productdesc-hcli/hcli_01.html

修订记录

发布日期 文档版本 修订说明
2021-07-06 1.0 文档首次发布
2021-07-29 1.1 增加ECS规格变更、状态管理操作