共用方式為


建立 Azure Operator Nexus 虛擬機器的映像

在本文中,您將了解如何建立可用於在 Operator Nexus 中建立虛擬機器的容器映像。 具體來說,您將了解如何將虛擬磁碟新增至容器映像。 一旦建置了容器映像並將其推送到 Azure 容器登錄,它就可以用於在 Operator Nexus 中建立虛擬機器。

必要條件

在開始建立虛擬機器 (VM) 映像之前,請確保符合下列先決條件:

  • 安裝最新版的必要 Azure CLI 延伸模組

  • 本文需要 2.49.0 版或更新版本的 Azure CLI。 如果您是使用 Azure Cloud Shell,就已安裝最新版本。

  • Azure Container Registry (ACR):設定運作中的 Azure Container Registry 來儲存和管理您的容器映像。 ACR 提供了一個安全的私人登錄,用於儲存 VM 映像建立過程中使用的 Docker 映像。 您可以依照 Azure Container Registry 文件中的官方文件來建立 ACR。

  • Docker:在您的本機電腦上安裝 Docker。 Docker 是一個平台,可讓您將應用程式建置、封裝和散發為輕量型容器。 您可以使用 Docker 來建置和封裝 VM 映像。 您可以從 Docker 的官方網站下載 Docker。

注意

您可以使用 az login 命令向 Azure 進行驗證,指令碼會自動使用提供的 ACR 名稱和訂用帳戶識別碼來執行 ACR 登入。 如果您的機器上沒有安裝 Azure CLI,則您可以改為提供用於 ACR 登入的使用者名稱和密碼。

在繼續建立 VM 映像之前,請確定您的機器上已安裝了可運作的 Azure Container Registry (ACR) 和 Docker。 熟悉 ACR 和 Docker 的使用方式和功能,因為它們對於管理容器映像和建置 VM 映像至關重要。

虛擬機器映像需求

  • 請確定您的虛擬網路功能 (VNF) 映像採用 qcow2 格式,可以使用 cloud-init 啟動。

  • 您必須在映像中設定開機載入器、核心和 init 系統,以啟用以文字為基礎的序列主控台。 需要此設定才能為您的虛擬機器 (VM) 啟用主控台支援。 確保系統和終端機上的序列埠設定相符,以建立正確的通訊。

  • 您必須確定您的 VM 映像支援 cloud-init 第 2 版,以便在 VM 初始化過程中啟用進階組態選項。

  • 您必須確定您的 VM 映像包含具有 nocloud 資料來源的 cloud-init。 nocloud 資料來源允許在虛擬機器佈建期間進行初始設定和自訂。

  • 磁碟必須放置在容器內的 /disk 目錄中。

  • 支援原始和 qcow2 格式。 建議使用 Qcow2 以減少容器映像的大小。

  • 容器磁碟應基於 scratch 映像,該映像是一個空的基礎映像,除映像本身外不包含任何檔案或目錄。 使用 scratch 作為基礎映像可確保容器映像盡可能小,且只包含 VNF 的必要檔案。

建立 Operator Nexus 虛擬機器映像的步驟

您可以使用所提供的指令碼來為您的 VNF 建立映像。 它會產生一個 Dockerfile,將 VNF 磁碟映像檔複製到容器的 /disk 目錄中。

注意

下列指令碼作為範例提供。 如果您想要的話,您可以手動建立和推送容器映像,而不是按照指令碼執行。

下列環境變數可用來設定為您的 VNF 建立虛擬機器 (VM) 映像的指令碼。 在執行該指令碼之前,請使用您自己的值來修改和匯出這些變數:


# Azure subscription ID (provide if not using username-password)
export SUBSCRIPTION="your_subscription_id"

# (Mandatory) Azure Container Registry name
export ACR_NAME="your_acr_name"

# (Mandatory) Name of the container image
export CONTAINER_IMAGE_NAME="your_container_image_name"

# (Mandatory) Tag for the container image
export CONTAINER_IMAGE_TAG="your_container_image_tag"

# (Mandatory) VNF image (URL, local file, or full local path)
export VNF_IMAGE="your_vnf_image"

# (Optional) ACR URL (leave empty to derive from ACR_NAME)
export ACR_URL=""

# (Optional) ACR login username (provide if not using subscription)
export USERNAME=""

# (Optional) ACR login password (provide if not using subscription)
export PASSWORD=""

若要為您的虛擬網路功能 (VNF) 建立 VM 映像,請將提供的指令碼儲存為 create-container-disk.sh、設定必要的環境變數,然後執行該指令碼。

#!/bin/bash

# Define the required environment variables
required_vars=(
    "ACR_NAME"                  # Azure Container Registry name
    "CONTAINER_IMAGE_NAME"      # Name of the container image
    "CONTAINER_IMAGE_TAG"       # Tag for the container image
    "VNF_IMAGE"                 # VNF image (URL or file path)
)

# Verify if required environment variables are set
for var in "${required_vars[@]}"; do
    if [ -z "${!var}" ]; then
        echo "Error: $var environment variable is not set."
        exit 1
    fi
done

# Check if either SUBSCRIPTION or USERNAME with PASSWORD is provided
if [ -z "$SUBSCRIPTION" ] && [ -z "$USERNAME" ] && [ -z "$PASSWORD" ]; then
    echo "Error: Either provide SUBSCRIPTION or USERNAME with PASSWORD."
    exit 1
fi

# Set default value for DOCKERFILE_NAME if not set
if [ -z "$DOCKERFILE_NAME" ]; then
    DOCKERFILE_NAME="nexus-vm-img-dockerfile"
fi

# Check if ACR_URL is already set by the user
if [ -z "$ACR_URL" ]; then
    # Derive the ACR URL from the ACR_NAME
    ACR_URL="$ACR_NAME.azurecr.io"
fi

# Initialize variables for downloaded/copied files
downloaded_files=()

# Function to clean up downloaded files
cleanup() {
    for file in "${downloaded_files[@]}"; do
        if [ -f "$file" ]; then
            rm "$file"
        fi
    done
}

# Register the cleanup function to be called on exit
trap cleanup EXIT

# Check if the VNF image is a URL or a local file
if [[ "$VNF_IMAGE" == http* ]]; then
    # Use curl to download the file
    filename=$(basename "$VNF_IMAGE")
    # Download the VNF image file and save the output to a file
    curl -f -Lo "$filename" "$VNF_IMAGE"
    if [ $? -ne 0 ]; then
        echo "Error: Failed to download file."
        exit 1
    fi
    # Add the downloaded file to the list for cleanup
    downloaded_files+=("$filename")
elif [[ "$VNF_IMAGE" == /* ]]; then
    # Use the provided full local path
    filename=$(basename "$VNF_IMAGE")
    # Copy the VNF image file to the current directory for cleanup
    cp "$VNF_IMAGE" "./$filename"
    # Add the copied file to the list for cleanup
    downloaded_files+=("$filename")
else
    # Assume it's a local file in the current directory
    filename="$VNF_IMAGE"
fi

# Check if the file exists
if [ ! -f "$filename" ]; then
    echo "Error: File $filename does not exist."
    exit 1
fi

# Create a Dockerfile that copies the VNF image file into the container's /disk directory
# The containerDisk needs to be readable for the user with the UID 107 (qemu).
cat <<EOF > "$DOCKERFILE_NAME"
FROM scratch
ADD --chown=107:107 "$filename" /disk/
EOF

# Build the Docker image and tag it to the Azure Container Registry
docker build -f "$DOCKERFILE_NAME" -t "$CONTAINER_IMAGE_NAME:$CONTAINER_IMAGE_TAG" .

# Log in to Azure Container Registry
if [ -n "$USERNAME" ] && [ -n "$PASSWORD" ]; then
    docker login "$ACR_NAME.azurecr.io" -u "$USERNAME" -p "$PASSWORD"
else
    az acr login --name "$ACR_NAME" --subscription "$SUBSCRIPTION"
fi

docker tag "$CONTAINER_IMAGE_NAME:$CONTAINER_IMAGE_TAG" "$ACR_URL/$CONTAINER_IMAGE_NAME:$CONTAINER_IMAGE_TAG"
docker push "$ACR_URL/$CONTAINER_IMAGE_NAME:$CONTAINER_IMAGE_TAG"

# Remove the downloaded/copied files
cleanup

rm "$DOCKERFILE_NAME"

echo "VNF image $ACR_URL/$CONTAINER_IMAGE_NAME:$CONTAINER_IMAGE_TAG created successfully!"

執行該指令碼後,您將擁有一個針對您的虛擬網路功能 (VNF) 自訂的 VM 映像。 您可以使用此映像來部署您的 VNF。

範例使用方式

  1. 設定必要的環境變數。

    export SUBSCRIPTION=""00000000-0000-0000-0000-000000000000""
    export ACR_NAME="myvnfacr"
    export CONTAINER_IMAGE_NAME="ubuntu"
    export CONTAINER_IMAGE_TAG="20.04"
    export VNF_IMAGE="https://cloud-images.ubuntu.com/releases/focal/release/ubuntu-20.04-server-cloudimg-amd64.img"
    
  2. 將所提供的指令碼儲存為 create-container-disk.sh,並將其設為可執行檔。

    chmod +x create-container-disk.sh
    
  3. 執行指令碼。

    $ ./create-container-disk.sh
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                     Dload  Upload   Total   Spent    Left  Speed
    100  622M  100  622M    0     0  24.7M      0  0:00:25  0:00:25 --:--:-- 26.5M
    [+] Building 36.6s (5/5) FINISHED
     => [internal] load .dockerignore                                                              0.1s
     => => transferring context: 2B                                                                0.0s
     => [internal] load build definition from nexus-vm-img-dockerfile                              0.1s
     => => transferring dockerfile: 137B                                                           0.0s
     => [internal] load build context                                                              36.4s
     => => transferring context: 652.33MB                                                          36.3s
     => CACHED [1/1] ADD --chown=107:107 ubuntu-20.04-server-cloudimg-amd64.img /disk/             0.0s
     => exporting to image                                                                         0.0s
     => => exporting layers                                                                        0.0s
     => => writing image sha256:5b5f531c132cdbba202136b5ec41c9bfe9d91beeb5acee617c1ef902df4ca772   0.0s
     => => naming to docker.io/library/ubuntu:20.04                                                0.0s
    Login Succeeded
    The push refers to repository [myvnfacr.azurecr.io/ubuntu]
    b86efae7de58: Layer already exists
    20.04: digest: sha256:d514547ee28d9ed252167d0943d4e711547fda95161a3728c44a275f5d9669a8 size: 529
    VNF image myvnfacr.azurecr.io/ubuntu:20.04 created successfully!
    

下一步

請參閱快速入門指南,以使用您所建立的映像來部署 VNF。