简体   繁体   English

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

[英]Azure template to create public static IP address for each virtual machine

I using the CentOS-2nics-lb-cluster azure template which generates a Load Balance and NAT Rule which enables a specific SSH port for each of the Virtual Machines it creates. 我使用了CentOS-2nics-lb-cluster azure模板,该模板可生成负载平衡和NAT规则,该规则可为其创建的每个虚拟机启用特定的SSH端口。

My current ssh configuration look like 我当前的ssh配置看起来像

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

In my case, i really need to create a unique hostname for each of the virtual machines 就我而言,我确实需要为每个虚拟机创建一个唯一的主机名

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

Can someone suggest how i can change the template section below to achive this? 有人可以建议我如何更改下面的模板部分以实现此目标吗?

{
  "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')]"
          }
        }
      }
    ],

To connect to each of your VMs with an unique hostname and same port, you would need to assign a public IP address to every VM. 要使用唯一的主机名和相同的端口连接到每个VM,您需要为每个VM分配一个公共IP地址。 However, note that IPv4 public IP addresses are scarce and it is recommended to use NAT rules to access VMs behind a load balancer. 但是,请注意,IPv4公用IP地址很少,建议使用NAT规则访问负载均衡器后面的VM。 Can you please provide additional context on why it is important to connect on the same port & unique hostnames? 您能否提供其他背景说明为什么在同一端口和唯一的主机名上进行连接很重要?

In case this is absolutely needed, below is the relevant portion of a template to add a public IP address to a VM. 如果绝对需要这样做,则下面是模板的相关部分,用于向VM添加公共IP地址。 You need to create a publicIPAddresses resource, and then assign it to a NetworkInterfaces resource which is then assigned to a VirtualMachines resource. 您需要创建publicIPAddresses资源,然后将其分配给NetworkInterfaces资源,然后将其分配给VirtualMachines资源。

Take a look at the full template here, https://github.com/Azure/azure-quickstart-templates/blob/master/101-vm-sshkey/azuredeploy.json#L127 在这里查看完整的模板, 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')]"
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM