概述
Azure 部署方案与 GCP 方案的最大区别在于网络架构:本教程使用 Azure Bastion 作为安全跳板,VM 完全不暴露公网 IP,通过 Bastion 隧道进行所有管理操作。这种方案安全性更高,但成本也相应更高(Bastion Standard SKU 约每月 $140)。
整个部署过程约需 20-30 分钟。
准备工作
- 拥有计算和网络资源创建权限的 Azure 订阅
- 本地已安装 Azure CLI 并登录:
az login - 安装 Azure CLI SSH 扩展:
az extension add --name ssh - Ed25519 格式的 SSH 密钥对(推荐)
关键配置参数
以下变量将在部署脚本中使用,根据实际情况调整:
RG="rg-openclaw" # 资源组名称
LOCATION="westus2" # 部署区域
VNET_CIDR="10.40.0.0/16" # 虚拟网络地址段
VM_SUBNET="10.40.2.0/24" # VM 子网地址段
BASTION_SUBNET="10.40.1.0/26" # Bastion 子网(最小 /26)
VM_SIZE="Standard_B2as_v2" # VM 规格(2 vCPU,8GB RAM)
OS_DISK_GB=64 # 系统盘大小
第一步:注册资源提供商
az login
az extension add --name ssh
# 注册必要的资源提供商
az provider register --namespace Microsoft.Compute
az provider register --namespace Microsoft.Network
第二步:创建网络基础设施
# 创建资源组
az group create -n $RG -l $LOCATION
# 创建网络安全组,仅允许来自 Bastion 子网的 SSH 访问
az network nsg create -g $RG -n nsg-vm
# 规则 1:允许 Bastion 子网 SSH(优先级最高)
az network nsg rule create -g $RG --nsg-name nsg-vm \
-n AllowBastionSSH --priority 100 \
--source-address-prefixes $BASTION_SUBNET \
--destination-port-ranges 22 --access Allow
# 规则 2:拒绝公网 SSH
az network nsg rule create -g $RG --nsg-name nsg-vm \
-n DenyInternetSSH --priority 110 \
--source-address-prefixes Internet \
--destination-port-ranges 22 --access Deny
# 规则 3:拒绝其他 VNet SSH
az network nsg rule create -g $RG --nsg-name nsg-vm \
-n DenyVNetSSH --priority 120 \
--source-address-prefixes VirtualNetwork \
--destination-port-ranges 22 --access Deny
# 创建虚拟网络和子网
az network vnet create -g $RG -n vnet-openclaw \
--address-prefixes $VNET_CIDR
az network vnet subnet create -g $RG --vnet-name vnet-openclaw \
-n subnet-vm --address-prefixes $VM_SUBNET \
--network-security-group nsg-vm
az network vnet subnet create -g $RG --vnet-name vnet-openclaw \
-n AzureBastionSubnet --address-prefixes $BASTION_SUBNET
第三步:创建 VM 和 Bastion
# 创建无公网 IP 的 VM(Ubuntu 24.04 LTS)
az vm create -g $RG -n vm-openclaw \
--image Ubuntu2404 \
--size $VM_SIZE \
--os-disk-size-gb $OS_DISK_GB \
--subnet subnet-vm \
--vnet-name vnet-openclaw \
--public-ip-address "" \
--ssh-key-values ~/.ssh/id_ed25519.pub
# 创建 Azure Bastion(Standard SKU,支持隧道功能)
az network bastion create -g $RG -n bastion-openclaw \
--vnet-name vnet-openclaw \
--public-ip-address $(az network public-ip create \
-g $RG -n pip-bastion --sku Standard --query id -o tsv) \
--sku Standard \
--enable-tunneling true
第四步:安装 OpenClaw
通过 Bastion 连接 VM(无需公网 IP):
az network bastion ssh -g $RG -n bastion-openclaw \
--target-resource-id $(az vm show -g $RG -n vm-openclaw --query id -o tsv) \
--auth-type ssh-key --username azureuser \
--ssh-key ~/.ssh/id_ed25519
在 VM 内执行安装:
# 一键安装 OpenClaw
curl -fsSL https://openclaw.ai/install.sh | bash
# 验证安装
openclaw gateway status
本地访问配置
在本地建立 Bastion 隧道访问 OpenClaw 控制面板:
// 建立 SSH 隧道后,本地访问:http://127.0.0.1:18789/
// 下方命令在本地机器执行:
// az network bastion tunnel -g $RG -n bastion-openclaw
// --target-resource-id <vm-resource-id>
// --resource-port 18789 --port 18789
# 完整命令
az network bastion tunnel \
-g $RG -n bastion-openclaw \
--target-resource-id $(az vm show -g $RG -n vm-openclaw --query id -o tsv) \
--resource-port 18789 \
--port 18789
费用说明与优化
| 资源 | 月费用 | 优化建议 |
|---|---|---|
| Azure Bastion Standard | ~$140 | 不使用时删除 Bastion,需要时重建 |
| Standard_B2as_v2 VM | ~$55 | 不使用时解除分配 VM |
| 系统磁盘(64GB) | ~$5 | 解除分配 VM 时磁盘费用仍计算 |
# 不使用时解除 VM 分配(停止计费)
az vm deallocate -g $RG -n vm-openclaw
# 重新启动
az vm start -g $RG -n vm-openclaw
清理所有资源
# 删除整个资源组(包含所有相关资源)
az group delete -n $RG --yes --no-wait
实用技巧
区域选择:如果主要用户在中国,建议选择 eastasia(香港)或 japaneast(东京)区域,网络延迟更低,但这些区域部分服务价格略高于美国区域。
Bastion 成本权衡:Azure Bastion 的高固定费用是这个方案的主要缺点。如果预算有限,可以考虑先部署不含 Bastion 的方案(VM 配置公网 IP + NSG 限制 SSH 来源 IP),安全性略低但成本可降至约 $60/月。
数据备份:OpenClaw 的所有数据存储在 VM 磁盘上。建议定期使用 openclaw backup 命令导出配置和会话,上传至 Azure Blob Storage 作为备份。