Aracılığıyla paylaş


Öğretici: Python SDK'sını kullanarak Azure genel MEC'de sanal makine dağıtma

Bu öğreticide, Azure genel çoklu erişimli uç işlemde (MEC) kaynakları dağıtmak için Python SDK'sını kullanacaksınız. Öğretici, Azure genel MEC'de bir sanal makine (VM) ve bağımlılıklarını dağıtmak için Python kodu sağlar.

Python SDK'ları hakkında bilgi için bkz. Python kullanım desenleri için Azure kitaplıkları.

Bu öğreticide şunların nasıl yapıldığını öğreneceksiniz:

  • Gerekli Azure kitaplık paketlerini yükleme
  • Sanal makine sağlama
  • Betiği geliştirme ortamınızda çalıştırma
  • İlişkili bölgede atlama sunucusu oluşturma
  • VM'lere erişme

Ön koşullar

Gerekli Azure kitaplık paketlerini yükleme

  1. Bu örnekte kullanılan yönetim kitaplıklarını listeleyen requirements.txt adlı bir dosya oluşturun.

    azure-mgmt-resource
    azure-mgmt-compute
    azure-mgmt-network
    azure-identity
    azure-mgmt-extendedlocation==1.0.0b2
    
  2. Sanal ortamın etkinleştirildiği bir komut istemi açın ve requirements.txt'de listelenen yönetim kitaplıklarını yükleyin.

    pip install -r requirements.txt
    

Sanal makine sağlama

  1. provision_vm_edge.py adlı bir Python dosyası oluşturun ve bunu aşağıdaki Python betiğiyle doldurun. Betik, VM'yi ve ilişkili bağımlılığını Azure genel MEC'de dağıtır. Betikteki açıklamalar ayrıntıları açıklar.

    # 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. Betiği çalıştırmadan önce betiğin 1. adımında kullanılan değişkenleri doldurun:

    Değişken adı Açıklama
    KONUM Azure genel MEC konumuyla ilişkili Azure bölgesi
    PUBLIC_MEC_LOCATION Azure genel MEC konum tanımlayıcısı/edgezone kimliği
    PAROLA VM'de oturum açmak için kullanılacak parola

    Not

    Her Azure genel MEC sitesi bir Azure bölgesiyle ilişkilendirilir. Kaynağın dağıtılması gereken Azure genel MEC konumuna bağlı olarak, oluşturulacak kaynak grubu için uygun bölge değerini seçin. Daha fazla bilgi için bkz . Azure genel MEC için temel kavramlar.

Betiği geliştirme ortamınızda çalıştırma

  1. Önceki bölümden kopyaladığınız Python betiğini çalıştırın.

    python provision_vm_edge.py
    
  2. VM'nin ve destekleyici kaynakların oluşturulması için birkaç dakika bekleyin.

    Aşağıdaki örnekte VM oluşturma işleminin başarılı olduğu gösterilmektedir.

    (.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 alanının çıktısında kendi publicIpAddress değerinizi not edin. Sonraki bölümde VM'ye erişmek için bu adresi kullanın.

İlişkili bölgede atlama sunucusu oluşturma

SSH kullanarak Azure genel MEC'deki VM'ye bağlanmak için en iyi yöntem, kaynak grubunuzun önceki bölümde dağıtıldığı Azure bölgesinde bir atlama kutusu dağıtmaktır.

  1. Sanal makine sağlamak için Azure kitaplıklarını kullanma bölümündeki adımları izleyin.

  2. Atlama sunucusu VM'sinin python-example-ip alanındaki çıktıda kendi publicIpAddress değerinizi not edin. Sonraki bölümde VM'ye erişmek için bu adresi kullanın.

VM'lere erişme

  1. Bölgede dağıtmış olduğunuz atlama kutusu VM'sine daha önce not ettiğiniz IP adresiyle bağlanmak için SSH kullanın.

    ssh  azureuser@<python-example-ip>
    
  2. Atlama kutusundan SSH kullanarak Azure genel MEC'de oluşturduğunuz VM'ye daha önce not ettiğiniz IP adresiyle bağlanın.

    ssh azureuser@<python-example-ip-edge>
    
  3. Azure ağ güvenlik gruplarının 22 numaralı bağlantı noktasının oluşturduğunuz VM'lere erişmesine izin verin.

Kaynakları temizleme

Bu öğreticide, Python SDK'sını kullanarak Azure genel MEC'de bir VM oluşturdunuz. Gelecekte bu kaynaklara ihtiyaç duymayabilirsiniz, az group delete komutunu kullanarak kaynak grubunu, ölçek kümesini ve tüm ilgili kaynakları kaldırın. parametresi kullanıldığında --yes , onay istemi olmadan kaynaklar silinir.

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

Sonraki adımlar

Azure genel MEC hakkında sorular için ürün ekibine başvurun: