分享方式:


教學課程:使用 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 識別碼
    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-edge 欄位的輸出中,記下您自己的 publicIpAddress。 使用這個位址來存取下一節中的 VM。

存取 VM

  1. 使用 SSH 連線到您利用您先前所記錄的 IP 位址部署在區域中的跳躍方塊 VM。

    ssh  azureuser@<python-example-ip>
    
  2. 從跳躍方塊中,使用 SSH 連線到您利用您先前所記錄的 IP 位址在 Azure 公用 MEC 中建立的 VM。

    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 的相關問題,請連絡產品小組: