|
| 1 | +# Azure Service Operator Cosmos DB with Managed Identity demo |
| 2 | + |
| 3 | +This sample is a demonstration of how to use the Azure Service Operator (ASO) to provision a Cosmos DB backed |
| 4 | +application using Azure Managed Identities. This solution applies the principle of least priviledge to create |
| 5 | +an identity dedicated to the To-Do list application with the minimum set of permissions needed to run the application. |
| 6 | + |
| 7 | +This involves provisioning the following resources through Kubernetes: |
| 8 | +- A User Managed Identity and associted Federated Identity Credential (for use with Azure Workload Identity). |
| 9 | +- A Cosmos DB SQL database and container. |
| 10 | +- A web application (Service, Deployment, Pods) which uses the Comsos DB container to store its data. |
| 11 | + |
| 12 | +## Prerequisites |
| 13 | + |
| 14 | +To deploy this demo application you'll need the following: |
| 15 | + |
| 16 | +1. An Azure subscription to create Azure resources under. |
| 17 | + |
| 18 | +2. An [Azure Kubernetes Service (AKS) cluster](https://docs.microsoft.com/azure/aks/tutorial-kubernetes-deploy-cluster) |
| 19 | + deployed in your subscription, with [`kubectl`](https://kubernetes.io/docs/tasks/tools/#kubectl) configured to talk to it. |
| 20 | + The AKS cluster must have [OIDC issuer](https://learn.microsoft.com/azure/aks/cluster-configuration#oidc-issuer) enabled. |
| 21 | + |
| 22 | +3. The OIDC issuer of the cluster retrieved and stored in an enviornment variable along with the Azure Subscription ID. |
| 23 | + This can be done with the az cli via: |
| 24 | + |
| 25 | + ``` |
| 26 | + export AKS_OIDC_ISSUER=$(az aks show -n myAKScluster -g myResourceGroup --query "oidcIssuerProfile.issuerUrl" -otsv) |
| 27 | + export AZURE_SUBSCRIPTION_ID=<azure subscription id> |
| 28 | + ``` |
| 29 | + |
| 30 | +## Set up Azure Workload Identity |
| 31 | + |
| 32 | +Install Azure Workload Identity. You should already have your clusters OIDC issuer saved in a variable `AKS_OIDC_ISSUER` |
| 33 | +from above so you can just |
| 34 | +install the [Azure Workload Identity webhook](https://azure.github.io/azure-workload-identity/docs/installation/mutating-admission-webhook.html). |
| 35 | + |
| 36 | +## Set up Azure Service Operator |
| 37 | + |
| 38 | +ASO lets you manage Azure resources using Kubernetes tools. |
| 39 | +The operator is installed in your cluster and propagates changes to resources there to the Azure Resource Manager. |
| 40 | +[Read more about how ASO works](https://github.com/azure/azure-service-operator#what-is-it) |
| 41 | + |
| 42 | +Follow [these instructions](https://azure.github.io/azure-service-operator/#installation) to install the ASO v2 |
| 43 | +operator in your cluster. |
| 44 | +Part of this installs |
| 45 | +the [custom resource definitions](https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/) for the Azure and Cosmos DB resources |
| 46 | +we're going to create next: ResourceGroup, DatabaseAccount, |
| 47 | +SqlDatabase, SqlDatabaseContainer, UserAssignedIdentity, and FederatedIdentityCredential |
| 48 | + |
| 49 | + |
| 50 | +## Create the Cosmos DB resources |
| 51 | + |
| 52 | +The YAML documents in [cosmos-sql-demo.yaml](cosmos-sql-demo.yaml) create a number of things: |
| 53 | + |
| 54 | +* A Kubernetes namespace named `cosmos-todo`, |
| 55 | +* An Azure resource group named `aso-cosmos-demo`, |
| 56 | +* A User Assigned Identity named `cosmos-todo-identity` and associated Federated Identity Credential, |
| 57 | +* A Cosmos DB database account, |
| 58 | +* A SQL database and |
| 59 | +* 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)) |
| 60 | + |
| 61 | +Before running `kubectl apply` we must insert the OIDC URL retrieved above into the `FederatedIdentityCredential` resource. For example purposes this is most easily done |
| 62 | +by manually by editing the `cosmos-sql-demo.yaml` file, or using `envsubst`. We show the `envsubst` method below. In production, we would recommend using a tool like Kubebuilder |
| 63 | +to inject these values. |
| 64 | + |
| 65 | +Create the resources by applying the file: |
| 66 | +```sh |
| 67 | +envsubst <cosmos-sql-demo.yaml | kubectl apply -f - |
| 68 | +``` |
| 69 | + |
| 70 | +The operator will start creating the resource group and Cosmos DB items in Azure. |
| 71 | +You can monitor their progress with: |
| 72 | +```sh |
| 73 | +watch kubectl get -n cosmos-todo resourcegroup,databaseaccount,sqldatabase,sqldatabasecontainer,userassignedidentity,federatedidentitycredential |
| 74 | +``` |
| 75 | +You can also find the resource group in the [Azure portal](https://portal.azure.com) and watch the Cosmos DB resources being created there. |
| 76 | + |
| 77 | +It could take a few minutes for the Cosmos DB resources to be provisioned. |
| 78 | +In that time you might see some `ResourceNotFound` errors, or messages indicating that the database account isn't ready, on the SQL database or container. |
| 79 | +This is OK! |
| 80 | +The operator will keep creating them once the account is available and the errors should eventually resolve themselves. |
| 81 | + |
| 82 | +## Deploy the web application |
| 83 | + |
| 84 | +Now we can create the application deployment and service by running: |
| 85 | +```sh |
| 86 | +kubectl apply -f cosmos-app.yaml |
| 87 | +``` |
| 88 | + |
| 89 | +You can watch the state of the pod with: |
| 90 | +```sh |
| 91 | +watch kubectl get -n cosmos-todo pods |
| 92 | +``` |
| 93 | + |
| 94 | +Once the pod's running, we need to expose the service outside the cluster so we can make requests to the todo app. |
| 95 | +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. |
| 96 | +Run this command to set it up: |
| 97 | +```sh |
| 98 | +kubectl port-forward -n cosmos-todo service/cosmos-todo-service 8080:80 |
| 99 | +``` |
| 100 | + |
| 101 | +Now visiting [http://localhost:8080](http://localhost:8080) in your browser will hit the Cosmos DB application. |
| 102 | +You can create todo items and mark them as complete! |
| 103 | + |
| 104 | +Use the Cosmos DB account Data Explorer on the portal to expand the database and container, and you can see the todo-list items stored by the web app. |
| 105 | + |
| 106 | +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). |
| 107 | + |
| 108 | +## Clean up |
| 109 | + |
| 110 | +When you're finished with the sample application you can clean all of the Kubernetes and Azure resources up by deleting the `cosmos-todo` namespace in your cluster. |
| 111 | +```sh |
| 112 | +kubectl delete namespace cosmos-todo |
| 113 | +``` |
| 114 | + |
| 115 | +Kubernetes will delete the web application pod and the operator will delete the Azure resource group and all the Cosmos DB resources. |
| 116 | +(Deleting a Cosmos DB account can take several minutes.) |
0 commit comments