我在尝试通过Azure API for python在Azure上创建spot示例时遇到了RDP访问问题

l5tcr1uw  于 2023-04-07  发布在  Python
关注(0)|答案(1)|浏览(118)

我试图通过python代码从azure API在azure上创建一个spot示例。下面的代码成功地在Azure上创建了spot示例机器,但当我尝试通过RDP连接时,API中给出的凭据不起作用。

import secrets
from datetime import datetime

from azure.identity import ClientSecretCredential
from azure.mgmt.compute import ComputeManagementClient

from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.compute.models import (
    VirtualMachine,
    HardwareProfile,
    StorageProfile,
    InstanceViewTypes,
    OSDisk,
    ManagedDiskParameters,
    DiskCreateOptionTypes,
    OSProfile,
    NetworkProfile,
    NetworkInterfaceReference,
    VirtualMachinePriorityTypes,
    VirtualMachineEvictionPolicyTypes,
    BillingProfile
)
from azure.mgmt.network.models import PublicIPAddress, IPAllocationMethod, NetworkSecurityGroup, SecurityRule
from constance import config

from instance.client_utils.ssh_client import SSHClient
from instance.models import VMInstance
from syntric_cr.settings import (
    AZURE_CLIENT_ID,
    AZURE_CLIENT_SECRET,
    AZURE_TENANT,
    AZURE_SUBSCRIPTION_ID,
    AZURE_CAPTURE_IMAGE,
    AZURE_RESOURCE_GROUP,
    AZURE_RDP_USERNAME,
    AZURE_LOCATION, AZURE_EXISTING_NIC_NAME, AZURE_EXISTING_NSG_NAME,
)

def create_spot_instance(self):
        current_timestamp = round(datetime.timestamp(datetime.now()))

        vm_name = f"spot-{current_timestamp}"

        vm_username = AZURE_RDP_USERNAME
        vm_password = secrets.token_urlsafe(13)
        vm_location = AZURE_LOCATION
        vm_image_id = self.get_image_id(AZURE_RESOURCE_GROUP, AZURE_CAPTURE_IMAGE)
        new_nic = self.creating_nic_and_ip_from_existing_vm(vm_name, vm_location)

        vm_params = VirtualMachine(
            location=vm_location,
            hardware_profile=HardwareProfile(vm_size=config.AZURE_VIRTUAL_MACHINE_SIZE),
            storage_profile=StorageProfile(
                image_reference={'id': vm_image_id},
                os_disk=OSDisk(
                    name=vm_name,
                    create_option=DiskCreateOptionTypes.from_image,
                    managed_disk=ManagedDiskParameters(storage_account_type='Standard_LRS')
                )
            ),
            os_profile=OSProfile(
                computer_name=vm_name,
                admin_username='hello',
                admin_password='hello_123'
            ),
            network_profile=NetworkProfile(
                network_interfaces=[
                    NetworkInterfaceReference(id=new_nic.id)
                ]
            ),
            priority=VirtualMachinePriorityTypes.spot,
            eviction_policy=VirtualMachineEvictionPolicyTypes.deallocate,
            billing_profile=BillingProfile(max_price=config.AZURE_MAX_SPOT_INSTANCE_BUDGET)
        )

        try:
            self.service.virtual_machines.begin_create_or_update(AZURE_RESOURCE_GROUP, vm_name, vm_params)
            return VMInstance.objects.create(
                instance=vm_name, project=AZURE_RESOURCE_GROUP, provider='AZURE', status=False,
                script_run_on_start=False, username=vm_username, password=vm_password)
        except Exception as ex:
            return

创建spot示例时,用户名'hello'和密码'hello_123'不适用于RDP访问。我必须手动重置密码才能使其工作
我尝试了上面的代码,并从官方Azure文档中复制了此代码

uxhixvfz

uxhixvfz1#

创建spot示例时,用户名'hello'和密码'hello_123'不适用于RDP访问。我必须手动重置密码才能使其工作
默认情况下,由于部署Azure VM时的用户名和密码限制,不支持hello_123作为密码,我尝试使用hello_123密码创建Spot VM,由于命名约定和警告-该值必须在12到123个字符之间,请参阅以下内容:-

我尝试使用Python SDK创建一个Spot虚拟机,遵循本文档中提到的用户名和密码命名约定:-
**用户名-**有关Azure中Windows VM的常见问题解答- Azure虚拟机|微软学习
**密码-**有关Azure中Windows VM的常见问题解答- Azure虚拟机|微软学习
部署SpotVM的Python代码:-

# from azure.common.credentials import ServicePrincipalCredentials

from  azure.identity  import  DefaultAzureCredential

from  azure.mgmt.compute.v2019_07_01  import  ComputeManagementClient

from  azure.mgmt.compute.v2019_07_01.models  import  VirtualMachinePriorityTypes, VirtualMachineEvictionPolicyTypes, BillingProfile

SUBSCRIPTION_ID = '<subscription-id>'

GROUP_NAME = '<resource-group-name>'

LOCATION = 'Australia East'

VM_NAME = '<vm-name>'

credentials = DefaultAzureCredential()

compute_client = ComputeManagementClient(

credentials,

SUBSCRIPTION_ID

)

vm_parameters = {

'location': LOCATION,

'os_profile': {

'computer_name': VM_NAME,

'admin_username': '<username-accroding-to-naming-convention>',

'admin_password': '<password-according-to-naming-convention>'

},

'hardware_profile': {

'vm_size': 'Standard_D2s_v3'

},

'storage_profile': {

'image_reference': {

'publisher': 'MicrosoftWindowsServer',

'offer': 'WindowsServer',

'sku': '2019-Datacenter',

'version': 'latest'

}

},

'network_profile': {

'network_interfaces': [{

'id': "nic-id"

}]

},

'priority': VirtualMachinePriorityTypes.spot, # use Azure spot intance

'eviction_policy': VirtualMachineEvictionPolicyTypes.deallocate, # For Azure Spot virtual machines, the only supported value is 'Deallocate'

'billing_profile': BillingProfile(max_price=float(2))

}

  
  

creation_result = compute_client.virtual_machines.begin_create_or_update(

GROUP_NAME,

VM_NAME,

vm_parameters

)

print(creation_result.result())

输出:-

在Portal上创建了虚拟机我将NSG和公共IP连接到虚拟机尝试使用代码中提供的凭据将RDP连接到虚拟机,结果成功,请参阅以下内容:-

RDP:-

参考号:-

Azure python sdk, how to deploy a vm and it's a Azure Spot instance - Stack Overflow By Jim Xu

相关问题