Tutorial: Connect to Azure Cache for Redis from your application hosted on Azure Kubernetes Service
In this tutorial, you adapt the AKS sample voting application 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.
Prerequisites
- An Azure subscription. If you don't have an Azure subscription, create a free account.
- 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.
Important
This tutorial assumes that you are familiar with basic Kubernetes concepts like containers, pods and service.
Set up an Azure Cache for Redis instance
Create a new Azure Cache for Redis instance by using the Azure portal or your preferred CLI tool. Use the quickstart guide to get started.
For this tutorial, use a Standard C1 cache.
On the Advanced tab, enable Non-TLS port.
Follow the steps through to create the cache.
Important
This tutorial uses a non-TLS port for demonstration, but we highly recommend that you use a TLS port for anything in production.
Creating the cache can take a few minutes. You can move to the next section while the process finishes.
Install and connect to your AKS cluster
In this section, you first install the Kubernetes CLI and then connect to an AKS cluster.
Install the Kubernetes CLI
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.
az aks install-cli
If you use Azure Cloud Shell, kubectl is already installed, and you can skip this step.
Connect to your AKS cluster
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:
az aks get-credentials --resource-group myResourceGroup --name myClusterName
Verify that you're able to connect to your cluster by running the following command:
kubectl get nodes
You should see similar output showing the list of your cluster nodes.
NAME STATUS ROLES AGE VERSION
aks-agentpool-21274953-vmss000001 Ready agent 1d v1.24.15
aks-agentpool-21274953-vmss000003 Ready agent 1d v1.24.15
aks-agentpool-21274953-vmss000006 Ready agent 1d v1.24.15
Update the voting application to use Azure Cache for Redis
Use the .yml file in the sample for reference.
Make the following changes to the deployment file before you save the file as azure-vote-sample.yaml.
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.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.Define variable named
REDIS_PWD
, and set the value to the access key for the Azure Cache for Redis instance that you created earlier.
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.
apiVersion: apps/v1
kind: Deployment
metadata:
name: azure-vote-front
spec:
replicas: 1
selector:
matchLabels:
app: azure-vote-front
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 1
minReadySeconds: 5
template:
metadata:
labels:
app: azure-vote-front
spec:
nodeSelector:
"kubernetes.io/os": linux
containers:
- name: azure-vote-front
image: mcr.microsoft.com/azuredocs/azure-vote-front:v1
ports:
- containerPort: 80
resources:
requests:
cpu: 250m
limits:
cpu: 500m
env:
- name: REDIS
value: myrediscache.redis.cache.windows.net
- name: REDIS_PWD
value: myrediscacheaccesskey
---
apiVersion: v1
kind: Service
metadata:
name: azure-vote-front
spec:
type: LoadBalancer
ports:
- port: 80
selector:
app: azure-vote-front
Deploy and test your application
Run the following command to deploy this application to your AKS cluster:
kubectl apply -f azure-vote-sample.yaml
You get a response indicating your deployment and service was created:
deployment.apps/azure-vote-front created
service/azure-vote-front created
To test the application, run the following command to check if the pod is running:
kubectl get pods
You see your pod running successfully like:
NAME READY STATUS RESTARTS AGE
azure-vote-front-7dd44597dd-p4cnq 1/1 Running 0 68s
Run the following command to get the endpoint for your application:
kubectl get service azure-vote-front
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.
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
azure-vote-front LoadBalancer 10.0.166.147 20.69.136.105 80:30390/TCP 90s
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:
Clean up your deployment
To clean up your cluster, run the following commands:
kubectl delete deployment azure-vote-front
kubectl delete service azure-vote-front
Clean up resources
If you want to continue to use the resources you created in this article, keep the resource group.
Otherwise, if you're finished with the resources, you can delete the Azure resource group that you created to avoid charges.
Important
Deleting a resource group is irreversible. When you delete a resource group, all the resources in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources. If you created the resources inside an existing resource group that contains resources you want to keep, you can delete each resource individually instead of deleting the resource group.
To delete a resource group
Sign in to the Azure portal, and then select Resource groups.
Select the resource group you want to delete.
If there are many resource groups, use the Filter for any field... box, type the name of your resource group you created for this article. Select the resource group in the results list.
Select Delete resource group.
You're asked to confirm the deletion of the resource group. Type the name of your resource group to confirm, and then select Delete.
After a few moments, the resource group and all of its resources are deleted.
Related content
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for