|
| 1 | +--- |
| 2 | +title: Quickstart for using Azure App Configuration Feature Management in Azure Kubernetes Service |
| 3 | +description: In this quickstart, create an ASP.NET core web app and use feature flag in it running in AKS and use the Azure App Configuration Kubernetes Provider to load key-values and feature flags from App Configuration store. |
| 4 | +services: azure-app-configuration |
| 5 | +author: linglingye |
| 6 | +ms.service: azure-app-configuration |
| 7 | +ms.devlang: csharp |
| 8 | +ms.custom: devx-track-csharp |
| 9 | +ms.topic: quickstart |
| 10 | +ms.date: 02/23/2024 |
| 11 | +ms.author: linglingye |
| 12 | +#Customer intent: As an Azure Kubernetes Service user, I want to manage all my app settings in one place using Azure App Configuration. |
| 13 | +--- |
| 14 | + |
| 15 | +# Quickstart: Add feature flags to workloads in Azure Kubernetes Service |
| 16 | + |
| 17 | +In this quickstart, you'll create a feature flag in Azure App Configuration and use it to dynamically control the visibility of a new web page in an ASP.NET Core app running in AKS without restarting or redeploying it. |
| 18 | + |
| 19 | +## Prerequisites |
| 20 | + |
| 21 | +Follow the documents to use dynamic configuration in Azure Kubernetes Service. |
| 22 | + |
| 23 | +* [Quickstart: Use Azure App Configuration in Azure Kubernetes Service](./quickstart-azure-kubernetes-service.md) |
| 24 | +* [Tutorial: Use dynamic configuration in Azure Kubernetes Service](./enable-dynamic-configuration-azure-kubernetes-service.md) |
| 25 | + |
| 26 | +## Create a feature flag |
| 27 | + |
| 28 | +Add a feature flag called *Beta* to the App Configuration store and leave **Label** and **Description** with their default values. For more information about how to add feature flags to a store using the Azure portal or the CLI, go to [Create a feature flag](./quickstart-azure-app-configuration-create.md#create-a-feature-flag). |
| 29 | + |
| 30 | +> [!div class="mx-imgBorder"] |
| 31 | +>  |
| 32 | +
|
| 33 | +## Use a feature flag |
| 34 | + |
| 35 | +In this section, you will use feature flags in a simple ASP.NET web application and run it in Azure Kubernetes Service (AKS). |
| 36 | + |
| 37 | +1. Navigate into the project's directory you created in the [Quickstart](./quickstart-azure-kubernetes-service.md), and run the following command to add a reference to the [Microsoft.FeatureManagement.AspNetCore](https://www.nuget.org/packages/Microsoft.FeatureManagement.AspNetCore) NuGet package version 3.2.0 or later. |
| 38 | + |
| 39 | + ```dotnetcli |
| 40 | + dotnet add package Microsoft.FeatureManagement.AspNetCore |
| 41 | + ``` |
| 42 | +
|
| 43 | +1. Open *program.cs*, and add feature management to the service collection of your app by calling `AddFeatureManagement`. |
| 44 | +
|
| 45 | + ```csharp |
| 46 | + // Existing code in Program.cs |
| 47 | + // ... ... |
| 48 | +
|
| 49 | + // Add a JSON configuration source |
| 50 | + builder.Configuration.AddJsonFile("config/mysettings.json", reloadOnChange: true, optional: false); |
| 51 | +
|
| 52 | + // Add feature management to the container of services. |
| 53 | + builder.Services.AddFeatureManagement(); |
| 54 | +
|
| 55 | + var app = builder.Build(); |
| 56 | +
|
| 57 | + // The rest of existing code in program.cs |
| 58 | + // ... ... |
| 59 | + ``` |
| 60 | + |
| 61 | + Add `using Microsoft.FeatureManagement;` at the top of the file if it's not present. |
| 62 | +
|
| 63 | +1. Add a new empty Razor page named **Beta** under the *Pages* directory. It includes two files *Beta.cshtml* and *Beta.cshtml.cs*. |
| 64 | +
|
| 65 | + Open *Beta.cshtml*, and update it with the following markup: |
| 66 | +
|
| 67 | + ```cshtml |
| 68 | + @page |
| 69 | + @model MyWebApp.Pages.BetaModel |
| 70 | + @{ |
| 71 | + ViewData["Title"] = "Beta Page"; |
| 72 | + } |
| 73 | +
|
| 74 | + <h1>This is the beta website.</h1> |
| 75 | + ``` |
| 76 | +
|
| 77 | + Open *Beta.cshtml.cs*, and add `FeatureGate` attribute to the `BetaModel` class. The `FeatureGate` attribute ensures the *Beta* page is accessible only when the *Beta* feature flag is enabled. If the *Beta* feature flag isn't enabled, the page will return 404 Not Found. |
| 78 | +
|
| 79 | + ```csharp |
| 80 | + using Microsoft.AspNetCore.Mvc.RazorPages; |
| 81 | + using Microsoft.FeatureManagement.Mvc; |
| 82 | +
|
| 83 | + namespace MyWebApp.Pages |
| 84 | + { |
| 85 | + [FeatureGate("Beta")] |
| 86 | + public class BetaModel : PageModel |
| 87 | + { |
| 88 | + public void OnGet() |
| 89 | + { |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + ``` |
| 94 | +
|
| 95 | +1. Open *Pages/_ViewImports.cshtml*, and register the feature manager Tag Helper using an `@addTagHelper` directive: |
| 96 | +
|
| 97 | + ```cshtml |
| 98 | + @addTagHelper *, Microsoft.FeatureManagement.AspNetCore |
| 99 | + ``` |
| 100 | +
|
| 101 | + The preceding code allows the `<feature>` Tag Helper to be used in the project's *.cshtml* files. |
| 102 | +
|
| 103 | +1. Open *_Layout.cshtml* in the *Pages*\\*Shared* directory. Insert a new `<feature>` tag in between the *Home* and *Privacy* navbar items, as shown in the highlighted lines below. |
| 104 | +
|
| 105 | + :::code language="html" source="../../includes/azure-app-configuration-navbar.md" range="22-36" highlight="6-10"::: |
| 106 | +
|
| 107 | + The `<feature>` tag ensures the *Beta* menu item is shown only when the *Beta* feature flag is enabled. |
| 108 | +
|
| 109 | +1. [Containerize the application](./quickstart-azure-kubernetes-service.md#containerize-the-application) and [Push the image to Azure Container Registry](./quickstart-azure-kubernetes-service.md#push-the-image-to-azure-container-registry). |
| 110 | +
|
| 111 | +1. [Deploy the application](./quickstart-azure-kubernetes-service.md#deploy-the-application). Refresh the browser and the web page will look like this: |
| 112 | +
|
| 113 | +  |
| 114 | +
|
| 115 | +## Use Kubernetes Provider to load feature flags |
| 116 | +
|
| 117 | +1. Update the *appConfigurationProvider.yaml* file located in the *Deployment* directory with the following content. |
| 118 | + |
| 119 | + ```yaml |
| 120 | + apiVersion: azconfig.io/v1 |
| 121 | + kind: AzureAppConfigurationProvider |
| 122 | + metadata: |
| 123 | + name: appconfigurationprovider-sample |
| 124 | + spec: |
| 125 | + endpoint: <your-app-configuration-store-endpoint> |
| 126 | + target: |
| 127 | + configMapName: configmap-created-by-appconfig-provider |
| 128 | + configMapData: |
| 129 | + type: json |
| 130 | + key: mysettings.json |
| 131 | + auth: |
| 132 | + workloadIdentity: |
| 133 | + managedIdentityClientId: <your-managed-identity-client-id> |
| 134 | + featureFlag: |
| 135 | + selectors: |
| 136 | + - keyFilter: 'Beta' |
| 137 | + refresh: |
| 138 | + enabled: true |
| 139 | + ``` |
| 140 | +
|
| 141 | + > [!TIP] |
| 142 | + > When no `selectors` are specified in `featureFlag` section, the Kubernetes Provider will not load feature flags from your App Configuration store. The default refresh interval of feature flags is 30 seconds when `featureFlag.refresh` enabled. You can customize this behavior via the `featureFlag.refresh.interval` parameter. |
| 143 | +
|
| 144 | +1. Run the following command to apply the changes. |
| 145 | +
|
| 146 | + ```console |
| 147 | + kubectl apply -f ./Deployment -n appconfig-demo |
| 148 | + ``` |
| 149 | +
|
| 150 | +1. Update the **Beta** feature flag in your App Configuration store. Enable the flag by selecting the checkbox under **Enabled**. |
| 151 | +
|
| 152 | +1. After refreshing the browser multiple times, the updated content will become visible once the ConfigMap has been updated within 30 seconds. |
| 153 | +
|
| 154 | +  |
| 155 | +
|
| 156 | +1. Select the **Beta** menu. It will bring you to the beta website that you enabled dynamically. |
| 157 | +
|
| 158 | +  |
| 159 | +
|
| 160 | +## Clean up resources |
| 161 | +
|
| 162 | +Uninstall the App Configuration Kubernetes Provider from your AKS cluster if you want to keep the AKS cluster. |
| 163 | +
|
| 164 | +```console |
| 165 | +helm uninstall azureappconfiguration.kubernetesprovider --namespace azappconfig-system |
| 166 | +``` |
| 167 | + |
| 168 | +[!INCLUDE[Azure App Configuration cleanup](../../includes/azure-app-configuration-cleanup.md)] |
| 169 | + |
| 170 | +## Next steps |
| 171 | + |
| 172 | +In this quickstart, you: |
| 173 | + |
| 174 | +* Added feature management capability to an ASP.NET Core app running in Azure Kubernetes Service (AKS). |
| 175 | +* Connected your AKS cluster to your App Configuration store using the App Configuration Kubernetes Provider. |
| 176 | +* Created a ConfigMap with key-values and feature flags from your App Configuration store. |
| 177 | +* Ran the application with dynamic configuration from your App Configuration store without changing your application code. |
| 178 | + |
| 179 | +To learn more about the Azure App Configuration Kubernetes Provider, see [Azure App Configuration Kubernetes Provider reference](./reference-kubernetes-provider.md). |
| 180 | + |
| 181 | +To learn more about feature management capability, continue to the following tutorial. |
| 182 | + |
| 183 | +> [!div class="nextstepaction"] |
| 184 | +> [Enable features for targeted audiences](./howto-targetingfilter-aspnet-core.md) |
| 185 | +
|
| 186 | +> [!div class="nextstepaction"] |
| 187 | +> [Use feature filters for conditional feature flags](./howto-feature-filters-aspnet-core.md) |
0 commit comments