摘要:9个步骤通过API创建云服务器。
事情是这样的,公司成立了一个新项目,要给项目建官网,并搭建一个web服务器。按照我的习惯,搭建一个本地虚拟机就好了,部署简单方便还好配置,云服务器嘛,写了10年代码,还是不习惯那玩意儿。
我这刚开始动手,就看见实习生屁颠屁颠地跑过来,我心想这小子肯定是想来学虚拟机搭建部署的,这么简单的事都不会,真是白瞎了他那985的学校了。
可是...万万没想到啊,小实习生跑到我老大那里邀功去了!!!远远地听见他说:“公司新项目跑在云服务器上了,以后可以根据访问量随时进行弹性扩展,安全稳定还快。”
啥?云服务器?这小子居然会配置云服务器?听完汇报,老大看向了我,带着小实习生往我这边走过来……
“哼,叫你邀功,撞枪口上了吧,我就说云服务器配置没那么容易,还不是得来找我~”这么想着有点得意,老大走过来拍拍我的肩膀,说:“你跟他学一下怎么配置云服务器,别再用虚拟机了,以后都是麻烦事。”
我...他...让我跟一个实习生学???
实习生冲我微微一笑,就走了,没过一会扔给我一份文档,上面写着:“创建弹性云服务器流程详解”。打开粗略扫了一眼,代码都直接贴上来了?瞧不起谁?感觉有被冒犯到。
技术人嘛,看到代码还是没忍住跑一遍,等我发现bug或跑不通,再给他上上课!
首先免费领取了1台云服务器,然后开始跑代码
结果……真香。
内容是这样的:
通过API创建云服务器的9个步骤(以指定镜像方式创建云服务器为例)
步骤一:确定待创建云服务器所在的可用区。
1、查询可用区。
(1)接口相关信息
URI格式:GET /v2.1/{project_id}/os-availability-zone
(2)请求示例
GET: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/os-availability-zone
(3)响应示例
{
"availabilityZoneInfo": [
{
"hosts": null,
"zoneState": {
"available": true
},
"zoneName":"zone_01"
},
{
"hosts": null,
"zoneState": {
"available": true
},
"zoneName": "zone_01"
}
]
}
2、根据实际需要选择可用区,并记录可用区的zoneName。
步骤二:确定待创建云服务器的规格。
1、查询云服务器规格信息。
(1)接口相关信息
URI格式:GET/v2.1/{project_id}/flavors/detail{?minDisk,minRam,is_public,sort_key,sort_dir}
其中,“?”后的字段为查询规格时可选的查询检索参数。
(2)请求示例
GET: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/flavors/detail
(3)响应示例
{
"flavors": [
{
"name": "c1.2xlarge",
"links": [
{
"href": "https://xxx/v2.1/74610f3a5ad941998e91f076297ecf27/flavors/c1.2xlarge",
"rel": "self"
},
{
"href": "https://xxx/74610f3a5ad941998e91f076297ecf27/flavors/c1.2xlarge",
"rel": "bookmark"
}
],
"ram": 8192,
"OS-FLV-DISABLED:disabled": false,
"vcpus": 8,
"swap": "",
"os-flavor-access:is_public": true,
"rxtx_factor": 1,
"OS-FLV-EXT-DATA:ephemeral": 0,
"disk": 0,
"id": "c1.2xlarge"
}
]
}
2、 根据实际需要选择规格,并记录规格的ID。
步骤三:确定待创建云服务器使用的镜像。
1、查询镜像。
(1)接口相关信息
URI格式:GET /v2.1/{project_id}/images/detail
(2)请求示例
GET: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/images/detail
(3)响应示例
{
"images": [
{
"OS-EXT-IMG-SIZE:size": 0,
"metadata": {
"__os_type": "Linux",
"hw_vif_multiqueue_enabled": "true",
"__imagetype": "gold",
"__quick_start": "true",
"virtual_env_type": "FusionCompute",
"__support_xen": "true",
"__support_kvm": "true",
"__image_source_type": "uds",
"__platform": "EulerOS",
"__os_version": "EulerOS 2.2 64bit",
"__os_bit": "64",
"__isregistered": "false"
},
"created": "2018-05-14T06:13:50Z",
"minRam": 0,
"name": "DBS-MySQL-Image_2.1.3.3",
"progress": 100,
"links": [
{
"rel": "self",
"href": "https://None/v2.1/74610f3a5ad941998e91f076297ecf27/images/11e8f727-d439-4ed1-b3b8-33f46c0379c4"
},
{
"rel": "bookmark",
"href": "https://None/74610f3a5ad941998e91f076297ecf27/images/11e8f727-d439-4ed1-b3b8-33f46c0379c4"
},
{
"rel": "alternate",
"href": "https://None/images/11e8f727-d439-4ed1-b3b8-33f46c0379c4",
"type": "application/vnd.openstack.image"
}
],
"id": "11e8f727-d439-4ed1-b3b8-33f46c0379c4",
"updated": "2018-05-14T06:13:52Z",
"minDisk": 40,
"status": "ACTIVE"
}
]
}
2、根据需要选择镜像,并记录镜像ID。
步骤四:确定云服务器的网络信息。
1、查询网络。
(1)接口相关信息
URI格式:GET /v2.1/{project_id}/os-networks
(2)请求示例
GET: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/os-networks
(3)响应示例
{
"networks": [
{
"id": "07a9557d-4256-48ae-847c-415a9c8f7ff6",
"label": "b_tt3_td1b",
"broadcast": null,
"cidr": null,
"dns1": null,
"dns2": null,
"gateway": null,
"netmask": null,
"cidr_v6": null,
"gateway_v6": null,
"netmask_v6": null
}
]
}
2、根据需要选择网络,并记录网络ID。
步骤五:设置密钥对登录方式。
如需设置为密码登录方式,请直接跳转至步骤七。
1、创建密钥对。
(1)接口相关信息
URI格式:POST /v2.1/{project_id}/os-keypairs
(2)请求示例
POST: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/os-keypairs
Body:
{
"keypair": {
"type": "ssh",
"name": "demo1",
"user_id": "fake"
}
}
(3)响应示例
{
"keypair": {
"public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCrR5Gcwlh5ih7JOvzIUuQxS5qzWWPMYHeDXkDKSQ9W5pumOV05SiO3WCswnaQ5xMdOl31mNiHtwlwq9dJi7X6jJBB2shTD+00G5WuwkBbFU4CLvt1B44u0NUiaTJ35NAvW2/4XvpXm9OwiQ3B5ge6ZY7Esi38Unh+pkbhPkYxNBCK8yoOlojQhWs75abdxZBi811/8RwLcNiFiocA2RGxtRjBdpEScj+1TU+OcfZdQnr0AFbO11z7yxfIygwwzVTgUuJNbMbKHStQqRbklfMlHY4RBPQgb7RN/YaXKTQSXT84k+D9xlDNo7Wj4fwOJTOz/s/PvbIOqjRHt9D6Y4IKd Generated-by-Nova\n",
"private_key": "-----BEGIN RSA PRIVATE KEY-----\nMIIEogIBAAKCAQEAq0eRnMJYeYoeyTr8yFLkMUu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Bkuz11iydtxb3G/obSD2WO\nM9VaIycmzRPFzNwGRH/gOR0mhTluKp0wyJjbSd34oeqpH/2r2ivddrOysxoqa8jg\n4IQDZyLvj7MaKjQxrieqP89+y9Or9TMFo1xB46x2G8EN8/xHuA9YGnZSPFtWv72m\nhRqV0hv82amWsA0vHnRUSwKBgDsKHXvrTMbNkNhkykMXCH5iyWiBFSyZa1ZJMlgf\nknsqfdzeVPwF6E55QKAN2uuTlwzG/3ljPxahR1hvmUJjQN9JSBiUKbtW6GPCRVbr\nf/jLi1Iu99COZdluVKeybqn8Z/aSNP24DR9FM8kxzZ1IMPaTBmhFypp6BclhcLBt\nxTG1AoGAfcrkVbV1SOy7fECUtMpUECcw0yU4GWj3sR2RbII63C500RVYQlUpUaRR\naANbASHTVR4myOKtGSxEUhAQHlxFDwsDL7W3gzAqTFbEDp1xAAUyT/nkOAhQjEm4\nORFdDETeXLQG1KMUj+8AdnhfYp3JTdft6rmPpZEBUFiCAUMAvb0=\n-----END RSA PRIVATE KEY-----\n",
"user_id": "f79791beca3c48159ac2553fff22e166",
"name": "demo1",
"fingerprint": "57:a7:a2:ed:5f:aa:e7:54:62:2e:bb:e7:92:22:cb:40"
}
}
2、导入密钥。
(1)接口相关信息
URI格式:POST /v2.1/{project_id}/os-keypairs
(2)请求示例
POST: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/os-keypairs
Body:
{
"keypair": {
"public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDY8wMTdBYiJgi62o6eShoOlSKx3CZ3cE6PHisDblfK3Y0Bg7EHV7iV9c74pqsrIhK0xuGUuO1NxDQWbkwLTPN4F9Iy5CIYohLuMIpbln6LDtfRPpdhEh3lxL8MM61gyfpKzeKkwkEpSFj27Rgh6zCyJgBpkA2A0HTP737UlitahL4faCWDIS+Vj6mbcfkWiMhuMCzTZgSKAZ4PfoG4B5HJhR52C6A4XLiQFT9heh9gnIsIG+uTogTKUbcJKuN7M6AraJpul6eHhV9YI4433sDmuiBF/njvreVPWwAHlAkgT9I8q1T/cfEFiwzXpdGbkK5O8NC7K+qNbbdKihlahONt Generated-by-Nova\n",
"type": "ssh",
"name": "demo2",
"user_id": "fake"
}
}
(3)响应示例
{
"keypair": {
"public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDY8wMTdBYiJgi62o6eShoOlSKx3CZ3cE6PHisDblfK3Y0Bg7EHV7iV9c74pqsrIhK0xuGUuO1NxDQWbkwLTPN4F9Iy5CIYohLuMIpbln6LDtfRPpdhEh3lxL8MM61gyfpKzeKkwkEpSFj27Rgh6zCyJgBpkA2A0HTP737UlitahL4faCWDIS+Vj6mbcfkWiMhuMCzTZgSKAZ4PfoG4B5HJhR52C6A4XLiQFT9heh9gnIsIG+uTogTKUbcJKuN7M6AraJpul6eHhV9YI4433sDmuiBF/njvreVPWwAHlAkgT9I8q1T/cfEFiwzXpdGbkK5O8NC7K+qNbbdKihlahONt Generated-by-Nova\n",
"user_id": "f79791beca3c48159ac2553fff22e166",
"name": "demo2",
"fingerprint": "dd:44:45:49:d9:f6:4f:c0:24:2d:81:aa:c4:4b:83:c2"
}
}
2、 记录响应消息体中的name信息。如本例中为“demo2”。
步骤六:创建密钥对登录鉴权的云服务器。
(1)接口相关信息
URI格式:POST /v2.1/{project_id}/servers
说明:
本示例是指定image创建云服务器,因此:
● block_device_mapping_v2.1中,“source_type”需设置为“image”,“uuid”设置为选择的镜像ID,“destination_type”设置为“volume”,“boot_index”设置为“0”。
● “volume_size”应大于等于镜像元数据中指定的最小值。
(2)请求示例
POST: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/servers
Body:
{
"server": {
"flavorRef": "c1.large",
"name": "zttestvm1",
"block_device_mapping_v2.1": [{
"source_type": "image",
"destination_type": "volume",
"volume_type": "SATA",
"volume_size": "40",
"delete_on_termination": "true",
"uuid": "11e8f727-d439-4ed1-b3b8-33f46c0379c4",
"boot_index": "0"
}],
"networks": [{
"uuid": "fb68519f-a7c0-476e-98d4-2e4cf6de6def"
}],
"key_name": "demo2",
"availability_zone": "az_test_01"
}
}
(3)响应示例
{
"server": {
"security_groups": [
{
"name": "default"
}
],
"OS-DCF:diskConfig": "MANUAL",
"links": [
{
"rel": "self",
"href": "https://None/v2.1/74610f3a5ad941998e91f076297ecf27/servers/6d311127-bce1-48db-bf0f-cac9f8f7f077"
},
{
"rel": "bookmark",
"href": "https://None/74610f3a5ad941998e91f076297ecf27/servers/6d311127-bce1-48db-bf0f-cac9f8f7f077"
}
],
"id": "6d311127-bce1-48db-bf0f-cac9f8f7f077",
"adminPass": "WcC4QoVZPXpV"
}
}
步骤七:设置密码登录方式。
(1)使用非cloud-init镜像创建云服务器时,Windows和Linux系统都使用“adminPass”参数实现密码注入。
(2)使用cloud-init镜像创建云服务器时:
l 对于Linux系统,只能使用“user_data”进行密码注入,“user_data”字符串长度小于65535,且必须是base64加密的;
l 对于Windows系统,只能通过元数据“admin_pass”进行密码注入。
步骤八:创建密码方式登录鉴权的弹性云服务器。
说明:
本示例是指定image创建弹性云服务器,因此:
(1)block_device_mapping_v2.1中,“source_type”需设置为“image”,“uuid”设置为选择的镜像ID,“destination_type”设置为“volume”,“boot_index”设置为“0”。
(2)“volume_size”应大于等于镜像元数据中指定的最小值。
1、创建非cloud-init镜像、密码方式登录鉴权的弹性云服务器。
(1)接口相关信息
URI格式:POST /v2.1/{project_id}/servers
(2)请求示例
POST: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/servers
Body:
{
"server": {
"flavorRef": "c1.large",
"name": "zttestvm1",
"adminPass":"NOVAGLANCEI@123",
"block_device_mapping_v2.1": [{
"source_type": "image",
"destination_type": "volume",
"volume_type": "SATA",
"volume_size": "40",
"delete_on_termination": "true",
"uuid": "11e8f727-d439-4ed1-b3b8-33f46c0379c4",
"boot_index": "0"
}],
"networks": [{
"uuid": "fb68519f-a7c0-476e-98d4-2e4cf6de6def"
}],
"availability_zone": "az_test_01"
}
}
(3)响应示例
{
"server": {
"security_groups": [
{
"name": "default"
}
],
"OS-DCF:diskConfig": "MANUAL",
"links": [
{
"rel": "self",
"href": "https://None/v2.1/74610f3a5ad941998e91f076297ecf27/servers/73991cff-37c6-491a-9a8a-377397729fce"
},
{
"rel": "bookmark",
"href": "https://None/74610f3a5ad941998e91f076297ecf27/servers/73991cff-37c6-491a-9a8a-377397729fce"
}
],
"id": "73991cff-37c6-491a-9a8a-377397729fce",
"adminPass": "NOVAGLANCEI@123"
}
}
2、创建cloud-init镜像(以Linux为例)、密码方式登录鉴权的弹性云服务器。
(1)接口相关信息
URI格式:POST /v2.1/{project_id}/servers
(2)请求示例
POST: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/servers
Body:
{
"server": {
"flavorRef": "c1.large",
"name": "zttestvm2",
"user_data":"IyEvYmluL2Jhc2gKZWNobyAncm9vdDokNiRuaERlcUIkajBkelVMbkFrbWo4bWlFREFzT0VONkVPdGg1RmdqWnQybUVWZVRocC5OVDJXelZVdzFuNzhnL3dNdDE0dVpSQ1V3LzhHS3VGdEViQ0lPQS5vN1BZQzEnIHwgY2hwYXNzd2QgLWU7",
"block_device_mapping_v2.1": [{
"source_type": "image",
"destination_type": "volume",
"volume_type": "SATA",
"volume_size": "40",
"delete_on_termination": "true",
"uuid": "11e8f727-d439-4ed1-b3b8-33f46c0379c4",
"boot_index": "0"
}],
"networks": [{
"uuid": "fb68519f-a7c0-476e-98d4-2e4cf6de6def"
}],
"availability_zone": "az_test_01"
}
}
(3)响应示例
{
"server": {
"security_groups": [
{
"name": "default"
}
],
"OS-DCF:diskConfig": "MANUAL",
"links": [
{
"rel": "self",
"href": "https://None/v2.1/74610f3a5ad941998e91f076297ecf27/servers/7c11f362-cc96-4306-85b6-b432a639ca5e"
},
{
"rel": "bookmark",
"href": "https://None/74610f3a5ad941998e91f076297ecf27/servers/7c11f362-cc96-4306-85b6-b432a639ca5e"
}
],
"id": "7c11f362-cc96-4306-85b6-b432a639ca5e",
"admin_pass": "J3xMSivHCuq5"
}
}
步骤九:确认云服务器创建成功。
(1)接口相关信息
URI格式:GET /v2.1/{project_id}/servers/{server_id}
(2)请求示例
GET: https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/servers/0c71c0da-8852-4c56-a1d1-3a9b9bcb6da6
其中:
0c71c0da-8852-4c56-a1d1-3a9b9bcb6da6为创建的云服务器UUID。
(3)响应示例
{
"server": {
"tenant_id": "74610f3a5ad941998e91f076297ecf27",
"addresses": {
"2a6f4aa6-d93e-45f5-a8cb-b030dbf8cd68": [
{
"OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:88:01:1b",
"OS-EXT-IPS:type": "fixed",
"addr": "192.168.2.192",
"version": 4
}
]
},
"metadata": {},
"OS-EXT-STS:task_state": null,
"OS-DCF:diskConfig": "MANUAL",
"OS-EXT-AZ:availability_zone": "az_test_01",
"links": [
{
"rel": "self",
"href": "https://None/v2.1/74610f3a5ad941998e91f076297ecf27/servers/0c71c0da-8852-4c56-a1d1-3a9b9bcb6da6"
},
{
"rel": "bookmark",
"href": "https://None/74610f3a5ad941998e91f076297ecf27/servers/0c71c0da-8852-4c56-a1d1-3a9b9bcb6da6"
}
],
"OS-EXT-STS:power_state": 1,
"id": "0c71c0da-8852-4c56-a1d1-3a9b9bcb6da6",
"os-extended-volumes:volumes_attached": [
{
"id": "b551445a-e749-4d53-932a-638a455cb6c3"
}
],
"OS-EXT-SRV-ATTR:host": "pod1_test_01",
"image": {
"links": [
{
"rel": "bookmark",
"href": "https://None/74610f3a5ad941998e91f076297ecf27/images/11e8f727-d439-4ed1-b3b8-33f46c0379c4"
}
],
"id": "11e8f727-d439-4ed1-b3b8-33f46c0379c4"
},
"OS-SRV-USG:terminated_at": null,
"accessIPv4": "",
"accessIPv6": "",
"created": "2018-05-25T01:47:11Z",
"hostId": "b2792bef989888d2df1f51bff81de5ac58a4117f4e9ec3059c1a0410",
"OS-EXT-SRV-ATTR:hypervisor_hostname": "nova001@36",
"key_name": null,
"flavor": {
"links": [
{
"rel": "bookmark",
"href": "https://None/74610f3a5ad941998e91f076297ecf27/flavors/c1.large"
}
],
"id": "c1.large"
},
"security_groups": [
{
"name": "default"
}
],
"config_drive": "",
"OS-EXT-STS:vm_state": "active",
"OS-EXT-SRV-ATTR:instance_name": "instance-001883cd",
"user_id": "f79791beca3c48159ac2553fff22e166",
"name": "zttestvm1",
"progress": 0,
"OS-SRV-USG:launched_at": "2018-05-25T01:47:55.755922",
"updated": "2018-05-25T01:47:55Z",
"status": "ACTIVE"
}
}
按照步骤指示,我很快完成了云服务器的搭建,不禁有些惊讶,这小子怎么懂这么多?等等...华为云API Explorer?打开华为云API Explorer的官网,我好像打开了新世界的大门,有很多已配置好的环境和接口,直接调用调试就可以了,以后怕是没有什么能够难住我了!
目前API Explorer平台已开放EI企业智能、计算、应用服务、网络、软件开发平台、视频等70+云服务,共上线2000+个API、6000+个错误码。在前期试运行期间,华为云API Explorer平台上的API接口也已被多家企业成功接入。
点击查看详情:《华为云一站式API解决方案平台API Explorer上线》
据悉,华为云API Explorer平台在未来几个月会实现更多功能,比如支持SDK示例代码、CLI等特性,同时也会开放更多的云服务API接口,连接更多开发者实现创新、拓宽创新边界。
点击关注,第一时间了解华为云新鲜技术~
评论