Skip to content

Commit 8caaa4e

Browse files
authored
Merge pull request #44 from shpathak-msft/master
Add azure vote sample for Redis
2 parents 134d09e + 8aac9b5 commit 8caaa4e

File tree

2 files changed

+197
-0
lines changed

2 files changed

+197
-0
lines changed

azure-votes-redis/README.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: azure-vote
5+
---
6+
apiVersion: resources.azure.com/v1api20200601
7+
kind: ResourceGroup
8+
metadata:
9+
name: ${APP_NAME}
10+
namespace: azure-vote
11+
spec:
12+
location: westcentralus
13+
---
14+
apiVersion: cache.azure.com/v1api20201201
15+
kind: Redis
16+
metadata:
17+
name: ${APP_NAME}-redis
18+
namespace: azure-vote
19+
spec:
20+
location: westcentralus
21+
owner:
22+
name: ${APP_NAME}
23+
sku:
24+
family: C
25+
name: Basic
26+
capacity: 0
27+
enableNonSslPort: true
28+
minimumTlsVersion: "1.2"
29+
redisConfiguration:
30+
maxmemory-delta: "10"
31+
maxmemory-policy: allkeys-lru
32+
redisVersion: "6"
33+
operatorSpec:
34+
secrets:
35+
hostName:
36+
name: redis-hostname
37+
key: hostName
38+
primaryKey:
39+
name: redis-access-key
40+
key: primaryKey
41+
---
42+
apiVersion: apps/v1
43+
kind: Deployment
44+
metadata:
45+
name: azure-vote-front
46+
namespace: azure-vote
47+
spec:
48+
replicas: 1
49+
selector:
50+
matchLabels:
51+
app: azure-vote-front
52+
strategy:
53+
rollingUpdate:
54+
maxSurge: 1
55+
maxUnavailable: 1
56+
minReadySeconds: 5
57+
template:
58+
metadata:
59+
labels:
60+
app: azure-vote-front
61+
spec:
62+
nodeSelector:
63+
"kubernetes.io/os": linux
64+
containers:
65+
- name: azure-vote-front
66+
image: mcr.microsoft.com/azuredocs/azure-vote-front:v1
67+
ports:
68+
- containerPort: 80
69+
resources:
70+
requests:
71+
cpu: 250m
72+
limits:
73+
cpu: 500m
74+
env:
75+
- name: REDIS
76+
valueFrom:
77+
secretKeyRef:
78+
name: redis-hostname
79+
key: hostName
80+
optional: false
81+
- name: REDIS_PWD
82+
valueFrom:
83+
secretKeyRef:
84+
name: redis-access-key
85+
key: primaryKey
86+
optional: false
87+
---
88+
apiVersion: v1
89+
kind: Service
90+
metadata:
91+
name: azure-vote-front
92+
namespace: azure-vote
93+
spec:
94+
type: LoadBalancer
95+
ports:
96+
- port: 80
97+
selector:
98+
app: azure-vote-front
99+
100+

0 commit comments

Comments
 (0)