在 Azure 容器实例中装载 Azure 文件共享Mount an Azure file share in Azure Container Instances

默认情况下,Azure 容器实例是无状态的。By default, Azure Container Instances are stateless. 如果容器崩溃或停止,其所有状态都会丢失。If the container crashes or stops, all of its state is lost. 若要将状态保持至超过容器寿命,必须从外部存储装载卷。To persist state beyond the lifetime of the container, you must mount a volume from an external store. 如本文中所示,Azure 容器实例可以装载使用 Azure 文件创建的 Azure 文件共享。As shown in this article, Azure Container Instances can mount an Azure file share created with Azure Files. Azure 文件提供了承载在 Azure 存储中的完全托管的文件共享,这些共享项可通过行业标准的服务器消息块 (SMB) 协议进行访问。Azure Files offers fully managed file shares hosted in Azure Storage that are accessible via the industry standard Server Message Block (SMB) protocol. 将 Azure 文件共享与 Azure 容器实例配合使用可以提供文件共享功能,类似于将 Azure 文件共享与 Azure 虚拟机配合使用。Using an Azure file share with Azure Container Instances provides file-sharing features similar to using an Azure file share with Azure virtual machines.

备注

当前只有 Linux 容器能装载 Azure 文件共享。Mounting an Azure Files share is currently restricted to Linux containers. 可以在概述中找到当前的平台差异。Find current platform differences in the overview.

将 Azure 文件共享装载到容器实例类似于 Docker 绑定装载Mounting an Azure Files share to a container instance is similar to a Docker bind mount. 请注意,如果将共享装载到其中存在文件或目录的容器目录中,则这些文件或目录会被装载遮盖,在容器运行时将无法访问。Be aware that if you mount a share into a container directory in which files or directories exist, these files or directories are obscured by the mount and are not accessible while the container runs.

创建 Azure 文件共享Create an Azure file share

必须先创建 Azure 文件共享,才能将其用于 Azure 容器实例。Before using an Azure file share with Azure Container Instances, you must create it. 运行以下脚本来创建存储帐户,以托管文件共享和共享本身。Run the following script to create a storage account to host the file share, and the share itself. 存储帐户名必须是全局唯一的,因此该脚本会向基础字符串添加一个随机值。The storage account name must be globally unique, so the script adds a random value to the base string.

# Change these four parameters as needed
ACI_PERS_RESOURCE_GROUP=myResourceGroup
ACI_PERS_STORAGE_ACCOUNT_NAME=mystorageaccount$RANDOM
ACI_PERS_LOCATION=chinaeast2
ACI_PERS_SHARE_NAME=acishare

# Create the storage account with the parameters
az storage account create \
    --resource-group $ACI_PERS_RESOURCE_GROUP \
    --name $ACI_PERS_STORAGE_ACCOUNT_NAME \
    --location $ACI_PERS_LOCATION \
    --sku Standard_LRS

# Create the file share
az storage share create \
  --name $ACI_PERS_SHARE_NAME \
  --account-name $ACI_PERS_STORAGE_ACCOUNT_NAME

获取存储凭据Get storage credentials

若要在 Azure 容器实例中将 Azure 文件共享装载为卷,需要 3 个值:存储帐户名、共享名和存储访问密钥。To mount an Azure file share as a volume in Azure Container Instances, you need three values: the storage account name, the share name, and the storage access key.

  • 存储帐户名称 - 如果你使用了前面的脚本,则存储帐户名称存储在 $ACI_PERS_STORAGE_ACCOUNT_NAME 变量中。Storage account name - If you used the preceding script, the storage account name was stored in the $ACI_PERS_STORAGE_ACCOUNT_NAME variable. 若要查看帐户名称,请键入以下命令:To see the account name, type:

    echo $ACI_PERS_STORAGE_ACCOUNT_NAME
    
  • 共享名称 - 此值已知(在前面的脚本中定义为 acishareShare name - This value is already known (defined as acishare in the preceding script)

  • 存储帐户密钥 - 可以使用以下命令找到此值:Storage account key - This value can be found using the following command:

    STORAGE_KEY=$(az storage account keys list --resource-group $ACI_PERS_RESOURCE_GROUP --account-name $ACI_PERS_STORAGE_ACCOUNT_NAME --query "[0].value" --output tsv)
    echo $STORAGE_KEY
    

部署容器并装载卷 - CLIDeploy container and mount volume - CLI

若要使用 Azure CLI 将 Azure 文件共享作为卷装载到容器中,请在使用 az container create 创建容器时指定共享和卷装载点。To mount an Azure file share as a volume in a container by using the Azure CLI, specify the share and volume mount point when you create the container with az container create. 如果已执行前面的步骤,则可以使用以下命令装载先前创建的共享以创建容器:If you followed the previous steps, you can mount the share you created earlier by using the following command to create a container:

az container create \
    --resource-group $ACI_PERS_RESOURCE_GROUP \
    --name hellofiles \
    --image mcr.microsoft.com/azuredocs/aci-hellofiles \
    --dns-name-label aci-demo \
    --ports 80 \
    --azure-file-volume-account-name $ACI_PERS_STORAGE_ACCOUNT_NAME \
    --azure-file-volume-account-key $STORAGE_KEY \
    --azure-file-volume-share-name $ACI_PERS_SHARE_NAME \
    --azure-file-volume-mount-path /aci/logs/

--dns-name-label 值在创建容器实例的 Azure 区域中必须是唯一的。The --dns-name-label value must be unique within the Azure region where you create the container instance. 如果在执行命令时收到 DNS 名称标签错误消息,请更新前一命令中的值 。Update the value in the preceding command if you receive a DNS name label error message when you execute the command.

管理已装载卷中的文件Manage files in mounted volume

在容器启动后,可以使用通过 Microsoft aci-hellofiles 映像部署的简单 Web 应用在你指定的装载路径下的 Azure 文件共享中创建小的文本文件。Once the container starts up, you can use the simple web app deployed via the Microsoft aci-hellofiles image to create small text files in the Azure file share at the mount path you specified. 使用 az container show 命令获取 Web 应用的完全限定域名 (FQDN):Obtain the web app's fully qualified domain name (FQDN) with the az container show command:

az container show --resource-group $ACI_PERS_RESOURCE_GROUP \
  --name hellofiles --query ipAddress.fqdn --output tsv

使用应用保存文本后,可使用 Azure 门户Microsoft Azure 存储资源管理器之类的工具检索及检查写入到文件共享的文件。After saving text using the app, you can use the Azure portal or a tool like the Microsoft Azure Storage Explorer to retrieve and inspect the file or files written to the file share.

部署容器并装载卷 - YAMLDeploy container and mount volume - YAML

你还可以使用 Azure CLI 和 YAML 模板部署容器组并在容器中装载卷。You can also deploy a container group and mount a volume in a container with the Azure CLI and a YAML template. 在部署由多个容器组成的容器组时,通过 YAML 模板进行部署是首选方法。Deploying by YAML template is a preferred method when deploying container groups consisting of multiple containers.

以下 YAML 模板定义了一个容器组,其中包含使用 aci-hellofiles 映像创建的容器。The following YAML template defines a container group with one container created with the aci-hellofiles image. 该容器将之前创建的 Azure 文件共享 acishare 装载为卷。The container mounts the Azure file share acishare created previously as a volume. 在指定的位置,输入承载着文件共享的存储帐户的名称和存储密钥。Where indicated, enter the name and storage key for the storage account that hosts the file share.

与 CLI 示例中一样,dnsNameLabel 值在创建容器实例的 Azure 区域中必须是唯一的。As in the CLI example, the dnsNameLabel value must be unique within the Azure region where you create the container instance. 如果需要,请在 YAML 文件中更新该值。Update the value in the YAML file if needed.

apiVersion: '2018-10-01'
location: chinaeast2
name: file-share-demo
properties:
  containers:
  - name: hellofiles
    properties:
      environmentVariables: []
      image: mcr.microsoft.com/azuredocs/aci-hellofiles
      ports:
      - port: 80
      resources:
        requests:
          cpu: 1.0
          memoryInGB: 1.5
      volumeMounts:
      - mountPath: /aci/logs/
        name: filesharevolume
  osType: Linux
  restartPolicy: Always
  ipAddress:
    type: Public
    ports:
      - port: 80
    dnsNameLabel: aci-demo
  volumes:
  - name: filesharevolume
    azureFile:
      sharename: acishare
      storageAccountName: <Storage account name>
      storageAccountKey: <Storage account key>
tags: {}
type: Microsoft.ContainerInstance/containerGroups

若要使用 YAML 模板进行部署,请将前面的 YAML 保存到名为 deploy-aci.yaml 的文件中,然后使用 --file 参数执行 az container create 命令:To deploy with the YAML template, save the preceding YAML to a file named deploy-aci.yaml, then execute the az container create command with the --file parameter:

# Deploy with YAML template
az container create --resource-group myResourceGroup --file deploy-aci.yaml

部署容器并装载卷 - 资源管理器Deploy container and mount volume - Resource Manager

除了 CLI 和 YAML 部署外,还可以使用 Azure 资源管理器模板来部署容器组并在容器中装载卷。In addition to CLI and YAML deployment, you can deploy a container group and mount a volume in a container using an Azure Resource Manager template.

首先,在模板的容器组 properties 节中填充 volumes 数组。First, populate the volumes array in the container group properties section of the template.

然后,针对容器组中要在其中装载卷的每个容器,在容器定义的 properties 部分中填充 volumeMounts 数组。Then, for each container in which you'd like to mount the volume, populate the volumeMounts array in the properties section of the container definition.

以下资源管理器模板定义了一个容器组,其中包含使用 aci-hellofiles 映像创建的容器。The following Resource Manager template defines a container group with one container created with the aci-hellofiles image. 该容器将之前创建的 Azure 文件共享 acishare 装载为卷。The container mounts the Azure file share acishare created previously as a volume. 在指定的位置,输入承载着文件共享的存储帐户的名称和存储密钥。Where indicated, enter the name and storage key for the storage account that hosts the file share.

与前面的示例中一样,dnsNameLabel 值在创建容器实例的 Azure 区域中必须是唯一的。As in the previous examples, the dnsNameLabel value must be unique within the Azure region where you create the container instance. 如果需要,请在模板中更新该值。Update the value in the template if needed.

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "container1name": "hellofiles",
    "container1image": "mcr.microsoft.com/azuredocs/aci-hellofiles"
  },
  "resources": [
    {
      "name": "file-share-demo",
      "type": "Microsoft.ContainerInstance/containerGroups",
      "apiVersion": "2018-10-01",
      "location": "[resourceGroup().location]",
      "properties": {
        "containers": [
          {
            "name": "[variables('container1name')]",
            "properties": {
              "image": "[variables('container1image')]",
              "resources": {
                "requests": {
                  "cpu": 1,
                  "memoryInGb": 1.5
                }
              },
              "ports": [
                {
                  "port": 80
                }
              ],
              "volumeMounts": [
                {
                  "name": "filesharevolume",
                  "mountPath": "/aci/logs"
                }
              ]
            }
          }
        ],
        "osType": "Linux",
        "ipAddress": {
          "type": "Public",
          "ports": [
            {
              "protocol": "tcp",
              "port": "80"
            }
          ],
          "dnsNameLabel": "aci-demo"
        },
        "volumes": [
          {
            "name": "filesharevolume",
            "azureFile": {
                "shareName": "acishare",
                "storageAccountName": "<Storage account name>",
                "storageAccountKey": "<Storage account key>"
            }
          }
        ]
      }
    }
  ]
}

若要使用资源管理器模板进行部署,请将前面的 JSON 保存到名为 deploy-aci.json 的文件中,然后使用 --template-file 参数执行 az group deployment create 命令:To deploy with the Resource Manager template, save the preceding JSON to a file named deploy-aci.json, then execute the az group deployment create command with the --template-file parameter:

# Deploy with Resource Manager template
az group deployment create --resource-group myResourceGroup --template-file deploy-aci.json

装载多个卷Mount multiple volumes

若要将多个卷装载到容器实例中,必须使用 Azure 资源管理器模板、YAML 文件或其他编程方法进行部署。To mount multiple volumes in a container instance, you must deploy using an Azure Resource Manager template, a YAML file, or another programmatic method. 若要使用模板或 YAML 文件,请提供共享详细信息,并通过在文件的 properties 部分填充 volumes 数组来定义卷。To use a template or YAML file, provide the share details and define the volumes by populating the volumes array in the properties section of the file.

例如,如果已在存储帐户 myStorageAccount 中创建两个 Azure 文件存储(名为 share1share2),资源管理器模板中的 volumes 数组将类似于以下内容:For example, if you created two Azure Files shares named share1 and share2 in storage account myStorageAccount, the volumes array in a Resource Manager template would appear similar to the following:

"volumes": [{
  "name": "myvolume1",
  "azureFile": {
    "shareName": "share1",
    "storageAccountName": "myStorageAccount",
    "storageAccountKey": "<storage-account-key>"
  }
},
{
  "name": "myvolume2",
  "azureFile": {
    "shareName": "share2",
    "storageAccountName": "myStorageAccount",
    "storageAccountKey": "<storage-account-key>"
  }
}]

接下来,针对容器组中希望装载卷的每个容器,在容器定义的 properties 部分填充 volumeMounts 数组。Next, for each container in the container group in which you'd like to mount the volumes, populate the volumeMounts array in the properties section of the container definition. 例如,填充以下内容将装载之前定义的两个卷:myvolume1 和 myvolume2: For example, this mounts the two volumes, myvolume1 and myvolume2, previously defined:

"volumeMounts": [{
  "name": "myvolume1",
  "mountPath": "/mnt/share1/"
},
{
  "name": "myvolume2",
  "mountPath": "/mnt/share2/"
}]

后续步骤Next steps

了解如何在 Azure 容器实例中装载其他卷类型:Learn how to mount other volume types in Azure Container Instances: