تنظیم زمینه
شبکه شتابدهی یک ویژگی عالی است که توسط مایکروسافت برای افزایش عملکرد شبکه و استفاده از CPU برای ماشینهای مجازی Azure ارائه شده است. با این حال، برای چند سری و اندازه ماشین مجازی در دسترس است که در آن نیاز واقعی به عملکرد شبکه وجود دارد و آن ماشین های مجازی برای کارهای استفاده از CPU بالا در نظر گرفته شده اند.
در مقاله قبلی (به شبکههای شتابدهی برای ماشینهای مجازی Azure مراجعه کنید)، مفهوم شبکههای شتابدهی به همراه راهنمای گام به گام برای فعال کردن شبکههای شتابدهی با استفاده از پورتال Azure به طور عمیق توضیح داده شده است. این مقاله بیشتر به کد PowerShell می پردازد که به فعال کردن شبکه های شتاب دهنده برای ماشین های مجازی Azure کمک می کند. این مقاله را باید به عنوان ادامه مقاله قبلی دانست و توصیه می شود مقاله قبلی را مرور کنید و سپس وارد این مقاله شوید.
با استفاده از PowerShell، شبکه شتابدهی را برای ماشینهای مجازی Azure فعال کنید
در دنیای واقعی، بیشتر استقرارهای Azure برای مشتری با استفاده از اسکریپت PowerShell انجام می شود. از این رو، داشتن درک عمیق از استقرار منابع Azure با استفاده از PowerShell بسیار ضروری است. و در اینجا Accelerated Networking برای ماشین های مجازی با استفاده از اسکریپت PowerShell فعال می شود.
اجازه دهید با ایجاد یک مدیر منابع و تمام منابع لازم برای ماشین مجازی Azure شروع کنیم و سپس یک ماشین مجازی با فعال بودن شبکه شتابدهی ایجاد کنیم.
اجازه دهید با ورود به Azure شروع کنیم. با اجرای کد پاورشل زیر یک پاپ آپ باز می شود و از کلید زدن اعتبارنامه ها می خواهد و با ارائه اعتبارنامه صحیح وارد Azure می شود.
- # Log In to Azure
- LogIn-AzureRMAccount
حال، اجازه دهید یک Resource Manager ایجاد کنیم. زیر کد PowerShell یک مدیر منابع به نام rg-demo-accnetworking در مکان eastus ایجاد می کند.
- # Create Resource Group
- New-AzureRmResourceGroup -Name "rg-demo-accnetworking" -Location "eastus"
حال، اجازه دهید یک شبکه مجازی به نام vnet-demo-accnetworking در مکان eastus با فضای آدرس IP به عنوان 10.8.0.0/16 ایجاد کنیم. این یک زیرشبکه به نام subnet-demo-accnetworking خواهد داشت که فضای آدرس آن 10.8.1.0/24 است.
- # Create subnet configuration
- $subnet = New-AzureRmVirtualNetworkSubnetConfig -Name "subnet-demo-accnetworking" -AddressPrefix "10.8.1.0/24"
- # Create Virtual Network along with subnet
- # Splat all parameters together for better readability. We are bundling all parameters
- # for the command together to adhere to PowerShell coding best practices and have a better
- # code readability
- $paramVNet = @{
- ResourceGroupName = "rg-demo-accnetworking"
- Location = "eastus"
- Name = "vnet-demo-accnetworking"
- AddressPrefix = "10.8.0.0/16"
- Subnet = $subnet
- }
- $vnet = New-AzureRmVirtualNetwork @paramVNet
یک قانون امنیت شبکه ایجاد کنید که امکان اتصال RDP را فراهم می کند. یک پیکربندی قانون امنیت شبکه با نام rg-demo-nsg-allow-rdp ایجاد میشود و یک گروه امنیت شبکه nsg-demo-accnetworking با پیکربندی قانون امنیت شبکه ایجاد میشود.
- # Create Network security rule configuration that will allow RDP connection
- # Splat all parameters together for better readability. We are bundling all parameters
- # for the command together to adhere to PowerShell coding best practices and have a better
- # code readability
- $paramNetworkSecurityGroupRuleConfig = @{
- Name = "rg-demo-nsg-allow-rdp"
- Description = "Enable RDP connection"
- Access = "Allow"
- Protocol = "TCP"
- Direction = "Inbound"
- Priority = 100
- SourceAddressPrefix = "*"
- SourcePortRange = "*"
- DestinationAddressPrefix = "*"
- DestinationPortRange = 3389
- }
- $rdpConfig = New-AzureRmNetworkSecurityRuleConfig @paramNetworkSecurityGroupRuleConfig
- # Create Network security group configuration with security rule configuration defined previously
- # Splat all parameters together for better readability. We are bundling all parameters
- # for the command together to adhere to PowerShell coding best practices and have a better
- # code readability
- $paramNetworkSecurityGroup =@{
- Name = "nsg-demo-accnetworking"
- ResourceGroupName="rg-demo-accnetworking"
- Location = "eastus"
- SecurityRules = $rdpConfig
- }
- $networkSecurityGroup = New-AzureRmNetworkSecurityGroup @paramNetworkSecurityGroup
زیرشبکه مجازی را با گروه امنیت شبکه تنظیم کنید.
- # Set Virtual Network Subnet with Network security group
- # Splat all parameters together for better readability. We are bundling all parameters
- # for the command together to adhere to PowerShell coding best practices and have a better
- # code readability
- $paramNetworkSecurityGroupConfig =@{
- Name = "subnet-demo-accnetworking"
- VirtualNetwork=$vnet
- AddressPrefix = "10.8.1.0/24"
- NetworkSecurityGroup = $networkSecurityGroup
- }
- Set-AzureRmVirtualNetworkSubnetConfig @paramNetworkSecurityGroupConfig
ایجاد یک آدرس IP پویا ip-demo-accnetworking. یک کارت رابط شبکه با فعال بودن شبکه شتاب ایجاد کنید و IP ایجاد شده را به کارت رابط شبکه اختصاص دهید.
- # Create a dynamic IP
- # Splat all parameters together for better readability. We are bundling all parameters
- # for the command together to adhere to PowerShell coding best practices and have a better
- # code readability
- $paramIp = @{
- ResourceGroupName = "rg-demo-accnetworking"
- Name = "ip-demo-accnetworking"
- Location = "eastus"
- AllocationMethod = "Dynamic"
- }
- $ip = New-AzureRmPublicIpAddress @paramIp
- # Create a Network card with Accelerated Networking Enabled and in the same subnet created earlier
- # with the created IP address
- # Splat all parameters together for better readability. We are bundling all parameters
- # for the command together to adhere to PowerShell coding best practices and have a better
- # code readability
- $paramNetworkCard = @{
- ResourceGroupName = "rg-demo-accnetworking"
- Name = "nic-demo-accnetworking"
- Location = "eastus"
- SubnetId = $vnet.Subnets[0].Id
- PublicIpAddressId = $ip.Id
- EnableAcceleratedNetworking = $true
- }
- $nic = New-AzureRmNetworkInterface @paramNetworkCard
یک ماشین مجازی با سیستم عامل مورد نیاز و اندازه VM ایجاد کنید. کارت شبکه ایجاد شده را با شبکه شتاب دهنده فعال شده با ماشین مجازی مرتبط کنید. ایجاد یک ماشین مجازی با سیستم عامل پشتیبانی شده و اندازه ماشین مجازی بسیار ضروری است، در غیر این صورت شتاب شبکه فعال نمی شود.
- # Get credentials for Virtual Machines. It will prompt to key in user name and password
- # to be set for the admin account of Virtual Machine
- $cred = Get-Credential
- # Set size of Virtual Machine
- $config = New-AzureRmVMConfig -VMName "vm-demo-accnt" -VMSize "Standard_DS4_v2"
- # Set Operating System of Virtual Machine
- # Splat all parameters together for better readability. We are bundling all parameters
- # for the command together to adhere to PowerShell coding best practices and have a better
- # code readability
- $paramVMConfig = @{
- VM = $config
- Windows = $true
- ComputerName = "vm-demo-accnt"
- Credential = $cred
- }
- $config = Set-AzureRmVMOperatingSystem @paramVMConfig
- # Set Operating System Image of the Virtual Machine
- # Splat all parameters together for better readability. We are bundling all parameters
- # for the command together to adhere to PowerShell coding best practices and have a better
- # code readability
- $paramVMConfig = @{
- VM = $config
- PublisherName = "MicrosoftWindowsServer"
- Skus = "2016-Datacenter"
- Offer = "WindowsServer"
- Version = "Latest"
- }
- $config = Set-AzureRmVMSourceImage @paramVMConfig
- # Associate Network Interface Card created with Accelerated Networking eanbled with the Virtual machine
- $vmConfig = Add-AzureRmVMNetworkInterface -VM $config -Id $nic.Id
- # Create Azure Virtual Machine
- New-AzureRmVM -VM $vmConfig -ResourceGroupName "rg-demo-accnetworking" -Location "eastus"
بسته شدن
کد PowerShell مورد بحث پیوست شده است. به عنوان یک پیش نیاز، PowerShell 5.1 به همراه دستورات Azure Powershell باید قبل از اجرای این اسکریپت نصب شود. این اسکریپت باید با مجوزهای Administrator اجرا شود.