使用Json Template在Azure China建立ARM型別的虛擬機器

衡子發表於2016-07-01

前面幾篇文章介紹過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": "[concat(uniquestring(resourceGroup().id), 'salinuxvm')]",
    "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": "[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]",
    "subnetRef": "[concat(variables('vnetID'),'/subnets/',variables('subnetName'))]",
    "apiVersion": "2015-06-15"
  },

 

更改為:

  "variables": {
    "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'salinuxvm')]",
    "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": "[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]",
    "subnetRef": "[concat(variables('vnetID'),'/subnets/',variables('subnetName'))]",
    "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[Microsoft.Rest.Azure.IPage`1[Microsoft.Azure.Management.Compute.Models.VirtualMachine]]
Location                          : chinaeast
Tags                              : {}
DiagnosticsProfile                : 
  BootDiagnostics                 : 
    Enabled                       : True
    StorageUri                    : https://eyyvnizdwsddusalinuxvm.blob.core.chinacloudapi.cn/
HardwareProfile                   : 
  VmSize                          : Standard_A1
NetworkProfile                    : 
  NetworkInterfaces[0]            : 
    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[0]                    : 
    Lun                           : 0
    Name                          : datadisk1
    Vhd                           : 
      Uri                         : https://eyyvnizdwsddusalinuxvm.blob.core.chinacloudapi.cn/vhds/datadisk1.vhd
    Caching                       : None
    CreateOption                  : Empty
    DiskSizeGB                    : 100
DataDiskNames[0]                  : datadisk1
NetworkInterfaceIDs[0]            : /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 

 

相關文章