You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: 'Tutorial: Get started connecting an AKS application to a cache'
3
3
description: In this tutorial, you learn how to connect your AKS-hosted application to an Azure Cache for Redis instance.
4
-
5
-
6
-
7
-
8
4
ms.topic: tutorial
9
-
ms.date: 08/15/2023
5
+
ms.date: 10/01/2024
10
6
#CustomerIntent: As a developer, I want to see how to use a Azure Cache for Redis instance with an AKS container so that I see how I can use my cache instance with a Kubernetes cluster.
11
7
12
8
---
@@ -19,6 +15,7 @@ In this tutorial, you adapt the [AKS sample voting application](https://github.c
19
15
20
16
- An Azure subscription. If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F).
21
17
- An Azure Kubernetes Service Cluster - For more information on creating a cluster, see [Quickstart: Deploy an Azure Kubernetes Service (AKS) cluster using the Azure portal](/azure/aks/learn/quick-kubernetes-deploy-portal).
18
+
- An user assigned managed identity that you want to use to connect to your Azure Cache for Redis instance.
22
19
23
20
> [!IMPORTANT]
24
21
> This tutorial assumes that you are familiar with basic Kubernetes concepts like containers, pods and service.
@@ -30,17 +27,42 @@ In this tutorial, you adapt the [AKS sample voting application](https://github.c
30
27
For this tutorial, use a Standard C1 cache.
31
28
:::image type="content" source="media/cache-tutorial-aks-get-started/cache-new-instance.png" alt-text="Screenshot of creating a Standard C1 cache in the Azure portal":::
32
29
33
-
1. On the **Advanced** tab, enable **Non-TLS port**.
34
-
:::image type="content" source="media/cache-tutorial-aks-get-started/cache-non-tls.png" alt-text="Screenshot of the Advanced tab with Non-TLS enabled during cache creation.":::
30
+
1. Follow the steps through to create the cache.
35
31
36
-
1.Follow the steps through to create the cache.
32
+
1.Once your Redis cache instance is created, navigate to the **Authentication** tab. Select the user assigned managed identity you want to use to connect to your Redis cache instance, then select **Save**.
37
33
38
-
> [!IMPORTANT]
39
-
> This tutorial uses a non-TLS port for demonstration, but we highly recommend that you use a TLS port for anything in production.
34
+
1. Alternatively, you can navigate to Data Access Configuration on the Resource menu to create a new Redis user with your user assigned managed identity to connect to your cache.
35
+
36
+
1. Take note of the user name for your Redis user from the portal. You use this user name with the AKS workload.
37
+
38
+
## Configure your AKS cluster
40
39
41
-
Creating the cache can take a few minutes. You can move to the next section while the process finishes.
40
+
1. Follow these [steps](aks/workload-identity-deploy-cluster.md) to configure a workload identity for your AKS cluster. Complete the following steps:
42
41
43
-
## Install and connect to your AKS cluster
42
+
- Enable OIDC issuer and workload identity
43
+
- Skip the step to create user assigned managed identity if you have already created your managed identity. If you create a new managed identity, ensure that you create a new Redis User for your managed identity and assign appropriate data access permissions.
44
+
- Create a Kubernetes Service account annotated with the client id of your user assigned managed identity
45
+
- Create a federated identity credential for your AKS cluster.
46
+
47
+
## Configure your workload that connects to Azure Cache for Redis
48
+
49
+
Next, set up the AKS workload to connect to Azure Cache for Redis after you have configured the AKS cluster.
50
+
51
+
1. Download the code for the [sample app](https://github.com/Azure-Samples/azure-cache-redis-sample/connect-from-aks).
52
+
53
+
1. Build and push docker image to your Azure Container Registry using [az acr build](https://learn.microsoft.com/en-us/cli/azure/acr?view=azure-cli-latest#az-acr-build) command
54
+
55
+
```bash
56
+
az acr build --image sample/connect-from-aks-sample:1.0 --registry yourcontainerregistry --file Dockerfile .
57
+
```
58
+
59
+
1. Attach your container registry to your AKS cluster using following command:
60
+
61
+
```bash
62
+
az aks update --name clustername --resource-group mygroup --attach-acr youracrname
63
+
```
64
+
65
+
## Deploy your workload
44
66
45
67
In this section, you first install the Kubernetes CLI and then connect to an AKS cluster.
46
68
@@ -70,134 +92,100 @@ kubectl get nodes
70
92
71
93
You should see similar output showing the list of your cluster nodes.
## Update the voting application to use Azure Cache for Redis
81
-
82
-
Use the [.yml file](https://github.com/Azure-Samples/azure-voting-app-redis/blob/master/azure-vote-all-in-one-redis.yaml) in the sample for reference.
102
+
## Run your workload
83
103
84
-
Make the following changes to the deployment file before you save the file as _azure-vote-sample.yaml_.
104
+
1. This is the pod specification file that you use to run our workload. Take note that the pod has the label "azure.workloadidentity/use: "true"" and is annotated with _serviceAccountName_as required by AKS workload identity. Replace the value of CONNECTION_STRING, CACHE_NAME and USER_ASSIGNED_PRINCIPAL_ID environment variables that correspond with your cache and managed identity.
85
105
86
-
1. Remove the deployment and service named `azure-vote-back`. This deployment is used to deploy a Redis container to your cluster that is not required when using Azure Cache for Redis.
87
-
88
-
2. Replace the value `REDIS` variable from "azure-vote-back" to the _hostname_ of the Azure Cache for Redis instance that you created earlier. This change indicates that your application should use Azure Cache for Redis instead of a Redis container.
89
-
90
-
3. Define variable named `REDIS_PWD`, and set the value to the _access key_ for the Azure Cache for Redis instance that you created earlier.
91
-
92
-
After all the changes, the deployment file should look like following file with your _hostname_ and _access key_. Save your file as _azure-vote-sample.yaml_.
93
-
94
-
```YAML
95
-
apiVersion: apps/v1
96
-
kind: Deployment
97
-
metadata:
98
-
name: azure-vote-front
99
-
spec:
100
-
replicas: 1
101
-
selector:
102
-
matchLabels:
103
-
app: azure-vote-front
104
-
strategy:
105
-
rollingUpdate:
106
-
maxSurge: 1
107
-
maxUnavailable: 1
108
-
minReadySeconds: 5
109
-
template:
106
+
```YAML
107
+
apiVersion: v1
108
+
kind: Pod
110
109
metadata:
110
+
name: entrademo-pod
111
111
labels:
112
-
app: azure-vote-front
112
+
azure.workload.identity/use: "true"# Required. Only pods with this label can use workload identity.
Once the External-IP is available, open a web browser to the External-IP address of your service and you see the application running as follows:
186
-
187
-
:::image type="content" source="media/cache-tutorial-aks-get-started/cache-web-voting-app.png" alt-text="Screenshot of the voting application running in a browser with buttons for cats, dogs, and reset.":::
188
-
189
-
## Clean up your deployment
128
+
- name: CONNECTION_OPTION
129
+
value: "MANAGED_IDENTITY"#ACCESS_KEY
130
+
- name: CONNECTION_STRING # Required when connecting with access key
131
+
value: "your connection string"
132
+
- name: CACHE_NAME
133
+
value: "your cache name"
134
+
restartPolicy: Never
135
+
136
+
```
137
+
138
+
1. Save this file as podspec.yaml and then apply it to your AKS cluster by running the folloWing command:
139
+
140
+
```bash
141
+
kubectl apply -f podspec.yaml
142
+
```
143
+
144
+
You get a response indicating your pod was created:
145
+
146
+
```bash
147
+
pod/entrademo-pod created
148
+
```
149
+
150
+
1. To test the application, run the following command to check if the pod is running:
151
+
152
+
```bash
153
+
kubectl get pods
154
+
```
155
+
156
+
You see your pod running successfully like:
157
+
158
+
```bash
159
+
NAME READY STATUS RESTARTS AGE
160
+
entrademo-pod 0/1 Completed 0 42s
161
+
```
162
+
163
+
1. Because this is a console app, you need to check the logs of the pod to verify that it ran as expected using this command.
164
+
165
+
```bash
166
+
kubectl logs entrademo-app
167
+
```
168
+
169
+
You will see the following logs that indicates your pod has successfully connected to your Redis instance using user assigned managed identity
170
+
171
+
```bash
172
+
Connecting with managed identity..
173
+
Retrieved value from Redis: Hello, Redis!
174
+
Success! Previous value: Hello, Redis!
175
+
```
176
+
177
+
## Clean up your cluster
190
178
191
179
To clean up your cluster, run the following commands:
0 commit comments