$npx -y skills add chaterm/terminal-skills --skill azure-cliAzure CLI 操作
| 1 | # Azure CLI 操作 |
| 2 | |
| 3 | ## 概述 |
| 4 | Azure 资源管理、AKS、存储操作等技能。 |
| 5 | |
| 6 | ## 配置与认证 |
| 7 | |
| 8 | ```bash |
| 9 | # 登录 |
| 10 | az login |
| 11 | az login --use-device-code # 设备代码登录 |
| 12 | az login --service-principal -u <app-id> -p <password> --tenant <tenant-id> |
| 13 | |
| 14 | # 查看账户 |
| 15 | az account show |
| 16 | az account list --output table |
| 17 | |
| 18 | # 切换订阅 |
| 19 | az account set --subscription "subscription-name" |
| 20 | az account set --subscription "subscription-id" |
| 21 | |
| 22 | # 查看当前配置 |
| 23 | az configure --list-defaults |
| 24 | |
| 25 | # 设置默认值 |
| 26 | az configure --defaults group=myResourceGroup location=eastus |
| 27 | ``` |
| 28 | |
| 29 | ## 资源组 |
| 30 | |
| 31 | ```bash |
| 32 | # 列出资源组 |
| 33 | az group list --output table |
| 34 | |
| 35 | # 创建资源组 |
| 36 | az group create --name myResourceGroup --location eastus |
| 37 | |
| 38 | # 删除资源组 |
| 39 | az group delete --name myResourceGroup --yes --no-wait |
| 40 | |
| 41 | # 查看资源组中的资源 |
| 42 | az resource list --resource-group myResourceGroup --output table |
| 43 | ``` |
| 44 | |
| 45 | ## 虚拟机 |
| 46 | |
| 47 | ### VM 管理 |
| 48 | ```bash |
| 49 | # 列出 VM |
| 50 | az vm list --output table |
| 51 | az vm list --resource-group myResourceGroup --output table |
| 52 | |
| 53 | # 创建 VM |
| 54 | az vm create \ |
| 55 | --resource-group myResourceGroup \ |
| 56 | --name myVM \ |
| 57 | --image Ubuntu2204 \ |
| 58 | --admin-username azureuser \ |
| 59 | --generate-ssh-keys \ |
| 60 | --size Standard_B2s |
| 61 | |
| 62 | # 启动/停止 VM |
| 63 | az vm start --resource-group myResourceGroup --name myVM |
| 64 | az vm stop --resource-group myResourceGroup --name myVM |
| 65 | az vm deallocate --resource-group myResourceGroup --name myVM |
| 66 | az vm restart --resource-group myResourceGroup --name myVM |
| 67 | |
| 68 | # 删除 VM |
| 69 | az vm delete --resource-group myResourceGroup --name myVM --yes |
| 70 | |
| 71 | # 查看 VM 详情 |
| 72 | az vm show --resource-group myResourceGroup --name myVM |
| 73 | az vm get-instance-view --resource-group myResourceGroup --name myVM |
| 74 | ``` |
| 75 | |
| 76 | ### VM 操作 |
| 77 | ```bash |
| 78 | # 获取公网 IP |
| 79 | az vm list-ip-addresses --resource-group myResourceGroup --name myVM --output table |
| 80 | |
| 81 | # 打开端口 |
| 82 | az vm open-port --resource-group myResourceGroup --name myVM --port 80 |
| 83 | |
| 84 | # 调整大小 |
| 85 | az vm resize --resource-group myResourceGroup --name myVM --size Standard_D4s_v3 |
| 86 | |
| 87 | # 运行命令 |
| 88 | az vm run-command invoke \ |
| 89 | --resource-group myResourceGroup \ |
| 90 | --name myVM \ |
| 91 | --command-id RunShellScript \ |
| 92 | --scripts "apt-get update && apt-get install -y nginx" |
| 93 | ``` |
| 94 | |
| 95 | ## 存储账户 |
| 96 | |
| 97 | ### 账户管理 |
| 98 | ```bash |
| 99 | # 列出存储账户 |
| 100 | az storage account list --output table |
| 101 | |
| 102 | # 创建存储账户 |
| 103 | az storage account create \ |
| 104 | --name mystorageaccount \ |
| 105 | --resource-group myResourceGroup \ |
| 106 | --location eastus \ |
| 107 | --sku Standard_LRS |
| 108 | |
| 109 | # 获取连接字符串 |
| 110 | az storage account show-connection-string \ |
| 111 | --name mystorageaccount \ |
| 112 | --resource-group myResourceGroup |
| 113 | |
| 114 | # 获取密钥 |
| 115 | az storage account keys list \ |
| 116 | --account-name mystorageaccount \ |
| 117 | --resource-group myResourceGroup |
| 118 | ``` |
| 119 | |
| 120 | ### Blob 操作 |
| 121 | ```bash |
| 122 | # 设置环境变量 |
| 123 | export AZURE_STORAGE_CONNECTION_STRING="..." |
| 124 | |
| 125 | # 创建容器 |
| 126 | az storage container create --name mycontainer |
| 127 | |
| 128 | # 列出容器 |
| 129 | az storage container list --output table |
| 130 | |
| 131 | # 上传文件 |
| 132 | az storage blob upload \ |
| 133 | --container-name mycontainer \ |
| 134 | --name myblob \ |
| 135 | --file ./local-file.txt |
| 136 | |
| 137 | # 下载文件 |
| 138 | az storage blob download \ |
| 139 | --container-name mycontainer \ |
| 140 | --name myblob \ |
| 141 | --file ./downloaded-file.txt |
| 142 | |
| 143 | # 列出 blob |
| 144 | az storage blob list --container-name mycontainer --output table |
| 145 | |
| 146 | # 删除 blob |
| 147 | az storage blob delete --container-name mycontainer --name myblob |
| 148 | |
| 149 | # 生成 SAS URL |
| 150 | az storage blob generate-sas \ |
| 151 | --container-name mycontainer \ |
| 152 | --name myblob \ |
| 153 | --permissions r \ |
| 154 | --expiry 2024-12-31 \ |
| 155 | --full-uri |
| 156 | ``` |
| 157 | |
| 158 | ## AKS 集群 |
| 159 | |
| 160 | ```bash |
| 161 | # 列出集群 |
| 162 | az aks list --output table |
| 163 | |
| 164 | # 创建集群 |
| 165 | az aks create \ |
| 166 | --resource-group myResourceGroup \ |
| 167 | --name myAKSCluster \ |
| 168 | --node-count 3 \ |
| 169 | --node-vm-size Standard_D2s_v3 \ |
| 170 | --generate-ssh-keys |
| 171 | |
| 172 | # 获取凭证 |
| 173 | az aks get-credentials --resource-group myResourceGroup --name myAKSCluster |
| 174 | |
| 175 | # 查看集群 |
| 176 | az aks show --resource-group myResourceGroup --name myAKSCluster |
| 177 | |
| 178 | # 扩缩节点 |
| 179 | az aks scale \ |
| 180 | --resource-group myResourceGroup \ |
| 181 | --name myAKSCluster \ |
| 182 | --node-count 5 |
| 183 | |
| 184 | # 升级集群 |
| 185 | az aks get-upgrades --resource-group myResourceGroup --name myAKSCluster |
| 186 | az aks upgrade --resource-group myResourceGroup --name myAKSCluster --kubernetes-version 1.28.0 |
| 187 | |
| 188 | # 删除集群 |
| 189 | az aks delete --resource-group myResourceGroup --name myAKSCluster --yes |
| 190 | ``` |
| 191 | |
| 192 | ## App Service |
| 193 | |
| 194 | ```bash |
| 195 | # 列出 App Service 计划 |
| 196 | az appservice plan list --output table |
| 197 | |
| 198 | # 创建 App Service 计划 |
| 199 | az appservice plan create \ |
| 200 | --name myAppServicePlan \ |
| 201 | --resource-group myResourceGroup \ |
| 202 | --sku B1 \ |
| 203 | --is-linux |
| 204 | |
| 205 | # 创建 Web App |
| 206 | az webapp create \ |
| 207 | --resource-group myResourceGroup \ |
| 208 | --plan myAppServicePlan \ |
| 209 | --name myWebApp \ |
| 210 | --runtime "NODE:18-lts" |
| 211 | |
| 212 | # 部署代码 |
| 213 | az webapp deployment source config-zip \ |
| 214 | --resource-group myResourceGroup \ |
| 215 | --name myWebApp \ |
| 216 | --src app.zip |
| 217 | |
| 218 | # 查看日志 |
| 219 | az webapp log tail --resource-group myResourceGroup --name myWebApp |
| 220 | ``` |
| 221 | |
| 222 | ## 常见场景 |
| 223 | |
| 224 | ### 场景 1:批量操作资源 |
| 225 | ```bash |
| 226 | # 列出所有 VM 并停止 |
| 227 | az vm list --query "[].{name:name, rg:resourceGroup}" -o tsv | \ |
| 228 | while read name rg; do |
| 229 | az vm deallocate --resource-group "$rg" --name "$name" --no-wait |
| 230 | done |
| 231 | ``` |
| 232 | |
| 233 | ### 场景 2:导出资源模板 |
| 234 | ```bash |
| 235 | # 导出资源组模板 |
| 236 | az group export --name myResourceGroup > template.json |
| 237 | |
| 238 | # 部署模板 |
| 239 | az deployment group crea |