Skip to main content

使用ARM创建虚拟机

分类:  Azure指南 标签:  #Azure #基础 #Azure Cloud Architecting #Azure入门 发布于: 2023-05-28 15:21:31

本章介绍如何使用模板来创建虚拟机。

使用模板

ARM(Azure Resource Template)我们前面入门文章中有给大家介绍过,可以回头再看一下,如何使用ARM模板来创建资源有两种方式:

直接使用Portal来部署模板

这种方式需要将您的模板放置在一个Azure Portal可以读取的位置,由Azure Portal直接读取,然后利用Azure Portal的UI一步一步的创建资源。可以使用浏览器用下述的格式来访问:
https://portal.azure.com/#create/Microsoft.Template/uri/{您的模板位置}

例如下面的链接:
https://portal.azure.cn/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-quickstart-templates%2Fmaster%2Fquickstarts%2Fmicrosoft.compute%2Fvm-sshkey%2Fazuredeploy.json

将模板文件放置在github, 您可以尝试复制上述地址到浏览器中完成您的创建。

关于模板的介绍,您可以参考文档:模板介绍文档

使用Azure CLI + 模板

一般需要两个步骤:

  • 创建一个资源组
  • 创建虚拟机

我们需要使用到模板文件azuredeploy.json, 该文件的内容请参考这个URL:https://github.com/Azure/azure-quickstart-templates/blob/master/quickstarts/microsoft.compute/vm-sshkey/azuredeploy.json

请启动Azure cloud shell, 然后运行如下的命令:

echo "Enter the Resource Group name:" &&
read resourceGroupName &&
echo "Enter the location (i.e. centralus):" &&
read location &&
echo "Enter the project name (used for generating resource names):" &&
read projectName &&
echo "Enter the administrator username:" &&
read username &&
echo "Enter the SSH public key:" &&
read key &&
az group create --name $resourceGroupName --location "$location" &&
az deployment group create --resource-group $resourceGroupName --template-uri https://raw.githubusercontent.com/azure/azure-quickstart-templates/master/quickstarts/microsoft.compute/vm-sshkey/azuredeploy.json --parameters projectName=$projectName adminUsername=$username adminPublicKey="$key" &&
az vm show --resource-group $resourceGroupName --name "$projectName-vm" --show-details --query publicIps --output tsv

最后一个命令显示新创建的VM的public ip.。

然后使用如下的命令链接到虚拟机:

ssh <adminUsername>@<ipAddress>