Azure PowerShellで、仮想マシン V2を作成してみます。現在のAzure PowerShellには、
- サービスマネジメントモード(ASM)
- リソースマネージャーモード(ARM)
の2つがあります。
それらの切り替えは、Switch-AzureMode コマンドレットで行いますが、以下の通り今後廃止予定です。 現状は、同じ名前で動きが異なるコマンドがあったりして、色々混乱したりしているので、モードを統合しつつ、一部コマンド名が変更されるような感じみたいです。
PS C:\> Switch-AzureMode -Name AzureResourceManager 警告: The Switch-AzureMode cmdlet is deprecated and will be removed in a future release.
VMイメージの取得
ASMでは Get-AzureVMImage で一覧がでますが、ARMのGet-AzureVMImageでは一覧がでません。 そしてARMのVMイメージには、
- Publisher name
- SKU
- Version
と属性があるので、これらを指定しないと最終的なイメージ名を取得できません。(もっと楽チンな方法がないものですかね?)
結論から言うと、以下のように指定すればWindows Server 2012 R2のイメージが取得できます。
Get-AzureVMImageDetail -Location "West US" ` -PublisherName "MicrosoftWindowsServer" ` -Offer "WindowsServer"-Skus "2012-R2-Datacenter" -Version "4.0.201504"
とはいえ、Publisher name /SKU/Version ってどうやって取得するですか?何があるんですか?という感じです。これらを取得するには、別のコマンドレットを使います。
Publisher Name
Publisher name は、Get-AzureVMImagePublisherコマンドレット使います。多数表示されますので、Microsoftに限って表示すると以下のようになります。引数にはLocationが必要です。Locationの空白は削除しても構いません(ソースみるとそうなってました)
> Get-AzureVMImagePublisher -Location japanwest | Where-Object { $_.PublisherName -like "Microsoft*" } | Ft publishername PublisherName ------------- Microsoft Microsoft.Azure.Applications Microsoft.Azure.Backup.Test Microsoft.Azure.Diagnostics Microsoft.Azure.Extensions Microsoft.Azure.RecoveryServices Microsoft.Azure.Security Microsoft.Azure.Security.Internal Microsoft.Azure.ServiceFabric.Test Microsoft.Azure.WindowsFabric.Test Microsoft.AzureCAT.AzureEnhancedMonitoring Microsoft.AzureCAT.AzureEnhancedMonitoringTest Microsoft.Compute Microsoft.EnterpriseCloud.Monitoring Microsoft.EnterpriseCloud.Monitoring.Test Microsoft.HpcCompute Microsoft.HpcPack Microsoft.OSTCExtensions Microsoft.Powershell Microsoft.Powershell.Internal Microsoft.Powershell.Internal.Telemetry Microsoft.Powershell.Test Microsoft.SqlServer.Managability.IaaS.Test Microsoft.SqlServer.Management Microsoft.SystemCenter Microsoft.VisualStudio.Azure.RemoteDebug Microsoft.VisualStudio.Azure.RemoteDebug.Json Microsoft.Windows.AzureRemoteApp.Test Microsoft.Windows.RemoteDesktop Microsoft.WindowsAzure.Compute MicrosoftAzureSiteRecovery MicrosoftBizTalkServer MicrosoftDynamicsAX MicrosoftDynamicsNAV MicrosoftHybridCloudStorage MicrosoftSharePoint MicrosoftSQLServer MicrosoftVisualStudio MicrosoftWindowsServer MicrosoftWindowsServerEssentials MicrosoftWindowsServerHPCPack MicrosoftWindowsServerRemoteDesktop
Offer
Offerは同じく、Get-AzureVMIMageOfferです。Location/PublisherNameを指定する必要があります。
> Get-AzureVMImageOffer -Location japanwest -PublisherName MicrosoftWindowsServer Id : /Subscriptions/46dbdb18-c1c7-47e6-a66a-a1984c7a8372/Providers/Microsoft.Compute/Locations/japanwest/Publishers/MicrosoftWindo wsServer/ArtifactTypes/VMImage/Offers/WindowsServer Location : japanwest PublisherName : MicrosoftWindowsServer Offer : WindowsServer
SKU
同じくSKUは、Get-AzureVMImageSku コマンドレットで。
Skus ---- > Get-AzureVMImageSku -Location japanwest ` -PublisherName MicrosoftWindowsServer -Offer WindowsServer | ft Skusc 2008-R2-SP1 2012-Datacenter 2012-R2-Datacenter Windows-Server-Technical-Preview
詳細
ようやく、PublisherName/Offer/SKUが分かったところで、Get-AzureVMImage すると、詳細が出てきます。3つのバージョンがあることが分かります。
> Get-AzureVMImage -Location japanwest -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2012-R2-Datacenter Id : /Subscriptions/46dbdb18-c1c7-47e6-a66a-a1984c7a8372/Providers/Microsoft.Compute/Locations/japanwest/Publishers/MicrosoftWi ndowsServer/ArtifactTypes/VMImage/Offers/WindowsServer/Skus/2012-R2-Datacenter/Versions/4.0.201503 Location : japanwest PublisherName : MicrosoftWindowsServer Offer : WindowsServer Skus : 2012-R2-Datacenter Version : 4.0.201503 FilterExpression : Id : /Subscriptions/46dbdb18-c1c7-47e6-a66a-a1984c7a8372/Providers/Microsoft.Compute/Locations/japanwest/Publishers/MicrosoftWi ndowsServer/ArtifactTypes/VMImage/Offers/WindowsServer/Skus/2012-R2-Datacenter/Versions/4.0.201504 Location : japanwest PublisherName : MicrosoftWindowsServer Offer : WindowsServer Skus : 2012-R2-Datacenter Version : 4.0.201504 FilterExpression : Id : /Subscriptions/46dbdb18-c1c7-47e6-a66a-a1984c7a8372/Providers/Microsoft.Compute/Locations/japanwest/Publishers/MicrosoftWi ndowsServer/ArtifactTypes/VMImage/Offers/WindowsServer/Skus/2012-R2-Datacenter/Versions/4.0.201505 Location : japanwest PublisherName : MicrosoftWindowsServer Offer : WindowsServer Skus : 2012-R2-Datacenter Version : 4.0.201505 FilterExpression :
ここまでやって冒頭の、Get-AzureVMImageDetailにたどり着きます。
ちなみにUbuntu の 14.04-LTS は以下のとおりです。
Get-AzureVMimage -Location japanwest ` -PublisherName canonical -Offer UbuntuServer -Skus 14.04.2-LTS
リソースグループの作成
ARMでは、個々のリソースはリソースグループ単位に管理されるので、まずはリソースグループを作成します。
> New-AzureResourceGroup -Name testrg -Location japanwest 詳細: 15:21:48 - Created resource group 'testrg' in location 'japanwest' ResourceGroupName : testrg Location : japanwest ProvisioningState : Succeeded Tags : Permissions : Actions NotActions ======= ========== * ResourceId : /subscriptions/xxxx-xxxx-xxx-xxxx/resourceGroups/testrg
リソースの作成
リソースグループの作成が終わったら、個々のリソースを作成していきます。作成するリソースは前回と同様最小限。
- パブリックIP
- 仮想ネットワーク
- ネットワークアダプター
- ストレージアカウント
- 仮想マシン
パブリックIP
パブリックIPは、New-AzurePublicIpAddress コマンドレットで作成します。DomainLabelとLocationががURLの一部になります。
$ip = New-AzurePublicIpAddress -ResourceGroupName testrg -Location japanwest ` -Name myPublicIp -AllocationMethod Dynamic -DomainNameLabel bobobo119
上記の場合ですと
bobobo119.japanwest.cloudapp.azure.com
となります。ドメインラベル必須ではないらしく、最初に失敗してなしで作成してしまいましたが、意味があるんでしょうか。独自ドメイン割り当てればいいんですかね?
仮想ネットワーク
仮想ネットワークは、New-AzureVirtualNetwork コマンドレットで作成しますが、個々のサブネットは、New-AzureVirtualNetworkSubnetConfig コマンドで定義してそれを指定します。
$subnet = New-AzureVirtualNetworkSubnetConfig -Name "Subnet-1" ` -AddressPrefix "10.0.0.0/24" $vnet = New-AzureVirtualNetwork -ResourceGroupName testrg ` -Location japanwest -Name "myVnet" -AddressPrefix "10.0.0.0/8" -Subnet $subnet$
ネットワークアダプター
ネットワークアダプターは、 New-AzureNetworkInterface コマンドレットで作成します。先程作成した仮想ネットワークから、サブネットオブジェクトを取り出して指定します。
$subnet = Get-AzureVirtualNetworkSubnetConfig -Name "Subnet-1" -VirtualNetwork $vnet $nic = New-AzureNetworkInterface -ResourceGroupName testrg -Location japanwest ` -Name "myNic" -PrivateIpAddress 10.0.0.10 -Subnet $subnet ` -PublicIpAddress $ip
ストレージアカウント
ストレージアカウントは、New-AzureStorageAccount コマンドレットで作成します。以下、スタンダード LRSで作成した例。
New-AzureStorageAccount -ResourceGroupName testrg -Location japanwest -Name bobobo119 -Type Standard_LRS
仮想マシン
最後に仮想マシンを作成します。ASMのNew-AzureVMと同様にVMの構成オブジェクトをパイプでつなげて作成していきます。
$creds = Get-Credential $config = ` New-AzureVMConfig -VMName myUbuntu -VMSize Standard_A1 | Set-AzureVMOperatingSystem -Linux -ComputerName myUbuntu -Credential $creds | Set-AzureVMSourceImage -PublisherName canonical -Offer UbuntuServer -Skus 14.04.2-LTS -Version 14.04.201506100 | Set-AzureVMOSDisk -Name "ubuntuOsDisk" -CreateOption fromImage -Caching ReadWrite ` -VhdUri "https://bobobo119.blob.core.windows.net/vhds/OsDisk.vhd" | Add-AzureVMNetworkInterface -Id $nic.Id New-AzureVM -ResourceGroupName testrg -Location japanwest -VM $config -Name "myUbuntu"
SSHで接続
SSHは開いているので、あとはログインすればOKです。
$ ssh azureuser@bobobo119.japanwest.cloudapp.azure.com The authenticity of host 'bobobo119.japanwest.cloudapp.azure.com (191.239.97.118)' can't be established. ECDSA key fingerprint is 89:05:a9:63:3f:15:ff:17:6c:3d:22:30:f2:45:9e:a3. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'bobobo119.japanwest.cloudapp.azure.com,191.239.97.118' (ECDSA) to the list of known hosts. azureuser@bobobo119.japanwest.cloudapp.azure.com's password: Welcome to Ubuntu 14.04.2 LTS (GNU/Linux 3.16.0-40-generic x86_64)
ロードバランサーは構成していないので、直接インターネットにさらされていますのでご注意を。
$ ping bobobo119.japanwest.cloudapp.azure.com bobobo119.japanwest.cloudapp.azure.com [191.239.97.118]に ping を送信しています 32 バイトのデータ: 191.239.97.118 からの応答: バイト数 =32 時間 =19ms TTL=52 191.239.97.118 からの応答: バイト数 =32 時間 =20ms TTL=52
補足
冒頭でも言ったとおり、現状のAzure PowerShellコマンドレットは大きく変わりそうです。以下のような警告が出たりしますので、この記事の賞味期限もそんなに長くないでしょう。
警告: The Name parameter of XXXX cmdlet will be deprecated in a future release of Azure PowerShell, because the functionality of Name parameter is obsolete.
参考
Creating Azure VMs with ARM PowerShell cmdlets - Cloud Solution Architect - Site Home - MSDN Blogs