15158846557 在线咨询 在线咨询
15158846557 在线咨询
所在位置: 首页 > 营销资讯 > 网站运营 > 使用华为云API按需创建云服务器

使用华为云API按需创建云服务器

时间:2023-06-29 01:09:02 | 来源:网站运营

时间:2023-06-29 01:09:02 来源:网站运营

使用华为云API按需创建云服务器:今晚有时间,把去年的一个想法实现一下




蛋炒饭的想法 - 本站 https://www.zhihu.com/pin/1407860558989774848

去年的一个想法
经过不断学习尝试,最终的使用华为云创建虚拟主机,进行完成相应配置后,封装为系统镜像,每次通过该镜像创建虚拟机,电脑手机通过华为云的虚拟机访问github。

本篇重点分享使用华为云的API接口,来实现创建虚拟主机、删除虚拟主机的操作。







一、StrongSwan和Stunnel、Squid的配置实现。

(一)StrongSwan

配置部署可以参考大佬的一键脚本,链接如下:

使用证书认证的话,可以考虑美国加州公益组织ISRG 的 Let’s Encrypt . 优点在于可靠,可自动化,免费,口碑好. 缺点(其实算不上缺点)为有效期只有90天,但是完全可以自动化的进行续期来避免这个问题.

配置部署可以参考大佬的blog,链接如下:

(二)Stunnel+Squid

也可以结合Stunnel+Squid来实现一个加密的代理功能,方便访问github。这部分配置可以参考我去年的分享

二、使用华为云的API接口创建虚拟机

(一)华为公有云API

华为云比较爽的一个地方,在控制台->开发工具->API explorer,几乎涵盖了所有的华为云服务产品的接口。而且还提供代码示例,可以根据选择的参数等自动调整代码,非常方便。

很方便的API

(二)准备基本的数据

在调用接口进行操作前,我们需要先准备好这几个数据:

1、获取EndPoint

2、获取AK/SK

3、获取项目ID

4、获取账号名和账号ID

这些操作,可以参考官方文档,写的很清楚。文档后面有个“签名SDK与demo”,这个可以忽略。

三、通过API接口创建云服务器(按需)

(一)通过API接口创建云服务器

创建云服务器有很多接口可以选择,每个接口的每个参数都有详细的介绍

我将通过API创建一个云服务器,具备以下参数:

1、系统盘:SAS,按需计费

2、弹性IP: 按需付费,1M,独享

3、弹性IP地址类型: 5_bgp(也就这个了),自动分配

4、云服务器的规格: 1vcpu、1G内存

在图形界面,选择对应的参数和值后,会自动在右侧生成各类语言使用的代码,大致如下:

# coding: utf-8# 创建预付费云服务器,规格为1cpu,1G内存,1M EIP。from huaweicloudsdkcore.auth.credentials import BasicCredentialsfrom huaweicloudsdkecs.v2.region.ecs_region import EcsRegionfrom huaweicloudsdkcore.exceptions import exceptionsfrom huaweicloudsdkecs.v2 import *if __name__ == "__main__": ak = "<you ak>" sk = "<you sk>" credentials = BasicCredentials(ak, sk) / client = EcsClient.new_builder() / .with_credentials(credentials) / .with_region(EcsRegion.value_of("ap-southeast-1")) / .build() try: request = CreatePostPaidServersRequest() listPostPaidServerTagServerTagsServer = [] listPostPaidServerSecurityGroupSecurityGroupsServer = [] # 云服务器对应系统盘相关配置。 rootVolumePostPaidServerRootVolume = PostPaidServerRootVolume( # 云服务器系统盘对应的磁盘类型,需要与系统所提供的磁盘类型相匹配。 volumetype="SAS" ) # 创建弹性IP的附加信息。 extendparamPostPaidServerEipExtendParam = PostPaidServerEipExtendParam( # 公网IP的计费模式。取值范围:prePaid - 预付费, 即包年包月;postPaid - 后付费, 即按需付费; charging_mode="postPaid" ) # 弹性IP地址带宽参数。 bandwidthPostPaidServerEipBandwidth = PostPaidServerEipBandwidth( # 功能说明:带宽大小 带宽(Mbit / s), 取值范围为[ 1, 2000 ]。 size=1, # 带宽的共享类型。 共享类型枚举:PER,表示独享。WHOLE,表示共享。 sharetype="PER" ) # 配置云服务器自动分配弹性IP时,创建弹性IP的配置参数。 eipPostPaidServerEip = PostPaidServerEip( # 弹性IP地址类型。 iptype="5_bgp", bandwidth=bandwidthPostPaidServerEipBandwidth, extendparam=extendparamPostPaidServerEipExtendParam ) # 配置云服务器的弹性IP信息,弹性IP有三种配置方式。 # 不使用(无该字段) # 自动分配,需要指定新创建弹性IP的信息 # 使用已有,需要指定已创建弹性IP的信息 publicipPostPaidServerPublicip = PostPaidServerPublicip( eip=eipPostPaidServerEip ) # 待创建云服务器的网卡信息。 listPostPaidServerNicNicsServer = [ # 待创建云服务器所在的子网信息,需要指定vpcid对应VPC下的子网ID,UUID格式。 PostPaidServerNic( subnet_id="0d106f5c-0b40-4864-9767-57e63b51cf99" ) ] # 创建云服务器附加信息。 extendparamPostPaidServerExtendParam = PostPaidServerExtendParam( #计费模式: 0:按需计费。 charging_mode=0, # 云服务器所在区域ID。 region_id="ap-southeast-1" ) # 云服务器对应数据盘相关配置。 listPostPaidServerDataVolumeDataVolumesServer = [] # 弹性云服务器信息 serverPostPaidServer = PostPaidServer( # 云服务器对应数据盘相关配置 data_volumes=listPostPaidServerDataVolumeDataVolumesServer, # 创建云服务器附加信息。 extendparam=extendparamPostPaidServerExtendParam, # 待创建云服务器的系统规格的ID。 flavor_ref="s6.small.1", # 待创建云服务器的系统镜像,需要指定已创建镜像的ID,ID格式为通用唯一识别码(Universally Unique Identifier,简称UUID)。 image_ref="5f524b35-2e88-46e1-9aa4-c6e59c2971bd", # 如果需要使用SSH密钥方式登录云服务器,请指定已创建密钥的名称。 key_name="KeyPair", # 云服务器名称。 name="strongswan", # 待创建云服务器的网卡信息。 nics=listPostPaidServerNicNicsServer, # 配置云服务器的弹性IP信息 publicip=publicipPostPaidServerPublicip, # 云服务器对应系统盘相关配置。 root_volume=rootVolumePostPaidServerRootVolume, # 云服务器对应安全组信息。 security_groups=listPostPaidServerSecurityGroupSecurityGroupsServer, # 弹性云服务器的标签。 server_tags=listPostPaidServerTagServerTagsServer, # 待创建云服务器所属虚拟私有云(简称VPC),需要指定已创建VPC的ID,UUID格式。 vpcid="45c02e54-3ec0-4efd-8c66-c7000984704a" ) request.body = CreatePostPaidServersRequestBody( server=serverPostPaidServer, # 是否只预检此次请求。 # true:发送检查请求,不会创建实例。检查项包括是否填写了必需参数、请求格式等。 # 如果检查不通过,则返回对应错误。 # 如果检查通过,则返回202状态码。 # false:发送正常请求,通过检查后并且执行创建云服务器请求。 dry_run=False ) response = client.create_post_paid_servers(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)执行后,会返回一个job_id和server_id

{ "job_id":"ff8080817e498e160180040b84db1149", "serverIds":[ "68f8d8d1-3b10-4cfc-b55f-c74036c3b9d8" ]}

(二)通过job_id查看创建情况

# coding: utf-8from huaweicloudsdkcore.auth.credentials import BasicCredentialsfrom huaweicloudsdkecs.v2.region.ecs_region import EcsRegionfrom huaweicloudsdkcore.exceptions import exceptionsfrom huaweicloudsdkecs.v2 import *if __name__ == "__main__": # 输入准备基本数据中的AK/SK ak = "<YOUR AK>" sk = "<YOUR AK>" credentials = BasicCredentials(ak, sk) / client = EcsClient.new_builder() / .with_credentials(credentials) / .with_region(EcsRegion.value_of("ap-southeast-1")) / .build() try: request = ShowJobRequest() # 根据job_id,来查询云主机生命周期内的任何操作任务。 request.job_id = "ff8080817e498e160180040b84db1149" response = client.show_job(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)返回查询结果

{ "begin_time":"2022-04-07T12:41:45.434Z", "end_time":"2022-04-07T12:42:33.109Z", "entities":{ "sub_jobs":[ { "status":"SUCCESS", "entities":{ "server_id":"68f8d8d1-3b10-4cfc-b55f-c74036c3b9d8" }, "job_id":"ff8080817e498e160180040b8536114d", "job_type":"createSingleServer", "begin_time":"2022-04-07T12:41:45.525Z", "end_time":"2022-04-07T12:42:21.462Z" } ], "sub_jobs_total":1 }, "job_id":"ff8080817e498e160180040b84db1149", "job_type":"createServer", "status":"SUCCESS"}可以从web页面看到,已经创建了一个云服务器。

通过API接口创建的云服务器

(三)通过API接口查询云服务器信息

# coding: utf-8from huaweicloudsdkcore.auth.credentials import BasicCredentialsfrom huaweicloudsdkecs.v2.region.ecs_region import EcsRegionfrom huaweicloudsdkcore.exceptions import exceptionsfrom huaweicloudsdkecs.v2 import *if __name__ == "__main__": # 输入准备基本数据中的AK/SK ak = "<YOUR AK>" sk = "<YOUR AK>" credentials = BasicCredentials(ak, sk) / client = EcsClient.new_builder() / .with_credentials(credentials) / .with_region(EcsRegion.value_of("ap-southeast-1")) / .build() try: # 默认列出所有云服务器 request = ListServersDetailsRequest() response = client.list_servers_details(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)返回了云服务器的所有相关信息

{ "count":1, "servers":[ { "status":"ACTIVE", "updated":"2022-04-07T12:42:14Z", "auto_terminate_time":"", "hostId":"1820bda7ebd23bfdc088f7242ab64eb18b492b8f9c23220059f28173", "OS-EXT-SRV-ATTR:host":"1820bda7ebd23bfdc088f7242ab64eb18b492b8f9c23220059f28173", "addresses":{ "45c02e54-3ec0-4efd-8c66-c7000984704a":[ { "version":"4", "addr":"192.168.2.97", "OS-EXT-IPS:type":"fixed", "OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:5b:a4:ff", "OS-EXT-IPS:port_id":"88fa7a14-0400-4065-a2ca-d1e788e30df2" }, { "version":"4", "addr":"159.138.140.179", "OS-EXT-IPS:type":"floating", "OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:5b:a4:ff", "OS-EXT-IPS:port_id":"88fa7a14-0400-4065-a2ca-d1e788e30df2" } ] }, "key_name":"KeyPair", "image":{ "id":"5f524b35-2e88-46e1-9aa4-c6e59c2971bd" }, "OS-EXT-STS:vm_state":"active", "OS-EXT-SRV-ATTR:instance_name":"instance-005b0627", "OS-EXT-SRV-ATTR:hypervisor_hostname":"b8ecd1578f1f22a6ec8d43489dd400a509da6890969e43f5f90e707f", "flavor":{ "id":"s6.small.1", "name":"s6.small.1", "disk":"0", "vcpus":"1", "ram":"1024" }, "id":"68f8d8d1-3b10-4cfc-b55f-c74036c3b9d8", "security_groups":[ { "name":"default", "id":"b6fb502b-41b4-4153-9d05-59ff07417c8a" } ], "OS-EXT-AZ:availability_zone":"ap-southeast-1b", "user_id":"10157ed2b3824a30b1a47a17434dc3ab", "name":"strongswan", "created":"2022-04-07T12:41:46Z", "tenant_id":"42592e3253cc404e9a0f7b5a890ce536", "OS-DCF:diskConfig":"MANUAL", "accessIPv4":"", "accessIPv6":"", "progress":0, "OS-EXT-STS:power_state":1, "config_drive":"", "metadata":{ "cascaded.instance_extrainfo":"pcibridge:1", "charging_mode":"0", "image_name":"StrongSwan", "metering.image_id":"5f524b35-2e88-46e1-9aa4-c6e59c2971bd", "metering.imagetype":"private", "metering.resourcespeccode":"s6.small.1.linux", "metering.resourcetype":"1", "os_bit":"64", "os_type":"Linux", "vpc_id":"45c02e54-3ec0-4efd-8c66-c7000984704a" }, "OS-SRV-USG:launched_at":"2022-04-07T12:42:05.303833", "os-extended-volumes:volumes_attached":[ { "id":"e97eab60-c486-4efa-ab3f-090a2dc83055", "delete_on_termination":"true", "bootIndex":"0", "device":"/dev/vda" } ], "description":"", "host_status":"UP", "OS-EXT-SRV-ATTR:hostname":"strongswan", "OS-EXT-SRV-ATTR:reservation_id":"r-b2frc95i", "OS-EXT-SRV-ATTR:launch_index":0, "OS-EXT-SRV-ATTR:kernel_id":"", "OS-EXT-SRV-ATTR:ramdisk_id":"", "OS-EXT-SRV-ATTR:root_device_name":"/dev/vda", "locked":false, "tags":[ ], "os:scheduler_hints":{ }, "enterprise_project_id":"0", "sys_tags":[ { "key":"_sys_enterprise_project_id", "value":"0" } ], "cpu_options":{ } } ]}

(四)通过API接口删除虚拟服务器

通过api接口删除云服务器,只需要云服务器的Server_ID即可。

# coding: utf-8from huaweicloudsdkcore.auth.credentials import BasicCredentialsfrom huaweicloudsdkecs.v2.region.ecs_region import EcsRegionfrom huaweicloudsdkcore.exceptions import exceptionsfrom huaweicloudsdkecs.v2 import *if __name__ == "__main__": # 输入准备基本数据中的AK/SK ak = "<YOUR AK>" sk = "<YOUR AK>" credentials = BasicCredentials(ak, sk) / client = EcsClient.new_builder() / .with_credentials(credentials) / .with_region(EcsRegion.value_of("ap-southeast-1")) / .build() try: request = DeleteServersRequest() # 所需要删除的云服务器列表。 listServerIdServersbody = [ # 云服务器ID。 ServerId( id="68f8d8d1-3b10-4cfc-b55f-c74036c3b9d8" ) ] request.body = DeleteServersRequestBody( servers=listServerIdServersbody, # true:删除云服务器时会同时删除挂载在云服务器上的数据盘。 delete_volume=True, # true:删除云服务器时会同时删除绑定在云服务器上的弹性IP。 delete_publicip=True ) response = client.delete_servers(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)执行后,会返回一个job_id

{"job_id": "ff8080817e498e1601800424e9cc181a"}

(五)通过API接口查看云服务器删除情况

可以通过ShowJobRequest接口,来查看job执行情况

# coding: utf-8from huaweicloudsdkcore.auth.credentials import BasicCredentialsfrom huaweicloudsdkecs.v2.region.ecs_region import EcsRegionfrom huaweicloudsdkcore.exceptions import exceptionsfrom huaweicloudsdkecs.v2 import *if __name__ == "__main__": # 输入准备基本数据中的AK/SK ak = "<YOUR AK>" sk = "<YOUR AK>" credentials = BasicCredentials(ak, sk) / client = EcsClient.new_builder() / .with_credentials(credentials) / .with_region(EcsRegion.value_of("ap-southeast-1")) / .build() try: request = ShowJobRequest() # 根据job_id,来查询云主机生命周期内的任何操作任务。 request.job_id = "ff8080817e498e1601800424e9cc181a" response = client.show_job(request) print(response) except exceptions.ClientRequestException as e: print(e.status_code) print(e.request_id) print(e.error_code) print(e.error_msg)执行后,返回job执行情况

{ "begin_time":"2022-04-07T13:09:29.675Z", "end_time":"2022-04-07T13:10:13.021Z", "entities":{ "sub_jobs":[ { "status":"SUCCESS", "entities":{ "server_id":"68f8d8d1-3b10-4cfc-b55f-c74036c3b9d8" }, "job_id":"ff8080817e498e1601800424ed3c181d", "job_type":"deleteVM", "begin_time":"2022-04-07T13:09:30.555Z", "end_time":"2022-04-07T13:10:03.983Z" } ], "sub_jobs_total":1 }, "job_id":"ff8080817e498e1601800424e9cc181a", "job_type":"deleteVMs", "status":"SUCCESS"}

关键词:创建,服务,使用

74
73
25
news

版权所有© 亿企邦 1997-2025 保留一切法律许可权利。

为了最佳展示效果,本站不支持IE9及以下版本的浏览器,建议您使用谷歌Chrome浏览器。 点击下载Chrome浏览器
关闭