Een virtuele Linux-machine maken met Azure Resource Manager-sjablonen

Van toepassing op: ✔️ Flexibele schaalsets voor Linux-VM's ✔️

Meer informatie over het maken van een virtuele Linux-machine (VM) met behulp van een Azure Resource Manager-sjabloon en de Azure CLI vanuit de Azure Cloud Shell. Zie Een virtuele Windows-machine maken op basis van een Resource Manager-sjabloon als u een virtuele Windows-machine wilt maken.

Een alternatief is het implementeren van de sjabloon vanuit Azure Portal. Als u de sjabloon in de portal wilt openen, selecteert u de knop Implementeren in Azure .

Button to deploy the Resource Manager template to Azure.

Overzicht van sjablonen

Azure Resource Manager-sjablonen zijn JSON-bestanden die de infrastructuur en configuratie van uw Azure-oplossing definiëren. Door het gebruik van een sjabloon kunt u gedurende de levenscyclus de oplossing herhaaldelijk implementeren en erop vertrouwen dat uw resources consistent worden geïmplementeerd. Zie de quickstart: Azure Resource Manager-sjablonen maken en implementeren met behulp van Azure Portal voor meer informatie over de indeling van de sjabloon en hoe u deze maakt. Als u de JSON-syntaxis voor resourcetypen wilt bekijken, raadpleegt u Define resources in Azure Resource Manager templates (Resources definiëren in Azure Resource Manager-sjablonen).

Quickstartsjabloon

Notitie

Met de opgegeven sjabloon wordt standaard een Azure Generation 2-VM gemaakt.

Notitie

Alleen SSH-verificatie is standaard ingeschakeld wanneer u de quickstart-sjabloon gebruikt. Geef desgevraagd de waarde op van uw eigen openbare SSH-sleutel, zoals de inhoud van ~/.ssh/id_rsa.pub.

Als u geen SSH-sleutelpaar hebt, maakt en gebruikt u een SSH-sleutelpaar voor Linux-VM's in Azure.

Klik op Kopiëren om de quickstart-sjabloon toe te voegen aan het Klembord:

{
  "$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": "0001-com-ubuntu-server-jammy",
            "sku": "22_04-lts-gen2",
            "version": "latest"
          },
          "osDisk": {
            "createOption": "fromImage"
          }
        },
        "networkProfile": {
          "networkInterfaces": [
            {
              "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('networkInterfaceName'))]"
            }
          ]
        }
      }
    }
  ]
}

U kunt ook een sjabloon downloaden of maken en het lokale pad opgeven met de --template-file parameter.

Een quickstartsjabloon-VM maken met Azure CLI

Nadat u een quickstart-sjabloon hebt verworven of gemaakt, maakt u er een VIRTUELE machine mee met behulp van de Azure CLI.

Met de volgende opdracht worden verschillende soorten invoer van de gebruiker aangevraagd. Deze omvatten:

  • Naam van de resourcegroep (resourceGroupName)
  • Locatie van het Azure-datacenter dat als host fungeert voor de VIRTUELE machine (locatie)
  • Een naam voor resources die zijn gerelateerd aan de VM (projectName)
  • Gebruikersnaam voor de gebruiker van de beheerder (gebruikersnaam)
  • Een openbare SSH-sleutel voor toegang tot de terminal van de VIRTUELE machine (sleutel)

Voor het maken van een virtuele Azure-machine is een resourcegroep vereist. Quickstart-sjablonen omvatten het maken van een resourcegroep als onderdeel van het proces.

Als u het CLI-script wilt uitvoeren, klikt u op Cloudshell openen. Zodra u toegang hebt tot Azure CloudShell, klikt u op Kopiëren om de opdracht te kopiëren, klikt u met de rechtermuisknop op de shell en selecteert u Plakken.

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

Op de laatste regel in de opdracht wordt het openbare IP-adres van de zojuist gemaakte VM weergegeven. U hebt het openbare IP-adres nodig om verbinding te maken met de virtuele machine.

Verbinding maken met de virtuele machine

Vervolgens kunt u SSH naar uw VIRTUELE machine als normaal uitvoeren. Geef uw eigen openbare IP-adres op uit de voorgaande opdracht:

ssh <adminUsername>@<ipAddress>

Andere sjablonen

In dit voorbeeld hebt u een eenvoudige Linux-VM gemaakt. Blader door de Azure-quickstartsjablonen voor meer Resource Manager-sjablonen die toepassingsframeworks bevatten of complexere omgevingen maken.

Bekijk de JSON-syntaxis en eigenschappen voor de resourcestypen die u hebt geïmplementeerd voor meer informatie over het maken van sjablonen:

Volgende stappen