在 Azure 容器執行個體中掛接 Azure 檔案共用

根據預設,Azure 容器執行個體均為無狀態。 如果容器重新啟動、損毀或停止,則其所有狀態都會遺失。 若要在容器超過存留期後保存其狀態,您必須從外部存放區掛接磁碟區。 如本文所示,Azure 容器執行個體可以掛接使用 Azure 檔案儲存體建立的 Azure 檔案共用。 Azure 檔案儲存體提供裝載於 Azure 儲存體中完全受控的檔案共用,可透過業界標準伺服器訊息區 (SMB) 通訊協定來存取。 將 Azure 檔案共用與 Azure 容器執行個體搭配使用,可提供類似於將 Azure 檔案共用與 Azure 虛擬機器搭配使用的檔案共用功能。

限制

  • 您只能將 Azure 檔案儲存體共用掛接至 Linux 容器。 請檢閱概觀以深入了解 Linux 和 Windows 容器群組在功能支援方面的差異。
  • Azure 檔案儲存體共用磁碟區掛接需要 Linux 容器以根使用者身分執行。
  • Azure 檔案共用磁碟區掛接僅限於 CIFS 支援。

注意

將 Azure 檔案儲存體共用掛接至類似於 Docker 繫結掛接的容器執行個體。 如果您將共用掛接至檔案或目錄所在的容器目錄中,掛接會遮蔽檔案或目錄,使其在容器執行時無法存取。

重要

如果您要將容器群組部署至 Azure 虛擬網路,您必須將服務端點新增至 Azure 儲存體帳戶。

建立 Azure 檔案共用

在搭配使用 Azure 檔案共用與 Azure 容器執行個體前,您必須先建立 Azure 檔案共用。 請執行下列指令碼來建立儲存體帳戶,以裝載檔案共用和共用本身。 儲存體帳戶名稱必須為全域獨有,指令碼才能為基礎字串加上隨機值。

# Change these four parameters as needed
ACI_PERS_RESOURCE_GROUP=myResourceGroup
ACI_PERS_STORAGE_ACCOUNT_NAME=mystorageaccount$RANDOM
ACI_PERS_LOCATION=eastus
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

取得儲存體認證

若要在 Azure 容器執行個體中掛接 Azure 檔案共用來作為磁碟區,您需要三個值:儲存體帳戶名稱、共用名稱和儲存體存取金鑰。

  • 儲存體帳戶名稱 - 如果您使用上述指令碼,儲存體帳戶名稱會儲存於 $ACI_PERS_STORAGE_ACCOUNT_NAME 變數中。 若要查看帳戶名稱,請輸入:

    echo $ACI_PERS_STORAGE_ACCOUNT_NAME
    
  • 共用名稱 - 此值為已知 (如上述指令碼中的 acishare 所定義)

  • 儲存體帳戶金鑰 - 您可以使用下列命令找到此值:

    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
    

部署容器和掛接磁碟區 - CLI

若要使用 Azure CLI 掛接 Azure 檔案共用以作為容器中的磁碟區,請在您使用 az container create 建立容器時,指定共用和磁碟區掛接點。 如果您已遵循上述步驟,則可以使用下列命令來掛接先前建立的共用並建立容器:

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/

這在您建立容器執行個體所在的 Azure 區域中,必須是唯一的 --dns-name-label 值。 如果您在執行命令時收到 DNS 名稱標籤錯誤訊息,請更新上方命令中的值。

管理已掛接磁碟區中的檔案

容器啟動後,即可使用透過 Microsoft aci-hellofiles 映像部署的簡單 Web 應用程式,在您指定之掛接路徑上的 Azure 檔案共用中建立小型文字檔。 使用 az container show 命令取得 Web 應用程式的完整網域名稱 (FQDN):

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

使用應用程式儲存之後,您可以使用 Azure 入口網站Microsoft Azure 儲存體總管之類的工具擷取和檢查寫入至檔案共用的檔案。

部署容器和掛接磁碟區 - YAML

您也可以使用 Azure CLI 和 YAML 範本,在容器中部署容器群組和掛接磁碟區。 部署由多個容器組成的容器群組時,偏好經由 YAML 範本進行部署。

下列 YAML 範本可定義含有一個使用 aci-hellofiles 映像所建立容器的容器群組。 容器會掛接先前建立為磁碟區的 Azure 檔案共用 acishare。 如果指出,請針對用來裝載檔案共用的儲存體帳戶輸入名稱和儲存體金鑰。

如同 CLI 範例中,這在您建立容器執行個體所在的 Azure 區域中,必須是唯一的 dnsNameLabel 值。 視需要更新 YAML 檔案中的值。

apiVersion: '2019-12-01'
location: eastus
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 的檔案,然後使用 參數執行 az container create--file 命令:

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

部署容器和掛接磁碟區 - Resource Manager

除了 CLI 和 YAML 部署,您可以使用 Azure Resource Manager 範例來部署容器群組掛接磁碟區。

首先,填入範本的容器群組 properties 區段中的 volumes 陣列。

然後,針對您想要掛接磁碟區所在的每個容器,填入容器定義 properties 區段中的 volumeMounts 陣列。

下列 Resource Manager 範本可定義含有一個使用 aci-hellofiles 映像所建立容器的容器群組。 容器會掛接先前建立為磁碟區的 Azure 檔案共用 acishare。 如果指出,請針對用來裝載檔案共用的儲存體帳戶輸入名稱和儲存體金鑰。

如同先前範例中,這在您建立容器執行個體所在的 Azure 區域中,必須是唯一的 dnsNameLabel 值。 視需要更新範本中的值。

{
  "$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": "2019-12-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>"
            }
          }
        ]
      }
    }
  ]
}

若要透過 Resource Manager 範本進行部署,請將上述 JSON 儲存至名為 deploy-aci.json 的檔案,然後使用 --template-file 參數執行 az deployment group create 命令:

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

掛接多個磁碟區

若要在容器執行個體中掛接多個磁碟區,您必須使用 Azure Resource Manager 範本、YAML 檔案或其他程式設計方法進行部署。 若要使用範本或 YAML 檔案,請提供共用詳細資料,並藉由在檔案的 properties 區段中填入 volumes 陣列來定義磁碟區。

例如,如果您已在儲存體帳戶 myStorageAccount 中建立名為 share1 和 share2 的兩個 Azure 檔案共用,則 Resource Manager 範本中會顯示與下列類似的 volumes 陣列:

"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 陣列。 例如,這會掛接兩個先前定義的磁碟區:myvolume1myvolume2

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

下一步

了解如何在 Azure 容器執行個體中掛接其他類型的磁碟區: