Skip to content

Commit 0413f30

Browse files
Resolve comments from zhenlan
1 parent c38ef4c commit 0413f30

File tree

1 file changed

+104
-174
lines changed

1 file changed

+104
-174
lines changed

articles/azure-app-configuration/quickstart-azure-kubernetes-service.md

Lines changed: 104 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -14,127 +14,49 @@ ms.author: junbchen
1414
# Quickstart: Use Azure App Configuration in Azure Kubernetes Service (preview)
1515
In Kubernetes, you set up pods to consume ConfigMaps for configuration. It lets you decouple configuration from your container images, making your applications easily portable. Azure App Configuration Kubernetes Provider can construct ConfigMaps based on your data 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.
1616

17-
In this quickstart, you will incorporate Azure App Configuration provider for Kubernetes in an Azure Kubernetes Service workload where you run a simple ASP.NET Core app consuming configuration from environment variables.
17+
In this quickstart, you will 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.
1818

1919
## Prerequisites
2020

21-
* Azure subscription - [create one for free](https://azure.microsoft.com/free/dotnet)
21+
* An Azure account with an active subscription. [Create one for free](https://azure.microsoft.com/free/).
22+
* An App Configuration store. [Create a store](./quickstart-azure-app-configuration-create.md#create-an-app-configuration-store).
23+
* An Azure Container Registry. [Create a registry](/azure/aks/tutorial-kubernetes-prepare-acr?tabs=azure-cli#create-an-azure-container-registry).
24+
* An Azure Kubernetes Service (AKS) cluster that integrates with the Azure Container Registry you created. [Create an AKS cluster](/azure/aks/tutorial-kubernetes-deploy-cluster?tabs=azure-cli#create-a-kubernetes-cluster).
2225
* [.NET Core SDK](https://dotnet.microsoft.com/download)
2326
* [Azure CLI](/cli/azure/install-azure-cli)
2427
* [helm](https://helm.sh/docs/intro/install/)
2528
* [kubectl](https://kubernetes.io/docs/tasks/tools/)
2629

2730
> [!TIP]
2831
> 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)
32+
>
2933
30-
## Create an Azure App Configuration store
31-
[!INCLUDE[Azure App Configuration resource creation steps](../../includes/azure-app-configuration-create.md)]
32-
33-
1. Select **Operations** > **Configuration explorer** > **Create** > **Key-value** to add the following key-values:
34-
35-
|**Key**|**Value**|
36-
|---|---|
37-
|TestApp__Settings__BackgroundColor|*white*|
38-
|TestApp__Settings__FontColor|*black*|
39-
|TestApp__Settings__FontSize|*24*|
40-
|TestApp__Settings__Message|*Data from Azure App Configuration*|
41-
42-
Leave **Label** and **Content type** empty for now. Select **Apply**.
43-
44-
## Create a Container Registry and AKS cluster
45-
46-
1. Create an Azure Container Registry (ACR) by following this [doc](/azure/aks/tutorial-kubernetes-prepare-acr?tabs=azure-cli#create-an-azure-container-registry).
47-
48-
2. Create an Azure Kubernetes Service (AKS) cluster, which integrates with the ACR you created by following this [doc](/azure/aks/tutorial-kubernetes-deploy-cluster?tabs=azure-cli#create-a-kubernetes-cluster).
49-
50-
3. Run the following command to set environment variables, set **ACR_Name** with the name of ACR you created, **AKS_Name** with the name of AKS you created, **AKS_Resource_Group** with the resource group of AKS you created:
51-
52-
```bash
53-
export ACR_Name='name-of-acr-you-just-created'
54-
export AKS_Name='name-of-aks-you-just-created'
55-
export AKS_Resource_Group='resource-group-of-aks-you-just-created'
56-
```
57-
58-
Restart the command prompt to allow the change to take effect. Print the value of the environment variable to validate that it's set properly.
59-
60-
## Enable System Assigned Managed Identity of AKS NodePool
61-
62-
Go to the corresponding Virtual Machine Scale Sets resource of AKS, and enable system-assigned managed identity on the Virtual Machine Scale Sets by following this [doc](/azure/active-directory/managed-identities-azure-resources/qs-configure-portal-windows-vmss#enable-system-assigned-managed-identity-on-an-existing-virtual-machine-scale-set).
63-
64-
## System Assigned Managed Identity role assignment
65-
66-
Once the system-assigned managed identity has been enabled, you need to grant it read access to Azure AppConfiguration. You can do it by following the instructions in this [doc](/azure/azure-app-configuration/howto-integrate-azure-managed-service-identity?tabs=core5x&pivots=framework-dotnet#grant-access-to-app-configuration).
34+
## Create an application consuming environment variables
35+
If you already have an application that is consuming environment variables as configuration, you can just skip this step. We just create an ASP.NET Core Web App `MyWebApp` as an example.
6736

68-
## Install App Configuration Kubernetes Provider to your AKS cluster
69-
70-
Install the Azure App Configuration Kubernetes Provider to your AKS cluster, which was created in the previous step.
71-
1. Get the credential to manage your AKS cluster:
72-
```bash
73-
az aks get-credentials --name $AKS_Name --resource-group $AKS_Resource_Group
37+
1. Use the .NET Core command-line interface (CLI) to create a new ASP.NET Core web app project. Run the following command to create an ASP.NET Core web app in a new TestAppConfig folder:
38+
``` dotnetcli
39+
dotnet new webapp --output MyWebApp --framework net6.0
7440
```
75-
2. Install Azure App Configuration Kubernetes Provider to your AKS cluster using `helm`:
76-
``` bash
77-
helm install azureappconfiguration.kubernetesprovider oci://mcr.microsoft.com/azure-app-configuration/helmchart/kubernetes-provider --version 1.0.0-alpha --namespace azappconfig-system --create-namespace
78-
```
79-
80-
## Create a sample ASP.NET Core web app
81-
82-
Use the .NET Core command-line interface (CLI) to create a new ASP.NET Core web app project. The Azure Cloud Shell provides these tools for you. They're also available across the Windows, macOS, and Linux platforms.
8341
84-
Run the following command to create an ASP.NET Core web app in a new TestAppConfig folder:
85-
86-
``` dotnetcli
87-
dotnet new webapp --output TestAppConfig --framework net6.0
88-
```
89-
Do several updates to the web app project you created.
90-
1. Add a *Settings.cs* file at the root of your project directory. It defines a strongly typed Settings class for the configuration you're going to use. Replace the namespace with the name of your project.
91-
``` csharp
92-
namespace TestAppConfig
93-
{
94-
public class Settings
95-
{
96-
public string BackgroundColor { get; set; }
97-
public long FontSize { get; set; }
98-
public string FontColor { get; set; }
99-
public string Message { get; set; }
100-
}
101-
}
102-
```
103-
2. Bind the `TestApp:Settings` section in configuration to the `Settings` object.
104-
105-
Update *Program.cs* with the following code and add the `TestAppConfig` namespace at the beginning of the file.
106-
107-
```csharp
108-
using TestAppConfig;
109-
110-
// Existing code in Program.cs
111-
// ... ...
112-
113-
builder.Services.AddRazorPages();
114-
115-
// Bind configuration "TestApp:Settings" section to the Settings object
116-
builder.Services.Configure<Settings>(builder.Configuration.GetSection("TestApp:Settings"));
117-
118-
var app = builder.Build();
119-
120-
// The rest of existing code in program.cs
121-
// ... ...
122-
```
123-
3. Open *Index.cshtml.cs* in the Pages directory, and update the `IndexModel` class with the following code. Add the `using Microsoft.Extensions.Options` namespace at the beginning of the file, if it's not already there.
42+
1. Open *Index.cshtml.cs* in the Pages directory, and update the `IndexModel` class with the following code.
12443
``` csharp
12544
public class IndexModel : PageModel
12645
{
127-
private readonly ILogger<IndexModel> _logger;
128-
public Settings Settings { get; }
46+
private readonly IConfiguration _configuration;
12947
130-
public IndexModel(IOptionsSnapshot<Settings> options, ILogger<IndexModel> logger)
48+
public IndexModel(IConfiguration configuration)
49+
{
50+
_configuration = configuration;
51+
}
52+
public void OnGet()
13153
{
132-
Settings = options.Value;
133-
_logger = logger;
54+
ViewData["Message"] = _configuration.GetSection("Settings")["Message"];
55+
ViewData["FontColor"] = _configuration.GetSection("Settings")["FontColor"];
13456
}
13557
}
13658
```
137-
4. Open *Index.cshtml* in the Pages directory, and update the content with the following code.
59+
1. Open *Index.cshtml* in the Pages directory, and update the content with the following code.
13860
``` html
13961
@page
14062
@model IndexModel
@@ -143,71 +65,107 @@ Do several updates to the web app project you created.
14365
}
14466
14567
<style>
146-
body {
147-
background-color: @Model.Settings.BackgroundColor;
148-
}
149-
15068
h1 {
151-
color: @Model.Settings.FontColor;
152-
font-size: @Model.Settings.FontSize;
69+
color: @ViewData["FontColor"];
15370
}
15471
</style>
155-
156-
<h1>@Model.Settings.Message</h1>
72+
<div class="text-center">
73+
<h1 class="display-4">@ViewData["Message"]</h1>
74+
</div>
15775
```
15876
159-
## Containerize the web app and push to ACR
160-
161-
1. Create a *Dockerfile* at the root of your project directory, and put the following content into the file.
77+
## Containerize the application
78+
If you already have an application, you can containerize it in a way that depends on your application. We just show you how to build the `MyWebApp` project to an image.
79+
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.
80+
``` dotnetcli
81+
dotnet publish -c Release -o published
82+
```
83+
1. Create a file named *Dockerfile* in the directory containing your .csproj file, 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.
16284
``` dockerfile
163-
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
85+
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS runtime
16486
WORKDIR /app
165-
EXPOSE 80
166-
EXPOSE 443
87+
COPY published/ ./
88+
ENTRYPOINT ["dotnet", "MyWebApp.dll"]
89+
```
90+
1. Build the container by running the following command.
91+
``` docker
92+
docker build --tag aspnetapp .
93+
```
16794

168-
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
169-
WORKDIR /src
170-
COPY ["TestAppConfig.csproj", "TestAppConfig/"]
171-
RUN dotnet restore "TestAppConfig/TestAppConfig.csproj"
172-
WORKDIR "/src/TestAppConfig"
173-
COPY . .
174-
RUN dotnet build "TestAppConfig.csproj" -c Release -o /app/build
95+
## Push the image to Azure Container Registry
96+
1. Run the [az acr login](/cli/azure/acr#az-acr-login) command to log in to the registry.
17597

176-
FROM build AS publish
177-
RUN dotnet publish "TestAppConfig.csproj" -c Release -o /app/publish /p:UseAppHost=false
98+
```azurecli
99+
az acr login --name myregistry
100+
```
178101
179-
FROM base AS final
180-
WORKDIR /app
181-
COPY --from=publish /app/publish .
182-
ENTRYPOINT ["dotnet", "TestAppConfig.dll"]
102+
The command returns `Login Succeeded` once login is successful.
103+
104+
1. Use [docker tag](https://docs.docker.com/engine/reference/commandline/tag/) to tag the image appropriate details.
105+
106+
```docker
107+
docker tag aspnetapp myregistry.azurecr.io/aspnetapp:v1
183108
```
184109
185-
2. Sign in Azure Container Registry to have permission to push image.
186-
```bash
187-
az acr login -n $ACR_Name
110+
> [!TIP]
111+
> 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`.
112+
113+
1. Use [docker push](https://docs.docker.com/engine/reference/commandline/push/) to push the image to the container registry. This example creates the *aspnetapp* repository in ACR containing the `aspnetapp` image. In the example below, replace the placeholders `<login-server`, `<image-name>` and `<tag>` by the ACR's log-in server value, the image name and the image tag.
114+
115+
Method:
116+
117+
```docker
118+
docker push <login-server>/<image-name>:<tag>
188119
```
189120
190-
3. Run the following command to build the docker image under the directory of your project root, and push it to the Azure Container Registry that created in the previous step
121+
Example:
122+
123+
```docker
124+
docker push myregistry.azurecr.io/aspnetapp:v1
125+
```
126+
127+
## Configure the Azure App Configuration store
128+
1. Add the key-values to your Azure App Configuration that should be consumed as environment variables by your application. We're just using key-values that be consumed by `MyWebApp` as an example, 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).
129+
130+
|**Key**|**Value**|
131+
|---|---|
132+
|Settings__FontColor|*Red*|
133+
|Settings__Message|*Hello from Azure App Configuration*|
134+
135+
2. Enable System Assigned Managed Identity of AKS NodePool
136+
137+
Go to the corresponding Virtual Machine Scale Sets resource of AKS, and enable system-assigned managed identity on the Virtual Machine Scale Sets by following this [doc](/azure/active-directory/managed-identities-azure-resources/qs-configure-portal-windows-vmss#enable-system-assigned-managed-identity-on-an-existing-virtual-machine-scale-set).
138+
139+
3. Assign Data Reader role to the System Assigned Managed Identity
140+
141+
Once the system-assigned managed identity has been enabled, you need to grant it read access to Azure AppConfiguration. You can do it by following the instructions in this [doc](/azure/azure-app-configuration/howto-integrate-azure-managed-service-identity?tabs=core5x&pivots=framework-dotnet#grant-access-to-app-configuration).
142+
143+
## Install App Configuration Kubernetes Provider to your AKS cluster
144+
145+
1. Get the credential to manage your AKS cluster, replace the `name` and `resource-group` parameters with yours:
191146
```bash
192-
docker build . -t $ACR_Name.azurecr.io/testappconfig
193-
docker push $ACR_Name.azurecr.io/testappconfig
147+
az aks get-credentials --name my_aks --resource-group my_aks_resource_group
148+
```
149+
2. Install Azure App Configuration Kubernetes Provider to your AKS cluster using `helm`:
150+
``` bash
151+
helm install azureappconfiguration.kubernetesprovider oci://mcr.microsoft.com/azure-app-configuration/helmchart/kubernetes-provider --version 1.0.0-alpha --namespace azappconfig-system --create-namespace
194152
```
195153
196-
## Create Kubernetes resources
154+
## Create and deploy Kubernetes resources
197155
198156
1. Create a *AKS-AppConfiguration-Demo* directory in the root directory of your project.
199-
2. Create *appConfigurationProvider.yaml* in the *AKS-AppConfiguration-Demo* directory with the following YAML content. Replace the value of the `endpoint` field with the endpoint of the Azure AppConfiguration store you created in the previous step.
157+
1. Create *appConfigurationProvider.yaml* in the *AKS-AppConfiguration-Demo* directory with the following YAML content. Replace the value of the `endpoint` field with the endpoint of the Azure AppConfiguration store you created in the previous step.
200158
``` yaml
201159
apiVersion: azconfig.io/v1beta1
202160
kind: AzureAppConfigurationProvider
203161
metadata:
204162
name: appconfigurationprovider-sample
205163
spec:
206-
endpoint: https://<your_app_config>.azconfig.io
164+
endpoint: https://myappconfig.azconfig.io
207165
target:
208166
configMapName: demo-configmap
209167
```
210-
3. Create *deployment.yaml* in the *AKS-AppConfiguration-Demo* directory with the following YAML content. Replace the value of `template.containers.image` with the image you created in the previous step.
168+
1. Create *deployment.yaml* in the *AKS-AppConfiguration-Demo* directory with the following YAML content. Replace the value of `template.containers.image` with the image you created in the previous step.
211169
``` yaml
212170
apiVersion: apps/v1
213171
kind: Deployment
@@ -227,14 +185,14 @@ Do several updates to the web app project you created.
227185
spec:
228186
containers:
229187
- name: configmap-demo-app
230-
image: <your_acr_name>.azurecr.io/testappconfig
188+
image: myregistry.azurecr.io/aspnetapp:v1
231189
ports:
232190
- containerPort: 80
233191
envFrom:
234192
- configMapRef:
235193
name: demo-configmap
236194
```
237-
4. Create *service.yaml* in the *AKS-AppConfiguration-Demo* with the following YAML content.
195+
1. Create *service.yaml* in the *AKS-AppConfiguration-Demo* with the following YAML content.
238196
``` yaml
239197
apiVersion: v1
240198
kind: Service
@@ -247,14 +205,13 @@ Do several updates to the web app project you created.
247205
selector:
248206
app: configmap-demo-app
249207
```
208+
1. Apply the YAML files to the AKS cluster
209+
``` bash
210+
kubectl create namespace quickstart-appconfig
211+
kubectl apply -f ./AKS-AppConfiguration-Demo -n quickstart-appconfig
212+
```
250213
251-
## Apply the YAML files to the AKS cluster and validate that it works
252-
253-
Apply the resources to AKS cluster by running:
254-
``` bash
255-
kubectl create namespace quickstart-appconfig
256-
kubectl apply -f ./AKS-AppConfiguration-Demo -n quickstart-appconfig
257-
```
214+
## Validate ConfigMap creation
258215
259216
To check the synchronization status of AppConfigurationProvider, run the following command in your terminal. If the `phase` property in the `status` section of the output is `COMPLETE` , it means that the key-values have been successfully synced from Azure App Configuration.
260217
``` bash
@@ -271,40 +228,13 @@ A configMap *demo-configmap* is being created in the *quickstart-appconfig* name
271228
``` bash
272229
kubectl get configmap demo-configmap -n quickstart-appconfig
273230
```
231+
232+
## Validate key-values from Azure App Configuration are affecting the app.
274233
Run the following command and get the External IP that exposed by the LoadBalancer service. Use it to visit the web app, You'll see that the configuration settings from the Azure App Configuration store are affecting the page.
275234
``` bash
276235
kubectl get service configmap-demo-service -n quickstart-appconfig
277236
```
278237

279-
> [!TIP]
280-
> Currently, the provider doesn't support real-time configuration updating. Updating configuration in Azure App Configuration doesn't automatically update settings in ConfigMap. There are three options to update the ConfigMap:
281-
>
282-
> Option 1: Delete and re-deploy that AzureAppConfigurationProvider resource.
283-
>
284-
> Option 2: Delete the ConfigMap being created by the provider. It will automatically generate a new one.
285-
>
286-
> Option 3: Set a dedicated annotation in the AzureAppConfigurationProvider to trigger a settings update in ConfigMap.
287-
>
288-
> For example, set an annotation dynamic/timestamp with a time stamp and refresh the time to trigger a settings update in ConfigMap.
289-
>
290-
> ``` yaml
291-
> apiVersion: azconfig.io/v1beta1
292-
> kind: AzureAppConfigurationProvider
293-
> metadata:
294-
> name: appconfigurationprovider-sample
295-
> annotations:
296-
> dynamic/timestamp: 2023-01-01T00:00:00.000
297-
> spec:
298-
> endpoint: https://<yourappconfig>.azconfig.io
299-
> target:
300-
> configMapName: demo-configmap
301-
> ```
302-
303-
> [!NOTE]
304-
> In spite of the data in ConfigMap being updated, if the ConfigMap change is not watched by your workload (Deployment, Pod, etc.), your workload will not be able to apply the updated key-values in ConfigMap. We recommend using 3rd-party tools like [stakater/Reloader](https://github.com/stakater/Reloader) to watch the changes in ConfigMap and perform automatic rolling update of correlated workloads.
305-
306-
307-
308238
## Clean up resources
309239

310240
[!INCLUDE[Azure App Configuration cleanup](../../includes/azure-app-configuration-cleanup.md)]

0 commit comments

Comments
 (0)