|
| 1 | +--- |
| 2 | +title: Quickstart for using Azure App Configuration in Azure Kubernetes Service (preview) | Microsoft Docs |
| 3 | +description: "In this quickstart, create an Azure Kubernetes Service with an ASP.NET core web app workload and use the Azure App Configuration Kubernetes Provider to load key-values from App Configuration store." |
| 4 | +services: azure-app-configuration |
| 5 | +author: junbchen |
| 6 | +ms.service: azure-app-configuration |
| 7 | +ms.devlang: csharp |
| 8 | +ms.custom: devx-track-csharp, mode-other |
| 9 | +ms.topic: quickstart |
| 10 | +ms.date: 04/06/2023 |
| 11 | +ms.author: junbchen |
| 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: Use Azure App Configuration in Azure Kubernetes Service (preview) |
| 16 | + |
| 17 | +In Kubernetes, you set up pods to consume configuration from ConfigMaps. It lets you decouple configuration from your container images, making your applications easily portable. [Azure App Configuration Kubernetes Provider](https://mcr.microsoft.com/product/azure-app-configuration/kubernetes-provider/about) can construct ConfigMaps and Secrets from your key-values and Key Vault references in Azure App Configuration. It enables you to take advantage of Azure App Configuration for the centralized storage and management of your configuration without any changes to your application code. |
| 18 | + |
| 19 | +In this quickstart, you incorporate Azure App Configuration Kubernetes Provider in an Azure Kubernetes Service workload where you run a simple ASP.NET Core app consuming configuration from environment variables. |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +* An App Configuration store. [Create a store](./quickstart-azure-app-configuration-create.md#create-an-app-configuration-store). |
| 24 | +* An Azure Container Registry. [Create a registry](/azure/aks/tutorial-kubernetes-prepare-acr#create-an-azure-container-registry). |
| 25 | +* An Azure Kubernetes Service (AKS) cluster that is granted permission to pull images from your Azure Container Registry. [Create an AKS cluster](/azure/aks/tutorial-kubernetes-deploy-cluster#create-a-kubernetes-cluster). |
| 26 | +* [.NET Core SDK](https://dotnet.microsoft.com/download) |
| 27 | +* [Azure CLI](/cli/azure/install-azure-cli) |
| 28 | +* [Docker Desktop](https://www.docker.com/products/docker-desktop/) |
| 29 | +* [helm](https://helm.sh/docs/intro/install/) |
| 30 | +* [kubectl](https://kubernetes.io/docs/tasks/tools/) |
| 31 | + |
| 32 | +> [!TIP] |
| 33 | +> The Azure Cloud Shell is a free, interactive shell that you can use to run the command line instructions in this article. It has common Azure tools preinstalled, including the .NET Core SDK. If you're logged in to your Azure subscription, launch your [Azure Cloud Shell](https://shell.azure.com) from shell.azure.com. You can learn more about Azure Cloud Shell by [reading our documentation](../cloud-shell/overview.md) |
| 34 | +> |
| 35 | +
|
| 36 | +## Create an application running in AKS |
| 37 | +In this section, you will create a simple ASP.NET Core web application running in Azure Kubernetes Service (AKS). The application reads configuration from the environment variables defined in a Kubernetes deployment. In the next section, you will enable it to consume configuration from Azure App Configuration without changing the application code. If you already have an AKS application that reads configuration from environment variables, you can skip this section and go to [Use App Configuration Kubernetes Provider](#use-app-configuration-kubernetes-provider). |
| 38 | + |
| 39 | +### Create an application |
| 40 | + |
| 41 | +1. Use the .NET Core command-line interface (CLI) and run the following command to create a new ASP.NET Core web app project in a new *MyWebApp* directory: |
| 42 | + |
| 43 | + ```dotnetcli |
| 44 | + dotnet new webapp --output MyWebApp --framework net6.0 |
| 45 | + ``` |
| 46 | +
|
| 47 | +1. Open *Index.cshtml* in the Pages directory, and update the content with the following code. |
| 48 | + |
| 49 | + ```html |
| 50 | + @page |
| 51 | + @model IndexModel |
| 52 | + @using Microsoft.Extensions.Configuration |
| 53 | + @inject IConfiguration Configuration |
| 54 | + @{ |
| 55 | + ViewData["Title"] = "Home page"; |
| 56 | + } |
| 57 | +
|
| 58 | + <style> |
| 59 | + h1 { |
| 60 | + color: @Configuration["Settings:FontColor"]; |
| 61 | + } |
| 62 | + </style> |
| 63 | +
|
| 64 | + <div class="text-center"> |
| 65 | + <h1>@Configuration["Settings:Message"]</h1> |
| 66 | + </div> |
| 67 | + ``` |
| 68 | +
|
| 69 | +### Containerize the application |
| 70 | +
|
| 71 | +1. Run the [dotnet publish](/dotnet/core/tools/dotnet-publish) command to build the app in release mode and create the assets in the *published* folder. |
| 72 | + |
| 73 | + ```dotnetcli |
| 74 | + dotnet publish -c Release -o published |
| 75 | + ``` |
| 76 | +
|
| 77 | +1. Create a file named *Dockerfile* at the root of your project directory, open it in a text editor, and enter the following content. A Dockerfile is a text file that doesn't have an extension and that is used to create a container image. |
| 78 | +
|
| 79 | + ```dockerfile |
| 80 | + FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS runtime |
| 81 | + WORKDIR /app |
| 82 | + COPY published/ ./ |
| 83 | + ENTRYPOINT ["dotnet", "MyWebApp.dll"] |
| 84 | + ``` |
| 85 | +
|
| 86 | +1. Build a container image named *aspnetapp* by running the following command. |
| 87 | +
|
| 88 | + ```docker |
| 89 | + docker build --tag aspnetapp . |
| 90 | + ``` |
| 91 | + |
| 92 | +### Push the image to Azure Container Registry |
| 93 | + |
| 94 | +1. Run the [az acr login](/cli/azure/acr#az-acr-login) command to login your container registry. The following example logs into a registry named *myregistry*. Replace the registry name with yours. |
| 95 | + |
| 96 | + ```azurecli |
| 97 | + az acr login --name myregistry |
| 98 | + ``` |
| 99 | +
|
| 100 | + The command returns `Login Succeeded` once login is successful. |
| 101 | +
|
| 102 | +1. Use [docker tag](https://docs.docker.com/engine/reference/commandline/tag/) to create a tag *myregistry.azurecr.io/aspnetapp:v1* for the image *aspnetapp*. |
| 103 | +
|
| 104 | + ```docker |
| 105 | + docker tag aspnetapp myregistry.azurecr.io/aspnetapp:v1 |
| 106 | + ``` |
| 107 | +
|
| 108 | + > [!TIP] |
| 109 | + > To review the list of your existing docker images and tags, run `docker image ls`. In this scenario, you should see at least two images: `aspnetapp` and `myregistry.azurecr.io/aspnetapp`. |
| 110 | +
|
| 111 | +1. Use [docker push](https://docs.docker.com/engine/reference/commandline/push/) to upload the image to the container registry. For example, the following command pushes the image to a repository named *aspnetapp* with tag *v1* under the registry *myregistry*. |
| 112 | +
|
| 113 | + ```docker |
| 114 | + docker push myregistry.azurecr.io/aspnetapp:v1 |
| 115 | + ``` |
| 116 | +
|
| 117 | +### Deploy the application |
| 118 | +
|
| 119 | +1. Create a *Deployment* directory in the root directory of your project. |
| 120 | +
|
| 121 | +1. Add a *deployment.yaml* file to the *Deployment* directory with the following content to create a deployment. Replace the value of `template.spec.containers.image` with the image you created in the previous step. |
| 122 | +
|
| 123 | + ```yaml |
| 124 | + apiVersion: apps/v1 |
| 125 | + kind: Deployment |
| 126 | + metadata: |
| 127 | + name: aspnetapp-demo |
| 128 | + labels: |
| 129 | + app: aspnetapp-demo |
| 130 | + spec: |
| 131 | + replicas: 1 |
| 132 | + selector: |
| 133 | + matchLabels: |
| 134 | + app: aspnetapp-demo |
| 135 | + template: |
| 136 | + metadata: |
| 137 | + labels: |
| 138 | + app: aspnetapp-demo |
| 139 | + spec: |
| 140 | + containers: |
| 141 | + - name: aspnetapp |
| 142 | + image: myregistry.azurecr.io/aspnetapp:v1 |
| 143 | + ports: |
| 144 | + - containerPort: 80 |
| 145 | + env: |
| 146 | + - name: Settings__Message |
| 147 | + value: "Message from the local configuration" |
| 148 | + - name: Settings__FontColor |
| 149 | + value: "Black" |
| 150 | + ``` |
| 151 | +
|
| 152 | +1. Add a *service.yaml* file to the *Deployment* directory with the following content to create a LoadBalancer service. |
| 153 | + |
| 154 | + ```yaml |
| 155 | + apiVersion: v1 |
| 156 | + kind: Service |
| 157 | + metadata: |
| 158 | + name: aspnetapp-demo-service |
| 159 | + spec: |
| 160 | + type: LoadBalancer |
| 161 | + ports: |
| 162 | + - port: 80 |
| 163 | + selector: |
| 164 | + app: aspnetapp-demo |
| 165 | + ``` |
| 166 | +
|
| 167 | +1. Run the following command to deploy the application to the AKS cluster. |
| 168 | +
|
| 169 | + ```console |
| 170 | + kubectl create namespace appconfig-demo |
| 171 | + kubectl apply -f ./Deployment -n appconfig-demo |
| 172 | + ``` |
| 173 | +
|
| 174 | +1. Run the following command and get the External IP address exposed by the LoadBalancer service. |
| 175 | + |
| 176 | + ```console |
| 177 | + kubectl get service configmap-demo-service -n appconfig-demo |
| 178 | + ``` |
| 179 | +
|
| 180 | +1. Open a browser window, and navigate to the IP address obtained in the previous step. The web page looks like this: |
| 181 | +
|
| 182 | +  |
| 183 | +
|
| 184 | +## Use App Configuration Kubernetes Provider |
| 185 | +
|
| 186 | +Now that you have an application running in AKS, you'll deploy the App Configuration Kubernetes Provider to your AKS cluster running as a Kubernetes controller. The provider retrieves data from your App Configuration store and creates a ConfigMap, which is consumable as environment variables by your application. |
| 187 | +
|
| 188 | +### Setup the Azure App Configuration store |
| 189 | +
|
| 190 | +1. Add following key-values to the App Configuration store and leave **Label** and **Content Type** with their default values. For more information about how to add key-values to a store using the Azure portal or the CLI, go to [Create a key-value](./quickstart-azure-app-configuration-create.md#create-a-key-value). |
| 191 | +
|
| 192 | + |**Key**|**Value**| |
| 193 | + |---|---| |
| 194 | + |Settings__FontColor|*Green*| |
| 195 | + |Settings__Message|*Hello from Azure App Configuration*| |
| 196 | + |
| 197 | +1. [Enabling the system-assigned managed identity on the Virtual Machine Scale Sets of your AKS cluster](/azure/active-directory/managed-identities-azure-resources/qs-configure-portal-windows-vmss#enable-system-assigned-managed-identity-on-an-existing-virtual-machine-scale-set). This allows the App Configuration Kubernetes Provider to use the managed identity to connect to your App Configuration store. |
| 198 | +
|
| 199 | +1. Grant read access to your App Configuration store by [assigning the managed identity the App Configuration Data Reader role](/azure/azure-app-configuration/howto-integrate-azure-managed-service-identity#grant-access-to-app-configuration). |
| 200 | +
|
| 201 | +### Install App Configuration Kubernetes Provider to AKS cluster |
| 202 | +1. Run the following command to get access credentials for your AKS cluster. Replace the value of the `name` and `resource-group` parameters with your AKS instance: |
| 203 | + |
| 204 | + ```console |
| 205 | + az aks get-credentials --name <your-aks-instance-name> --resource-group <your-aks-resource-group> |
| 206 | + ``` |
| 207 | +
|
| 208 | +1. Install Azure App Configuration Kubernetes Provider to your AKS cluster using `helm`: |
| 209 | + |
| 210 | + ```console |
| 211 | + helm install azureappconfiguration.kubernetesprovider \ |
| 212 | + oci://mcr.microsoft.com/azure-app-configuration/helmchart/kubernetes-provider \ |
| 213 | + --version 1.0.0-preview \ |
| 214 | + --namespace azappconfig-system \ |
| 215 | + --create-namespace |
| 216 | + ``` |
| 217 | +
|
| 218 | +1. Add an *appConfigurationProvider.yaml* file to the *Deployment* directory with the following content to create an `AzureAppConfigurationProvider` resource. `AzureAppConfigurationProvider` is a custom resource that defines what data to download from an Azure App Configuration store and creates a ConfigMap. |
| 219 | +
|
| 220 | + Replace the value of the `endpoint` field with the endpoint of your Azure App Configuration store. |
| 221 | + |
| 222 | + ```yaml |
| 223 | + apiVersion: azconfig.io/v1beta1 |
| 224 | + kind: AzureAppConfigurationProvider |
| 225 | + metadata: |
| 226 | + name: appconfigurationprovider-sample |
| 227 | + spec: |
| 228 | + endpoint: <your-app-configuration-store-endpoint> |
| 229 | + target: |
| 230 | + configMapName: configmap-created-by-appconfig-provider |
| 231 | + ``` |
| 232 | + |
| 233 | + > [!NOTE] |
| 234 | + > `AzureAppConfigurationProvider` is a declarative API object. It defines the desired state of the ConfigMap created from the data in your App Configuration store with the following behavior: |
| 235 | + > |
| 236 | + > - The ConfigMap will fail to be created if a ConfigMap with the same name already exists in the same namespace. |
| 237 | + > - The ConfigMap will be reset based on the present data in your App Configuration store if it's deleted or modified by any other means. |
| 238 | + > - The ConfigMap will be deleted if the App Configuration Kubernetes Provider is uninstalled. |
| 239 | +
|
| 240 | +2. Update the *deployment.yaml* file in the *Deployment* directory to use the ConfigMap `configmap-created-by-appconfig-provider` for environment variables. |
| 241 | + |
| 242 | + Replace the `env` section |
| 243 | + ```yaml |
| 244 | + env: |
| 245 | + - name: Settings__Message |
| 246 | + value: "Message from the local configuration" |
| 247 | + - name: Settings__FontColor |
| 248 | + value: "Black" |
| 249 | + ``` |
| 250 | + with |
| 251 | + ```yaml |
| 252 | + envFrom: |
| 253 | + - configMapRef: |
| 254 | + name: configmap-created-by-appconfig-provider |
| 255 | + ``` |
| 256 | +
|
| 257 | +3. Run the following command to deploy the changes. Replace the namespace if you are using your existing AKS application. |
| 258 | + |
| 259 | + ```console |
| 260 | + kubectl apply -f ./Deployment -n appconfig-demo |
| 261 | + ``` |
| 262 | +
|
| 263 | +4. Refresh the browser. The page shows updated content. |
| 264 | +
|
| 265 | +  |
| 266 | +
|
| 267 | +### Troubleshooting |
| 268 | +
|
| 269 | +If you don't see your application picking up the data from your App Configuration store, run the following command to validate that the ConfigMap is created properly. |
| 270 | +
|
| 271 | +```console |
| 272 | +kubectl get configmap configmap-created-by-appconfig-provider -n appconfig-demo |
| 273 | +``` |
| 274 | + |
| 275 | +If the ConfigMap is not created properly, run the following command to get the data retrieval status. |
| 276 | + |
| 277 | +```console |
| 278 | +kubectl get AzureAppConfigurationProvider appconfigurationprovider-sample -n appconfig-demo -o yaml |
| 279 | +``` |
| 280 | + |
| 281 | +If the Azure App Configuration Kubernetes Provider retrieved data from your App Configuration store successfully, the `phase` property under the status section of the output should be `COMPLETE`, as shown in the following example. |
| 282 | + |
| 283 | +```console |
| 284 | +$ kubectl get AzureAppConfigurationProvider appconfigurationprovider-sample -n appconfig-demo -o yaml |
| 285 | + |
| 286 | +apiVersion: azconfig.io/v1beta1 |
| 287 | +kind: AzureAppConfigurationProvider |
| 288 | + ... ... ... |
| 289 | +status: |
| 290 | + lastReconcileTime: "2023-04-06T06:17:06Z" |
| 291 | + lastSyncTime: "2023-04-06T06:17:06Z" |
| 292 | + message: Complete sync settings to ConfigMap or Secret |
| 293 | + phase: COMPLETE |
| 294 | +``` |
| 295 | + |
| 296 | +If the phase is not `COMPLETE`, the data isn't downloaded from your App Configuration store properly. Run the following command to show the logs of the Azure App Configuration Kubernetes Provider. |
| 297 | + |
| 298 | +```console |
| 299 | +kubectl logs deployment/az-appconfig-k8s-provider -n azappconfig-system |
| 300 | +``` |
| 301 | + |
| 302 | +Use the logs for further troubleshooting. For example, if you see requests to your App Configuration store are responded with *RESPONSE 403: 403 Forbidden*, it may indicate the App Configuration Kubernetes Provider doesn't have the necessary permission to access your App Configuration store. Follow the instructions in [Setup the Azure App Configuration store](#setup-the-azure-app-configuration-store) to ensure the managed identity is enabled and it's assigned the proper permission. |
| 303 | + |
| 304 | +## Clean up resources |
| 305 | + |
| 306 | +Uninstall the App Configuration Kubernetes Provider from your AKS cluster if you want to keep the AKS cluster. |
| 307 | + |
| 308 | +```console |
| 309 | +helm uninstall azureappconfiguration.kubernetesprovider --namespace azappconfig-system |
| 310 | +``` |
| 311 | + |
| 312 | +[!INCLUDE[Azure App Configuration cleanup](../../includes/azure-app-configuration-cleanup.md)] |
| 313 | + |
| 314 | +## Summary |
| 315 | + |
| 316 | +In this quickstart, you: |
| 317 | + |
| 318 | +* Created an application running in Azure Kubernetes Service (AKS). |
| 319 | +* Connected your AKS cluster to your App Configuration store using the App Configuration Kubernetes Provider. |
| 320 | +* Created a ConfigMap with data from your App Configuration store. |
| 321 | +* Ran the application with configuration from your App Configuration store without changing your application code. |
0 commit comments