安装软件并设置教程所需资源Install software and set up resources for the tutorial

本教程由三部分组成,将带你创建机器学习模型来预测铁矿石杂质二氧化硅的百分比,然后在 Azure SQL Edge 中部署该模型。In this three-part tutorial, you'll create a machine learning model to predict iron ore impurities as a percentage of Silica, and then deploy the model in Azure SQL Edge. 在第一部分中,你将安装所需的软件并部署 Azure 资源。In part one, you'll install the required software and deploy Azure resources.

先决条件Prerequisites

  1. 如果没有 Azure 订阅,请创建一个试用帐户If you don't have an Azure subscription, create a trial account.
  2. 安装 Python 3.6.8Install Python 3.6.8.
    • 使用 Windows x86-x64 可执行安装程序Use the Windows x86-x64 executable installer
    • python.exe 添加到 PATH 环境变量 downloads/)。Add python.exe to the PATH environment variable downloads/). 可以在“针对 Visual Studio 2019 的工具”下找到下载内容。You can find the download under "Tools For Visual Studio 2019".
  3. 安装 Microsoft ODBC Driver 17 for SQL ServerInstall Microsoft ODBC Driver 17 for SQL Server.
  4. 安装 Azure Data StudioInstall Azure Data Studio
  5. 打开 Azure Data Studio 并为笔记本配置 Python。Open Azure Data Studio and configure Python for notebooks. 有关详细信息,请参阅为 Notebook 配置 Python。此步骤可能需要几分钟的时间。 For details, see Configure Python for Notebook.This step can take several minutes.
  6. 安装最新版本的 Azure CLIInstall the latest version of Azure CLI. 以下脚本要求 AZ PowerShell 是最新版本(3.5.0,2020 年 2 月)。The following scripts require that AZ PowerShell be the latest version (3.5.0, Feb 2020).
  7. 下载将在教程中使用的 DACPACAMD/ARM Docker 映像文件Download the DACPAC and AMD/ARM Docker Image files that will be utilized in the tutorial.

使用 PowerShell 脚本部署 Azure 资源Deploy Azure resources using PowerShell Script

部署本 Azure SQL Edge 教程所需的 Azure 资源。Deploy the Azure resources required by this Azure SQL Edge tutorial. 可以使用 PowerShell 脚本或通过 Azure 门户进行部署。These can be deployed either by using a PowerShell script or through the Azure portal. 本教程使用 PowerShell 脚本。This tutorial uses a PowerShell script.

  1. 导入在本教程中运行 PowerShell 脚本所需的模块。Import the modules needed to run the PowerShell script in this tutorial.

    Import-Module Az.Accounts -RequiredVersion 1.7.3
    Import-Module -Name Az -RequiredVersion 3.5.0
    Import-Module Az.IotHub -RequiredVersion 2.1.0
    Import-Module Az.Compute -RequiredVersion 3.5.0
    az extension add --name azure-iot
    az extension add --name azure-cli-ml
    

    备注

    本文使用最新版本的 Azure IoT 扩展(称为 azure-iot)。This article uses the newest version of the Azure IoT extension, called azure-iot. 旧版本称为 azure-iot-cli-ext。只应同时安装有一个版本。The legacy version is called azure-iot-cli-ext.You should only have one version installed at a time. 可以使用命令 az extension list 来验证当前安装的扩展。You can use the command az extension list to validate the currently installed extensions.

    使用 az extension remove --name azure-cli-iot-ext 可删除扩展的旧版本。Use az extension remove --name azure-cli-iot-ext to remove the legacy version of the extension.

    使用 az extension add --name azure-iot 可添加扩展的新版本。Use az extension add --name azure-iot to add the new version of the extension.

    若要查看已安装了哪些扩展,请使用 az extension listTo see what extensions you have installed, use az extension list.

  2. 声明 PowerShell 脚本所需的变量。Declare the variables required by the PowerShell script.

    $ResourceGroup = "<name_of_the_resource_group>"
    $IoTHubName = "<name_of_the_IoT_hub>"
    $location = "<location_of_your_Azure_Subscription>"
    $SubscriptionName = "<your_azure_subscription>"
    $NetworkSecGroup = "<name_of_your_network_security_group>"
    $StorageAccountName = "<name_of_your_storage_account>"
    
  3. 声明其余变量。Declare the rest of the variables.

    $IoTHubSkuName = "S1"
    $IoTHubUnits = 4
    $EdgeDeviceId = "IronOrePredictionDevice"
    $publicIpName = "VMPublicIP"
    $imageOffer = "iot_edge_vm_ubuntu"
    $imagePublisher = "microsoft_iot_edge"
    $imageSku = "ubuntu_1604_edgeruntimeonly"
    $AdminAcc = "iotadmin"
    $AdminPassword = ConvertTo-SecureString "IoTAdmin@1234" -AsPlainText -Force
    $VMSize = "Standard_DS3"
    $NetworkName = "MyNet"
    $NICName = "MyNIC"
    $SubnetName = "MySubnet"
    $SubnetAddressPrefix = "10.0.0.0/24"
    $VnetAddressPrefix = "10.0.0.0/16"
    $MyWorkSpace = "SQLDatabaseEdgeDemo"
    $containerRegistryName = $ResourceGroup + "ContRegistry"
    
  4. 要开始创建资产,请登录到 Azure。To begin creating assets, log in to Azure.

    Login-AzAccount -Environment AzureChinaCloud
    
    az login
    
  5. 设置 Azure 订阅 ID。Set the Azure subscription ID.

    Select-AzSubscription -Subscription $SubscriptionName
    az account set --subscription $SubscriptionName
    
  6. 创建资源组(如果尚不存在)。Create the resource group if it doesn't already exist.

    $rg = Get-AzResourceGroup -Name $ResourceGroup
    if($rg -eq $null)
    {
        Write-Output("Resource Group $ResourceGroup does not exist, creating Resource Gorup")
        New-AzResourceGroup -Name $ResourceGroup -Location $location
    }
    else
    {
        Write-Output ("Resource Group $ResourceGroup exists")
    }
    
  7. 在资源组中创建存储帐户和存储帐户容器。Create the storage account and storage account container in the resource group.

    $sa = Get-AzStorageAccount -ResourceGroupName $ResourceGroup -Name $StorageAccountName 
    if ($sa -eq $null)
    {
        New-AzStorageAccount -ResourceGroupName $ResourceGroup -Name $StorageAccountName -SkuName Standard_LRS -Location $location -Kind Storage
        $sa = Get-AzStorageAccount -ResourceGroupName $ResourceGroup -Name $StorageAccountName 
        $storagekey = Get-AzStorageAccountKey -ResourceGroupName $ResourceGroup -Name $StorageAccountName 
        $storageContext = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $storagekey[0].Value
        New-AzStorageContainer -Name "sqldatabasedacpac" -Context $storageContext 
    }
    else
    {
       Write-Output ("Storage Account $StorageAccountName exists in Resource Group $ResourceGroup")     
    }
    
  8. 将数据库 dacpac 文件上传到存储帐户,并为 blob 生成 SAS URL。Upload the database dacpac file to the storage account and generate a SAS URL for the blob. 记下数据库 dacpac blob 的 SAS URL。Make a note of the SAS URL for the database dacpac blob.

    $file = Read-Host "Please Enter the location to the zipped Database DacPac file:"
    Set-AzStorageBlobContent -File $file -Container "sqldatabasedacpac" -Blob "SQLDatabasedacpac.zip" -Context $sa.Context
    $DacpacFileSASURL = New-AzStorageBlobSASToken -Container "sqldatabasedacpac" -Blob "SQLDatabasedacpac.zip" -Context $sa.Context -Permission r -StartTime (Get-Date).DateTime -ExpiryTime (Get-Date).AddMonths(12) -FullUri
    
  9. 在本资源组中创建 Azure 容器注册表。Create an Azure container registry within this resource group.

    $containerRegistry = Get-AzContainerRegistry -ResourceGroupName $ResourceGroup -Name $containerRegistryName 
    if ($containerRegistry -eq $null)
    {
        New-AzContainerRegistry -ResourceGroupName $ResourceGroup -Name $containerRegistryName -Sku Standard -Location $location -EnableAdminUser 
        $containerRegistry = Get-AzContainerRegistry -ResourceGroupName $ResourceGroup -Name $containerRegistryName 
    }
    else
    {
        Write-Output ("Container Registry $containerRegistryName exists in Resource Group $ResourceGroup")
    }
    
  10. 将 ARM/AMD docker 映像推送到容器注册表。Push the ARM/AMD docker images to the container registry.

    $containerRegistryCredentials = Get-AzContainerRegistryCredential -ResourceGroupName $ResourceGroup -Name $containerRegistryName
    
    $amddockerimageFile = Read-Host "Please Enter the location to the amd docker tar file:"
    $armdockerimageFile = Read-Host "Please Enter the location to the arm docker tar file:"
    $amddockertag = $containerRegistry.LoginServer + "/silicaprediction" + ":amd64"
    $armdockertag = $containerRegistry.LoginServer + "/silicaprediction" + ":arm64"
    
    docker login $containerRegistry.LoginServer --username $containerRegistryCredentials.Username --password $containerRegistryCredentials.Password
    
    docker import $amddockerimageFile $amddockertag
    docker push $amddockertag
    
    docker import $armdockerimageFile $armdockertag
    docker push $armdockertag
    
  11. 在资源组中创建网络安全组。Create the network security group within the resource group.

    $nsg = Get-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroup -Name $NetworkSecGroup 
    if($nsg -eq $null)
    {
        Write-Output("Network Security Group $NetworkSecGroup does not exist in the resource group $ResourceGroup")
    
        $rule1 = New-AzNetworkSecurityRuleConfig -Name "SSH" -Description "Allow SSH" -Access Allow -Protocol Tcp -Direction Inbound -Priority 100 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 22
        $rule2 = New-AzNetworkSecurityRuleConfig -Name "SQL" -Description "Allow SQL" -Access Allow -Protocol Tcp -Direction Inbound -Priority 101 -SourceAddressPrefix Internet -SourcePortRange * -DestinationAddressPrefix * -DestinationPortRange 1600
        New-AzNetworkSecurityGroup -Name $NetworkSecGroup -ResourceGroupName $ResourceGroup -Location $location -SecurityRules $rule1, $rule2
    
        $nsg = Get-AzNetworkSecurityGroup -ResourceGroupName $ResourceGroup -Name $NetworkSecGroup
    }
    else
    {
        Write-Output ("Network Security Group $NetworkSecGroup exists in the resource group $ResourceGroup")
    }
    
  12. 创建启用 SQL Edge 的 Azure 虚拟机。Create an Azure virtual machine enabled with SQL Edge. 此 VM 将充当 Edge 设备。This VM will act as an Edge device.

    $AzVM = Get-AzVM -ResourceGroupName $ResourceGroup -Name $EdgeDeviceId
    If($AzVM -eq $null)
    {
        Write-Output("The Azure VM with Name- $EdgeVMName is not present in the Resource Group- $ResourceGroup ")
    
        $SingleSubnet = New-AzVirtualNetworkSubnetConfig -Name $SubnetName -AddressPrefix $SubnetAddressPrefix
        $Vnet = New-AzVirtualNetwork -Name $NetworkName -ResourceGroupName $ResourceGroup -Location $location -AddressPrefix $VnetAddressPrefix -Subnet $SingleSubnet
        $publicIp = New-AzPublicIpAddress -Name $publicIpName -ResourceGroupName $ResourceGroup -AllocationMethod Static -Location $location  
        $NIC = New-AzNetworkInterface -Name $NICName -ResourceGroupName $ResourceGroup -Location $location -SubnetId $Vnet.Subnets[0].Id -NetworkSecurityGroupId $nsg.Id -PublicIpAddressId $publicIp.Id
    
        ##Set-AzNetworkInterfaceIpConfig -Name "ipconfig1"  -NetworkInterface $NIC -PublicIpAddress $publicIp
    
        $Credential = New-Object System.Management.Automation.PSCredential ($AdminAcc, $AdminPassword);
    
        $VirtualMachine = New-AzVMConfig -VMName $EdgeDeviceId -VMSize $VMSize
        $VirtualMachine = Set-AzVMOperatingSystem -VM $VirtualMachine -Linux -ComputerName $EdgeDeviceId -Credential $Credential
        $VirtualMachine = Add-AzVMNetworkInterface -VM $VirtualMachine -Id $NIC.Id  
        $VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -PublisherName $imagePublisher -Offer $imageOffer -Skus $imageSku -Version latest 
        $VirtualMachine = Set-AzVMPlan -VM $VirtualMachine -Name $imageSku -Publisher $imagePublisher -Product $imageOffer
    
        $AzVM = New-AzVM -ResourceGroupName $ResourceGroup -Location $location -VM $VirtualMachine -Verbose
        $AzVM = Get-AzVM -ResourceGroupName $ResourceGroup -Name $EdgeDeviceId
    
    }
    else
    {
        Write-Output ("The Azure VM with Name- $EdgeDeviceId is present in the Resource Group- $ResourceGroup ")
    }
    
  13. 在资源组中创建 IoT 中心。Create an IoT hub within the resource group.

    $iotHub = Get-AzIotHub -ResourceGroupName $ResourceGroup -Name $IoTHubName
    If($iotHub -eq $null)
    {
        Write-Output("IoTHub $IoTHubName does not exists, creating The IoTHub in the resource group $ResourceGroup")
        New-AzIotHub -ResourceGroupName $ResourceGroup -Name $IoTHubName -SkuName $IoTHubSkuName -Units $IoTHubUnits -Location $location -Verbose
    }
    else
    {
        Write-Output ("IoTHub $IoTHubName present in the resource group $ResourceGroup") 
    }
    
  14. 将 Edge 设备添加到 IoT 中心。Add an Edge device to the IoT hub. 此步骤仅创建设备数字标识。This step only creates the device digital identity.

    $deviceIdentity = Get-AzIotHubDevice -ResourceGroupName $ResourceGroup -IotHubName $IoTHubName -DeviceId $EdgeDeviceId
    If($deviceIdentity -eq $null)
    {
        Write-Output("The Edge Device with DeviceId- $EdgeDeviceId is not registered to the IoTHub- $IoTHubName ")
        Add-AzIotHubDevice -ResourceGroupName $ResourceGroup -IotHubName $IoTHubName -DeviceId $EdgeDeviceId -EdgeEnabled  
    }
    else
    {
        Write-Output ("The Edge Device with DeviceId- $EdgeDeviceId is registered to the IoTHub- $IoTHubName")
    }
    $deviceIdentity = Get-AzIotHubDevice -ResourceGroupName $ResourceGroup -IotHubName $IoTHubName -DeviceId $EdgeDeviceId
    
  15. 获取设备主连接字符串。Get the device primary connection string. 这将是 VM 稍后需要的。This will be needed later for the VM. 以下命令使用 Azure CLI 进行部署。The following command uses Azure CLI for deployments.

    $deviceConnectionString = az iot hub device-identity show-connection-string --device-id $EdgeDeviceId --hub-name $IoTHubName --resource-group $ResourceGroup --subscription $SubscriptionName
    $connString = $deviceConnectionString[1].Substring(23,$deviceConnectionString[1].Length-24)
    $connString
    
  16. 更新 Edge 设备上 IoT Edge 配置文件中的连接字符串。Update the connection string in the IoT Edge configuration file on the Edge device. 以下命令使用 Azure CLI 进行部署。The following commands use Azure CLI for deployments.

    $script = "/etc/iotedge/configedge.sh '" + $connString + "'"
    az vm run-command invoke -g $ResourceGroup -n $EdgeDeviceId  --command-id RunShellScript --script $script
    
  17. 在资源组中创建 Azure 机器学习工作区。Create an Azure Machine Learning workspace within the resource group.

    az ml workspace create -w $MyWorkSpace -g $ResourceGroup
    

后续步骤Next Steps