自动化过程中,创建虚拟主机是避不开的操作。由于系统本身的复杂性,很难用一两条简单的命令完成虚拟主机的创建。所以专门写一篇文章来记录使用 PowerShell 在 azure 上创建虚拟主机(Ubuntu 服务器)。包括:网卡、public IP 地址、虚拟网络、网络安全组、存储等。下图包含了新建一个虚机所需要的其他组件:
修改或使用脚本的参数进行初始化:
=
=
= +
= +
=
= +
=
= +
= +
= +
=
= +
=
=
=
=
登录虚机的凭据
用户,你需要指定用户的名称和登录密码(我们的创建的用户通过公钥认证登录,这个密码并不真正使用)。通过用户名和密码创建凭据对象:
= ConvertTo-SecureString -AsPlainText - = New-Object System.Management.Automation.PSCredential (,)
New-AzureRmResourceGroup -Name -Location
= New-AzureRmVirtualNetworkSubnetConfig -Name -AddressPrefix 192.168.1.0/24
= New-AzureRmVirtualNetwork -ResourceGroupName -Location -Name -AddressPrefix 192.168.0.0/16 -Subnet
= New-AzureRmPublicIpAddress -ResourceGroupName -Location -AllocationMethod Static -IdleTimeoutInMinutes 4-Name
= New-AzureRmNetworkSecurityRuleConfig -Name --Direction Inbound -Priority 200 -SourceAddressPrefix * -SourcePortRange * -DestinationAddressPrefix *-DestinationPortRange 22 -Access Allow
= New-AzureRmNetworkSecurityGroup -ResourceGroupName -Location -Name -SecurityRules
= New-AzureRmNetworkInterface -Name -ResourceGroupName -Location -SubnetId .Subnets[0].Id -PublicIpAddressId .Id -NetworkSecurityGroupId .Id
文件是以 blob 的形式存放在 azure 的存储中的,所以我们需要创建一个 StorageAccount 来存储磁盘文件:
= New-AzureRmStorageAccount -ResourceGroupName -Name -Type -Location
文件的存放位置和名称:
= .PrimaryEndpoints.Blob.ToString() + + +
= New-AzureRmVMConfig -VMName -VMSize |-AzureRmVMOperatingSystem -Linux -ComputerName -Credential -DisablePasswordAuthentication |-AzureRmVMSourceImage -PublisherName Canonical -Offer UbuntuServer -Skus -Version latest |-AzureRmVMNetworkInterface -Id .Id |-AzureRmVMOSDisk -VhdUri -CreateOption FromImage
禁止使用用户名密码的方式登录。要让用户能够通过公钥的方式登录必须提供用户的公钥:
=
用户的 authorized_keys 文件中:
Add-AzureRmVMSshPublicKey -VM -KeyData -Path
New-AzureRmVM -ResourceGroupName -Location -VM