为 Azure 本地 VM 准备 Ubuntu Azure 市场平台镜像

本文介绍如何准备用于 Azure 本地虚拟机(VM)的 Ubuntu Azure 市场映像。 按照以下步骤作,可确保 VM 具有最新的安全更新、支持和集成功能。

先决条件

登录并设置订阅

  1. 连接到 Azure Local 上的计算机

  2. 登录。 类型:

    az login --use-device-code
    
  3. 设置订阅。

    az account set --subscription <Subscription ID>
    

设置和准备 Azure VM

若要设置和准备 Azure VM,请执行以下步骤:

  1. 登录到 Azure 门户

  2. 在左窗格中,依次选择 “虚拟机”、“ 创建”和“ 虚拟机”。

  3. 浏览可用的映像并选择首选的 Ubuntu 版本。

    Azure 门户图像选择页的屏幕截图。

  4. 在向导中输入所需的详细信息并完成 Azure VM 的设置。

    注释

    使用 Azure 本地不使用的用户名创建 Azure VM 来避免用户名冲突。 如果在 Azure VM 和 Azure 本地上使用同一用户名(例如“usernameA”),然后重复使用 VHD,则 VM 将保留原始登录信息。 为了获得最佳结果,请使用不同的凭据(如“usernameB”)设置 Azure 本地 VM。

  5. 部署 VM 后,转到 VM 概述 页,选择 “连接 ”选项,然后选择“ 串行控制台”。

    Azure 门户中串行控制台登录选项的屏幕截图。

  6. 使用凭据连接到 VM 并运行以下命令:

    1. 以根用户身份登录到 VM:

      sudo su
      
    2. cloud-init清理默认配置,因为它与 Azure 本地 VM 无关。

      sudo cloud-init clean
      sudo rm -rf /var/lib/cloud/ /var/log/* /tmp/*
      
    3. 清理特定于 VM 的 SSH 主机密钥。

      sudo rm -f /etc/ssh/ssh_host*
      

更改 VM 映像的数据源

若要更改 VM 映像的数据源,请执行以下步骤:

  1. 将目录更改为以下路径,并列出文件以查找数据源文件 90_dpkg.cfg。运行以下命令:

    cd /etc/cloud/cloud.cfg.d/
    ls
    

    示例输出:

    azureuser@ubuntu-image:/etc/cloud/cloud.cfg.d$ ls
    05_logging.cfg  10-azure-kvp.cfg  90-azure.cfg  90_dpkg.cfg
    
  2. 打开 90_dpkg.cfg文件。 运行以下命令:

    cat 90_dpkg.cfg
    

    示例输出:

    azureuser@ubuntu-image:/etc/cloud/cloud.cfg.d$ cat 90_dpkg.cfg
    # to update this file, run dpkg-reconfigure cloud-init
    datasource_list: [ Azure ]
    
  3. 打开 datasource_list 并将其从 Azure 更新到 NoCloud。 运行以下命令:

    sudo dpkg-reconfigure cloud-init
    

    示例输出:

    Cloud-init supports searching different "Data Sources" for information that it uses to configure a cloud instance.
    
    Warning: Only select 'Ec2' if this system will be run on a system with the EC2 metadata service present. Doing so incorrectly will result in a substantial timeout on boot.
    
    Which data sources should be searched?
    
    [ ] NoCloud: Reads info from /var/lib/cloud/seed only  
    [ ] ConfigDrive: Reads data from Openstack Config Drive  
    [ ] OpenNebula: read from OpenNebula context disk  
    [ ] DigitalOcean: reads data from Droplet datasource  
    [*] Azure: read from MS Azure cdrom. Requires walinux-agent
    <Ok>
    
    1. 通过按空格键切换(*),激活NoCloud并删除Azure。

    2. Enter 保存文件。

    示例输出:

    Cloud-init supports searching different "Data Sources" for information that it uses to configure a cloud instance.
    
    Warning: Only select 'Ec2' if this system will be run on a system with the EC2 metadata service present. Doing so incorrectly will result in a substantial timeout on boot.
    
    Which data sources should be searched?
    
    [*] NoCloud: Reads info from /var/lib/cloud/seed only  
    [ ] ConfigDrive: Reads data from Openstack Config Drive  
    [ ] OpenNebula: read from OpenNebula context disk  
    [ ] DigitalOcean: reads data from Droplet datasource  
    [ ] Azure: read from MS Azure cdrom. Requires walinux-agent
    <Ok>
    
  4. 若要检查文件是否已更新,请运行以下命令:

    cat 90_dpkg.cfg
    

    示例输出:

    azureuser@ubuntu-image:/etc/cloud/cloud.cfg.d$ cat 90_dpkg.cfg
    # to update this file, run dpkg-reconfigure cloud-init
    datasource_list: [ NoCloud ]
    
  5. 删除 bash 历史记录。 运行以下命令:

    sudo rm -f ~/.bash_history
    export HISTSIZE=0
    exit
    
  6. 停止 Azure VM,因为配置更改现已完成。

将 Azure VM OS 磁盘导出到 Azure 本地群集上的 VHD

若要将 Azure VM OS 磁盘导出到 Azure 本地群集上的 VHD,请执行以下步骤:

  1. 在 Azure 门户的 Azure 本地资源中,转到 VM 概述。 在 “设置” 选项下,选择“ 磁盘”,然后选择 “磁盘名称 ”链接。

    OS 磁盘详细信息页的屏幕截图。

  2. “设置”下,选择“ 磁盘导出”,然后选择“ 生成 URL ”以生成磁盘的安全 URL。

    包含安全 URL 生成的磁盘导出选项的屏幕截图。

  3. 复制下一步生成的安全 URL 链接。

创建 Azure 本地映像

若要使用 SAS 令牌创建 Azure 本地映像,请运行以下命令:

$rg = "<resource-group>"
$cl = "/subscriptions/<sub>/resourcegroups/$rg/providers/microsoft.extendedlocation/customlocations/<customlocation-name>"
$sas = '"https://EXAMPLE.blob.storage.azure.net/EXAMPLE/abcd<sas-token>"'

az stack-hci-vm image create -g $rg --custom-location $cl --name "<IMAGE-NAME>" --os-type "Linux" --image-path $sas

创建 Azure 本地 VM

若要使用创建的 Azure 本地 VM 映像创建 Azure 本地 VM,请按照 Azure Arc 启用的“创建 Azure 本地虚拟机”中的步骤作。