將具有受控識別的應用程式部署到 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 參考

使用者指派的身分識別

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

應用程式範本

若要啟用具有使用者指派身分識別的應用程式,請先將身分識別屬性新增至類型為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]"
      }
    ]
  }
}

在上述範例中,使用者指派身分識別的資源名稱正用來作為應用程式的受控識別易記名稱。 下列範例假設實際的易記名稱為 「管理員 User」。

應用程式套件

  1. 針對 Azure Resource Manager 樣本區managedIdentities段中定義的每個身分識別,請在 [主體] 區段下的應用程式指令清單中新增標籤<ManagedIdentity>。 屬性 Name 必須符合 name 區段中定義的 managedIdentities 屬性。

    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. 更新服務指令清單,在 Resources 區段中新增 ManagedIdentity,其名稱符合ServiceIdentityRef應用程式指令清單中的 :IdentityBindingPolicy

    ServiceManifest.xml

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

系統指派的受控識別

應用程式範本

若要啟用具有系統指派受控識別的應用程式,請將身分識別屬性新增至應用程式資源,類型為 systemAssigned,如下列範例所示:

    {
      "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. 更新應用程式指令清單以在 Principals 區段中新增 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. 更新服務指令清單,在 Resources 區段中新增 ManagedIdentity 元素,其名稱符合應用程式指令清單中定義設定的值IdentityBindingPolicyServiceIdentityRef

    ServiceManifest.xml

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

    這是與上述服務對應的身分識別,但從服務定義的觀點來看。 身分識別會依其易記名稱 (WebAdmin) 在這裡參考,如應用程式指令清單中所宣告。

下一步