把一个 HelloWorld 应用推到 Azure,关键在于先选好部署目标(App Service、Function、Container 或 VM),再按资源组、存储、身份、CI/CD、监控逐步落地。本文以最实用的步骤和真实命令示例,带你从本地开发、容器化、部署、到自动化流水线与监控,确保可复现、可扩缩、可运维。


为什么要把 HelloWorld 和 Azure 集成?先说清楚
听起来像是教小孩子背乘法表,但这件事其实是把一套最简单的工作流程搭好:代码写好、打包、部署、监控和自动化。把 HelloWorld 当作示例是因为它足够简单,能让你专注在 Azure 的核心概念与操作上:资源管理、身份认证、部署选项、日志与性能观测、以及自动化流程。学会这套流程,之后替换成实际业务代码,门就打开了。
先准备:前提条件与工具
- Azure 账号:有订阅并能创建资源组、应用服务、存储等权限。
- 本地开发环境:Node.js 或 .NET SDK(示例将覆盖两种常见栈),Git。
- Azure CLI(建议最新版本)或 Azure PowerShell。
- Docker(如果打算容器化)
- 可选工具:Visual Studio Code、Azure Functions Core Tools、GitHub(用于 CI/CD)
设计决策:四种常见部署目标与适用场景
在 Azure 上,你可以选择不同托管模型,按需选择:
| 部署目标 | 适用场景 | 优点/注意点 |
| App Service | 标准 Web 应用或 API(Node/.NET/PHP 等) | 管理层面友好,自动扩缩、内置 TLS、部署方便 |
| Azure Functions | 事件驱动、短时任务、Serverless 场景 | 按调用计费,快速响应,但冷启动需注意 |
| Container(ACI / AKS) | 容器化应用、微服务、需要自定义运行环境 | 灵活,可移植,AKS 适合复杂编排 |
| VM / VM Scale Set | 对底层操作系统或特定依赖有控制需求 | 最灵活但运维成本高 |
示例选型:我们会做什么?
本文以两个并行示例展示:一个 Node.js Express HelloWorld 部署到 App Service(以及容器化到 ACI);一个简单的 C# .NET HelloWorld 发布为 Azure Function。每个示例都会给出关键命令、CI/CD 配置思路、以及监控与身份访问的配置要点。
示例一:Node.js HelloWorld 部署到 App Service(步骤详解)
1. 本地项目结构(最小示例)
先创建一个最小的 Express 应用:
/hello-node package.json index.js
index.js 内容示例:
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
app.get('/', (req, res) => res.send('Hello from Azure!'));
app.listen(port, () => console.log(`Listening on ${port}`));
2. 本地测试
- npm install
- node index.js
- 在浏览器或 curl 访问 http://localhost:3000/ 确认返回
3. 使用 Azure CLI 创建资源与部署(App Service,Linux 堆栈为例)
关键命令示例:
# 登录 az login创建资源组
az group create -n rg-helloworld -l eastus
创建 App Service Plan(Linux,B1 或 S1 视需求)
az appservice plan create -g rg-helloworld -n plan-helloworld --is-linux --sku B1
创建 Web App(Node)
az webapp create -g rg-helloworld -p plan-helloworld -n my-hello-node --runtime "NODE|16-lts"
部署(zip 部署)
zip -r app.zip . az webapp deployment source config-zip -g rg-helloworld -n my-hello-node --src app.zip
执行后,访问 https://my-hello-node.azurewebsites.net/ 应能看到 HelloWorld 响应。
4. 容器化并部署到 ACI(可选)
如果你希望容器化:
# Dockerfile 示例 FROM node:16-alpine WORKDIR /app COPY package*.json ./ RUN npm ci --production COPY . . ENV PORT 3000 CMD ["node", "index.js"]
本地构建并推到容器注册表(例如 Azure Container Registry):
# 登录 ACR(先创建 ACR) az acr create -g rg-helloworld -n myacrhelloworld --sku Basic登录本地 docker 到 ACR
az acr login -n myacrhelloworld
构建并推送
docker build -t myacrhelloworld.azurecr.io/hello-node:v1 . docker push myacrhelloworld.azurecr.io/hello-node:v1
在 ACI 启动容器
az container create -g rg-helloworld -n aci-hello --image myacrhelloworld.azurecr.io/hello-node:v1 --cpu 1 --memory 1.5 --dns-name-label hello-node-demo --ports 3000
5. 配置应用设置与环境变量
- 在 App Service 中,通过 Azure Portal 或 az webapp config appsettings set 设置环境变量(如连接字符串、密钥等)。
- 敏感信息优先使用 Azure Key Vault,并在 App Service 中使用托管身份(Managed Identity)访问。
示例二:C# .NET HelloWorld 作为 Azure Function(步骤概览)
1. 本地创建与测试
使用 .NET CLI:
dotnet new func -n HelloFuncApp -lang C# cd HelloFuncApp # 创建 HttpTrigger 函数 func new --name Hello --template "HTTP trigger" func start
本地访问 http://localhost:7071/api/Hello 验证。
2. 部署到 Azure Functions
# 在 Azure 创建资源 az group create -n rg-func -l eastus az storage account create -n stfunc1234 -g rg-func -l eastus --sku Standard_LRS az functionapp create -g rg-func -n my-hello-func --storage-account stfunc1234 --consumption-plan-location eastus --runtime dotnet # 使用 Azure Functions Core Tools 部署 func azure functionapp publish my-hello-func
3. 注意点
- 选择 Consumption Plan(按量)或 Premium(有更低冷启动)取决于延迟要求。
- 若需要长期运行或 WebSocket 支持,Functions 不是最佳选择,App Service 或容器更合适。
CI/CD 实践:用 GitHub Actions 将 HelloWorld 自动部署到 Azure
把部署自动化能大幅提升迭代效率。以 Node.js 部署到 App Service 为例,最小 GitHub Actions 工作流:
name: Deploy to Azure WebApp
on:
push:
branches:
- main
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Node.js
uses: actions/setup-node@v3
with:
node-version: 16
- name: Build
run: npm install
- name: Archive files
run: zip -r app.zip .
- name: Upload to Azure WebApp
uses: azure/webapps-deploy@v2
with:
app-name: my-hello-node
package: app.zip
publish-profile: ${{ secrets.AZURE_WEBAPP_PUBLISH_PROFILE }}
关键点:使用 Azure 发布配置文件或 Service Principal(更推荐)存放在 GitHub Secrets 中。
身份与安全:使用托管身份与 Key Vault
把凭据放在代码库里肯定不行。常见做法:
- 在 Web App / Function / VM 上启用 Managed Identity(System Assigned 或 User Assigned)。
- 在 Azure Key Vault 中存放机密并授予托管身份访问策略。
- 在应用中使用 Azure SDK(例如 @azure/identity for Node)获取凭据并访问 Key Vault 或 Storage。
示例:Node.js 用托管身份访问 Key Vault
const { DefaultAzureCredential } = require('@azure/identity');
const { SecretClient } = require('@azure/keyvault-secrets');
const credential = new DefaultAzureCredential();
const client = new SecretClient("https://.vault.azure.net", credential);
async function getSecret(name) {
const res = await client.getSecret(name);
return res.value;
}
监控与故障排查:Application Insights 与日志策略
无论多小的应用,打上监控标签很重要。建议:
- 启用 Application Insights(在 App Service 创建时直接关联)。
- 在代码中添加请求追踪与异常捕获,并上报自定义事件与指标。
- 配置日志保留策略与告警:响应时间、错误率、异常堆栈大小。
简单的 Node.js 上报示例
const appInsights = require('applicationinsights');
appInsights.setup(process.env.APPINSIGHTS_INSTRUMENTATIONKEY).start();
const client = appInsights.defaultClient;
client.trackEvent({ name: "app_started" });
可观测性以外:性能与扩缩策略
- App Service:设置自动扩缩规则(基于 CPU、请求队列或自定义指标)
- Functions:选择正确的 plan(Consumption、Premium、Dedicated)
- 容器:AKS 使用 HPA(Horizontal Pod Autoscaler),结合指标服务器或 Prometheus
常见问题与排错技巧
部署失败或 500 错误
- 检查应用日志(在 App Service 的 Log Stream 或 Application Insights)。
- 确认启动命令与环境变量是否正确(尤其是 PORT)。
- 若是容器,确认镜像可以在本地运行,端口映射是否匹配。
无法访问 Key Vault 或 Storage 权限被拒绝
- 检查是否为应用分配了托管身份,并确保 Key Vault 访问策略或 Azure RBAC 已授予对应权限。
- 查看 Azure AD 日志以排查认证失败原因。
冷启动或延迟过高
- Functions 可切换到 Premium Plan 或启用预热实例。
- App Service 可使用 Always On(Windows/Linux 对应设置)避免空闲回收。
成本控制小贴士
- 非生产环境使用较低规格,或关停不使用的资源组以节省费用。
- Container Instances 适合短期测试,长期运行考虑 AKS 或 App Service 更划算。
- 使用 Azure Cost Management 报表监控消耗并设置预算告警。
把这些知识串起来:一步到位的实战路线
- 在本地把 HelloWorld 写好并测试。
- 决定部署目标:App Service(快速)、Function(事件驱动)、容器(灵活)、VM(可控)。
- 用 Azure CLI 创建资源组与基础资源,启用监控与托管身份。
- 初次部署采用 Zip 或 Core Tools,确认应用运行。
- 容器化的话,构建镜像并推到 ACR,再部署到 ACI 或 AKS。
- 建立 CI/CD(GitHub Actions / Azure DevOps),自动化测试与发布。
- 配置 Application Insights 与告警,设置扩缩策略与成本监控。
- 把密钥迁移到 Key Vault,应用使用托管身份拉取。
一些额外的实用命令速查(Azure CLI)
- 登录:az login
- 列出资源组:az group list
- 查看 Web App 日志流:az webapp log tail -n <app-name> -g <rg>
- 获取发布配置文件(可用于 CI):az webapp deployment list-publishing-profiles -n <app-name> -g <rg>
参考资料与进一步阅读
- Azure 官方文档:App Service、Functions、Container Instances、AKS
- Azure CLI 文档与 Azure SDK 使用指南
- Application Insights 与 Azure Monitor 文献
好了,整体流程其实不复杂:先把基本环节搞清楚——哪里运行、怎么认证、怎么部署、怎么监控——然后一步一步把自动化、容器化和安全补上。实践中你会发现,很多问题都是配置细节或者权限问题,多看日志就能快速定位。接下来,挑一个场景(比如 App Service + GitHub Actions)按本文示例做一遍,出问题再来修就行——这就是把 HelloWorld 拉到云端最实在的学习路径。