|
| 1 | +--- |
| 2 | +title: 'Tutorial: Get started connecting an AKS application to a cache' |
| 3 | +description: In this tutorial, you learn how to connect your AKS-hosted application to an Azure Cache for Redis instance. |
| 4 | +author: flang-msft |
| 5 | + |
| 6 | +ms.author: franlanglois |
| 7 | +ms.service: cache |
| 8 | +ms.topic: tutorial |
| 9 | +ms.date: 08/15/2023 |
| 10 | +#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 | + |
| 12 | +--- |
| 13 | + |
| 14 | +# Tutorial: Connect to Azure Cache for Redis from your application hosted on Azure Kubernetes Service |
| 15 | + |
| 16 | +In this tutorial, you adapt the [AKS sample voting application](https://github.com/Azure-Samples/azure-voting-app-redis/tree/master) to use with an Azure Cache for Redis instance instead. The original sample uses a Redis cache deployed as a container to your AKS cluster. Following some simple steps, you can configure the AKS sample voting application to connect to your Azure Cache for Redis instance. |
| 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 | +- 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). |
| 22 | + |
| 23 | +> [!IMPORTANT] |
| 24 | +> This tutorial assumes that you are familiar with basic Kubernetes concepts like containers, pods and service. |
| 25 | +
|
| 26 | +## Set up an Azure Cache for Redis instance |
| 27 | + |
| 28 | +1. Create a new Azure Cache for Redis instance by using the Azure portal or your preferred CLI tool. Use the [quickstart guide](quickstart-create-redis.md) to get started. |
| 29 | + |
| 30 | + For this tutorial, use a Standard C1 cache. |
| 31 | + :::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 | + |
| 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."::: |
| 35 | + |
| 36 | +1. Follow the steps through to create the cache. |
| 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 | +Creating the cache can take a few minutes. You can move to the next section while the process finishes. |
| 42 | + |
| 43 | +## Install and connect to your AKS cluster |
| 44 | + |
| 45 | +In this section, you first install the Kubernetes CLI and then connect to an AKS cluster. |
| 46 | + |
| 47 | +### Install the Kubernetes CLI |
| 48 | + |
| 49 | +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_. |
| 50 | + |
| 51 | +```bash |
| 52 | +az aks install-cli |
| 53 | +``` |
| 54 | + |
| 55 | +If you use Azure Cloud Shell, _kubectl_ is already installed, and you can skip this step. |
| 56 | + |
| 57 | +### Connect to your AKS cluster |
| 58 | + |
| 59 | +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: |
| 60 | + |
| 61 | +```bash |
| 62 | + az aks get-credentials --resource-group myResourceGroup --name myClusterName |
| 63 | + ``` |
| 64 | + |
| 65 | +Verify that you're able to connect to your cluster by running the following command: |
| 66 | + |
| 67 | +```bash |
| 68 | +kubectl get nodes |
| 69 | +``` |
| 70 | + |
| 71 | +You should see similar output showing the list of your cluster nodes. |
| 72 | + |
| 73 | +```output |
| 74 | +NAME STATUS ROLES AGE VERSION |
| 75 | +aks-agentpool-21274953-vmss000001 Ready agent 1d v1.24.15 |
| 76 | +aks-agentpool-21274953-vmss000003 Ready agent 1d v1.24.15 |
| 77 | +aks-agentpool-21274953-vmss000006 Ready agent 1d v1.24.15 |
| 78 | +``` |
| 79 | + |
| 80 | +## 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. |
| 83 | + |
| 84 | +Make the following changes to the deployment file before you save the file as _azure-vote-sample.yaml_. |
| 85 | + |
| 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: |
| 110 | + metadata: |
| 111 | + labels: |
| 112 | + app: azure-vote-front |
| 113 | + spec: |
| 114 | + nodeSelector: |
| 115 | + "kubernetes.io/os": linux |
| 116 | + containers: |
| 117 | + - name: azure-vote-front |
| 118 | + image: mcr.microsoft.com/azuredocs/azure-vote-front:v1 |
| 119 | + ports: |
| 120 | + - containerPort: 80 |
| 121 | + resources: |
| 122 | + requests: |
| 123 | + cpu: 250m |
| 124 | + limits: |
| 125 | + cpu: 500m |
| 126 | + env: |
| 127 | + - name: REDIS |
| 128 | + value: myrediscache.redis.cache.windows.net |
| 129 | + - name: REDIS_PWD |
| 130 | + value: myrediscacheaccesskey |
| 131 | +--- |
| 132 | +apiVersion: v1 |
| 133 | +kind: Service |
| 134 | +metadata: |
| 135 | + name: azure-vote-front |
| 136 | +spec: |
| 137 | + type: LoadBalancer |
| 138 | + ports: |
| 139 | + - port: 80 |
| 140 | + selector: |
| 141 | + app: azure-vote-front |
| 142 | +``` |
| 143 | +
|
| 144 | +## Deploy and test your application |
| 145 | +
|
| 146 | +Run the following command to deploy this application to your AKS cluster: |
| 147 | +
|
| 148 | +```bash |
| 149 | +kubectl apply -f azure-vote-sample.yaml |
| 150 | +``` |
| 151 | + |
| 152 | +You get a response indicating your deployment and service was created: |
| 153 | + |
| 154 | +```output |
| 155 | +deployment.apps/azure-vote-front created |
| 156 | +service/azure-vote-front created |
| 157 | +``` |
| 158 | + |
| 159 | +To test the application, run the following command to check if the pod is running: |
| 160 | + |
| 161 | +```bash |
| 162 | +kubectl get pods |
| 163 | +``` |
| 164 | + |
| 165 | +You see your pod running successfully like: |
| 166 | + |
| 167 | +```output |
| 168 | +NAME READY STATUS RESTARTS AGE |
| 169 | +azure-vote-front-7dd44597dd-p4cnq 1/1 Running 0 68s |
| 170 | +``` |
| 171 | + |
| 172 | +Run the following command to get the endpoint for your application: |
| 173 | + |
| 174 | +```bash |
| 175 | +kubectl get service azure-vote-front |
| 176 | +``` |
| 177 | + |
| 178 | +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. |
| 179 | + |
| 180 | +```output |
| 181 | +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| 182 | +azure-vote-front LoadBalancer 10.0.166.147 20.69.136.105 80:30390/TCP 90s |
| 183 | +``` |
| 184 | + |
| 185 | +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 |
| 190 | + |
| 191 | +To clean up your cluster, run the following commands: |
| 192 | + |
| 193 | +```bash |
| 194 | +kubectl delete deployment azure-vote-front |
| 195 | +kubectl delete service azure-vote-front |
| 196 | +``` |
| 197 | + |
| 198 | +[!INCLUDE [cache-delete-resource-group](includes/cache-delete-resource-group.md)] |
| 199 | + |
| 200 | +## Related content |
| 201 | + |
| 202 | +- [Quickstart: Deploy an Azure Kubernetes Service (AKS) cluster using the Azure portal](/azure/aks/learn/quick-kubernetes-deploy-portal) |
| 203 | +- [AKS sample voting application](https://github.com/Azure-Samples/azure-voting-app-redis/tree/master) |
0 commit comments