你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

教程:使用 Python SDK 在 Azure 公共 MEC 中部署虚拟机

本教程中,你将使用 Python SDK 在 Azure 公共多接入边缘计算 (MEC) 中部署资源。 本教程提供了 Python 代码,用于在 Azure 公共 MEC 中部署虚拟机 (VM) 及其依赖项。

有关 Python SDK 的信息,请参阅用于 Python 的 Azure 库的使用模式

在本教程中,你将了解如何执行以下操作:

  • 安装所需的 Azure 库包
  • 预配虚拟机
  • 在开发环境中运行脚本
  • 在关联的区域中创建跳转服务器
  • 访问 VM

先决条件

  • 如果没有 Azure 订阅,请在开始之前创建一个免费帐户

  • 将允许列表中的订阅添加到 Azure 帐户,这使你能够在 Azure 公共 MEC 中部署资源。 如果没有有效的支持的订阅,请联系 Azure 公共 MEC 产品团队

  • 按照配置适用于 Azure 的本地 Python 开发环境中的说明,在本地开发环境中设置 Python。 务必创建用于本地开发的服务主体,并为此教程项目创建虚拟环境,然后将其激活。

安装所需的 Azure 库包

  1. 创建一个名为 requirements.txt 的文件,其中列出了此示例中使用的管理库。

    azure-mgmt-resource
    azure-mgmt-compute
    azure-mgmt-network
    azure-identity
    azure-mgmt-extendedlocation==1.0.0b2
    
  2. 在激活虚拟环境的情况下打开命令提示符,并安装 requirements.txt 中列出的管理库。

    pip install -r requirements.txt
    

预配虚拟机

  1. 创建一个名 provision_vm_edge.py 的 Python 文件,并使用以下 Python 脚本填充该文件。 此脚本将在 Azure 公共 MEC 中部署 VM 及其关联的依赖项。 脚本中的注释说明了详细信息。

    # Import the needed credential and management objects from the libraries.
    from azure.identity import AzureCliCredential
    from azure.mgmt.resource import ResourceManagementClient
    from azure.mgmt.network import NetworkManagementClient
    from azure.mgmt.compute import ComputeManagementClient
    import os
    
    print(f"Provisioning a virtual machine...some operations might take a minute or two.")
    
    # Acquire a credential object using CLI-based authentication.
    credential = AzureCliCredential()
    
    # Retrieve subscription ID from environment variable.
    subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
    
    # Step 1: Provision a resource group
    
    # Obtain the management object for resources, using the credentials from the CLI login.
    resource_client = ResourceManagementClient(credential, subscription_id)
    
    # Constants we need in multiple places: the resource group name, the region and the public mec location
    # in which we provision resources. Populate the variables with appropriate values. 
    RESOURCE_GROUP_NAME = "PythonAzureExample-VM-rg"
    LOCATION = "<region>"
    PUBLIC_MEC_LOCATION = "<edgezone id>"
    USERNAME = "azureuser"
    PASSWORD = "<password>"
    # Provision the resource group.
    rg_result = resource_client.resource_groups.create_or_update(RESOURCE_GROUP_NAME,
       {
           "location": LOCATION
       }
    )
    
    print(f"Provisioned resource group {rg_result.name} in the {rg_result.location} region")
    
    # For details on the previous code, see Example: Use the Azure libraries to provision a resource group
    # at https://learn.microsoft.com/azure/developer/python/azure-sdk-example-resource-group
    
    # Step 2: Provision a virtual network
    
    # A virtual machine requires a network interface client (NIC). A NIC requires
    # a virtual network and subnet along with an IP address. Therefore, we must provision
    # these downstream components first, then provision the NIC, after which we
    # can provision the VM.
    
    # Network and IP address names
    VNET_NAME = "python-example-vnet-edge"
    SUBNET_NAME = "python-example-subnet-edge"
    IP_NAME = "python-example-ip-edge"
    IP_CONFIG_NAME = "python-example-ip-config-edge"
    NIC_NAME = "python-example-nic-edge"
    
    # Obtain the management object for networks
    network_client = NetworkManagementClient(credential, subscription_id)
    
    # Provision the virtual network and wait for completion
    poller = network_client.virtual_networks.begin_create_or_update(RESOURCE_GROUP_NAME,
        VNET_NAME,
        {
            "location": LOCATION,
            "extendedLocation": {"type": "EdgeZone", "name": PUBLIC_MEC_LOCATION},
            "address_space": {
                "address_prefixes": ["10.1.0.0/16"]
            }
        }
    )
    
    vnet_result = poller.result()
    
    print(f"Provisioned virtual network {vnet_result.name} with address prefixes {vnet_result.address_space.address_prefixes}")
    
    # Step 3: Provision the subnet and wait for completion
    poller = network_client.subnets.begin_create_or_update(RESOURCE_GROUP_NAME, 
        VNET_NAME, SUBNET_NAME,
        { "address_prefix": "10.1.0.0/24" }
    )
    subnet_result = poller.result()
    
    print(f"Provisioned virtual subnet {subnet_result.name} with address prefix {subnet_result.address_prefix}")
    
    # Step 4: Provision an IP address and wait for completion
    # Only the standard public IP SKU is supported at EdgeZones
    poller = network_client.public_ip_addresses.begin_create_or_update(RESOURCE_GROUP_NAME,
        IP_NAME,
        {
            "location": LOCATION,
            "extendedLocation": {"type": "EdgeZone", "name": PUBLIC_MEC_LOCATION},
            "sku": { "name": "Standard" },
            "public_ip_allocation_method": "Static",
            "public_ip_address_version" : "IPV4"
        }
    )
    
    ip_address_result = poller.result()
    
    print(f"Provisioned public IP address {ip_address_result.name} with address {ip_address_result.ip_address}")
    
    # Step 5: Provision the network interface client
    poller = network_client.network_interfaces.begin_create_or_update(RESOURCE_GROUP_NAME,
        NIC_NAME, 
        {
            "location": LOCATION,
            "extendedLocation": {"type": "EdgeZone", "name": PUBLIC_MEC_LOCATION},
            "ip_configurations": [ {
                "name": IP_CONFIG_NAME,
                "subnet": { "id": subnet_result.id },
                "public_ip_address": {"id": ip_address_result.id }
            }]
        }
    )
    
    nic_result = poller.result()
    
    print(f"Provisioned network interface client {nic_result.name}")
    
    # Step 6: Provision the virtual machine
    
    # Obtain the management object for virtual machines
    compute_client = ComputeManagementClient(credential, subscription_id)
    
    VM_NAME = "ExampleVM-edge"
    
    print(f"Provisioning virtual machine {VM_NAME}; this operation might take a few minutes.")
    
    # Provision the VM specifying only minimal arguments, which defaults to an Ubuntu 18.04 VM
    # on a Standard DSv2-series with a public IP address and a default virtual network/subnet.
    
    poller = compute_client.virtual_machines.begin_create_or_update(RESOURCE_GROUP_NAME, VM_NAME,
        {
            "location": LOCATION,
            "extendedLocation": {"type": "EdgeZone", "name": PUBLIC_MEC_LOCATION},
            "storage_profile": {
                "image_reference": {
                    "publisher": 'Canonical',
                    "offer": "UbuntuServer",
                    "sku": "18.04-LTS",
                    "version": "latest"
                }
            },
            "hardware_profile": {
                "vm_size": "Standard_DS2_v2"
            },
            "os_profile": {
                "computer_name": VM_NAME,
                "admin_username": USERNAME,
                "admin_password": PASSWORD
            },
            "network_profile": {
                "network_interfaces": [{
                    "id": nic_result.id,
                }]
            }
        }
    )
    
    vm_result = poller.result()
    
    print(f"Provisioned virtual machine {vm_result.name}")
    
  2. 在运行脚本之前,请填充脚本在步骤 1 部分中使用的这些变量:

    变量名称 说明
    LOCATION 与 Azure 公共 MEC 位置关联的 Azure 区域
    PUBLIC_MEC_LOCATION Azure 公共 MEC 位置标识符/edgezone ID
    PASSWORD 用于登录到 VM 的密码

    注意

    每个 Azure 公共 MEC 站点都与一个 Azure 区域相关联。 根据需要部署资源的 Azure 公共 MEC 位置,为要创建的资源组选择适当的区域值。 有关详细信息,请参阅 Azure 公共 MEC 的重要概念

在开发环境中运行脚本

  1. 运行从上一部分复制的 Python 脚本。

    python provision_vm_edge.py
    
  2. 等待几分钟,以创建 VM 和支持资源。

    以下示例输出表明 VM 创建操作已成功。

    (.venv) C:\Users >python provision_vm_edge.py
    Provisioning a virtual machine...some operations might take a minute or two.
    Provisioned resource group PythonAzureExample-VM-rg in the <region> region
    Provisioned virtual network python-example-vnet-edge with address prefixes ['10.1.0.0/16']
    Provisioned virtual subnet python-example-subnet-edge with address prefix 10.1.0.0/24
    Provisioned public IP address python-example-ip-edge with address <public ip>
    Provisioned network interface client python-example-nic-edge
    Provisioning virtual machine ExampleVM-edge; this operation might take a few minutes.
    Provisioned virtual machine ExampleVM-edge
    
  3. 在 python-example-ip-edge 字段的输出中,记下自己的 publicIpAddress。 在后续步骤中,使用此地址访问 VM。

在关联的区域中创建跳转服务器

若要使用 SSH 连接到 Azure 公共 MEC 中的 VM,最好的方法是在 Azure 区域中(上一节在其中部署了资源组)部署跳转盒。

  1. 按照使用 Azure 库配置虚拟机中的步骤操作。

  2. 记下跳转服务器 VM 的 python-example-ip 字段输出中你自己的 publicIpAddress。 在后续步骤中,使用此地址访问 VM。

访问 VM

  1. 使用 SSH 连接到部署在区域中的跳转盒 VM,其中包含之前记下的 IP 地址。

    ssh  azureuser@<python-example-ip>
    
  2. 通过跳转盒,使用 SSH 连接到在 Azure 公共 MEC 中创建的 VM,其中包含之前记下的 IP 地址。

    ssh azureuser@<python-example-ip-edge>
    
  3. 确保 Azure 网络安全组支持端口 22 访问所创建的 VM。

清理资源

在本教程中,你使用 Python SDK 在 Azure 公共 MEC 中创建了 VM。 如果将来不需要这些资源,请使用 az group delete 命令删除资源组、规模集和所有相关资源。 使用 --yes 参数,将在不提示确认的情况下删除资源。

az group delete --name PythonAzureExample-VM-rg --yes

后续步骤

有关 Azure 公共 MEC 的问题,请与产品团队联系: