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