Azure CLI的安装
Windows版本
从官网下载安装包并安装:https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-windows?view=azure-cli-latest&tabs=azure-cli
Linux版本
运行命令:
bash
curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
如果在运行az命令的时候出现错误:
bash
az -version
/usr/bin/az: line 3: /usr/bin/../../opt/az/bin/python3: cannot execute binary file: Exec format error
可以运行下面命令来解决:
bash
pip install azure-cli
连接自己的Azure账户
运行:
bash
az login
然后按照提示访问认证页面并输入命令行提供的ID,这样就会把命令行和你的Azure Cloud账户绑定。
如果不能自动开启浏览器,或者没有GUI,则可以运行命令:
bash
az login --use-device-code
然后将命令行中的认证码复制到 https://microsoft.com/devicelogin 即可。
和AWS CLI类似,Azure CLI会在用户的.azure目录下创建相应的配置文件azureProfile:
json
{
"installationId": "xxxxxxxxxxxxxxxxx",
"subscriptions": [
{
"id": "xxxxxxxxxxxxxxxxx",
"name": "Azure subscription 1",
"state": "Warned",
"user": {
"name": "[email protected]",
"type": "user"
},
"isDefault": false,
"tenantId": "xxxxxxxxxxxxxxxxx",
"environmentName": "AzureCloud",
"homeTenantId": "xxxxxxxxxxxxxxxxx",
"managedByTenants": []
},
{
"id": "xxxxxxxxxxxxxxxxx",
"name": "Azure subscription 1",
"state": "Enabled",
"user": {
"name": "[email protected]",
"type": "user"
},
"isDefault": true,
"tenantId": "xxxxxxxxxxxxxxxxx",
"environmentName": "AzureCloud",
"homeTenantId": "xxxxxxxxxxxxxxxxx",
"managedByTenants": []
}
]
}
可以看到,如果添加了多个账户的话,可以设置一个默认账户。
CLI的使用
ACR
登录ACR
bash
az acr login --name REGISTRY_NAME
创建并推送一个自己的Docker镜像
在自己App中创建Dockerfile后运行:
bash
sudo docker build -t lcodingapp .
sudo docker run -it --rm -p 5000:5000 --name lcodingapp_instance lcodingapp
这样就能测试容器是否正常工作。一切正常后,可以将镜像推送到ACR中:
bash
sudo docker tag lcodingapp lcodingapp.azurecr.io/lcodingapp
sudo docker push lcodingapp.azurecr.io/lcodingapp
验证registry name是否可用
bash
az acr check-name --name YOUR_REGISTRY_NAME
创建一个Registry
bash
az acr create --resource-group YOUR_RESOURCE_GROUP --name YOUR_REGISTRY_NAME --sku Basic
列举ACR中的所有registries
bash
az acr list
或
bash
az acr list --resource-group YOUR_RESOURCE_GROUP --query "[0].name" -o tsv
获取azure registry name
bash
acrName=$(az acr list --query "max_by([], &creationDate).name" --output tsv)
echo $acrName
列举所有的repositories
bash
az acr repository list -n YOUR_REGISTRY_NAME | jq -r '.[]'
获取最近10个container中最旧的一个时间戳
bash
az acr manifest list-metadata --name YOUR_ACR_REPOSITORY_NAME --registry YOUR_REGISTRY_NAME --only-show-errors --orderby time_desc --top 10 --query "[].lastUpdateTime" | jq -r 'sort | .[0]'
删除除去最近10个container image之外的镜像:
bash
az acr manifest list-metadata --name YOUR_ACR_REPOSITORY_NAME --registry YOUR_REGISTRY_NAME --only-show-errors \
--orderby time_asc --query "[?lastUpdateTime < '$containerTimestamp'].digest" -o tsv \
| xargs -I% az acr repository delete --name YOUR_REGISTRY_NAME --image $repository@% --yes
上传源代码并创建容器镜像
bash
az acr build --registry YOUR_ACR_NAME --image XXXXXX:latest .
查看Container日志
bash
az container logs --resource-group YOUR_RESOURCE_GROUP --name CONTAINER_NAME
Config
禁用自动更新
bash
az config set auto-upgrade.enable=no
禁用自动更新提示:
bash
az config set auto-upgrade.prompt=no
Cosmos DB
创建CosmosDB
bash
az cosmosdb create --name lcodingdb --resource-group lcoding-rg --default-consistency-level Eventual --kind GlobaldocumentDB
Resource Group
在使用CLI创建Resource Group的时候,Location和Name为必须提供的信息。
创建Resource Group:
bash
az group create -l uksouth -n RESOURCE_GROUP_NAME
列举所有的RG:
bash
az group list --query "[].name" --output tsv
删除Resource Group中的所有资源:
bash
az group delete --name RESOURCE_GROUP_NAME
以表格方式显示所有resource:
none
Get-AzResource | Format-Table
Storage
注意:所有系统级resoruce(以$开头的)不会被列举出来
列举所有storage account
bash
az storage account list
创建Storage Account:
bash
az storage account create -n STORAGE_ACCOUNT_NAME -g YOUR_RESOURCE_GROUP -l westus --sku Standard_LRS
创建一个container
bash
az storage container create --account-name STORAGE_ACCOUNT_NAME --name YOUR_STORAGE_CONTAINER_NAME
列举所有container
bash
az storage container list --account-name STORAGE_ACCOUNT_NAME --subscription YOUR_SUBSCRIPTION_ID --auth-mode login
列举所有container,包括被soft delete的
bash
az storage container list --account-name STORAGE_ACCOUNT_NAME --include-deleted --subscription YOUR_SUBSCRIPTION_ID --auth-mode login
上传文件到container
bash
az storage blob upload --account-name STORAGE_ACCOUNT_NAME --container-name YOUR_STORAGE_CONTAINER_NAME --name test.html --file test.html
表
bash
az storage table list --account-name STORAGE_ACCOUNT_NAME --subscription YOUR_SUBSCRIPTION_ID --auth-mode login
VM
创建VM
bash
az vm create --resource-group YOUR_RESOURCE_GROUP --name YOUR_VM_NAME --image Debian --size Standard_B2s --admin-username YOUR_USERNAME --admin-password YOUR_PASSWORD
显示VM信息
bash
az vm show --resource-group RESOURCE_GROUP_NAME --name VM_NAME
Web app
检查支持的运行时
bash
az webapp list-runtimes
检查Linux的运行时
bash
az webapp list-runtimes --os-type=linux
创建App Service Plan
运行如下命令创建App Service Plan:
bash
az appservice plan create --name <App service plan name> --resource-group YOUR_RESOURCE_GROUP --is-linux
创建一个Web App
bash
az webapp create --resource-group YOUR_RESOURCE_GROUP --plan <App service plan name> --name <web app name> --deployment-container-image-name nginx
部署Vue/React构建后的项目到Azure App Service
bash
cd vuedemo
npm run build
cd dist
az webapp up --location westeurope --name lcodingsitedemo --html
更改App设置
可以通过命令行更改App的设置,比如运行时版本等:
bash
az webapp config set --linux-fx-version "DOTNETCORE|3.1" --resource-group <groupname> --name <appname>
az webapp config set --php-version 7.0 --resource-group <groupname> --name <appname>
az webapp config set --python-version 3.9 --resource-group <groupname> --name <appname>
az webapp config set --resource-group <groupname> --name <appname> --java-version 1.8 --java-container Tomcat --java-container-version 9.0
az webapp config set --resource-group <groupname> --name <appname> --settings WEBSITE_NODE_DEFAULT_VERSION=16.15.1
下载Webapi logs
bash
az webapp log download --name DOCKER_CONTAINER_NAME --resource-group YOUR_RESOURCE_GROUP