|
| 1 | +# Azure Service Operator sample for Azure Cache for Redis |
| 2 | + |
| 3 | +This sample is a demonstration of how to use the Azure Service Operator (ASO) to provision an Azure Cache for Redis, |
| 4 | +and then deploy a web application that uses that managed Redis instance to store its data. |
| 5 | + |
| 6 | +## Prerequisites |
| 7 | + |
| 8 | +To deploy this demo application you'll need the following: |
| 9 | + |
| 10 | +1. A Kubernetes cluster (at least version 1.21) [created and |
| 11 | + running](https://kubernetes.io/docs/tutorials/kubernetes-basics/create-cluster/), |
| 12 | + and [`kubectl`](https://kubernetes.io/docs/tasks/tools/#kubectl) configured to talk to it. (You can check your cluster |
| 13 | + version with `kubectl version`.) This could be a local [Kind cluster](https://kind.sigs.k8s.io/docs/user/quick-start/) |
| 14 | + or an [Azure Kubernetes Service |
| 15 | + cluster](https://docs.microsoft.com/en-us/azure/aks/tutorial-kubernetes-deploy-cluster) |
| 16 | + running in your subscription. |
| 17 | + |
| 18 | +2. An Azure subscription to create Azure resources under. |
| 19 | + |
| 20 | +## Set up Azure Service Operator |
| 21 | + |
| 22 | +ASO lets you manage Azure resources using Kubernetes tools. |
| 23 | +The operator is installed in your cluster, and propagates changes from cluster resources to Azure, using the Azure Resource Manager. |
| 24 | +[Read more about how ASO works](https://github.com/azure/azure-service-operator#what-is-it) |
| 25 | + |
| 26 | +Follow [these |
| 27 | +instructions](https://github.com/Azure/azure-service-operator/tree/master/v2#installation) to install the ASO v2 operator in your cluster. |
| 28 | +Part of this installs |
| 29 | +the [custom resource definitions](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/) for some of the Azure Resources. |
| 30 | + |
| 31 | +### Note: |
| 32 | +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.) |
| 33 | + |
| 34 | + |
| 35 | +## Deploy the application and Azure resources |
| 36 | + |
| 37 | +The YAML documents in [azure-vote-managed-redis.yaml](azure-vote-managed-redis.yaml) create: |
| 38 | + |
| 39 | +* A Kubernetes namespace named `azure-vote`, |
| 40 | +* An Azure resource group named `aso-redis-demo`, |
| 41 | +* An Azure Cache for Redis instance. |
| 42 | +* A deployment and service for the popular [AKS voting sample app](https://github.com/Azure-Samples/azure-voting-app-redis). |
| 43 | + |
| 44 | +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](https://azure.github.io/azure-service-operator/guide/secrets/#how-to-retrieve-secrets-created-by-azure), 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. |
| 45 | + |
| 46 | +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. |
| 47 | + |
| 48 | +### Steps to install |
| 49 | +1. Create environment variables to hold app name. This APP_NAME below is used to generate the names of some resources in Azure below. |
| 50 | +```sh |
| 51 | +export APP_NAME=my-azure-vote |
| 52 | +``` |
| 53 | +**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 |
| 54 | + |
| 55 | + |
| 56 | +Create them all by applying the file: |
| 57 | +```sh |
| 58 | +envsubst < azure-vote-managed-redis.yaml | kubectl apply -f - |
| 59 | +``` |
| 60 | + |
| 61 | +The operator will start creating the resource group and Azure Cache for Redis instance in Azure. |
| 62 | +You can monitor their progress with: |
| 63 | +```sh |
| 64 | +watch kubectl get -n azure-vote resourcegroup,redis |
| 65 | +``` |
| 66 | +You can also find the resource group in the [Azure portal](https://portal.azure.com) and watch the Azure Cache for Redis instance being created there. |
| 67 | + |
| 68 | +### Note |
| 69 | +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. |
| 70 | +*This is OK!* |
| 71 | +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. |
| 72 | + |
| 73 | +## Test the application |
| 74 | +When the application runs, a Kubernetes service exposes the application front end to the internet. This process can take a few minutes to complete. |
| 75 | + |
| 76 | +```sh |
| 77 | +kubectl get service azure-vote-front |
| 78 | +``` |
| 79 | + |
| 80 | +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. |
| 81 | + |
| 82 | +Alternatively, for kind clusters, you can also use the following command |
| 83 | + |
| 84 | +```sh |
| 85 | +kubectl port-forward -n azure-vote service/azure-vote-front 8080:80 |
| 86 | +``` |
| 87 | + |
| 88 | +If you're interested in code for the application, it is available [here](https://github.com/Azure-Samples/azure-voting-app-redis). |
| 89 | + |
| 90 | +## Clean up |
| 91 | + |
| 92 | +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. |
| 93 | +```sh |
| 94 | +kubectl delete namespace azure-vote |
| 95 | +``` |
| 96 | + |
| 97 | +Kubernetes will delete the web application pod and the operator will delete the Azure resource group and resources. |
0 commit comments