设为首页 收藏本站
查看: 972|回复: 0

[经验分享] 使用Json Template在Azure China创建ARM类型的虚拟机

[复制链接]

尚未签到

发表于 2017-6-30 18:36:43 | 显示全部楼层 |阅读模式
  前面几篇文章介绍过Azure的两种VM的模式,包括ASM和ARM。并且介绍了如何用Azure CLI和PowerShell创建虚拟机。本文将介绍如何采用Json的Template来创建基于ARM的VM。
  当然采用Json Template的方式创建虚拟机是几种方式中最好的,这样可以便于批量部署、Json文件可以重用。
  ARM的Template的格式采用的是Json的格式。其需要的几个部分如下:
DSC0000.png

  需要定义的有: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

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-389748-1-1.html 上篇帖子: 在 Azure 平台配置可用性集加负载 下篇帖子: Azure Redis Cache (4) 配置和管理Redis Cache
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表