|
| 1 | +# Azure Service Operator Cosmos DB demo |
| 2 | + |
| 3 | +This sample is a demonstration of how to use the Azure Service Operator (ASO) to provision a Cosmos DB SQL database and container, |
| 4 | +and then deploy a web application that uses that container to store its data, |
| 5 | +by creating resources in a Kubernetes cluster. |
| 6 | + |
| 7 | +## Prerequisites |
| 8 | + |
| 9 | +To deploy this demo application you'll need the following: |
| 10 | + |
| 11 | +1. A Kubernetes cluster (at least version 1.21) [created and |
| 12 | + running](https://kubernetes.io/docs/tutorials/kubernetes-basics/create-cluster/), |
| 13 | + and [`kubectl`](https://kubernetes.io/docs/tasks/tools/#kubectl) configured to talk to it. (You can check your cluster |
| 14 | + version with `kubectl version`.) This could be a local [Kind cluster](https://kind.sigs.k8s.io/docs/user/quick-start/) |
| 15 | + or an [Azure Kubernetes Service |
| 16 | + cluster](https://docs.microsoft.com/en-us/azure/aks/tutorial-kubernetes-deploy-cluster) |
| 17 | + running in your subscription. |
| 18 | + |
| 19 | +2. An Azure subscription to create Azure resources under. |
| 20 | + |
| 21 | +## Set up Azure Service Operator |
| 22 | + |
| 23 | +ASO lets you manage Azure resources using Kubernetes tools. |
| 24 | +The operator is installed in your cluster and propagates changes to resources there to the Azure Resource Manager. |
| 25 | +[Read more about how ASO works](https://github.com/azure/azure-service-operator#what-is-it) |
| 26 | + |
| 27 | +Follow [these |
| 28 | +instructions](https://github.com/Azure/azure-service-operator/tree/master/v2#installation) to install the ASO v2 operator in your cluster. |
| 29 | +Part of this installs |
| 30 | +the [custom resource definitions](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/) for the Azure and Cosmos DB resources |
| 31 | +we're going to create next: ResourceGroup, DatabaseAccount, |
| 32 | +SqlDatabase, and SqlDatabaseContainer. |
| 33 | + |
| 34 | + |
| 35 | +## Create the Cosmos DB resources |
| 36 | + |
| 37 | +The YAML documents in [cosmos-sql-demo.yaml](cosmos-sql-demo.yaml) create a number of things: |
| 38 | + |
| 39 | +* A Kubernetes namespace named `cosmosdb`, |
| 40 | +* An Azure resource group named `aso-cosmos-demo`, |
| 41 | +* A Cosmos DB database account, |
| 42 | +* A SQL database and |
| 43 | +* A container (equivalent to a table in the [Cosmos DB resource model](https://docs.microsoft.com/en-us/azure/cosmos-db/account-databases-containers-items)) |
| 44 | + |
| 45 | +Create them all by applying the file: |
| 46 | +```sh |
| 47 | +kubectl apply -f cosmos-sql-demo.yaml |
| 48 | +``` |
| 49 | + |
| 50 | +The operator will start creating the resource group and Cosmos DB items in Azure. |
| 51 | +You can monitor their progress with: |
| 52 | +```sh |
| 53 | +watch kubectl get -n cosmosdb resourcegroup,databaseaccount,sqldatabase,sqldatabasecontainer |
| 54 | +``` |
| 55 | + |
| 56 | +You can also find the resource group in the [Azure portal](https://portal.azure.com) and watch the Cosmos DB resources being created there. |
| 57 | + |
| 58 | +## Create the `cosmos-settings` secret |
| 59 | + |
| 60 | +We need to provide the web application with access to the database. |
| 61 | +Once the database account is created, store the connection details into environment variables with the following commands: |
| 62 | +```sh |
| 63 | +COSMOS_DB_ACCOUNT="$(az cosmosdb show --resource-group aso-cosmos-demo --name sample-db-account -otsv --query 'locations[0].documentEndpoint')" |
| 64 | +COSMOS_DB_KEY="$(az cosmosdb keys list --resource-group aso-cosmos-demo --name sample-db-account -otsv --query 'primaryMasterKey')" |
| 65 | +``` |
| 66 | + |
| 67 | +Then create the secret the pod will use with: |
| 68 | +```sh |
| 69 | +kubectl --namespace cosmosdb create secret generic cosmos-settings \ |
| 70 | + --from-literal=Account="$COSMOS_DB_ACCOUNT" \ |
| 71 | + --from-literal=Key="$COSMOS_DB_KEY" \ |
| 72 | + --from-literal=DatabaseName="sample-sql-db" \ |
| 73 | + --from-literal=ContainerName="sample-sql-container" |
| 74 | +``` |
| 75 | + |
| 76 | +(Secret handling is an area we're still working on in ASO - in the future the operator should automatically get these details from Azure and create the secret itself once the database account is ready.) |
| 77 | + |
| 78 | +## Deploy the web application |
| 79 | + |
| 80 | +Now we can create the application deployment and service by running: |
| 81 | +```sh |
| 82 | +kubectl apply -f deploy-cosmos-app.yaml |
| 83 | +``` |
| 84 | + |
| 85 | +You can watch the state of the pod with: |
| 86 | +```sh |
| 87 | +watch kubectl get -n cosmosdb pods |
| 88 | +``` |
| 89 | + |
| 90 | +Once the pod's running, we need to expose the service outside the cluster so we can make requests to the todo app. |
| 91 | +There are a [number of ways](https://kubernetes.io/docs/tutorials/kubernetes-basics/expose/expose-intro/) to do this in Kubernetes, but a simple option for this demonstration is using port-forwarding. |
| 92 | +Run this command to set it up: |
| 93 | +```sh |
| 94 | +kubectl port-forward -n cosmosdb service/cosmos-todo-service 8080:80 |
| 95 | +``` |
| 96 | + |
| 97 | +Now visiting [http://localhost:8080](http://localhost:8080) in your browser will hit the Cosmos DB application. |
| 98 | + |
| 99 | +If you're interested in how the todo application uses the Cosmos DB API, the code is available [here](https://github.com/Azure-Samples/cosmos-dotnet-core-todo-app/tree/main/src). |
0 commit comments