徐冬丽 发表于 2017-6-30 18:36:43

使用Json Template在Azure China创建ARM类型的虚拟机

  前面几篇文章介绍过Azure的两种VM的模式,包括ASM和ARM。并且介绍了如何用Azure CLI和PowerShell创建虚拟机。本文将介绍如何采用Json的Template来创建基于ARM的VM。
  当然采用Json Template的方式创建虚拟机是几种方式中最好的,这样可以便于批量部署、Json文件可以重用。
  ARM的Template的格式采用的是Json的格式。其需要的几个部分如下:

  需要定义的有:parameters,variables,resources和outputs。但只有resources是必须的。
  由于Template的内容比较复杂,一般都采用复制已有的Template Jason文件修改的方式。
  目前,在Github上有大量的Azure ARM Jason Template,可以下载修改使用。
  具体的网址在:
  https://github.com/Azure/azure-quickstart-templates
  比如我们要创建一台CentOS 6.5的Linux虚拟机。在上面的链接中,没有相应的template。我们通过一台Ubuntu的Template进行修改。
  首先下载两个Jason文件:
  https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-vm-simple-linux/azuredeploy.json
  https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-vm-simple-linux/azuredeploy.parameters.json
  第一个文件是部署文件,第二个文件是参数文件。
  我们首先看部署文件:
  一、修改parameter中的内容
  1 把Ubuntu版本改成CentOS版本:



    "ubuntuOSVersion": {
"type": "string",
"defaultValue": "14.04.2-LTS",
"allowedValues": [
"12.04.5-LTS",
"14.04.2-LTS",
"15.10"
],
  改成



    "CentOSVersion": {
"type": "string",
"defaultValue": "6.5",
"allowedValues": [
"6.5",
"6.6",
"7.0",
"7.1"
],
  2 把metadata的描述更改



"metadata": {
"description": "The Ubuntu version for the VM. This will pick a fully patched image of this given Ubuntu version. Allowed values: 12.04.5-LTS, 14.04.2-LTS, 15.10."
}
  改为:



"metadata": {
"description": "The CentOS version for the VM. This will pick a fully patched image of this given CentOS version. Allowed values: 6.5, 6.6, 7.0, 7.1."
}
  二、修改variables中的内容
  原始的变量定义如下:



"variables": {
"storageAccountName": "",
"dataDisk1VhdName": "datadisk1",
"imagePublisher": "Canonical",
"imageOffer": "UbuntuServer",
"OSDiskName": "osdiskforlinuxsimple",
"nicName": "myVMNic",
"addressPrefix": "10.0.0.0/16",
"subnetName": "Subnet",
"subnetPrefix": "10.0.0.0/24",
"storageAccountType": "Standard_LRS",
"publicIPAddressName": "myPublicIP",
"publicIPAddressType": "Dynamic",
"vmStorageAccountContainerName": "vhds",
"vmName": "MyUbuntuVM",
"vmSize": "Standard_D1",
"virtualNetworkName": "MyVNET",
"vnetID": "",
"subnetRef": "",
"apiVersion": "2015-06-15"
},
  更改为:



"variables": {
"storageAccountName": "",
"dataDisk1VhdName": "datadisk1",
"imagePublisher": "OpenLogic",
"imageOffer": "CentOS",
"OSDiskName": "osdiskforlinuxsimple",
"nicName": "myVMNic",
"addressPrefix": "10.0.0.0/16",
"subnetName": "Subnet",
"subnetPrefix": "10.0.0.0/24",
"storageAccountType": "Standard_LRS",
"publicIPAddressName": "myPublicIP",
"publicIPAddressType": "Dynamic",
"vmStorageAccountContainerName": "vhds",
"vmName": "MyCentOSVM",
"vmSize": "Standard_A1",
"virtualNetworkName": "MyVNET",
"vnetID": "",
"subnetRef": "",
"apiVersion": "2015-06-15"
},
  修改完部署文件后,修改参数文件:
  参数文件如下:



{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminUsername": {
"value": "azureUser"
},
"adminPassword": {
"value": "GEN-PASSWORD"
},
"dnsLabelPrefix": {
"value": "GEN-UNIQUE"
},
"ubuntuOSVersion": {
"value": "14.04.2-LTS"
}
}
}
  更改为:



{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminUsername": {
"value": "hengwei"
},
"adminPassword": {
"value": "abc@123456"
},
"dnsLabelPrefix": {
"value": "hwvm"
},
"CentOSVersion": {
"value": "6.5"
}
}
}
  并把两个文件上传到Github上,链接如下:
  https://raw.githubusercontent.com/hengv/Hengwei/Azure/101-simple-linux/azuredeploy.json
  https://raw.githubusercontent.com/hengv/Hengwei/Azure/101-simple-linux/azuredeploy.parameters.json
  下面可以通过PowerShell命令通过Jason Template创建VM了:



New-AzureRmResourceGroup -Name hw01 -Location chinaeast

ResourceGroupName : hw01
Location          : chinaeast
ProvisioningState : Succeeded
Tags            :
ResourceId      : /subscriptions/xxxxxxxx/resourceGroups/hw01


New-AzureRmResourceGroupDeployment -Name hwvm01 -ResourceGroupName hw01 -TemplateUri https://raw.githubusercontent.com/hengv/Hengwei/Azure/101-simple-linux/azuredeploy.json -TemplateParameterUri https://raw.githubusercontent.com/hengv/Hengwei/Azure/101-simple-linux/azuredeploy.parameters.json -Mode Incremental

DeploymentName          : hwvm01
ResourceGroupName       : hw01
ProvisioningState       : Succeeded
Timestamp               : 2016/7/1 14:09:58
Mode                  : Incremental
TemplateLink            :
Uri            : https://raw.githubusercontent.com/hengv/Hengwei/Azure/101-simple-linux/azuredeploy.json
ContentVersion : 1.0.0.0
Parameters            :
Name             Type                     Value   
==================================================
adminUsername    String                     hengwei   
adminPassword    SecureString                        
dnsLabelPrefix   String                     hwvm      
centOSVersion    String                     6.5      
Outputs               :
Name             Type                     Value   
==================================================
hostname         String                     hwvm.chinaeast.cloudapp.azure.com
sshCommand       String                     ssh hengwei@hwvm.chinaeast.cloudapp.azure.com
DeploymentDebugLogLevel :
  创建成功。
  再通过下面的命令查看:



get-azurermvm
RequestId                         : afe8fb47-4e2f-434f-aa40-4d230a549598
StatusCode                        : OK
ResourceGroupName               : HW01
Id                              : /subscriptions/xxxxxxxx/resourceGroups/HW01/providers/Microsoft.Compute/virtualMachines/MyCentOSVM
Name                              : MyCentOSVM
Type                              : Microsoft.Rest.Azure.AzureOperationResponse`1]
Location                        : chinaeast
Tags                              : {}
DiagnosticsProfile                :
BootDiagnostics               :
Enabled                     : True
StorageUri                  : https://eyyvnizdwsddusalinuxvm.blob.core.chinacloudapi.cn/
HardwareProfile                   :
VmSize                        : Standard_A1
NetworkProfile                  :
NetworkInterfaces            :
Id                            : /subscriptions/xxxxxxxx/resourceGroups/hw01/providers/Microsoft.Network/networkInterfaces/myVMNic
OSProfile                         :
ComputerName                  : MyCentOSVM
AdminUsername                   : hengwei
LinuxConfiguration            :
DisablePasswordAuthentication : False
ProvisioningState               : Succeeded
StorageProfile                  :
ImageReference                  :
Publisher                     : OpenLogic
Offer                         : CentOS
Sku                           : 6.5
Version                     : latest
OsDisk                        :
OsType                        : Linux
Name                        : osdisk
Vhd                           :
Uri                         : https://eyyvnizdwsddusalinuxvm.blob.core.chinacloudapi.cn/vhds/osdiskforlinuxsimple.vhd
Caching                     : ReadWrite
CreateOption                  : FromImage
DataDisks                  :
Lun                           : 0
Name                        : datadisk1
Vhd                           :
Uri                         : https://eyyvnizdwsddusalinuxvm.blob.core.chinacloudapi.cn/vhds/datadisk1.vhd
Caching                     : None
CreateOption                  : Empty
DiskSizeGB                  : 100
DataDiskNames                  : datadisk1
NetworkInterfaceIDs            : /subscriptions/xxxxxxxx/resourceGroups/hw01/providers/Microsoft.Network/networkInterfaces/myVMNic


Get-AzureRmNetworkInterface
Name               : myVMNic
ResourceGroupName    : hw01
Location             : chinaeast
Id                   : /subscriptions/xxxxxxxx/resourceGroups/hw01/providers/Microsoft.Network/networkInterfaces/myVMNic
Etag               : W/"1e379401-4980-42c6-ba83-9e26f19133bd"
ResourceGuid         : c4e50ac6-fdb1-416c-a5b2-d71baace6b55
ProvisioningState    : Succeeded
Tags               :
VirtualMachine       : {
"Id": "/subscriptions/xxxxxxxx/resourceGroups/hw01/providers/Microsoft.Compute/virtualMachines/MyCentOSVM"
}
IpConfigurations   : [
{
"Name": "ipconfig1",
"Etag": "W/\"1e379401-4980-42c6-ba83-9e26f19133bd\"",
"Id": "/subscriptions/xxxxxxxx/resourceGroups/hw01/providers/Microsoft.Network/networkInterfaces/myVMNic/ipConfigurations/ipconfig1",
"PrivateIpAddress": "10.0.0.4",
"PrivateIpAllocationMethod": "Dynamic",
"Subnet": {
"Id": "/subscriptions/xxxxxxxx/resourceGroups/hw01/providers/Microsoft.Network/virtualNetworks/MyVNET/subnets/Subnet"
},
"PublicIpAddress": {
"Id": "/subscriptions/xxxxxxxx/resourceGroups/hw01/providers/Microsoft.Network/publicIPAddresses/myPublicIP"
},
"ProvisioningState": "Succeeded",
"LoadBalancerBackendAddressPools": [],
"LoadBalancerInboundNatRules": [],
"ApplicationGatewayBackendAddressPools": []
}
]
DnsSettings          : {
"DnsServers": [],
"AppliedDnsServers": []
}
EnableIPForwarding   : False
NetworkSecurityGroup : null
Primary            : True


Get-AzureRmPublicIpAddress
Name                     : myPublicIP
ResourceGroupName      : hw01
Location               : chinaeast
Id                     : /subscriptions/xxxxxxxx/resourceGroups/hw01/providers/Microsoft.Network/publicIPAddresses/myPublicIP
Etag                     : W/"0d4e57f8-25a1-4bf7-bec9-e98785ed8179"
ResourceGuid             : f8216ff0-06a6-478b-8270-f18fc717c0f7
ProvisioningState      : Succeeded
Tags                     :
PublicIpAllocationMethod : Dynamic
IpAddress                : 42.159.235.75
IdleTimeoutInMinutes   : 4
IpConfiguration          : {
"Id": "/subscriptions/xxxxxxxx/resourceGroups/hw01/providers/Microsoft.Network/networkInterfaces/myVMNic/ipConfigurations/ipconfig1"
}
DnsSettings            : {
"DomainNameLabel": "hwvm",
"Fqdn": "hwvm.chinaeast.cloudapp.chinacloudapi.cn"



Get-AzureRmVirtualNetwork
Name            : MyVNET
ResourceGroupName : hw01
Location          : chinaeast
Id                : /subscriptions/xxxxxxxx/resourceGroups/hw01/providers/Microsoft.Network/virtualNetworks/MyVNET
Etag            : W/"888d8b21-091b-4f40-b2ba-284f16c0f641"
ResourceGuid      : a69ef02a-728e-4212-8c2a-7a7c0f4bb881
ProvisioningState : Succeeded
Tags            :
AddressSpace      : {
"AddressPrefixes": [
"10.0.0.0/16"
]
}
DhcpOptions       : null
Subnets         : [
{
"Name": "Subnet",
"Etag": "W/\"888d8b21-091b-4f40-b2ba-284f16c0f641\"",
"Id": "/subscriptions/xxxxxxxx/resourceGroups/hw01/providers/Microsoft.Network/virtualNetworks/MyVNET/subnets/Subnet",
"AddressPrefix": "10.0.0.0/24",
"IpConfigurations": [
{
"Id": "/subscriptions/xxxxxxxx/resourceGroups/hw01/providers/Microsoft.Network/networkInterfaces/myVMNic/ipConfigurations/ipconfig1"
}
],
"ProvisioningState": "Succeeded"
}
]


Get-AzureRmStorageAccount

ResourceGroupName   : hw01
StorageAccountName: eyyvnizdwsddusalinuxvm
Id                  : /subscriptions/xxxxxxxx/resourceGroups/hw01/providers/Microsoft.Storage/storageAccounts/eyyvnizdwsddusalinuxvm
Location            : chinaeast
Sku               : Microsoft.Azure.Management.Storage.Models.Sku
Kind                : Storage
Encryption          :
AccessTier          :
CreationTime      : 2016/7/1 14:07:14
CustomDomain      :
LastGeoFailoverTime :
PrimaryEndpoints    : Microsoft.Azure.Management.Storage.Models.Endpoints
PrimaryLocation   : chinaeast
ProvisioningState   : Succeeded
SecondaryEndpoints:
SecondaryLocation   :
StatusOfPrimary   : Available
StatusOfSecondary   :
Tags                : {}
Context             : Microsoft.WindowsAzure.Commands.Common.Storage.AzureStorageContext
页: [1]
查看完整版本: 使用Json Template在Azure China创建ARM类型的虚拟机