|
| 1 | +# Azure managed Prometheus |
| 2 | + |
| 3 | +In order to authenticate against the Azure Monitor Workspace Query endpoint, you have multiple options: |
| 4 | + |
| 5 | +- Create an Azure Active Directory authentication app [Option #1](#option-1-create-an-azure-authentication-app) |
| 6 | + - Pros: |
| 7 | + - Quick setup. Just need to create an app, get the credentials and add them to the manifests |
| 8 | + - Other pods can't use the Service Principal without having the secret |
| 9 | + - Cons: |
| 10 | + - Requires a service principal (Azure AD permission) |
| 11 | + - Need the client secret in the kubernetes manifests |
| 12 | + - Client secret expires, you need to manage its rotation |
| 13 | +- Use Kubelet's Managed Identity [Option #2](#option-2-use-kubelets-managed-identity) |
| 14 | + - Pros: |
| 15 | + - Quick setup. Get the Managed Identity Client ID and add them to the manifests |
| 16 | + - No need to manage secrets. Removing the password element decreases the risk of the credentials being compromised |
| 17 | + - Cons: |
| 18 | + - Managed Identity is bound to the AKS nodepool, so any pods can use it if they know/get the client ID |
| 19 | +- Use Azure AD Workload Identity [Option #3](#option-3-use-azure-workload-identity-recommended) |
| 20 | + - Pros: |
| 21 | + - Most secure option as Managed Identity is only bound to the pod. No other pods can use it |
| 22 | + - No need to manage secrets. Removing the password element decreases the risk of the credentials being compromised |
| 23 | + - Cons: |
| 24 | + - Extra setup needed: need AKS cluster with Workload Identity add-on enabled, get the OIDC issuer URL and add it to the manifests |
| 25 | + |
| 26 | +## Get the Azure prometheus query endpoint |
| 27 | + |
| 28 | +1. Go to [Azure Monitor workspaces](https://portal.azure.com/#view/HubsExtension/BrowseResource/resourceType/microsoft.monitor%2Faccounts>) and choose your monitored workspace. |
| 29 | +2. In your monitored workspace, `overview`, find the ``Query endpoint`` and copy it. |
| 30 | + |
| 31 | +## Option #1: Create an Azure authentication app |
| 32 | + |
| 33 | +We will now create an Azure authentication app and get the necesssary credentials so Robusta can access Prometheus data. |
| 34 | + |
| 35 | +1. Follow this Azure guide to [Register an app with Azure Active Director](https://learn.microsoft.com/en-us/azure/azure-monitor/essentials/prometheus-self-managed-grafana-azure-active-directory#register-an-app-with-azure-active-directory) |
| 36 | + |
| 37 | +```python |
| 38 | + # Create a custom Prometheus client for Azure Prometheus using Service Principal Authentication |
| 39 | + azure_config = AzurePrometheusConfig( |
| 40 | + url="https://azure-prometheus.example.com", # Replace with your Azure Monitor workspace query endpoint |
| 41 | + azure_resource="https://prometheus.monitor.azure.com", # Default resource for Azure Monitor |
| 42 | + azure_token_endpoint="https://azure-token.example.com", |
| 43 | + azure_client_id="YOUR_AZURE_CLIENT_ID", |
| 44 | + azure_tenant_id="YOUR_AZURE_TENANT_ID", |
| 45 | + azure_client_secret="YOUR_AZURE_CLIENT_SECRET", |
| 46 | + additional_labels={"job": "azure-prometheus"}, |
| 47 | + ) |
| 48 | + azure_client = get_custom_prometheus_connect(azure_config) |
| 49 | +``` |
| 50 | + |
| 51 | +3. Complete the [Allow your app access to your workspace](https://learn.microsoft.com/en-us/azure/azure-monitor/essentials/prometheus-self-managed-grafana-azure-active-directory#allow-your-app-access-to-your-workspace>) step, so your app can query data from your Azure Monitor workspace. |
| 52 | + |
| 53 | +## Option #2: Use Kubelet's Managed Identity |
| 54 | + |
| 55 | +1. Get the AKS kubelet's Managed Identity Client ID: |
| 56 | + |
| 57 | +```bash |
| 58 | + az aks show -g <resource-group> -n <cluster-name> --query identityProfile.kubeletidentity.clientId -o tsv |
| 59 | +``` |
| 60 | + |
| 61 | +2. Set the following settings based from the previous step. |
| 62 | + |
| 63 | +```python |
| 64 | + # Create a custom Prometheus client for Azure Prometheus using Azure Managed Identity |
| 65 | + azure_config = AzurePrometheusConfig( |
| 66 | + url="https://azure-prometheus.example.com", # Replace with your Azure Monitor workspace query endpoint |
| 67 | + azure_use_managed_id=True, |
| 68 | + azure_resource="https://prometheus.monitor.azure.com", # Default resource for Azure Monitor |
| 69 | + azure_metadata_endpoint="http://169.254.169.254/metadata/identity/oauth2/token", # Default endpoint for Managed Identity |
| 70 | + azure_client_id="YOUR_AZURE_CLIENT_ID", # Client ID from step 1 |
| 71 | + azure_tenant_id="YOUR_AZURE_TENANT_ID", |
| 72 | + additional_labels={"job": "azure-prometheus"}, |
| 73 | + ) |
| 74 | + azure_client = get_custom_prometheus_connect(azure_config) |
| 75 | +``` |
| 76 | + |
| 77 | +3. Give access to your Managed Identity on your Azure Monitor Workspace: |
| 78 | + - Open the Access Control (IAM) page for your Azure Monitor workspace in the Azure portal. |
| 79 | + - Select Add role assignment. |
| 80 | + - Select Monitoring Data Reader and select Next. |
| 81 | + - For Assign access to, select Managed identity. |
| 82 | + - Select + Select members. |
| 83 | + - Select the Managed Identity you got from step 1 |
| 84 | + - Select Review + assign to save the configuration. |
| 85 | + |
| 86 | +## Option #3: Use Azure Workload Identity (Recommended) |
| 87 | + |
| 88 | +1. Requirements |
| 89 | + |
| 90 | +AKS cluster needs to have Workload Identity add-on and OIDC issuer enabled. You can use `--enable-oidc-issuer --enable-workload-identity` with `az aks create` or `az aks update` to enable them. |
| 91 | + |
| 92 | +2. Create a new Managed Identity. Change the Identity name, resource group and location to match your environment. |
| 93 | + |
| 94 | +```bash |
| 95 | + export SUBSCRIPTION="$(az account show --query id --output tsv)" |
| 96 | + az identity create --name <identity-name> --resource-group <resource-group> --location "eastus" --subscription "${SUBSCRIPTION}" # keep the identity name for step 4 |
| 97 | + az identity show --name <identity-name> --resource-group <resource-group> -query clientId -o tsv # keep this value for the step #3 |
| 98 | +``` |
| 99 | + |
| 100 | +3. Set the following settings based from the previous step. |
| 101 | + |
| 102 | +```python |
| 103 | + # Create a custom Prometheus client for Azure Prometheus using Azure Managed Identity |
| 104 | + azure_config = AzurePrometheusConfig( |
| 105 | + url="https://azure-prometheus.example.com", # Replace with your Azure Monitor workspace query endpoint |
| 106 | + azure_use_workload_id=True, |
| 107 | + azure_resource="https://prometheus.monitor.azure.com", # Default resource for Azure Monitor |
| 108 | + azure_token_endpoint="https://azure-token.example.com", |
| 109 | + azure_client_id="YOUR_AZURE_CLIENT_ID", # Client ID from step 2 |
| 110 | + azure_tenant_id="YOUR_AZURE_TENANT_ID", |
| 111 | + additional_labels={"job": "azure-prometheus"}, |
| 112 | + ) |
| 113 | + azure_client = get_custom_prometheus_connect(azure_config) |
| 114 | +``` |
| 115 | + |
| 116 | +4. Federate the Service Account with the Managed Identity. Replace the values with the ones from the step #1. |
| 117 | + |
| 118 | +```bash |
| 119 | + export AKS_OIDC_ISSUER="$(az aks show -g <resource-group> -n <cluster-name> --query "oidcIssuerProfile.issuerUrl" -otsv)" # Replace with the corresponding values of your AKS clusters. |
| 120 | + MY_NAMESPACE="mynamespace" # Replace with the namespace where your application is deployed |
| 121 | + MY_SERVICE_ACCOUNT="my-service-account" # Replace with the service account name used by your application |
| 122 | + az identity federated-credential create --name <federated-identity-name> --identity-name <identity-name> --resource-group <resource-group> --issuer ${AKS_OIDC_ISSUER} --subject system:serviceaccount:$MY_NAMESPACE:$MY_SERVICE_ACCOUNT # Use identity name from step 2 |
| 123 | +``` |
| 124 | + |
| 125 | +5. Give access to your Managed Identity on your workspace: |
| 126 | + - Open the Access Control (IAM) page for your Azure Monitor workspace in the Azure portal. |
| 127 | + - Select Add role assignment. |
| 128 | + - Select Monitoring Data Reader and select Next. |
| 129 | + - For Assign access to, select Managed identity. |
| 130 | + - Select + Select members. |
| 131 | + - Select the Managed Identity you got from step 2 |
| 132 | + - Select Review + assign to save the configuration. |
0 commit comments