Azure模板,用于为每个虚拟机创建公共静态IP地址

时间:2016-01-21 13:32:43

标签: templates azure azure-resource-manager

我使用CentOS-2nics-lb-cluster azure模板生成负载平衡和NAT规则,为每个模块启用特定的SSH端口 它创建的虚拟机。

我当前的ssh配置看起来像

ssh admin@test01.cloudapp.azure.com -p 50000 // aka vm0
ssh admin@test01.cloudapp.azure.com -p 50001 // aka vm1

就我而言,我真的需要为每个虚拟机创建一个唯一的主机名

ssh admin@test01vm0.cloudapp.azure.com -p 22
ssh admin@test01vm1.cloudapp.azure.com -p 22

有人可以建议我如何更改下面的模板部分来实现这个目标吗?

{
  "apiVersion": "2015-05-01-preview",
  "name": "[variables('lbName')]",
  "type": "Microsoft.Network/loadBalancers",
  "location": "[resourceGroup().location]",
  "dependsOn": [
    "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]"
  ],
  "properties": {
    "frontendIPConfigurations": [
      {
        "name": "LoadBalancerFrontEnd",
        "properties": {
          "publicIPAddress": {
            "id": "[variables('publicIPAddressID')]"
          }
        }
      }
    ],
    "backendAddressPools": [
      {
        "name": "BackendPool1"
      }
    ],
    "inboundNatRules": [
      {
        "name": "ssh0",
        "properties": {
          "frontendIPConfiguration": {
            "id": "[variables('frontEndIPConfigID')]"
          },
          "protocol": "tcp",
          "frontendPort": 50000,
          "backendPort": 22,
          "enableFloatingIP": false
        }
      },
      {
        "name": "ssh1",
        "properties": {
          "frontendIPConfiguration": {
            "id": "[variables('frontEndIPConfigID')]"
          },
          "protocol": "tcp",
          "frontendPort": 50001,
          "backendPort": 22,
          "enableFloatingIP": false
        }
      },
      {
        "name": "ssh2",
        "properties": {
          "frontendIPConfiguration": {
            "id": "[variables('frontEndIPConfigID')]"
          },
          "protocol": "tcp",
          "frontendPort": 50002,
          "backendPort": 22,
          "enableFloatingIP": false
        }
      }
    ],
    "loadBalancingRules": [
      {
        "name": "LBRule",
        "properties": {
          "frontendIPConfiguration": {
            "id": "[variables('frontEndIPConfigID')]"
          },
          "backendAddressPool": {
            "id": "[variables('lbPoolID')]"
          },
          "protocol": "tcp",
          "frontendPort": 80,
          "backendPort": 80,
          "enableFloatingIP": true,
          "idleTimeoutInMinutes": 10,
          "probe": {
            "id": "[variables('lbProbeID')]"
          }
        }
      }
    ],

1 个答案:

答案 0 :(得分:1)

要使用唯一的主机名和相同的端口连接到每个VM,您需要为每个VM分配一个公共IP地址。但请注意,IPv4公共IP地址很少,建议使用NAT规则访问负载均衡器后面的VM。您能否提供一些关于为什么在同一端口上连接很重要的附加背景信息。独特的主机名?

如果绝对需要,下面是向VM添加公共IP地址的模板的相关部分。您需要创建 publicIPAddresses 资源,然后将其分配给 NetworkInterfaces 资源,然后将其分配给 VirtualMachines 资源。

在这里查看完整模板https://github.com/Azure/azure-quickstart-templates/blob/master/101-vm-sshkey/azuredeploy.json#L127

{
  "apiVersion": "[variables('apiVersion')]",
  "type": "Microsoft.Network/publicIPAddresses",
  "name": "[variables('publicIPAddressName')]",
  "location": "[variables('location')]",
  "properties": {
    "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
    "dnsSettings": {
      "domainNameLabel": "[parameters('dnsLabelPrefix')]"
    }
  }
},
{
  "apiVersion": "[variables('apiVersion')]",
  "type": "Microsoft.Network/networkInterfaces",
  "name": "[variables('nicName')]",
  "location": "[variables('location')]",
  "dependsOn": [
    "[concat('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]",
    "[concat('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
  ],
  "properties": {
    "ipConfigurations": [
      {
        "name": "ipconfig1",
        "properties": {
          "privateIPAllocationMethod": "Dynamic",
          "publicIPAddress": {
            "id": "[resourceId('Microsoft.Network/publicIPAddresses', variables('publicIPAddressName'))]"
          },
          "subnet": {
            "id": "[variables('subnet1Ref')]"
          }
        }
      }
    ]
  }
},
{
  "apiVersion": "[variables('apiVersion')]",
  "type": "Microsoft.Compute/virtualMachines",
  "name": "[parameters('vmName')]",
  "location": "[variables('location')]",
  "dependsOn": [
    "[concat('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
    "[concat('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
  ],
  "properties": {
    "hardwareProfile": {
      "vmSize": "[parameters('vmSize')]"
    },
    "osProfile": {
      "computerName": "[parameters('vmName')]",
      "adminUsername": "[parameters('adminUsername')]",
      "linuxConfiguration": {
        "disablePasswordAuthentication": "true",
        "ssh": {
          "publicKeys": [
            {
              "path": "[variables('sshKeyPath')]",
              "keyData": "[parameters('sshKeyData')]"
            }
          ]
        }
      }
    },
    "storageProfile": {
      "imageReference": {
        "publisher": "[variables('imagePublisher')]",
        "offer": "[variables('imageOffer')]",
        "sku": "[parameters('ubuntuOSVersion')]",
        "version": "latest"
      },
      "osDisk": {
        "name": "osdisk",
        "vhd": {
          "uri": "[concat('http://',variables('storageAccountName'),'.blob.core.windows.net/',variables('vmStorageAccountContainerName'),'/', variables('osDiskName'),'.vhd')]"
        },
        "caching": "ReadWrite",
        "createOption": "FromImage"
      }
    },
    "networkProfile": {
      "networkInterfaces": [
        {
          "id": "[resourceId('Microsoft.Network/networkInterfaces', variables('nicName'))]"
        }
      ]
    },
    "diagnosticsProfile": {
      "bootDiagnostics": {
         "enabled": "true",
         "storageUri": "[concat('http://',variables('storageAccountName'),'.blob.core.windows.net')]"
      }
    }
  }
}