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

已完成

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

限制

  • 您只能將 Azure 檔案儲存體的共用掛接至 Linux 容器。
  • Azure 檔案共用的磁碟區掛接需要 Linux 容器以身分執行。
  • Azure 檔案共用的磁碟區掛接僅限 CIFS 支援。

部署容器和掛接磁碟區

若要使用 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 名稱標籤錯誤訊息,請更新上述命令中的值。

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

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

下列 YAML 範本定義容器群組以及使用 aci-hellofiles 映像建立的一個容器。 該容器掛接先前建立為磁碟區的 Azure 檔案共用 acishare。 以下是 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

掛接多個磁碟區

若要在容器執行個體中掛接多個磁碟區,您必須使用 Azure Resource Manager 範本或 YAML 檔案進行部署。 若要使用範本或 YAML 檔案,請提供共用的詳細資料,並在範本的 properties 區段中填寫 volumes 陣列以定義磁碟區。

例如,如果您已在儲存體帳戶 myStorageAccount 中建立兩個 Azure 檔案儲存體的共用,名為 share1share2,則 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/"
}]