共用方式為


將具有受控識別的應用程式部署至 Service Fabric 受控叢集

若要部署具有受控識別的 Service Fabric 應用程式,則須透過 Azure Resource Manager 部署該應用程式,且通常使用 Azure Resource Manager 範本。 如需有關如何透過 Azure Resource Manager 部署 Service Fabric 應用程式的詳細資訊,請參閱使用 Azure Resource Manager 將應用程式部署至受控叢集

注意

未部署為 Azure 資源的應用程式無法具有受控識別。

受控叢集上的 API 版本 "2021-05-01" 支援使用受控識別的 Service Fabric 應用程式部署。

以下是可用的受控叢集範本範例:Service Fabric 受控叢集範本

Service Fabric 受控叢集中的受控識別支援

當 Service Fabric 應用程式設定適用於 Azure 資源的受控識別,並部署至叢集時,其會在 Service Fabric 受控叢集上觸發受控識別權杖服務的自動設定。 這項服務負責驗證使用了受控識別的 Service Fabric 應用程式,並代表應用程式取得存取權杖。 啟用服務後,該服務隨即顯示於左窗格 [系統] 區段下的 Service Fabric Explorer,並在名稱 fabric:/System/ManagedIdentityTokenService 下執行。

注意

當應用程式第一次使用受控識別進行部署時,您應預期會因為自動叢集設定變更而看到一次時間較長的部署。 您應預期需要 15 分鐘 (針對區域性叢集) 至 45 分鐘 (針對跨區域性叢集) 的時間。 如果正式發行前小眾測試版中有任何其他部署,受控識別設定必須等候這些部署先完成。

應用程式資源支援 SystemAssigned 或 UserAssigned 的指派,而指派可以透過下列所示的程式碼片段來完成。

{
  "type": "Microsoft.ServiceFabric/managedclusters/applications",
  "apiVersion": "2021-05-01",
  "identity": {
    "type": "SystemAssigned",
    "userAssignedIdentities": {}
  },
}

完整的 JSON 參考

使用者指派的身分識別

若應用程式要採用使用者指派的身分識別,請先將 identity 屬性新增至類型為 userAssigned 的應用程式資源,以及參考的使用者指派身分識別。 接著在包含自訂名稱清單的應用程式 properties 區段中,針對每個使用者指派的身分識別將 managedIdentities 區段新增至 principalId 對應。 如需有關使用者指派身分識別的詳細資訊,請參閱建立、列出或刪除使用者指派的受控識別

應用程式範本

若應用程式要採用使用者指派的身分識別,請先將 identity 屬性新增至類型為 userAssigned 的應用程式資源,以及參考的使用者指派身分識別;接著在包含自訂名稱清單的 properties 區段中,針對每個使用者指派的身分識別將 managedIdentities 物件新增至 principalId 對應。

{
  "apiVersion": "2021-05-01",
  "type": "Microsoft.ServiceFabric/managedclusters/applications",
  "name": "[concat(parameters('clusterName'), '/', parameters('applicationName'))]",
  "location": "[resourceGroup().location]",
  "dependsOn": [
    "[parameters('applicationVersion')]",
    "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('userAssignedIdentityName'))]"
  ],
  "identity": {
    "type" : "userAssigned",
    "userAssignedIdentities": {
        "[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('userAssignedIdentityName'))]": {}
    }
  },
  "properties": {
    "version": "[parameters('applicationVersion')]",
    "parameters": {
    },
    "managedIdentities": [
      {
        "name" : "[parameters('userAssignedIdentityName')]",
        "principalId" : "[reference(resourceId('Microsoft.ManagedIdentity/userAssignedIdentities/', parameters('userAssignedIdentityName')), '2018-11-30').principalId]"
      }
    ]
  }
}

在上述範例中,使用者指派的身分識別資源名稱會作為應用程式受控識別的自訂名稱。 以下範例假設實際自訂名稱為 "AdminUser"。

應用程式套件

  1. 針對 Azure Resource Manager 範本區段中 managedIdentities 定義的每個身分識別,在 Principals 區段下的應用程式資訊清單中新增 <ManagedIdentity> 標記。 Name 屬性須符合 managedIdentities 區段中定義的 name 屬性。

    ApplicationManifest.xml

      <Principals>
        <ManagedIdentities>
          <ManagedIdentity Name="AdminUser" />
        </ManagedIdentities>
      </Principals>
    
  2. ServiceManifestImport 區段中,為使用受控識別的服務新增 IdentityBindingPolicy。 此原則會將 AdminUser 身分識別對應至服務專屬的身分識別名稱 (稍後須新增至服務資訊清單)。

    ApplicationManifest.xml

      <ServiceManifestImport>
        <Policies>
          <IdentityBindingPolicy ServiceIdentityRef="WebAdmin" ApplicationIdentityRef="AdminUser" />
        </Policies>
      </ServiceManifestImport>
    
  3. 使用與應用程式資訊清單 IdentityBindingPolicyServiceIdentityRef 相符的名稱,在 Resources 區段內新增ManagedIdentity,以更新服務資訊清單:

    ServiceManifest.xml

      <Resources>
        ...
        <ManagedIdentities DefaultIdentity="WebAdmin">
          <ManagedIdentity Name="WebAdmin" />
        </ManagedIdentities>
      </Resources>
    

系統指派的受控識別

應用程式範本

若要使用系統所指派的受控識別來啟用應用程式,請將類型為 systemAssignedidentity 屬性新增至應用程式資源,如下例所示:

    {
      "apiVersion": "2021-05-01",
      "type": "Microsoft.ServiceFabric/managedclusters/applications",
      "name": "[concat(parameters('clusterName'), '/', parameters('applicationName'))]",
      "location": "[resourceGroup().location]",
      "dependsOn": [
        "[concat('Microsoft.ServiceFabric/clusters/', parameters('clusterName'), '/applicationTypes/', parameters('applicationTypeName'), '/versions/', parameters('applicationTypeVersion'))]"
      ],
      "identity": {
        "type" : "systemAssigned"
      },
      "properties": {
        "typeName": "[parameters('applicationTypeName')]",
        "typeVersion": "[parameters('applicationTypeVersion')]",
        "parameters": {
        }
      }
    }

此屬性會分別向 Azure Resource Manager、受控識別和 Service Fabric 資源提供者宣告,此資源應具有隱含的 (system assigned) 受控身分識別。

應用程式和服務封裝

  1. 更新應用程式資訊清單,在 [主體] 區段中新增含單一項目的 ManagedIdentity 元素,如下所示:

    ApplicationManifest.xml

    <Principals>
      <ManagedIdentities>
        <ManagedIdentity Name="SystemAssigned" />
      </ManagedIdentities>
    </Principals>
    

    這會將指派給應用程式的身分識別對應至自訂名稱,進一步指派給構成應用程式的服務。

  2. 在要指派受控識別的服務對應 ServiceManifestImport 區段中新增 IdentityBindingPolicy 元素,如下所示:

    ApplicationManifest.xml

      <ServiceManifestImport>
        <Policies>
          <IdentityBindingPolicy ServiceIdentityRef="WebAdmin" ApplicationIdentityRef="SystemAssigned" />
        </Policies>
      </ServiceManifestImport>
    

    此元素會將應用程式的身分識別指派給服務;若未進行此指派,該服務將無法存取應用程式的身分識別。 在上述程式碼片段中,SystemAssigned 身分識別 (為保留關鍵字) 會對應至自訂名稱 WebAdmin 下的服務定義。

  3. 更新服務資訊清單,在 [資源] 區段中新增 ManagedIdentity 元素,且其名稱應符合應用程式資訊清單中 IdentityBindingPolicy 定義的 ServiceIdentityRef 設定值:

    ServiceManifest.xml

      <Resources>
        ...
        <ManagedIdentities DefaultIdentity="WebAdmin">
          <ManagedIdentity Name="WebAdmin" />
        </ManagedIdentities>
      </Resources>
    

    此為身分識別與服務的相等對應 (如上述),但從服務定義的觀點而言, 此處則會依應用程式資訊清單中宣告的自訂名稱 (WebAdmin)參考該身分識別。

下一步