文档首页/ 弹性云服务器 ECS/ API参考/ 应用示例/ 弹性云服务器挂载网卡
更新时间:2024-11-14 GMT+08:00
分享

弹性云服务器挂载网卡

场景描述

当弹性云服务器需要多个网卡时,可以调用接口创建网卡,然后挂载至弹性云服务器。

您可以在创建云服务器时,通过设置“nics”参数值挂载网卡,也可以在创建云服务器成功后挂载网卡。本节主要介绍云服务器创建成功后挂载网卡的场景。

操作步骤

  1. 创建网卡。

    1. 创建网络。
      • 接口相关信息

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

        详情请参见创建网络

      • 请求示例

        POST https://{Endpoint}/v1/{project_id}/vpcs

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

        Body:

        {
            "vpc": {
                "name": "vpc",
                "description": "test",
                "cidr": "192.168.0.0/16"
            }
        }
      • 响应示例
        {
        	"vpc": {
        		"id": "99d9d709-8478-4b46-9f3f-2206b1023fd3",
        		"name": "vpc",
        		"description": "test",
        		"cidr": "192.168.0.0/16",
        		"status": "CREATING",
        		"routes": []
        	}
        }
    2. 记录响应中“vpc”的ID。
    3. 创建子网。
      • 接口相关信息

        URI格式:POST /v2.0/subnets

        详情请参见创建子网

      • 请求示例

        POST https://{endpoint}/v2.0/subnets

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

        Body:

        {
            "subnet": {
                "name": "testsubnet",
                "enable_dhcp": true,
                "network_id": "c4a3019d-1ac0-4cfb-a838-2342eb992e6b",
                "tenant_id": "74610f3a5ad941998e91f076297ecf27",
                "dns_nameservers": [
                    "8.8.8.8",
                    "8.8.8.7"
                ],
                "allocation_pools": [
                    {
                        "start": "10.0.10.2",
                        "end": "10.0.10.254"
                    }
                ],
                "host_routes": [],
                "ip_version": 4,
                "gateway_ip": "10.0.10.1",
                "cidr": "10.0.10.0/24"
            }
        }
      • 响应示例
        {
          "subnet": {
            "name": "testsubnet",
            "cidr": "10.0.10.0/24",
            "id": "877b5567-e8c6-4a0d-aabf-0f13da225fe5",
            "enable_dhcp": true,
            "network_id": "c4a3019d-1ac0-4cfb-a838-2342eb992e6b",
            "tenant_id": "74610f3a5ad941998e91f076297ecf27",
            "dns_nameservers": [
              "8.8.8.8",
              "8.8.8.7"
            ],
            "allocation_pools": [
              {
                "start": "10.0.10.2",
                "end": "10.0.10.254"
              }
            ],
            "host_routes": [],
            "ip_version": 4,
            "gateway_ip": "10.0.10.1"
          }
        }
    4. 记录响应中“subnet”的ID。
    5. 创建端口。
      • 接口相关信息

        URI格式:POST /v2.0/ports

        详情请参见创建端口

      • 请求示例

        POST https://{endpoint}/v2.0/ports

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

        Body:

        {
            "port": {
                "admin_state_up": true, 
        
                "fixed_ips": [
                    {
                        "subnet_id": "877b5567-e8c6-4a0d-aabf-0f13da225fe5"
                    }
                ], 
                "name": "test", 
                "network_id": "c4a3019d-1ac0-4cfb-a838-2342eb992e6b",
                "tenant_id": "74610f3a5ad941998e91f076297ecf27"
            }
        }
      • 响应示例
        {
          "port": {
            "id": "7bf1c36f-e7f8-478a-be3d-674b486abbc4",
            "name": "test",
            "status": "DOWN",
            "admin_state_up": true,
            "fixed_ips": [
              {
                "subnet_id": "877b5567-e8c6-4a0d-aabf-0f13da225fe5",
                "ip_address": "10.0.10.233"
              }
            ],
            "mac_address": "fa:16:3e:db:91:f6",
            "network_id": "c4a3019d-1ac0-4cfb-a838-2342eb992e6b",
            "tenant_id": "74610f3a5ad941998e91f076297ecf27",
            "device_id": "",
            "device_owner": "",
            "security_groups": [
              "93031677-2895-4b83-855a-637e309aa9e6"
            ],
            "extra_dhcp_opts": [],
            "allowed_address_pairs": [],
            "binding:vnic_type": "normal",
            "binding:vif_details": {},
            "binding:profile": {}
          }
        }
    6. 记录响应中“port”的ID。

  2. 挂载网卡。

    • 接口相关信息

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

      详情请参见“批量添加云服务器网卡”。

    • 请求示例

      POST https://{endpoint}/v1/74610f3a5ad941998e91f076297ecf27/cloudservers/9f4d9281-95e7-4915-a126-1ee597101e2e/nics

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

      Body:

      {
          "nics": [
              {
                  "subnet_id": "877b5567-e8c6-4a0d-aabf-0f13da225fe5" 
              }
          ]
      }
    • 响应示例
      {      
          "job_id": "ff80808288d41e1b018990260955686a" 
      } 

  3. 确认挂载结果。

    • 接口相关信息

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

      详情请参见查询云服务器网卡信息

    • 请求示例

      GET https://{endpoint}/v1/74610f3a5ad941998e91f076297ecf27/cloudservers/9f4d9281-95e7-4915-a126-1ee597101e2e/os-interface

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

    • 响应示例
      {
          "interfaceAttachments": [
              {
                  "port_state": "ACTIVE",
                  "fixed_ips": [
                      {
                          "subnet_id": "46712fe4-25bd-4eae-874b-a528abfb76be",
                          "ip_address": "192.168.0.50"
                      }
                  ],
                  "net_id": "17251a8f-a671-4d7c-85d9-af5415962994",
                  "port_id": "dd706739-b696-40be-a9f4-477ce478cb18",
                  "mac_addr": "fa:16:3e:a5:e0:3c"
              },
             {
                  "port_state": "ACTIVE",
                  "fixed_ips": [
                      {
                          "subnet_id": "877b5567-e8c6-4a0d-aabf-0f13da225fe5",
                          "ip_address": "10.0.10.233"
                      }
                  ],
                  "net_id": "c4a3019d-1ac0-4cfb-a838-2342eb992e6b",
                  "port_id": "7bf1c36f-e7f8-478a-be3d-674b486abbc4",
                  "mac_addr": "fa:16:3e:db:91:f6"
              }
          ]
      }

相关文档