This sample is a demonstration of how to use the Azure Service Operator (ASO) to provision an Azure Cache for Redis, and then deploy a web application that uses that managed Redis instance to store its data.
To deploy this demo application you'll need the following:
-
A Kubernetes cluster (at least version 1.21) created and running, and
kubectl
configured to talk to it. (You can check your cluster version withkubectl version
.) This could be a local Kind cluster or an Azure Kubernetes Service cluster running in your subscription. -
An Azure subscription to create Azure resources under.
ASO lets you manage Azure resources using Kubernetes tools. The operator is installed in your cluster, and propagates changes from cluster resources to Azure, using the Azure Resource Manager. Read more about how ASO works
Follow these instructions to install the ASO v2 operator in your cluster. Part of this installs the custom resource definitions for some of the Azure Resources.
As you follow the installation instructions for Azure Service Operator, add cache.azure.com/*
to the configuration of CRD Patterns. (ASO doesn't automatically install all available Custom Resource Definitions, as most users only want a small subset.)
The YAML documents in azure-vote-managed-redis.yaml create:
- A Kubernetes namespace named
azure-vote
, - An Azure resource group named
aso-redis-demo
, - An Azure Cache for Redis instance.
- A deployment and service for the popular AKS voting sample app.
The redis.cache.azure.com instance is configured to retrieve two secrets that are produced by the Azure Cache for Redis instance - hostname and primaryKey. As described here, these secrets need to be mapped to our sample application and the container for our sample application will be blocked until these two secrets are created.
The Voting Sample is configured with environment variables that read the secrets for the managed Redis hostname and access key, allowing the sample to use the managed cache.
- Create environment variables to hold app name. This APP_NAME below is used to generate the names of some resources in Azure below.
export APP_NAME=my-azure-vote
Warning:: Some of these names must be unique, so we recommend you edit APP_NAME above to be something unique to yourself to avoid conflicts. For example: APP_NAME=annas-voting-app
Create them all by applying the file:
envsubst < azure-vote-managed-redis.yaml | kubectl apply -f -
The operator will start creating the resource group and Azure Cache for Redis instance in Azure. You can monitor their progress with:
watch kubectl get -n azure-vote resourcegroup,redis
You can also find the resource group in the Azure portal and watch the Azure Cache for Redis instance being created there.
It may take a few minutes for the Azure Cache for Redis to be provisioned. In that time, you may see some ResourceNotFound
messages in the logsindicating that the secret, the Azure Cache for Redis or the application deployment are not ready.
This is OK!
Once the Redis instance is created, secrets will be created and will unblock the sample application container creation. All errors will eventually resolve once the Redis instance is provisioned. These errors are ASO monitoring the creation of each resource, allowing it to take the next step as soon as the resource is available.
When the application runs, a Kubernetes service exposes the application front end to the internet. This process can take a few minutes to complete.
kubectl get service azure-vote-front
Copy the EXTERNAL-IP address from the output. To see the application in action, open a web browser to the external IP address of your service.
Alternatively, for kind clusters, you can also use the following command
kubectl port-forward -n azure-vote service/azure-vote-front 8080:80
If you're interested in code for the application, it is available here.
When you're finished with the sample application you can clean all of the Kubernetes and Azure resources up by deleting the azure-vote
namespace in your cluster.
kubectl delete namespace azure-vote
Kubernetes will delete the web application pod and the operator will delete the Azure resource group and resources.