Lompat ke konten Lompat ke sidebar Lompat ke footer

Start Easy Ubuntu Deployment Azure Resource Group

Skip to main content

How to create a Linux virtual machine with Azure Resource Manager templates

Applies to: ✔️ Linux VMs ✔️ Flexible scale sets

Learn how to create a Linux virtual machine (VM) by using an Azure Resource Manager template and the Azure CLI from the Azure Cloud shell. To create a Windows virtual machine, see Create a Windows virtual machine from a Resource Manager template.

An alternative is to deploy the template from the Azure portal. To open the template in the portal, select the Deploy to Azure button.

Deploy to Azure

Templates overview

Azure Resource Manager templates are JSON files that define the infrastructure and configuration of your Azure solution. By using a template, you can repeatedly deploy your solution throughout its lifecycle and have confidence your resources are deployed in a consistent state. To learn more about the format of the template and how you construct it, see Quickstart: Create and deploy Azure Resource Manager templates by using the Azure portal. To view the JSON syntax for resources types, see Define resources in Azure Resource Manager templates.

Create a virtual machine

Creating an Azure virtual machine usually includes two steps:

  1. Create a resource group. An Azure resource group is a logical container into which Azure resources are deployed and managed. A resource group must be created before a virtual machine.
  2. Create a virtual machine.

The following example creates a VM from an Azure Quickstart template. This template creates an Azure Generation 2 VM by default. See Support for generation 2 VMs on Azure to learn more about Azure Generation 2 VMs. Only SSH authentication is allowed for this deployment. When prompted, provide the value of your own SSH public key, such as the contents of ~/.ssh/id_rsa.pub. If you need to create an SSH key pair, see How to create and use an SSH key pair for Linux VMs in Azure. Here is a copy of the template:

              {   "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",   "contentVersion": "1.0.0.0",   "parameters": {     "projectName": {       "type": "string",       "metadata": {         "description": "Specifies a name for generating resource names."       }     },     "location": {       "type": "string",       "defaultValue": "[resourceGroup().location]",       "metadata": {         "description": "Specifies the location for all resources."       }     },     "adminUsername": {       "type": "string",       "metadata": {         "description": "Specifies a username for the Virtual Machine."       }     },     "adminPublicKey": {       "type": "string",       "metadata": {         "description": "Specifies the SSH rsa public key file as a string. Use \"ssh-keygen -t rsa -b 2048\" to generate your SSH key pairs."       }     },     "vmSize": {       "type": "string",       "defaultValue": "Standard_D2s_v3",       "metadata": {         "description": "description"       }     }   },   "variables": {     "vNetName": "[concat(parameters('projectName'), '-vnet')]",     "vNetAddressPrefixes": "10.0.0.0/16",     "vNetSubnetName": "default",     "vNetSubnetAddressPrefix": "10.0.0.0/24",     "vmName": "[concat(parameters('projectName'), '-vm')]",     "publicIPAddressName": "[concat(parameters('projectName'), '-ip')]",     "networkInterfaceName": "[concat(parameters('projectName'), '-nic')]",     "networkSecurityGroupName": "[concat(parameters('projectName'), '-nsg')]",     "networkSecurityGroupName2": "[concat(variables('vNetSubnetName'), '-nsg')]"   },   "resources": [     {       "type": "Microsoft.Network/networkSecurityGroups",       "apiVersion": "2020-05-01",       "name": "[variables('networkSecurityGroupName')]",       "location": "[parameters('location')]",       "properties": {         "securityRules": [           {             "name": "ssh_rule",             "properties": {               "description": "Locks inbound down to ssh default port 22.",               "protocol": "Tcp",               "sourcePortRange": "*",               "destinationPortRange": "22",               "sourceAddressPrefix": "*",               "destinationAddressPrefix": "*",               "access": "Allow",               "priority": 123,               "direction": "Inbound"             }           }         ]       }     },     {       "type": "Microsoft.Network/publicIPAddresses",       "apiVersion": "2020-05-01",       "name": "[variables('publicIPAddressName')]",       "location": "[parameters('location')]",       "properties": {         "publicIPAllocationMethod": "Dynamic"       },       "sku": {         "name": "Basic"       }     },     {       "comments": "Simple Network Security Group for subnet [variables('vNetSubnetName')]",       "type": "Microsoft.Network/networkSecurityGroups",       "apiVersion": "2020-05-01",       "name": "[variables('networkSecurityGroupName2')]",       "location": "[parameters('location')]",       "properties": {         "securityRules": [           {             "name": "default-allow-22",             "properties": {               "priority": 1000,               "access": "Allow",               "direction": "Inbound",               "destinationPortRange": "22",               "protocol": "Tcp",               "sourceAddressPrefix": "*",               "sourcePortRange": "*",               "destinationAddressPrefix": "*"             }           }         ]       }     },     {       "type": "Microsoft.Network/virtualNetworks",       "apiVersion": "2020-05-01",       "name": "[variables('vNetName')]",       "location": "[parameters('location')]",       "dependsOn": [         "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName2'))]"       ],       "properties": {         "addressSpace": {           "addressPrefixes": [             "[variables('vNetAddressPrefixes')]"           ]         },         "subnets": [           {             "name": "[variables('vNetSubnetName')]",             "properties": {               "addressPrefix": "[variables('vNetSubnetAddressPrefix')]",               "networkSecurityGroup": {                 "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName2'))]"               }             }           }         ]       }     },     {       "type": "Microsoft.Network/networkInterfaces",       "apiVersion": "2020-05-01",       "name": "[variables('networkInterfaceName')]",       "location": "[parameters('location')]",       "dependsOn": [         "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]",         "[resourceId('Microsoft.Network/virtualNetworks', variables('vNetName'))]",         "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"       ],       "properties": {         "ipConfigurations": [           {             "name": "ipconfig1",             "properties": {               "privateIPAllocationMethod": "Dynamic",               "publicIPAddress": {                 "id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]"               },               "subnet": {                 "id": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('vNetName'), variables('vNetSubnetName'))]"               }             }           }         ]       }     },     {       "type": "Microsoft.Compute/virtualMachines",       "apiVersion": "2021-11-01",       "name": "[variables('vmName')]",       "location": "[parameters('location')]",       "dependsOn": [         "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"       ],       "properties": {         "hardwareProfile": {           "vmSize": "[parameters('vmSize')]"         },         "osProfile": {           "computerName": "[variables('vmName')]",           "adminUsername": "[parameters('adminUsername')]",           "linuxConfiguration": {             "disablePasswordAuthentication": true,             "ssh": {               "publicKeys": [                 {                   "path": "[concat('/home/', parameters('adminUsername'), '/.ssh/authorized_keys')]",                   "keyData": "[parameters('adminPublicKey')]"                 }               ]             }           }         },         "storageProfile": {           "imageReference": {             "publisher": "Canonical",             "offer": "UbuntuServer",             "sku": "18_04-lts-gen2",             "version": "latest"           },           "osDisk": {             "createOption": "fromImage"           }         },         "networkProfile": {           "networkInterfaces": [             {               "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"             }           ]         }       }     }   ] }                          

To run the CLI script, Select Try it to open the Azure Cloud shell. To paste the script, right-click the shell, and then select Paste:

              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                          

The last Azure CLI command shows the public IP address of the newly created VM. You need the public IP address to connect to the virtual machine. See the next section of this article.

In the previous example, you specified a template stored in GitHub. You can also download or create a template and specify the local path with the --template-file parameter.

Here are some additional resources:

  • To learn how to develop Resource Manager templates, see Azure Resource Manager documentation.
  • To see the Azure virtual machine schemas, see Azure template reference.
  • To see more virtual machine template samples, see Azure Quickstart templates.

Connect to virtual machine

You can then SSH to your VM as normal. Provide you own public IP address from the preceding command:

              ssh <adminUsername>@<ipAddress>                          

Next steps

In this example, you created a basic Linux VM. For more Resource Manager templates that include application frameworks or create more complex environments, browse the Azure Quickstart templates.

To learn more about creating templates, view the JSON syntax and properties for the resources types you deployed:

  • Microsoft.Network/networkSecurityGroups
  • Microsoft.Network/publicIPAddresses
  • Microsoft.Network/virtualNetworks
  • Microsoft.Network/networkInterfaces
  • Microsoft.Compute/virtualMachines

ayalathencestraes.blogspot.com

Source: https://docs.microsoft.com/en-us/azure/virtual-machines/linux/create-ssh-secured-vm-from-template

Posting Komentar untuk "Start Easy Ubuntu Deployment Azure Resource Group"