Skip to content

Commit 7427f46

Browse files
Merge pull request #251570 from shpathak-msft/active-repl-demo
Active geo replication demo for AKS apps
2 parents 6561a73 + 716107e commit 7427f46

File tree

2 files changed

+317
-8
lines changed

2 files changed

+317
-8
lines changed

articles/azure-cache-for-redis/TOC.yml

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,18 @@
3737
href: cache-rust-get-started.md
3838
- name: Tutorials
3939
items:
40-
- name: Connect an AKS application to a cache
41-
href: cache-tutorial-aks-get-started.md
40+
- name: Use Azure Functions
41+
items:
42+
- name: Create a simple Function app for Redis triggers
43+
href: cache-tutorial-functions-getting-started.md
44+
- name: Using Azure Functions to create a write-behind cache
45+
href: cache-tutorial-write-behind.md
46+
- name: Connect from apps hosted on Azure Kubernetes Service
47+
items:
48+
- name: Use active geo-replication with an AKS-hosted application
49+
href: cache-tutorial-active-replication.md
50+
- name: Connect an AKS application to a cache
51+
href: cache-tutorial-aks-get-started.md
4252
- name: ASP.NET
4353
items:
4454
- name: Use session state provider
@@ -61,12 +71,6 @@
6171
href: cache-web-app-cache-aside-leaderboard.md
6272
- name: Deploy a machine learning model
6373
href: cache-ml.md
64-
- name: Use Azure Functions
65-
items:
66-
- name: Create a simple Function app for Redis triggers
67-
href: cache-tutorial-functions-getting-started.md
68-
- name: Using Azure Functions to create a write-behind cache
69-
href: cache-tutorial-write-behind.md
7074
- name: Samples
7175
items:
7276
- name: PowerShell samples
Lines changed: 305 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,305 @@
1+
---
2+
title: 'Tutorial: Get started using Azure Cache for Redis Enterprise active replication with an AKS-hosted application'
3+
description: In this tutorial, you learn how to connect your AKS hosted application to a cache that uses active geo-replication.
4+
author: flang-msft
5+
6+
ms.author: franlanglois
7+
ms.service: cache
8+
ms.topic: tutorial
9+
ms.date: 09/18/2023
10+
#CustomerIntent: As a developer, I want to see how to use a Enterprise cache that uses active geo-replication to capture data from two apps running against different caches in separate geo-locations.
11+
12+
---
13+
14+
# Get started using Azure Cache for Redis Enterprise active replication with an AKS-hosted application
15+
16+
In this tutorial, you will host an inventory application on Azure Kubernetes Service (AKS) and find out how you can use active geo-replication to replicate data in your Azure Cache for Redis Enterprise instances across Azure regions.
17+
18+
## Prerequisites
19+
20+
- 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+
- One 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). Alternatively, you can host two instances of the demo application on the two different AKS clusters. In a production environment, you would use two different clusters located in the same regions as your clusters to deploy two versions of the application. For this tutorial, you deploy both instances of the application on the same AKS cluster.
22+
23+
> [!IMPORTANT]
24+
> This tutorial assumes that you are familiar with basic Kubernetes concepts like containers, pods and service.
25+
26+
## Overview
27+
28+
This tutorial uses a sample inventory page that shows three different T-shirt options. The user can "purchase" each T-shirt and see the inventory drop. The unique thing about this demo is that we run the inventory app in two different regions. Typically, you would have to run the database storing inventory data in a single region so that there are no consistency issues. With other database backends and synchronization, customers might have unpleasant experience due to higher latency for calls across different Azure regions. When you use Azure Cache for Redis Enterprise as the backend, you can link two caches together with active geo-replication so that the inventory remains consistent across both regions while enjoying low latency performance from Redis Enterprise in the same region.
29+
30+
## Set up two Azure Cache for Redis instances
31+
32+
1. Create a new Azure Cache for Redis Enterprise instance in **West US 2** region by using the Azure portal or your preferred CLI tool. Alternately, you can use any region of your choice. Use the [quickstart guide](quickstart-create-redis-enterprise.md) to get started.
33+
34+
1. On the **Advanced** tab:
35+
36+
1. Enable **Non-TLS access only**.
37+
1. Set **Clustering Policy** to **Enterprise**
38+
1. Configure a new active geo-replication group using [this guide](cache-how-to-active-geo-replication.md). Eventually, you add both caches to the same replication group. Create the group name with the first cache, and add the second cache to the same group.
39+
40+
> [!IMPORTANT]
41+
> This tutorial uses a non-TLS port for demonstration, but we highly recommend that you use a TLS port for anything in production.
42+
43+
1. Set up another Azure Cache for Redis Enterprise in **East US** region with the same configuration as the first cache. Alternatively, you can use any region of your choice. Ensure that you choose the same replication group as the first cache.
44+
45+
## Prepare Kubernetes deployment files
46+
47+
Create two .yml files using the following procedure. One file for each cache you created in the two regions.
48+
49+
To demonstrate data replication across regions, we run two instances of the same application in different regions. Let's make one instance run in Seattle, west namespace, while the second runs in New York, east namespace.
50+
51+
### West namespace
52+
53+
Update the following fields in the following YAML file and save it as _app_west.yaml_.
54+
55+
1. Update the variable `REDIS_HOST` with the **Endpoint value** URL after removing the port suffix: 10000
56+
1. Update `REDIS_PASSWORD` with the **Access key** of your _West US 2_ cache.
57+
1. Update `APP_LOCATION` to display the region where this application instance is running. For this cache, configure the `APP_LOCATION` to `Seattle` to indicate this application instance is running in Seattle.
58+
1. Verify that the variable `namespace` value is `west` in both places in the file.
59+
60+
It should look like following code:
61+
62+
```YAML
63+
apiVersion: apps/v1
64+
kind: Deployment
65+
metadata:
66+
name: shoppingcart-app
67+
namespace: west
68+
spec:
69+
replicas: 1
70+
selector:
71+
matchLabels:
72+
app: shoppingcart
73+
template:
74+
metadata:
75+
labels:
76+
app: shoppingcart
77+
spec:
78+
containers:
79+
- name: demoapp
80+
image: mcr.microsoft.com/azure-redis-cache/redisactivereplicationdemo:latest
81+
resources:
82+
limits:
83+
cpu: "0.5"
84+
memory: "250Mi"
85+
requests:
86+
cpu: "0.5"
87+
memory: "128Mi"
88+
env:
89+
- name: REDIS_HOST
90+
value: "DemoWest.westus2.redisenterprise.cache.azure.net"
91+
- name: REDIS_PASSWORD
92+
value: "myaccesskey"
93+
- name: REDIS_PORT
94+
value: "10000" # redis enterprise port
95+
- name: HTTP_PORT
96+
value: "8080"
97+
- name: APP_LOCATION
98+
value: "Seattle, WA"
99+
---
100+
apiVersion: v1
101+
kind: Service
102+
metadata:
103+
name: shoppingcart-svc
104+
namespace: west
105+
spec:
106+
type: LoadBalancer
107+
ports:
108+
- protocol: TCP
109+
port: 80
110+
targetPort: 8080
111+
selector:
112+
app: shoppingcart
113+
```
114+
115+
### East namespace
116+
117+
Save another copy of the same YAML file as _app_east.yaml_. This time, use the values that correspond with your second cache.
118+
119+
1. Update the variable `REDIS_HOST` with the **Endpoint value** after removing the port suffix: 10000
120+
1. Update `REDIS_PASSWORD` with the **Access key** of your _East US_ cache.
121+
1. Update `APP_LOCATION` to display the region where this application instance is running. For this cache, configure the `APP_LOCATION` to _New York_ to indicate this application instance is running in New York.
122+
1. Verify that the variable `namespace` value is `east` in both places in the file.
123+
124+
It should look like following code:
125+
126+
```YAML
127+
apiVersion: apps/v1
128+
kind: Deployment
129+
metadata:
130+
name: shoppingcart-app
131+
namespace: east
132+
spec:
133+
replicas: 1
134+
selector:
135+
matchLabels:
136+
app: shoppingcart
137+
template:
138+
metadata:
139+
labels:
140+
app: shoppingcart
141+
spec:
142+
containers:
143+
- name: demoapp
144+
image: mcr.microsoft.com/azure-redis-cache/redisactivereplicationdemo:latest
145+
resources:
146+
limits:
147+
cpu: "0.5"
148+
memory: "250Mi"
149+
requests:
150+
cpu: "0.5"
151+
memory: "128Mi"
152+
env:
153+
- name: REDIS_HOST
154+
value: "DemoEast.eastus.redisenterprise.cache.azure.net"
155+
- name: REDIS_PASSWORD
156+
value: "myaccesskey"
157+
- name: REDIS_PORT
158+
value: "10000" # redis enterprise port
159+
- name: HTTP_PORT
160+
value: "8080"
161+
- name: APP_LOCATION
162+
value: "New York, NY"
163+
---
164+
apiVersion: v1
165+
kind: Service
166+
metadata:
167+
name: shoppingcart-svc
168+
namespace: east
169+
spec:
170+
type: LoadBalancer
171+
ports:
172+
- protocol: TCP
173+
port: 80
174+
targetPort: 8080
175+
selector:
176+
app: shoppingcart
177+
```
178+
179+
## Install and connect to your AKS cluster
180+
181+
In this section, you first install the Kubernetes CLI and then connect to an AKS cluster.
182+
183+
> [!NOTE]
184+
> An Azure Kubernetes Service Cluster is required for this tutorial. You deploy both instances of the application on the same AKS cluster.
185+
186+
### Install the Kubernetes CLI
187+
188+
Use the Kubernetes CLI, _kubectl, to connect to the Kubernetes cluster from your local computer. If you're running locally, then you can use the following command to install kubectl.
189+
190+
```bash
191+
az aks install-cli
192+
```
193+
194+
If you use Azure Cloud Shell, _kubectl_ is already installed, and you can skip this step.
195+
196+
### Connect to your AKS clusters in two regions
197+
198+
Use the portal to copy the resource group and cluster name for your AKS cluster in the West US 2 region. To configure _kubectl_ to connect to your AKS cluster, use the following command with your resource group and cluster name:
199+
200+
```bash
201+
az aks get-credentials --resource-group myResourceGroup --name myClusterName
202+
```
203+
204+
Verify that you're able to connect to your cluster by running the following command:
205+
206+
```bash
207+
208+
kubectl get nodes
209+
```
210+
211+
You should see similar output showing the list of your cluster nodes.
212+
213+
```output
214+
NAME STATUS ROLES AGE VERSION
215+
aks-agentpool-21274953-vmss000001 Ready agent 1d v1.24.15
216+
aks-agentpool-21274953-vmss000003 Ready agent 1d v1.24.15
217+
aks-agentpool-21274953-vmss000006 Ready agent 1d v1.24.15
218+
```
219+
220+
## Deploy and test your application
221+
222+
You need two namespaces for your applications to run on your AKS cluster. Create a west and then deploy the application.
223+
224+
Run the following command to deploy the application instance to your AKS cluster in the _west_ namespace:
225+
226+
```bash
227+
kubectl create namespace west
228+
229+
kubectl apply -f app_west.yaml
230+
```
231+
232+
You get a response indicating your deployment and service was created:
233+
234+
```output
235+
deployment.apps/shoppingcart-app created
236+
service/shoppingcart-svc created
237+
```
238+
239+
To test the application, run the following command to check if the pod is running:
240+
241+
```bash
242+
kubectl get pods -n west
243+
```
244+
245+
You see your pod running successfully like:
246+
247+
```output
248+
NAME READY STATUS RESTARTS AGE
249+
shoppingcart-app-5fffdcb5cd-48bl5 1/1 Running 0 68s
250+
```
251+
252+
Run the following command to get the endpoint for your application:
253+
254+
```bash
255+
kubectl get service -n west
256+
```
257+
258+
You might see that the EXTERNAL-IP has status `<pending>` for a few minutes. Keep retrying until the status is replaced by an IP address.
259+
260+
```output
261+
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
262+
shoppingcart-svc LoadBalancer 10.0.166.147 20.69.136.105 80:30390/TCP 90s
263+
```
264+
265+
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:
266+
267+
<!-- screenshot for Seattle -->
268+
269+
Run the same deployment steps and deploy an instance of the demo application to run in East US region.
270+
271+
```bash
272+
kubectl create namespace east
273+
274+
kubectl apply -f app_east.yml
275+
276+
kubectl get pods -n east
277+
278+
kubectl get service -n east
279+
```
280+
281+
With two services opened in your browser, you should see that changing the inventory in one region is almost instantly reflected in the other region. The inventory data is stored in the Redis Enterprise instances that are replicating data across regions.
282+
283+
You did it! Click on the buttons and explore the demo. To reset the count, add `/reset` after the url:
284+
285+
`<IP address>/reset`
286+
287+
## Clean up your deployment
288+
289+
To clean up your cluster, run the following commands:
290+
291+
```bash
292+
kubectl delete deployment shoppingcart-app -n west
293+
kubectl delete service shoppingcart-svc -n west
294+
295+
kubectl delete deployment shoppingcart-app -n east
296+
kubectl delete service shoppingcart-svc -n east
297+
```
298+
299+
[!INCLUDE [cache-delete-resource-group](includes/cache-delete-resource-group.md)]
300+
301+
## Related Content
302+
303+
- [Tutorial: Connect to Azure Cache for Redis from your application hosted on Azure Kubernetes Service](cache-tutorial-aks-get-started.md)
304+
- [Quickstart: Deploy an Azure Kubernetes Service (AKS) cluster using the Azure portal](/azure/aks/learn/quick-kubernetes-deploy-portal)
305+
- [AKS sample voting application](https://github.com/Azure-Samples/azure-voting-app-redis/tree/master)

0 commit comments

Comments
 (0)