教程:使用 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 区域
    公共MEC位置 Azure 公共 MEC 位置标识符/边缘区 ID
    密码 用于登录到 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 字段的输出中,记下您自己的公网 IP 地址。 使用此地址访问下一部分中的 VM。

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

若要使用 SSH 连接到 Azure 公共 MEC 中的 VM,最佳方法是在上一部分部署资源组的 Azure 区域中部署跳转框。

  1. 请按照 中“使用 Azure 库来预配虚拟机”的步骤进行操作。

  2. 在跳转服务器 VM 的 python-example-ip 字段的输出中记下您自身的公网 IP 地址。 使用此地址访问下一部分中的 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 的问题,请联系产品团队: