【文章简介】
Azure Cloud Shell 是一个用于管理 Azure 资源的、可通过浏览器访问的交互式经验证 shell。 它使用户能够灵活选择最适合自己工作方式的 shell 体验,本篇文章主要针对Linux VirtualMachines 进行生命周期管理(包括启动、停止、重启等)
【Azure Cloud Shell功能介绍及限制】
- 功能介绍
Azure Cloud Shell 是一个用于管理 Azure 资源的、可通过浏览器访问的交互式经验证 shell。 它使用户能够灵活选择最适合自己工作方式的 shell 体验,无论是 Bash 还是 PowerShell。 - 条件限制
- Cloud Shell 在按会话按用户提供的临时主机上运行
- Cloud Shell 在 20 分钟没有交互活动后将超时
- Cloud Shell 需要装载 Azure 文件共享
- Cloud Shell 对 Bash 和 PowerShell 使用相同的 Azure 文件共享
- 将针对每个用户帐户为 Cloud Shell 分配一台计算机
- Cloud Shell 使用文件共享中保存的 5-GB 映像持久保存 $HOME
- 在 Bash 中权限是按常规 Linux 用户设置的
【操作步骤】
一.通过Azure Cloud Shell查询Azure公共映像
①我们若要是查询可用的公共映像列表,可以通过在Azure Cloud Shell使用az vm image list 命令进行查询,命令格式如下:az vm image list --output table
②执行回显如下代码
ma@Azure:~$ az vm image list --output table
You are viewing an offline list of images, use --all to retrieve an up-to-date list
Offer Publisher Sku Urn UrnAlias Version
---------------------------- ---------------------- ------------------ -------------------------------------------------------------- ------------------- ---------
CentOS OpenLogic 7.5 OpenLogic:CentOS:7.5:latest CentOS latest
debian-10 Debian 10 Debian:debian-10:10:latest Debian latest
flatcar-container-linux-free kinvolk stable kinvolk:flatcar-container-linux-free:stable:latest Flatcar latest
openSUSE-Leap SUSE 42.3 SUSE:openSUSE-Leap:42.3:latest openSUSE-Leap latest
RHEL RedHat 7-LVM RedHat:RHEL:7-LVM:latest RHEL latest
SLES SUSE 15 SUSE:SLES:15:latest SLES latest
UbuntuServer Canonical 18.04-LTS Canonical:UbuntuServer:18.04-LTS:latest UbuntuLTS latest
WindowsServer MicrosoftWindowsServer 2019-Datacenter MicrosoftWindowsServer:WindowsServer:2019-Datacenter:latest Win2019Datacenter latest
WindowsServer MicrosoftWindowsServer 2016-Datacenter MicrosoftWindowsServer:WindowsServer:2016-Datacenter:latest Win2016Datacenter latest
WindowsServer MicrosoftWindowsServer 2012-R2-Datacenter MicrosoftWindowsServer:WindowsServer:2012-R2-Datacenter:latest Win2012R2Datacenter latest
WindowsServer MicrosoftWindowsServer 2012-Datacenter MicrosoftWindowsServer:WindowsServer:2012-Datacenter:latest Win2012Datacenter latest
WindowsServer MicrosoftWindowsServer 2008-R2-SP1 MicrosoftWindowsServer:WindowsServer:2008-R2-SP1:latest Win2008R2SP1 latest
③同时,我们可以通过执行az vm image list --offer CentOS --all --output table
命令来过滤出含有“Centos”操作系统的映像(查询的时间会在1分钟-3分钟之间)
CentOS OpenLogic 7.7 OpenLogic:CentOS:7.7:7.7.2020111300 7.7.2020111300
CentOS OpenLogic 7.7 OpenLogic:CentOS:7.7:7.7.2021020400 7.7.2021020400
php-7-3-with-centos-7-8 cognosys php-7-3-with-centos-7-8 cognosys:php-7-3-with-centos-7-8:php-7-3-with-centos-7-8:1.2019.1010 1.2019.1010
CentOS OpenLogic 7_4 OpenLogic:CentOS:7_4:7.4.20200220 7.4.20200220
CentOS OpenLogic 7_4-gen2 OpenLogic:CentOS:7_4-gen2:7.4.20200220 7.4.20200220
php-7-3-with-centos-7-9 cognosys php-7-3-with-centos-7-9 cognosys:php-7-3-with-centos-7-9:php-7-3-with-centos-7-9:1.2019.1010 1.2019.1010
CentOS OpenLogic 7_5-gen2 OpenLogic:CentOS:7_5-gen2:7.5.20200214 7.5.20200214
CentOS OpenLogic 7_6-gen2 OpenLogic:CentOS:7_6-gen2:7.6.20200213 7.6.20200213
④后续若要使用特定的映像来部署虚拟机,可以通过指定映像的版本号的方式来指定您所需要的的镜像,若您这边想要使用当前最新版本的镜像,您可以通过增加“latest”的参数获取最新的发行版本,举个例子,假设我们这边要创建一台Linux虚拟机,Linux虚拟机信息如下:
- 虚拟机名为:myVM2
- 虚拟机映像为Centos 7.7最新发行版本
- 远程连接认证方式为generate-ssh-keys
⑤根据要求信息执行下述命令az vm create --resource-group myResourceGroupVM --name myVM2 --image OpenLogic:CentOS:7.7:latest --generate-ssh-keys
⑥执行完毕如下图所示
⑦返回VM控制台查看相关信息,发现与我们执行创建的版本信息一致
二.通过Azure Cloud Shell查询Azure VM实例规格参数
2.1实例参数规格对比
一般我们在选择使用Auzre VM之前都会对配置参数进行选型,比如说我们要购买多少CPU、多少内存的虚拟机,要去承载我们的业务需要什么类型参数的虚拟机等等,我们在选择购买前必须要做的一个配置选型工作,去选择不同实例规格的服务器,下述列出了针对不同业务场景Azure VM类型
规格簇 |
实例规格 |
适合的业务场景 |
通用型 |
B, Dsv3, Dv3, DSv2, Dv2, Av2, DC |
主要适合搭建web应用前后端、中间件、开发测试的场景 |
计算型 |
Fsv2 |
拥有高CPU、高内存占比,主要适用于中等流量的应用程序 |
内存增强型 |
Esv3、Ev3、M、DSv2、Dv2 |
拥有较高的内存,主要适用于中到大型的缓存数据库、关系型数据库 |
存储增强型 |
Lsv2、Ls |
拥有高磁盘吞吐量和IO,主要适用于搭建各种大数据、Nosql数据库场景 |
GPU型 |
NV、NVv2、NC、NCv2、NCv3、ND |
针对视频、图片、音频处理、GPU加速的图形渲染场景 |
高性能 |
H |
CPU处理能力极强、具有可选的高吞吐网络接口,适合用于深度学习等业务场景 |
2.2实例参数查询
我们要想查询某一个地域下面可用的VM实例类型列表,可通过az vm list-sizes命令进行查询,举个例子下述参数查询美国东部地域可用的实例规格,并通过表格的方式进行输出,可通过如下命令进行查询az vm list-sizes --location eastus --output table
查询命令输出如下(美国东部地域可用的实例规格):
MaxDataDiskCount MemoryInMb Name NumberOfCores OsDiskSizeInMb ResourceDiskSizeInMb
------------------ ------------ ------------------------- --------------- ---------------- ----------------------
2 512 Standard_B1ls 1 1047552 4096
2 2048 Standard_B1ms 1 1047552 4096
2 1024 Standard_B1s 1 1047552 4096
4 8192 Standard_B2ms 2 1047552 16384
4 4096 Standard_B2s 2 1047552 8192
8 16384 Standard_B4ms 4 1047552 32768
16 32768 Standard_B8ms 8 1047552 65536
16 49152 Standard_B12ms 12 1047552 98304
32 65536 Standard_B16ms 16 1047552 131072
32 81920 Standard_B20ms 20 1047552 16384
三.通过Azure Cloud Shell对Azure VM实例规格动态变配
3.1 查看当前VM大小
az vm show --resource-group myResourceGroupVM --name myVM --query hardwareProfile.vmSize
调整之前 需查询Azure集群是否可用az vm list-vm-resize-options --resource-group myResourceGroupVM --name myVM --query [].name
大小可用,可以从开机状态直接调整az vm resize --resource-group myResourceGroupVM --name myVM --size Standard_DS4_v2
大小不可用,需要解除分配VM,再进行调整大小az vm deallocate --resource-group myResourceGroupVM --name myVM
四.通过Azure Cloud Shell对Azure VM实例状态进行管理
4.1 Azure VM实例状态
一个VM实例状态分为不同的状态(正在启动、运行中、正在停止、已停止、正在解除分配、已解除分配),下述表格是针对VM实例生命周期的不同状态的解释描述
实例状态 |
描述 |
正在启动 |
虚拟机正在启动 |
运行 |
虚拟机正在运行 |
正在停止 |
虚拟机正在停止 |
已停止 |
虚拟机已停止 |
正在解除分配 |
虚拟机正在解除分配 |
已解除分配 |
虚拟机已从虚拟机监控程序中删除,但仍在控制面板中使用 |
- |
虚拟机状态未知 |
4.2 查询Auzre VM状态
通过az vm get-instance-view 命令查询当前虚拟机状态,执行命令如下
az vm get-instance-view \
--name myVM2 \
--resource-group myResourceGroupVM \
--query instanceView.statuses[1] --output table
执行完毕以后,如下图所示:
4.3 停止Auzre VM
若要停止当前已经启动的虚拟机,或者将虚拟机由启动状态改变为停止状态,可通过"az vm stop "命令进行将虚拟机停止
az vm stop --resource-group 资源组名称 --name 虚拟机名称
举个例子:将虚拟机名为myVM2由运行状态变更为停止状态,可执行如下命令,进行操作
az vm stop --resource-group myResourceGroupVM --name myVM2
执行完毕以后,如下图所示
通过VM的门户进行查看当前虚拟机状态为stopped已停止状态,如下图所示
4.4 启动Auzre VM
若要将已停止的Auzre VM状态变更为运行中,则可以通过“az vm start”命令进行启动Azure VM虚拟机
举个例子:将虚拟机名为myVM2由停止状态变更为启动状态,可执行如下命令,进行操作
az vm start --resource-group myResourceGroupVM --name myVM2
执行完毕以后,如下图所示
通过VM的门户进行查看当前虚拟机状态为由之前的stopped已停止状态变更为运行状态,如下图所示
以上内容就是使用Azure Cloud Shell对Linux VirtualMachines 进行生命周期管理全部内容教程了,你学会了么?