|
| 1 | +--- |
| 2 | +title: 'Tutorial: Get started with leveraging Azure Cache for Redis Enterprise active replication with your AKS hosted application' |
| 3 | +description: In this tutorial, you learn how to connect your AKS hosted application to Azure Cache for Redis Enterprise instances and leverage active geo-replication. |
| 4 | +author: flang-msft |
| 5 | + |
| 6 | +ms.author: franlanglois |
| 7 | +ms.service: cache |
| 8 | +ms.topic: tutorial |
| 9 | +ms.date: 09/07/2023 |
| 10 | +#CustomerIntent: As a developer, I want to see how to use a Azure Cache for Redis Enterprise instance with an AKS container so that I see how I can use my cache instance with a Kubernetes cluster. |
| 11 | + |
| 12 | +--- |
| 13 | + |
| 14 | +# Tutorial: Connect to Azure Cache for Redis from your application hosted on Azure Kubernetes Service |
| 15 | + |
| 16 | +In this tutorial, you will host a simple inventory application on AKS and find out how you can leverage active geo-replication to replicate data in your Azure Cache for Redis Enterprise instances across Azure regions. |
| 17 | + |
| 18 | +## Overview |
| 19 | +This tutorial uses a sample inventory page which 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. That can result in unpleasant customer experience due to higher latency for calls across different Azure regions. By using Azure Cache for Redis Enterprise as the backend, however, 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. |
| 20 | + |
| 21 | + |
| 22 | +## Prerequisites |
| 23 | + |
| 24 | +- An Azure subscription. If you don't have an Azure subscription, create a [free account](https://azure.microsoft.com/free/?WT.mc_id=A261C142F). |
| 25 | +- Two Azure Kubernetes Service Clusters in different regions- 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). Alternately, you can host two instances of the demo application on the same AKS cluster. |
| 26 | + |
| 27 | +> [!IMPORTANT] |
| 28 | +> This tutorial assumes that you are familiar with basic Kubernetes concepts like containers, pods and service. |
| 29 | +
|
| 30 | +## Set up an Azure Cache for Redis instance |
| 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 | +1. On the **Advanced** tab, |
| 34 | + * enable **Non-TLS access only** |
| 35 | + * set **Clustering Policy** to Enterprise |
| 36 | + * set up active geo-replication using [this guide](cache-how-to-active-geo-replication.md) |
| 37 | + |
| 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. |
| 40 | +
|
| 41 | +3. Set up another Azure Cache for Redis Enterprise in **East US** region with the exact same configuration as the first cache. Alternately, you can use any region of your choice. Ensure that you choose the same replication group as the first cache. |
| 42 | + |
| 43 | +## Prepare Kubernetes deployment files |
| 44 | + |
| 45 | +To demonstrate data replication across regions, we will run two instances of the same application in different regions. Let's assume one instance runs in Seattle (west) while the second in New York(east). |
| 46 | + |
| 47 | +Update the following fields in the YAML file below and save it as app_west.yaml |
| 48 | +* Update environment variables REDIS_HOST and REDIS_PASSWORD with hostname and access key of your Azure Cache for Redis Enterprise instance in West US 2 or one of the two regions your chose earlier. |
| 49 | +* Update APP_LOCATION to display the region where this application instance is running. In this sample, we are configuring the APP_LOCATION to Seattle to indicate this application instance is running in Seattle. |
| 50 | + |
| 51 | +```YAML |
| 52 | +apiVersion: apps/v1 |
| 53 | +kind: Deployment |
| 54 | +metadata: |
| 55 | + name: shoppingcart-app |
| 56 | + namespace: west |
| 57 | +spec: |
| 58 | + replicas: 1 |
| 59 | + selector: |
| 60 | + matchLabels: |
| 61 | + app: shoppingcart |
| 62 | + template: |
| 63 | + metadata: |
| 64 | + labels: |
| 65 | + app: shoppingcart |
| 66 | + spec: |
| 67 | + containers: |
| 68 | + - name: demoapp |
| 69 | + image: mcr.microsoft.com/azure-redis-cache/redisactivereplicationdemo:latest |
| 70 | + resources: |
| 71 | + limits: |
| 72 | + cpu: "0.5" |
| 73 | + memory: "250Mi" |
| 74 | + requests: |
| 75 | + cpu: "0.5" |
| 76 | + memory: "128Mi" |
| 77 | + env: |
| 78 | + - name: REDIS_HOST |
| 79 | + value: "DemoWest.westus.redisenterprise.cache.azure.net" |
| 80 | + - name: REDIS_PASSWORD |
| 81 | + value: "myaccesskey" |
| 82 | + - name: REDIS_PORT |
| 83 | + value: "10000" # redis enterprise port |
| 84 | + - name: HTTP_PORT |
| 85 | + value: "8080" |
| 86 | + - name: APP_LOCATION |
| 87 | + value: "Seattle, WA" |
| 88 | +--- |
| 89 | +apiVersion: v1 |
| 90 | +kind: Service |
| 91 | +metadata: |
| 92 | + name: shoppingcart-svc |
| 93 | + namespace: west |
| 94 | +spec: |
| 95 | + type: LoadBalancer |
| 96 | + ports: |
| 97 | + - protocol: TCP |
| 98 | + port: 80 |
| 99 | + targetPort: 8080 |
| 100 | + selector: |
| 101 | + app: shoppingcart |
| 102 | +``` |
| 103 | +
|
| 104 | +Save another copy of the same YAML file as app_east.yaml. This time, update the REDIS_HOST, REDIS_PASSWORD and APP_LOCATION to point to Redis Enterprise instance in East US or your second region of choice. |
| 105 | +
|
| 106 | +## Connect to your AKS cluster |
| 107 | +
|
| 108 | +### Install the Kubernetes CLI |
| 109 | +
|
| 110 | +Use the Kubernetes CLI, _kubectl_ , to connect to the Kubernetes cluster from your local computer. If you are running locally, then you can using the following command to install kubectl. |
| 111 | +
|
| 112 | +```bash |
| 113 | +az aks install-cli |
| 114 | +``` |
| 115 | +If you use Azure Cloud Shell, _kubectl_ is already installed, and you can skip this step. |
| 116 | + |
| 117 | +### Connect to your AKS cluster |
| 118 | + |
| 119 | +Use the portal to copy the resource group and cluster name for your AKS cluster. To configure _kubectl_ to connect to your AKS cluster, use the following command with your resource group and cluster name: |
| 120 | + |
| 121 | +```bash |
| 122 | + az aks get-credentials --resource-group myResourceGroup --name myClusterName |
| 123 | + ``` |
| 124 | + |
| 125 | +Verify that you are able to connect to your cluster by running the following command: |
| 126 | + |
| 127 | +```bash |
| 128 | +kubectl get nodes |
| 129 | +``` |
| 130 | + |
| 131 | +You should see similar output showing the list of your cluster nodes. |
| 132 | + |
| 133 | +```output |
| 134 | +NAME STATUS ROLES AGE VERSION |
| 135 | +aks-agentpool-21274953-vmss000001 Ready agent 1d v1.24.15 |
| 136 | +aks-agentpool-21274953-vmss000003 Ready agent 1d v1.24.15 |
| 137 | +aks-agentpool-21274953-vmss000006 Ready agent 1d v1.24.15 |
| 138 | +``` |
| 139 | + |
| 140 | +## Deploy and test your application |
| 141 | + |
| 142 | +Run the following command to deploy the application instance to your AKS cluster in **West US 2**: |
| 143 | + |
| 144 | +```bash |
| 145 | +kubectl apply -f app_west.yaml |
| 146 | +``` |
| 147 | + |
| 148 | +You will get a response indicating your deployment and service was created: |
| 149 | + |
| 150 | +```output |
| 151 | +deployment.apps/shoppingcart-app created |
| 152 | +service/shoppingcart-svc created |
| 153 | +``` |
| 154 | + |
| 155 | +To test the application, run the following command to check if the pod is running: |
| 156 | + |
| 157 | +```bash |
| 158 | +kubectl get pods -n west |
| 159 | +``` |
| 160 | + |
| 161 | +You see your pod running successfully like: |
| 162 | + |
| 163 | +```output |
| 164 | +NAME READY STATUS RESTARTS AGE |
| 165 | +shoppingcart-app-5fffdcb5cd-48bl5 1/1 Running 0 68s |
| 166 | +``` |
| 167 | + |
| 168 | +Run the following command to get the endpoint for your application: |
| 169 | +```bash |
| 170 | +kubectl get service -n west |
| 171 | +``` |
| 172 | + |
| 173 | +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. |
| 174 | + |
| 175 | +```output |
| 176 | +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| 177 | +shoppingcart-svc LoadBalancer 10.0.166.147 20.69.136.105 80:30390/TCP 90s |
| 178 | +``` |
| 179 | + |
| 180 | +Once the External-IP is available, open a web browser to the External-IP address of your service and you see the application running like below: |
| 181 | +<screenshot for Seattle> |
| 182 | + |
| 183 | +Run the same deployment steps and deploy an instance of the demo application to run in East US region. |
| 184 | + |
| 185 | +```bash |
| 186 | +kubectl apply -f app_east |
| 187 | + |
| 188 | +kubectl get pods -n east |
| 189 | + |
| 190 | +kubectl get service -n east |
| 191 | +``` |
| 192 | +With two services opened in your browser, you should see that changing the inventory in one region is virtually instantly reflected in the other region. The inventory data is stored in the Redis Enterprise instances which are replicating data across regions. |
| 193 | + |
| 194 | +You did it! Click on the buttons and explore the demo. To reset the count, add "/reset" after the url. e.g. <IP address>/reset |
| 195 | + |
| 196 | + |
| 197 | +## Clean up your deployment |
| 198 | + |
| 199 | +To clean up your cluster, run the following commands: |
| 200 | + |
| 201 | +```bash |
| 202 | +kubectl delete deployment shoppingcart-app -n west |
| 203 | +kubectl delete service shoppingcart-svc -n west |
| 204 | +kubectl delete deployment shoppingcart-app -n east |
| 205 | +kubectl delete service shoppingcart-svc -n east |
| 206 | +``` |
| 207 | + |
| 208 | +[!INCLUDE [cache-delete-resource-group](includes/cache-delete-resource-group.md)] |
| 209 | + |
| 210 | +## Related Content |
| 211 | +<!-- Do we want this particular link in this section. |
| 212 | +
|
| 213 | +- [Use Azure Key Vault Provider to securely store your access key](https://learn.microsoft.com/azure/aks/csi-secrets-store-driver) --> |
0 commit comments