更新时间:2024-12-13 GMT+08:00

创建弹性云服务器

场景描述

本章节指导用户通过API创建云服务器。API的调用方法请参见如何调用API

创建云服务器时,支持通过卷和镜像两种方式进行创建。本节以指定镜像的方式为例,介绍如何创建云服务器

约束限制

该接口创建出来的是按需付费的实例。

涉及接口

创建云服务器时,需要进行规格查询、可用区查询、云硬盘创建等操作,涉及的接口如下:

操作步骤

  1. 确定待创建云服务器所在的可用区。

    1. 查询可用区。
      • 接口相关信息

        URI格式:GET /v2.1/{project_id}/os-availability-zone

        详情请参见查询可用区列表

      • 请求示例

        GET https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/os-availability-zone

        {endpoint}信息请从地区和终端节点获取。

      • 响应示例
         {
          "availabilityZoneInfo": [
            {
              "hosts": null,
              "zoneState": {
                "available": true
              },
              "zoneName":"zone_01" 
            },
            {
              "hosts": null,
              "zoneState": {
                "available": true
              },
              "zoneName": "zone_01" 
            }
          ]
        }
    2. 根据实际需要选择可用区,并记录可用区的zoneName。

  2. 确定待创建云服务器的规格。

    1. 查询云服务器规格信息。
      • 接口相关信息

        URI格式:GET /v1/{project_id}/cloudservers/flavors?availability_zone={availability_zone}

        其中,“?”后的字段为查询规格时可选的查询检索参数,详情请参见查询规格详情和规格扩展信息列表

      • 请求示例

        GET https://{endpoint}/v1/74610f3a5ad941998e91f076297ecf27/cloudservers/flavors?availability_zone=zone_01

        {endpoint}信息请从地区和终端节点获取。

        availability_zone的值“zone_01”通过步骤1获取。

      • 响应示例
        {
            "flavors": [
                {
                    "attachableQuantity": {
                        "free_scsi": 60,
                        "free_blk": 24,
                        "free_disk": 60,
                        "free_nic": 12
                    },
                    "id": "c3.2xlarge.2",
                    "name": "c3.2xlarge.2",
                    "vcpus": "8",
                    "ram": 8192,
                    "disk": "0",
                    "swap": "",
                    "links": [
                        {
                            "rel": "self",
                            "href": "https://xxx/v1.0/74610f3a5ad941998e91f076297ecf27/flavors/c3.2xlarge.2",
                            "type": null
                        },
                        {
                            "rel": "bookmark",
                            "href": "https://xxx/74610f3a5ad941998e91f076297ecf27/flavors/c3.2xlarge.2",
                            "type": null
                        }
                    ],
                    "OS-FLV-EXT-DATA:ephemeral": 0,
                    "rxtx_factor": 1,
                    "OS-FLV-DISABLED:disabled": false,
                    "rxtx_quota": null,
                    "rxtx_cap": null,
                    "os-flavor-access:is_public": true,
                    "os_extra_specs": {
                        "ecs:virtualization_env_types": "CloudCompute",
                        "ecs:generation": "c3",
                        "ecs:instance_architecture":"arm64" ,
                        "ecs:performancetype": "computingv3",
                        "resource_type": "IOoptimizedC3_2"
                    }
                }
            ]                              
        }
    2. 根据实际需要选择规格,并记录规格的ID。

  3. 确定待创建云服务器使用的镜像。

    1. 查询镜像。
      • 接口相关信息

        URI格式:GET /v2.1/{project_id}/images/detail

        详情请参见查询镜像列表详情

      • 请求示例

        GET https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/images/detail

        {endpoint}信息请从地区和终端节点获取。

      • 响应示例
        {
          "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。

  4. 确定云服务器的网络信息。

    1. 查询网络。
      • 接口相关信息

        URI格式:GET /v2.1/{project_id}/os-networks

        详情请参见查询网络列表(废弃)

      • 请求示例

        GET https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/os-networks

        {endpoint}信息请从地区和终端节点获取。

      • 响应示例
        {
          "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。

  5. 设置密钥对登录方式。

    1. 创建密钥对。
      • 接口相关信息

        URI格式:POST /v2.1/{project_id}/os-keypairs

        详情请参见创建和导入SSH密钥(废弃)

      • 请求示例

        POST https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/os-keypairs

        {endpoint}信息请从地区和终端节点获取。

        Body:

        {
            "keypair": {
                "type": "ssh",
                "name": "demo1",
                "user_id": "fake"
            }
        }
      • 响应示例
        {
          "keypair": {
            "public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCrR5Gcwlh5ih7JOvzIUuQxS5qzWWPMYHeDXkDKSQ9W5pumOV05SiO3WCswnaQ5xMdOl31mNiHtwlwq9dJi7X6jJBB2shT******************************************************************************************************************************************************************************************************************************************************************************************************* Generated-by-Nova\n",
            "private_key": "-----BEGIN RSA PRIVATE KEY-----\nMIIEogIBAAKCAQEAq0eRnMJYeYoeyTr8yFLkMUuas1ljzGB3g15AykkPVuabpjld\nOUojt1grMJ2kOcTHTpd9ZjYh7cJcKvXSYu1+oyQQdrIUw/tNBuVrsJAWxVOAi77d\nQeOLtDVImkyd+TQL1tv+F76V5vTsIkNweYHumWOxLIt/FJ4fqZG4T5GMTQQivMqD\npaI0IVrO+Wm3cWQYvNdf/EcC3DYhYqHANkRsbUYwXaREnI/tU1PjnH2XUJ69ABWz\ntdc+8sXyMoMMM1U4FLiTWzGyh0rUKkW5JXzJR2OEQT0IG+0Tf2Glyk0El0/OJPg/\ncZQzaO1o+H8DiUzs/7Pz72yDqo0R7fQ+mOCCn***********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************\n-----END RSA PRIVATE KEY-----\n",
            "user_id": "f79791beca3c48159ac2553fff22e166",
            "name": "demo1",
            "fingerprint": "57:a7:a2:ed:5f:aa:e7:**:**:**:**:**:**:**:**:**"
          }
        }
    2. 导入密钥。
      • 接口相关信息

        URI格式:POST /v2.1/{project_id}/os-keypairs

        详情请参见创建和导入SSH密钥(废弃)

      • 请求示例

        POST https://{endpoint}/v2.1/74610f3a5ad941998e91f076297ecf27/os-keypairs

        {endpoint}信息请从地区和终端节点获取。

        Body:

        {
            "keypair": {
                "public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDY8wMTdBYiJgi62o6eShoOlSKx3CZ3cE6PHisDblfK3Y0Bg7EHV7iV9c74pqsrIhK0xuGUuO1NxDQWbkwLTPN4F9Iy5CI********************************************************************************************************************************************************************************************************************************************************* Generated-by-Nova\n",
                "type": "ssh",
                "name": "demo2",
                "user_id": "fake"
            }
        }
      • 响应示例
        {
          "keypair": {
            "public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDY8wMTdBYiJgi62o6eShoOlSKx3CZ3cE6PHisDblfK3Y0Bg7EHV7iV9c74pqsrIhK0xuGUuO1NxDQWbkwLTPN4F9Iy5CI********************************************************************************************************************************************************************************************************************************************************* Generated-by-Nova\n",
            "user_id": "f79791beca3c48159ac2553fff22e166",
            "name": "demo2",
            "fingerprint": "dd:44:45:49:d9:f6:4f:**:**:**:**:**:**:**:**:**"
          }
        }
    3. 记录响应消息体中的name信息。如本例中为“demo2”。

  6. 创建密钥对登录鉴权的云服务器

    • 接口相关信息

      URI格式:POST /v1/{project_id}/cloudservers

      接口约束及请求参数说明详情,请参见创建云服务器(按需)

    • 请求示例

      POST https://{endpoint}/v1/74610f3a5ad941998e91f076297ecf27/cloudservers

      {endpoint}信息请从地区和终端节点获取。

      Body:

      {
          "server": {
              "availability_zone":"zone_01",
              "name": "newserver", 
              "imageRef": "67f433d8-ed0e-4321-a8a2-a71838539e09", 
              "root_volume": {
                  "volumetype": "SSD"
              }, 
              "data_volumes": [ 
                  {
                      "volumetype": "SSD", 
                      "size": 100,
                      "multiattach": true,
                      "hw:passthrough": true
                  }
              ], 
              "flavorRef": "s3.xlarge.2", 
              "vpcid": "0dae26c9-9a70-4392-93f3-87d53115d171", 
              "security_groups": [
                  {
                      "id": "507ca48f-814c-4293-8706-300564d54620"
                  }
              ], 
              "nics": [
                  {
                      "subnet_id": "157ee789-03ea-45b1-a698-76c92660dd83"
                  }
              ], 
              "publicip": {
                  "eip": {
                      "iptype": "5_bgp",
                      "bandwidth": {
                          "size": 10, 
                          "sharetype": "PER"
                      }
                  }
              }, 
              "key_name": "sshkey-123", 
              "count": 1, 
              "server_tags": [
                  {
                      "key": "key1",
                      "value": "value1"
                  }
              ],
              "metadata": {
                     "op_svc_userid": "8ea65f4099ba412883e2a0da72b96873",
                     "agency_name": "test"
              }
          }
      }
    • 响应示例
      {
          "job_id": "ff808082739334d80173941567f21d4f",
          "serverIds": [
              "6d311127-bce1-48db-bf0f-cac9f8f7f077"
          ]
      }
      

  7. 确认云服务器创建成功。

    • 接口相关信息

      URI格式:GET /v1/{project_id}/cloudservers/{server_id}

      详情请参见查询云服务器详情

    • 请求示例

      GET https://{endpoint}/v1/74610f3a5ad941998e91f076297ecf27/cloudservers/0c71c0da-8852-4c56-a1d1-3a9b9bcb6da6

      其中:

      0c71c0da-8852-4c56-a1d1-3a9b9bcb6da6为创建的云服务器UUID。

      {endpoint}信息请从地区和终端节点获取。

    • 响应示例
      {
          "server":{
              "id":"0c71c0da-8852-4c56-a1d1-3a9b9bcb6da6",
              "name":"ecs-2ecf",
              "addresses":{
                  "2a6f4aa6-d93e-45f5-a8cb-b030dbf8cd68":[
                      {
                          "version":"4",
                          "addr":"192.168.1.99",
                          "OS-EXT-IPS-MAC:mac_addr":"fa:16:3e:df:18:6d",
                          "OS-EXT-IPS:port_id":"23037c18-027a-44e5-b6b9-f8d8f113fe02",
                          "OS-EXT-IPS:type":"fixed"
                      }
                  ]
              },
              "flavor":{
                  "disk":"0",
                  "vcpus":"2",
                  "ram":"1024",
                  "id":"s3.large.1",
                  "name":"s3.large.1",
                  "gpus": [],
                  "asic_accelerators": [] 
              },
              "accessIPv4":"",
              "accessIPv6":"",
              "status":"ACTIVE",
              "progress":0,
              "hostId":"c7145889b2e3202cd295ceddb1742ff8941b827b586861fd0acedf64",
              "updated":"2018-09-13T07:06:51Z",
              "created":"2018-09-13T07:03:44Z",
              "image":{
                  "id":"1ce5800a-e487-4c1b-b264-3353a39e2b4b"
              },
              "metadata":{
                  "metering.order_id":"CS1809131459IGC24",
                  "metering.image_id":"c71b64e7-4767-4406-afde-2c7c7ac2242c",
                  "metering.imagetype":"gold",
                  "metering.resourcespeccode":"s3.small.1.linux",
                  "image_name":"HEC_Public_Cloudinit_Oracle_Linux_7.4_64bit_40G",
                  "metering.resourcetype":"1",
                  "metering.product_id":"00301-117024-0--0",
                  "cascaded.instance_extrainfo":"pcibridge:2",
                  "os_bit":"64",
                  "vpc_id":"0431c5e5-bc94-4a44-8263-15da2a642435",
                  "os_type":"Linux",
                  "charging_mode":"1"
              },
              "tags":[
              ],
              "description":"",
              "locked":false,
              "config_drive":"",
              "tenant_id":"74610f3a5ad941998e91f076297ecf27",
              "user_id":"f79791beca3c48159ac2553fff22e166",
              "key_name":"KeyPair-d750",
              "os-extended-volumes:volumes_attached":[
                  {
                      "device":"/dev/vda",
                      "bootIndex":"0",
                      "id":"80c15cff-2473-4982-a816-d760cad6c42c",
                      "delete_on_termination":"false"
                  }
              ],
              "OS-EXT-STS:task_state":null,
              "OS-EXT-STS:power_state":1,
              "OS-EXT-STS:vm_state":"active",
              "OS-EXT-SRV-ATTR:host":"az21.dc1",
              "OS-EXT-SRV-ATTR:instance_name":"instance-0015147f",
              "OS-EXT-SRV-ATTR:hypervisor_hostname":"nova003@74",
              "OS-EXT-SRV-ATTR:user_data":null,
              "OS-DCF:diskConfig":"MANUAL",
              "OS-EXT-AZ:availability_zone":"zone_01",
              "os:scheduler_hints":{
              },
              "OS-EXT-SRV-ATTR:root_device_name":"/dev/vda",
              "OS-EXT-SRV-ATTR:ramdisk_id":"",
              "OS-SRV-USG:launched_at":"2018-09-13T07:04:09.197749",
              "OS-EXT-SRV-ATTR:kernel_id":"",
              "OS-EXT-SRV-ATTR:launch_index":0,
              "host_status":"UP",
              "OS-EXT-SRV-ATTR:reservation_id":"r-nrd8b5c4",
              "OS-EXT-SRV-ATTR:hostname":"ecs-2ecf",
              "sys_tags":[
                  {
                      "key":"_sys_enterprise_project_id",
                      "value":"0"
                  }
              ],
              "security_groups":[
                  {
                      "name":"sg-95ec",
                      "id":"6505b5d1-7837-41eb-8a1c-869d4355baa3"
                  }
              ]
          }
      }