使用 Azure 创建虚拟机所涉及的各项资源和设置

0 阅读5分钟

🌐 使用 Azure 构建虚拟机环境(含 Bastion、NSG、共享磁盘等)


🔐 1. 创建 Azure 账号和访问控制设置(IAM)

✅ 步骤:

  1. 登录 Azure Portal

  2. 搜索并进入 “订阅”

  3. 选择你的订阅,点击左侧菜单中的 “访问控制(IAM)”

  4. 点击 “添加角色分配”

    • 角色:选择 所有者参与者、或 虚拟机管理员
    • 成员:选择你的账户或用户组。
  5. 点击“下一步”直至“复查 + 分配”完成设置。


📦 2. 创建资源组(Resource Group)

✅ 步骤:

  1. 搜索 “资源组” → 点击 “创建”。

  2. 填写:

    • 订阅:选择已有的。
    • 资源组名称:如 RG-VM-DEMO
    • 区域:比如 東日本東南アジア
  3. 创建完成后,会成为所有资源的容器。


🏠 3. 创建虚拟网络(Virtual Network)

✅ 步骤:

  1. 搜索 “Virtual Network” → “创建”。

  2. 基本设置:

    • 名称:如 VNET-VM-DEMO
    • 地址空间:如 10.0.0.0/16
  3. 子网设置:

    • Subnet-VM: 10.0.1.0/24
    • Subnet-Bastion: 10.0.2.0/27(Bastion 专用)
  4. 保持默认,点击“复查 + 创建”。


🔒 4. 创建 NSG(网络安全组)

✅ NSG 用于限制入出站流量。

创建两个 NSG:

  • NSG-Bastion
  • NSG-VNET

配置:

  1. 创建 → 资源组选 RG-VM-DEMO

  2. 进入 NSG 后,添加规则:

    • NSG-Bastion 入站规则

      • 允许 443 端口(Bastion 用)
    • NSG-VNET 入站规则

      • 允许 3389(远程桌面 RDP)或 22(SSH),来源设置为 VNET

🔐 5. 创建 Bastion

✅ 用于安全远程连接虚拟机,无需公网 IP。

步骤:

  1. 搜索 “Bastion” → 创建。

  2. 设置:

    • 名称:如 Bastion-VM
    • VNET:选择上一步创建的 VNET-VM-DEMO
    • 子网:选择 Subnet-Bastion
    • 公网 IP:新建一个
  3. 创建完成后,在 VM 里点“连接”可以通过 Bastion 登录。


💾 6. 创建共享磁盘(Azure Disk Shared)

✅ 步骤:

  1. 搜索 “磁盘” → “创建托管磁盘”

  2. 设置:

    • 名称:SharedDisk01
    • 大小:比如 128 GiB
    • 区域与 VM 保持一致
    • 启用共享:✅ 勾选“启用共享”
  3. 创建后,将此磁盘附加到多个 VM(需要支持共享磁盘的 VM 类型,如 DSEsv5 等)


📦 7. 创建存储账户(Storage Account)

✅ 用于存储 VHD、文件、镜像等。

步骤:

  1. 搜索 “Storage Account” → 创建。

  2. 设置:

    • 名称:如 storagedemo2025
    • 区域:与你 VM 区域一致
    • 性能层级:Standard
    • 复制方式:如 LRS(本地冗余)
  3. 创建后可用于挂载文件共享、Blob 存储等。


🖥️ 8. 创建客户端 CL 和服务器 SV(VM)

✅ 创建虚拟机(以 Windows 为例):

  1. 搜索 “虚拟机” → 创建。

  2. 设置:

    • 名称:如 VM-CL01(客户端),VM-SV01(服务器)
    • 区域:与你资源组一致
    • 映像:Windows Server 2022 / Ubuntu
    • 尺寸:如 Standard_D2s_v3
    • 身份验证:用户名 + 密码
  3. 网络设置:

    • VNET:选 VNET-VM-DEMO
    • 子网:选 Subnet-VM
    • 公网 IP:无(使用 Bastion 登录)
    • NSG:关联 NSG-VNET
  4. 高级 → 将共享磁盘附加到 SVCL(可选)


🌐 9. 切换公网 / 私网

Azure 支持以下网络连接切换方式:

模式方法
公网连接VM 分配公网 IP,直接使用 RDP / SSH
私网连接使用 Bastion,或通过 VPN / ExpressRoute 接入私网

✅ 切换方法:

  • 使用公网 IP:在 VM 网络设置中添加公网 IP。
  • 使用私网:将公网 IP 移除,通过 Bastion 或自建 VPN Gateway 访问。

📝 总结资源依赖图示(逻辑关系):

[Resource Group]
   ├── [VNET]
   │     ├── Subnet-VM (→ NSG-VNET)
   │     └── Subnet-Bastion (→ NSG-Bastion)
   ├── [NSG-VNET] ← 限制 VM 流量
   ├── [NSG-Bastion] ← 允许 443
   ├── [VM-CL01] ← 使用共享磁盘
   ├── [VM-SV01] ← 使用共享磁盘
   ├── [Bastion Host] ← 通过 Web 登录 VM
   ├── [共享磁盘 SharedDisk01]
   └── [Storage Account]

以下是一个完整的 Azure 虚拟机部署模板(ARM模板),包括提到的关键资源:资源组、VNET、子网、NSG、Bastion、存储账户、虚拟机(CL、SV)、共享磁盘等。


📦 Azure ARM 模板(JSON 版)

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "location": { "type": "string", "defaultValue": "japaneast" },
    "adminUsername": { "type": "string", "defaultValue": "azureuser" },
    "adminPassword": { "type": "securestring" }
  },
  "variables": {
    "vnetName": "VNET-VM-DEMO",
    "subnetVM": "Subnet-VM",
    "subnetBastion": "AzureBastionSubnet",
    "nsgVM": "NSG-VNET",
    "nsgBastion": "NSG-Bastion",
    "vmCL": "VM-CL01",
    "vmSV": "VM-SV01",
    "bastionName": "Bastion-VM",
    "storageAccount": "storagevmsharedisk",
    "sharedDiskName": "SharedDisk01",
    "vmSize": "Standard_D2s_v3"
  },
  "resources": [
    {
      "type": "Microsoft.Network/virtualNetworks",
      "apiVersion": "2021-02-01",
      "name": "[variables('vnetName')]",
      "location": "[parameters('location')]",
      "properties": {
        "addressSpace": {
          "addressPrefixes": ["10.0.0.0/16"]
        },
        "subnets": [
          {
            "name": "[variables('subnetVM')]",
            "properties": {
              "addressPrefix": "10.0.1.0/24",
              "networkSecurityGroup": {
                "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsgVM'))]"
              }
            }
          },
          {
            "name": "[variables('subnetBastion')]",
            "properties": {
              "addressPrefix": "10.0.2.0/27",
              "networkSecurityGroup": {
                "id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('nsgBastion'))]"
              }
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Network/networkSecurityGroups",
      "apiVersion": "2021-02-01",
      "name": "[variables('nsgVM')]",
      "location": "[parameters('location')]",
      "properties": {
        "securityRules": [
          {
            "name": "AllowRDP",
            "properties": {
              "protocol": "Tcp",
              "sourcePortRange": "*",
              "destinationPortRange": "3389",
              "sourceAddressPrefix": "VirtualNetwork",
              "destinationAddressPrefix": "*",
              "access": "Allow",
              "priority": 100,
              "direction": "Inbound"
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Network/networkSecurityGroups",
      "apiVersion": "2021-02-01",
      "name": "[variables('nsgBastion')]",
      "location": "[parameters('location')]",
      "properties": {
        "securityRules": [
          {
            "name": "AllowHTTPS",
            "properties": {
              "protocol": "Tcp",
              "sourcePortRange": "*",
              "destinationPortRange": "443",
              "sourceAddressPrefix": "*",
              "destinationAddressPrefix": "*",
              "access": "Allow",
              "priority": 100,
              "direction": "Inbound"
            }
          }
        ]
      }
    },
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2021-04-01",
      "name": "[variables('storageAccount')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "StorageV2",
      "properties": {}
    },
    {
      "type": "Microsoft.Compute/disks",
      "apiVersion": "2023-03-01",
      "name": "[variables('sharedDiskName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Premium_LRS"
      },
      "properties": {
        "creationData": {
          "createOption": "Empty"
        },
        "diskSizeGB": 128,
        "maxShares": 2,
        "shareable": true
      }
    },
    {
      "type": "Microsoft.Compute/virtualMachines",
      "apiVersion": "2023-03-01",
      "name": "[variables('vmCL')]",
      "location": "[parameters('location')]",
      "properties": {
        "hardwareProfile": {
          "vmSize": "[variables('vmSize')]"
        },
        "osProfile": {
          "computerName": "[variables('vmCL')]",
          "adminUsername": "[parameters('adminUsername')]",
          "adminPassword": "[parameters('adminPassword')]"
        },
        "storageProfile": {
          "imageReference": {
            "publisher": "MicrosoftWindowsServer",
            "offer": "WindowsServer",
            "sku": "2019-Datacenter",
            "version": "latest"
          },
          "osDisk": {
            "createOption": "FromImage"
          },
          "dataDisks": [
            {
              "lun": 0,
              "name": "[variables('sharedDiskName')]",
              "createOption": "Attach",
              "managedDisk": {
                "id": "[resourceId('Microsoft.Compute/disks', variables('sharedDiskName'))]"
              },
              "caching": "None"
            }
          ]
        },
        "networkProfile": {
          "networkInterfaces": []
        }
      }
    },
    {
      "type": "Microsoft.Compute/virtualMachines",
      "apiVersion": "2023-03-01",
      "name": "[variables('vmSV')]",
      "location": "[parameters('location')]",
      "properties": {
        "hardwareProfile": {
          "vmSize": "[variables('vmSize')]"
        },
        "osProfile": {
          "computerName": "[variables('vmSV')]",
          "adminUsername": "[parameters('adminUsername')]",
          "adminPassword": "[parameters('adminPassword')]"
        },
        "storageProfile": {
          "imageReference": {
            "publisher": "MicrosoftWindowsServer",
            "offer": "WindowsServer",
            "sku": "2019-Datacenter",
            "version": "latest"
          },
          "osDisk": {
            "createOption": "FromImage"
          },
          "dataDisks": [
            {
              "lun": 0,
              "name": "[variables('sharedDiskName')]",
              "createOption": "Attach",
              "managedDisk": {
                "id": "[resourceId('Microsoft.Compute/disks', variables('sharedDiskName'))]"
              },
              "caching": "None"
            }
          ]
        },
        "networkProfile": {
          "networkInterfaces": []
        }
      }
    }
  ],
  "outputs": {}
}

✅ 使用方法

通过 Azure Portal 导入模板

  1. 打开 Azure Portal
  2. 搜索 “模板部署(部署自定义模板)”
  3. 选择“构建你自己的模板”
  4. 粘贴上面的 JSON 模板,点击 “保存 + 查看 + 创建”
  5. 按照提示填写参数(管理员用户名、密码等),点击创建