在 Azure 容器執行個體中掛接秘密磁碟區

使用「秘密」磁碟區,將敏感性資訊提供給容器群組中的容器。 「秘密」磁碟區會將您的祕密儲存在磁碟區內的檔案中,容器群組中的容器即可存取這些檔案。 藉由在「秘密」磁碟區中儲存密碼,您可以避免將 SSH 金鑰或資料庫認證等敏感性資料新增至您的應用程式程式碼。

  • 在容器群組中使用秘密進行部署之後,秘密磁碟區是唯讀狀態。
  • 所有秘密磁碟區都由 RAM 型檔案系統 tmpfs 支援;其內容永遠不會寫入靜態儲存區。

注意

「祕密」磁碟需目前僅限於 Linux 容器。 了解如何在設定環境變數中,為 Windows 和 Linux 容器傳遞安全的環境變數。 在我們設法將所有功能導入 Windows 容器的同時,您可先在概觀中找到目前的平台差異。

掛接秘密磁碟區 - Azure CLI

若要使用 Azure CLI 部署具有一或多個祕密的容器,請在 az container create 命令中包和 --secrets--secrets-mount-path 參數。 此範例會在 /mnt/secrets 掛接由兩個祕密 ("mysecret1" 和 "mysecret2") 所組成的祕密磁碟區:

az container create \
    --resource-group myResourceGroup \
    --name secret-volume-demo \
    --image mcr.microsoft.com/azuredocs/aci-helloworld \
    --secrets mysecret1="My first secret FOO" mysecret2="My second secret BAR" \
    --secrets-mount-path /mnt/secrets

下列 az container exec 輸出顯示如何在執行中的容器中開啟殼層、列出祕密磁碟區內的檔案,然後顯示其內容:

az container exec \
  --resource-group myResourceGroup \
  --name secret-volume-demo --exec-command "/bin/sh"
/usr/src/app # ls /mnt/secrets
mysecret1
mysecret2
/usr/src/app # cat /mnt/secrets/mysecret1
My first secret FOO
/usr/src/app # cat /mnt/secrets/mysecret2
My second secret BAR
/usr/src/app # exit
Bye.

掛接秘密磁碟區 - YAML

您也可以使用 Azure CLI 和 YAML 範本部署容器群組。 部署由多個容器所組成的容器群組時,偏好經由 YAML 範本進行部署。

當您透過 YAML 範本進行部署時,範本中的祕密值必須為 Base64 編碼。 不過,祕密值會出現於容器中檔案內的純文字。

下列 YAML 範本可定義含有一個容器的容器群組,而該容器會在 /mnt/secrets 掛接「祕密」磁碟區。 此祕密磁碟區有兩個含有祕密 ("mysecret1" 和 "mysecret2") 的檔案。

apiVersion: '2019-12-01'
location: eastus
name: secret-volume-demo
properties:
  containers:
  - name: aci-tutorial-app
    properties:
      environmentVariables: []
      image: mcr.microsoft.com/azuredocs/aci-helloworld:latest
      ports: []
      resources:
        requests:
          cpu: 1.0
          memoryInGB: 1.5
      volumeMounts:
      - mountPath: /mnt/secrets
        name: secretvolume1
  osType: Linux
  restartPolicy: Always
  volumes:
  - name: secretvolume1
    secret:
      mysecret1: TXkgZmlyc3Qgc2VjcmV0IEZPTwo=
      mysecret2: TXkgc2Vjb25kIHNlY3JldCBCQVIK
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 陣列。 當您透過 Resource Manager 範本進行部署時,範本中的祕密值必須為 Base64 編碼。 不過,祕密值會出現於容器中檔案內的純文字。

接下來,針對您想要掛接祕密磁碟區所在容器群組中的每個容器,填入容器定義之 properties 區段中的 volumeMounts 陣列。

下列 Resource Manager 範本可定義含有一個容器的容器群組,而該容器會在 /mnt/secrets 掛接「祕密」磁碟區。 此祕密磁碟區有兩個祕密 "mysecret1" 和 "mysecret2"。

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "variables": {
    "container1name": "aci-tutorial-app",
    "container1image": "microsoft/aci-helloworld:latest"
  },
  "resources": [
    {
      "type": "Microsoft.ContainerInstance/containerGroups",
      "apiVersion": "2021-03-01",
      "name": "secret-volume-demo",
      "location": "[resourceGroup().location]",
      "properties": {
        "containers": [
          {
            "name": "[variables('container1name')]",
            "properties": {
              "image": "[variables('container1image')]",
              "resources": {
                "requests": {
                  "cpu": 1,
                  "memoryInGb": 1.5
                }
              },
              "ports": [
                {
                  "port": 80
                }
              ],
              "volumeMounts": [
                {
                  "name": "secretvolume1",
                  "mountPath": "/mnt/secrets"
                }
              ]
            }
          }
        ],
        "osType": "Linux",
        "ipAddress": {
          "type": "Public",
          "ports": [
            {
              "protocol": "tcp",
              "port": "80"
            }
          ]
        },
        "volumes": [
          {
            "name": "secretvolume1",
            "secret": {
              "mysecret1": "TXkgZmlyc3Qgc2VjcmV0IEZPTwo=",
              "mysecret2": "TXkgc2Vjb25kIHNlY3JldCBCQVIK"
            }
          }
        ]
      }
    }
  ]
}

若要透過 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 容器執行個體中掛接其他類型的磁碟區:

安全的環境變數

將敏感性資訊提供給容器 (包括 Windows 容器) 的另一種方法是使用安全的環境變數