什么是 Azure CLI?
Azure Command-Line 接口(CLI)是一种跨平台命令行工具,旨在连接到 Azure 并在 Azure 资源上执行管理命令。 它提供一致的可编写脚本的界面,用于管理云基础结构,无论是在终端以交互方式工作还是通过脚本自动执行任务。
什么是 Azure CLI?
使用 Azure CLI 可以:
- 以交互方式执行命令: 在终端提示符处键入命令,并查看即时结果。
- 通过脚本自动执行: 将 Bash、PowerShell 或其他 shell 脚本中的命令合并为重复任务。
- 管理所有 Azure 服务: 使用一致的语法创建、配置、删除和监视 Azure 资源。
- 随时随地工作: 在 Linux、macOS、Windows、Docker 容器或使用 Azure Cloud Shell 上安装。
安装和执行选项
平台支持:
| 平台 | Shell 选项 | 安装方法 |
|---|---|---|
| Linux | Bash、PowerShell 7 | 包管理器(apt、yum)或脚本 |
| macOS | Bash、Zsh、PowerShell 7 | Homebrew 或安装程序 |
| Windows操作系统 | cmd.exe、PowerShell、PowerShell 7 | MSI 安装程序 |
| Docker | Bash | 预生成的容器映像 |
| Azure Cloud Shell | Bash、PowerShell | 内置(无需安装) |
建议的环境:
- Azure Cloud Shell: 预安装始终保持最新版本的 Azure CLI,并自动配置身份验证。
- Linux/macOS 上的 Bash: Azure CLI 的自然 shell 具有出色的脚本功能。
- PowerShell 7: 跨平台 PowerShell 提供跨作系统的一致体验。
- Windows 命令提示符(cmd.exe): 用于简单命令执行的基本 Windows shell。
本模块稍后将深入介绍安装详细信息和命令执行模式。 在继续作之前,让我们了解 Azure CLI 体系结构和命令结构。
Azure CLI 引用类型
Azure CLI 命令 分为两类: 核心 引用和 扩展。 了解差异有助于管理 Azure CLI 安装和访问最新功能。
核心参考
核心引用 是使用 Azure CLI 自动安装的永久内置命令:
- 始终可用: 使用 Azure CLI 进行安装;不需要其他设置。
- 生产就绪: 适用于日常 Azure 管理的稳定、经过良好测试的命令。
- 自动更新: 使用 Azure Cloud Shell 时,核心引用始终是最新的。
- 全面覆盖: 包括大多数 Azure 服务和常见任务的命令。
核心引用组的示例:
-
az account- 管理 Azure 订阅。 -
az group- 管理资源组。 -
az storage- 管理 Azure 存储帐户。 -
az vm- 管理虚拟机。 -
az network- 管理网络资源。
Extensions
扩展 提供作为 Azure CLI 命令运行的可选专用功能:
- 选择加入安装: 默认情况下不包括;根据需要安装。
- 访问新功能:在 Azure 的预览版和实验性功能正式发布前抢先体验这些功能。
- 灵活更新:可独立于核心 Azure CLI 更新扩展。
- 自动提示: Azure CLI 会提示首次使用时安装扩展。
扩展的常见用例:
- Azure 服务的预览功能。
- 特定方案的专用工具(例如 Azure IoT、Azure 机器学习)。
- 供应商提供的用于第三方集成的命令。
例: 首次运行扩展命令时,Azure CLI 会提示你:
$ az aks create --resource-group myResourceGroup --name myAKSCluster
The command requires the extension aks-preview. Do you want to install it now? (Y/n): Y
Azure CLI 引用状态
Azure CLI 命令具有生命周期阶段 ,指示其稳定性和支持级别。 状态(而非类型)是判断命令是否达到了生产就绪的关键标准。
三个状态类别
| 状态 | 稳定性 | 在生产环境中使用 | 支持级别 |
|---|---|---|---|
| 正式发布 | 永久性 | 是的 | 完全Microsoft支持 |
| 公共预览版 | 可以根据反馈进行更改 | 谨慎使用 | 受 Azure 预览版条款约束 |
| 已弃用 | 将被删除 | 否 | 迁移至替代产品 |
状态详细信息:
GA(普遍可用):
- 面向生产环境的稳定指令。
- 仅在主要版本更新中包含中断性变更。
- 完整的 Microsoft 支持和 SLA 覆盖。
- 建议用于生产工作负载。
公共预览版:
- 可用于测试和反馈的新功能。
- 可能会发生中断性变更,恕不另行通知。
- 有限支持;通过 GitHub 或反馈渠道报告问题。
- 在生产使用之前在开发环境中进行测试。
荒废的:
- 计划在将来的版本中删除的命令。
- 通常替换为较新的、更好的命令。
- 文档中提供的迁移路径。
- 避免在新脚本或自动化中使用。
重要: 核心命令和扩展命令都可以具有任何状态。 核心命令可能处于预览状态,扩展命令可能为 GA。
Azure CLI 参考模式
Azure CLI 使用一致的分层命令结构 ,可轻松预测和记住命令。 了解模式后,即可使用任何 Azure 服务。
命令结构
语法模式:
az <reference-group> <subgroup> <command> --<parameter> <value>
组件细分:
| 组件 | Description | Example |
|---|---|---|
| az | 基本命令 (始终必需) | az |
| 引用组 | Azure 服务或类别 |
account、group、storage、vm |
| 子群 | 特定资源类型(可选) |
blob、container、disk |
| 命令 | 要执行的操作 |
create、list、delete、show |
| 参数 | 修改行为的选项 |
--name、--resource-group、--location |
| 价值 | 提供给参数的数据 |
myStorageAccount、eastus |
实际示例:更改 Azure 订阅
场景: 切换默认 Azure 订阅。
命令细分:
az account set --subscription "my subscription name"
-
引用组:
az account(管理 Azure 订阅)。 -
命令:
set(更改活动订阅)。 -
参数:
--subscription(指定哪个订阅)。 -
参数值:
"my subscription name"(订阅名称或 ID)。
更多命令示例
创建资源组:
az group create --name myResourceGroup --location eastus
-
引用组:
az group -
命令:
create -
参数:
--name(资源组名称),--location(Azure 区域)
列出存储帐户:
az storage account list --resource-group myResourceGroup --output table
-
引用组:
az storage -
子群:
account -
命令:
list -
参数:
--resource-group(按组筛选),--output(格式结果)
重启虚拟机:
az vm restart --name myVM --resource-group myResourceGroup
-
引用组:
az vm -
命令:
restart - 参数:(虚拟机名称),(包含组)
模式识别提示: 命令遵循自然语言模式。 如果要“在资源组中创建存储帐户”,请使用 az storage account create --resource-group。
执行 Azure CLI 命令
Azure CLI 支持两种执行模式: 交互式命令行使用和自动脚本执行。 选择最适合工作流的方法。
交互式执行
交互式模式 允许你在终端提示符处键入命令,并查看即时结果。 此方法非常适合:
- 学习 Azure CLI 命令和语法。
- 浏览 Azure 资源及其属性。
- 快速一次性任务(创建测试资源,检查状态)。
- 故障排除和调试。
工作原理:
- 打开首选 shell(Bash、PowerShell、cmd.exe、Azure Cloud Shell)。
- 在提示符下键入 Azure CLI 命令。
- 按 Enter 执行。
- 立即查看结果。
例: 以交互方式重启虚拟机:
az vm restart --resource-group MyResourceGroup --name MyVm
交互式工作流:
# Check current subscription
$ az account show --output table
# List all resource groups
$ az group list --output table
# Create a storage account
$ az storage account create --name mystorageacct --resource-group myRG --location eastus
脚本执行
脚本模式 将多个 Azure CLI 命令合并到一个脚本文件中进行自动化。 此方法对于以下方面至关重要:
- 重复任务(每天创建开发环境)。
- 复杂的多步骤操作(部署整个应用程序)。
- CI/CD 管道(自动部署)。
- 基础设施即代码 (IaC) 实现。
工作原理:
- 在脚本文件中编写 Azure CLI 命令(
.sh对于 Bash,.ps1对于 PowerShell)。 - 根据需要添加变量、循环和条件逻辑。
- 使脚本可执行(Bash:
chmod +x script.sh)。 - 使用参数执行脚本。
例: 创建多个存储帐户的 Bash 脚本:
#!/bin/bash
# File: myScriptFile.sh
resourceGroup=$1
location="eastus"
accountCount=3
for i in $(seq 1 $accountCount); do
accountName="storage${i}${RANDOM}"
echo "Creating storage account: $accountName"
az storage account create \
--name $accountName \
--resource-group $resourceGroup \
--location $location \
--sku Standard_LRS
done
执行脚本:
# Make script executable
chmod +x myScriptFile.sh
# Run script with resource group parameter
./myScriptFile.sh myResourceGroupName
PowerShell 脚本示例:
# File: myScript.ps1
param (
[Parameter(Mandatory=$true)]
[string]$ResourceGroupName
)
$location = "eastus"
$storageCount = 3
for ($i = 1; $i -le $storageCount; $i++) {
$storageAccount = "storage$i$(Get-Random)"
Write-Host "Creating storage account: $storageAccount"
az storage account create `
--name $storageAccount `
--resource-group $ResourceGroupName `
--location $location `
--sku Standard_LRS
}
执行 PowerShell 脚本:
.\myScript.ps1 -ResourceGroupName "myResourceGroupName"
选择正确的执行模式
| 用例 | 交互 | 已编写脚本 |
|---|---|---|
| 学习 | 最佳 | 不建议 |
| 一次性任务 | 最佳 | 不建议 |
| 故障排除 | 最佳 | 有时 |
| 重复任务 | 不建议 | 最佳 |
| 多步骤操作 | 有时 | 最佳 |
| CI/CD 管道 | 不建议 | 最佳 |
| 基础结构即代码 | 不建议 | 最佳 |
了解 Azure CLI 体系结构和命令结构后,让我们选择正确的环境并安装 Azure CLI。