Skip to content

Commit 6b07451

Browse files
authored
Merge pull request #268951 from rashmichandrashekar/rashmi/operator-docs
Updates for operator release
2 parents ccfb3f4 + 0999ff2 commit 6b07451

File tree

8 files changed

+304
-12
lines changed

8 files changed

+304
-12
lines changed
70.8 KB
Loading
58.7 KB
Loading
49.8 KB
Loading
49.1 KB
Loading

articles/azure-monitor/containers/prometheus-metrics-scrape-configuration.md

Lines changed: 152 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ This article provides instructions on customizing metrics scraping for a Kuberne
1515
Four different configmaps can be configured to provide scrape configuration and other settings for the metrics add-on. All config-maps should be applied to `kube-system` namespace for any cluster.
1616

1717
> [!NOTE]
18-
> None of the four configmaps exist by default in the cluster when Managed Prometheus is enabled. Depending on what needs to be customized, you need to deploy any or all of these four configmaps with the same name specified, in `kube-system` namespace. AMA-Metrics pods will pick up these configmaps after you deploy them to `kube-system` namespace, and will restart in 2-3 minutes to apply the configuration settings specified in the configmap(s).
18+
> None of the four configmaps exist by default in the cluster when Managed Prometheus is enabled. Depending on what needs to be customized, you need to deploy any or all of these four configmaps with the same name specified, in `kube-system` namespace. AMA-Metrics pods will pick up these configmaps after you deploy them to `kube-system` namespace, and will restart in 2-3 minutes to apply the configuration settings specified in the configmap(s).
19+
1920

2021
1. [`ama-metrics-settings-configmap`](https://aka.ms/azureprometheus-addon-settings-configmap)
2122
This config map has below simple settings that can be configured. You can take the configmap from the above git hub repo, change the settings are required and apply/deploy the configmap to `kube-system` namespace for your cluster
@@ -25,15 +26,20 @@ Four different configmaps can be configured to provide scrape configuration and
2526
* metric keep-lists - this setting is used to control which metrics are listed to be allowed from each default target and to change the default behavior
2627
* scrape intervals for default/pre-definetargets. `30 secs` is the default scrape frequency and it can be changed per default target using this configmap
2728
* debug-mode - turning this ON helps to debug missing metric/ingestion issues - see more on [troubleshooting](prometheus-metrics-troubleshoot.md#debug-mode)
28-
2. [`ama-metrics-prometheus-config`](https://aka.ms/azureprometheus-addon-rs-configmap) (**Recommended**)
29+
2. [`ama-metrics-prometheus-config`](https://aka.ms/azureprometheus-addon-rs-configmap)
2930
This config map can be used to provide Prometheus scrape config for addon replica. Addon runs a singleton replica, and any cluster level services can be discovered and scraped by providing scrape jobs in this configmap. You can take the sample configmap from the above git hub repo, add scrape jobs that you would need and apply/deploy the config map to `kube-system` namespace for your cluster.
31+
**Although this is supported, please note that the recommended way of scraping custom targets is using [custom resources](prometheus-metrics-scrape-configuration.md#custom-resource-definitions)**
3032
3. [`ama-metrics-prometheus-config-node`](https://aka.ms/azureprometheus-addon-ds-configmap) (**Advanced**)
3133
This config map can be used to provide Prometheus scrape config for addon DaemonSet that runs on every **Linux** node in the cluster, and any node level targets on each node can be scraped by providing scrape jobs in this configmap. When you use this configmap, you can use `$NODE_IP` variable in your scrape config, which gets substituted by corresponding node's ip address in DaemonSet pod running on each node. This way you get access to scrape anything that runs on that node from the metrics addon DaemonSet. **Please be careful when you use discoveries in scrape config in this node level config map, as every node in the cluster will setup & discover the target(s) and will collect redundant metrics**.
3234
You can take the sample configmap from the above git hub repo, add scrape jobs that you would need and apply/deploy the config map to `kube-system` namespace for your cluster
3335
4. [`ama-metrics-prometheus-config-node-windows`](https://aka.ms/azureprometheus-addon-ds-configmap-windows) (**Advanced**)
3436
This config map can be used to provide Prometheus scrape config for addon DaemonSet that runs on every **Windows** node in the cluster, and node level targets on each node can be scraped by providing scrape jobs in this configmap. When you use this configmap, you can use `$NODE_IP` variable in your scrape config, which will be substituted by corresponding node's ip address in DaemonSet pod running on each node. This way you get access to scrape anything that runs on that node from the metrics addon DaemonSet. **Please be careful when you use discoveries in scrape config in this node level config map, as every node in the cluster will setup & discover the target(s) and will collect redundant metrics**.
3537
You can take the sample configmap from the above git hub repo, add scrape jobs that you would need and apply/deploy the config map to `kube-system` namespace for your cluster
3638

39+
## Custom Resource Definitions
40+
The Azure Monitor metrics add-on supports scraping Prometheus metrics using Prometheus - Pod Monitors and Service Monitors, similar to the OSS Prometheus operator. Enabling the add-on will deploy the Pod and Service Monitor custom resource definitions to allow you to create your own custom resources.
41+
Follow the instructions to [create and apply custom resources](prometheus-metrics-scrape-crd.md) on your cluster.
42+
3743
## Metrics add-on settings configmap
3844

3945
The [ama-metrics-settings-configmap](https://aka.ms/azureprometheus-addon-settings-configmap) can be downloaded, edited, and applied to the cluster to customize the out-of-the-box features of the metrics add-on.
@@ -142,17 +148,21 @@ and apply the YAML using the following command: `kubectl apply -f .\ama-metrics-
142148
143149
## Configure custom Prometheus scrape jobs
144150
145-
You can configure the metrics add-on to scrape targets other than the default ones by using the same configuration format as the [Prometheus configuration file](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#configuration-file).
146-
147-
Follow the instructions to [create, validate, and apply the configmap](prometheus-metrics-scrape-validate.md) for your cluster.
148-
151+
You can scrape Prometheus metrics using Prometheus - Pod Monitors and Service Monitors(**Recommended**), similar to the OSS Prometheus operator.
152+
Follow the instructions to [create and apply custom resources](prometheus-metrics-scrape-crd.md) on your cluster.
149153
154+
Additionally, you can follow the instructions to [create, validate, and apply the configmap](prometheus-metrics-scrape-validate.md) for your cluster.
155+
The configuration format is similar to [Prometheus configuration file](https://prometheus.io/docs/prometheus/latest/configuration/configuration/#configuration-file).
150156
151157
## Prometheus configuration tips and examples
152158
153159
Learn some tips from examples in this section.
154160
155-
### Configuration file for custom scrape config
161+
### [Configuration using CRD for custom scrape config](#tab/CRDConfig)
162+
Use the [Pod and Service Monitor templates](https://github.com/Azure/prometheus-collector/tree/main/otelcollector/customresources) and follow the API specification to create your custom resources([PodMonitor](https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#podmonitor) and [Service Monitor](https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#monitoring.coreos.com/v1.ServiceMonitor)). **Note** that the only change required to the existing OSS CRs for being picked up by the Managed Prometheus is the API group - **azmonitoring.coreos.com/v1**. See [here](prometheus-metrics-scrape-crd.md) to learn more
163+
164+
165+
### [Configuration file for custom scrape config](#tab/ConfigFile)
156166
157167
The configuration format is the same as the [Prometheus configuration file](https://aka.ms/azureprometheus-promioconfig). Currently, the following sections are supported:
158168
@@ -172,10 +182,143 @@ Any other unsupported sections must be removed from the config before they're ap
172182

173183
See the [Apply config file](prometheus-metrics-scrape-validate.md#deploy-config-file-as-configmap) section to create a configmap from the Prometheus config.
174184

185+
---
186+
175187
> [!NOTE]
176188
> When custom scrape configuration fails to apply because of validation errors, default scrape configuration continues to be used.
177189
190+
> If you want to use global settings that apply to all the scrape jobs, and only have [Custom Resources](prometheus-metrics-scrape-crd.md) you would still need to create a configmap with just the global settings(Settings for each of these in the custom resources will override the ones in the global section)
191+
192+
178193
## Scrape configs
194+
### [Scrape Configs using CRD](#tab/CRDScrapeConfig)
195+
Currently, the supported methods of target discovery for custom resources are pod and service monitor
196+
197+
#### Pod and Service Monitors
198+
Targets discovered using pod and service monitors have different `__meta_*` labels depending on what monitor is used. You can use the labels in the `relabelings` section to filter targets or replace labels for the targets.
199+
200+
See the [Pod and Service Monitor examples](https://github.com/Azure/prometheus-collector/tree/main/otelcollector/deploy/example-custom-resources) of pod and service monitors.
201+
202+
### Relabelings
203+
The `relabelings` section is applied at the time of target discovery and applies to each target for the job. The following examples show ways to use `relabelings`.
204+
205+
#### Add a label
206+
Add a new label called `example_label` with the value `example_value` to every metric of the job. Use `__address__` as the source label only because that label always exists and adds the label for every target of the job.
207+
208+
```yaml
209+
relabelings:
210+
- sourceLabels: [__address__]
211+
targetLabel: example_label
212+
replacement: 'example_value'
213+
```
214+
215+
#### Use Pod or Service Monitor labels
216+
217+
Targets discovered using pod and service monitors have different `__meta_*` labels depending on what monitor is used. The `__*` labels are dropped after discovering the targets. To filter by using them at the metrics level, first keep them using `relabelings` by assigning a label name. Then use `metricRelabelings` to filter.
218+
219+
```yaml
220+
# Use the kubernetes namespace as a label called 'kubernetes_namespace'
221+
relabelings:
222+
- sourceLabels: [__meta_kubernetes_namespace]
223+
action: replace
224+
targetLabel: kubernetes_namespace
225+
226+
# Keep only metrics with the kubernetes namespace 'default'
227+
metricRelabelings:
228+
- sourceLabels: [kubernetes_namespace]
229+
action: keep
230+
regex: 'default'
231+
```
232+
233+
#### Job and instance relabeling
234+
235+
You can change the `job` and `instance` label values based on the source label, just like any other label.
236+
237+
```yaml
238+
# Replace the job name with the pod label 'k8s app'
239+
relabelings:
240+
- sourceLabels: [__meta_kubernetes_pod_label_k8s_app]
241+
targetLabel: job
242+
243+
# Replace the instance name with the node name. This is helpful to replace a node IP
244+
# and port with a value that is more readable
245+
relabelings:
246+
- sourceLabels: [__meta_kubernetes_node_name]]
247+
targetLabel: instance
248+
```
249+
250+
### Metric Relabelings
251+
252+
Metric relabelings are applied after scraping and before ingestion. Use the `metricRelabelings` section to filter metrics after scraping. The following examples show how to do so.
253+
254+
#### Drop metrics by name
255+
256+
```yaml
257+
# Drop the metric named 'example_metric_name'
258+
metricRelabelings:
259+
- sourceLabels: [__name__]
260+
action: drop
261+
regex: 'example_metric_name'
262+
```
263+
264+
#### Keep only certain metrics by name
265+
266+
```yaml
267+
# Keep only the metric named 'example_metric_name'
268+
metricRelabelings:
269+
- sourceLabels: [__name__]
270+
action: keep
271+
regex: 'example_metric_name'
272+
```
273+
274+
```yaml
275+
# Keep only metrics that start with 'example_'
276+
metricRelabelings:
277+
- sourceLabels: [__name__]
278+
action: keep
279+
regex: '(example_.*)'
280+
```
281+
282+
#### Rename metrics
283+
Metric renaming isn't supported.
284+
285+
#### Filter metrics by labels
286+
287+
```yaml
288+
# Keep metrics only where example_label = 'example'
289+
metricRelabelings:
290+
- sourceLabels: [example_label]
291+
action: keep
292+
regex: 'example'
293+
```
294+
295+
```yaml
296+
# Keep metrics only if `example_label` equals `value_1` or `value_2`
297+
metricRelabelings:
298+
- sourceLabels: [example_label]
299+
action: keep
300+
regex: '(value_1|value_2)'
301+
```
302+
303+
```yaml
304+
# Keep metrics only if `example_label_1 = value_1` and `example_label_2 = value_2`
305+
metricRelabelings:
306+
- sourceLabels: [example_label_1, example_label_2]
307+
separator: ';'
308+
action: keep
309+
regex: 'value_1;value_2'
310+
```
311+
312+
```yaml
313+
# Keep metrics only if `example_label` exists as a label
314+
metricRelabelings:
315+
- sourceLabels: [example_label_1]
316+
action: keep
317+
regex: '.+'
318+
```
319+
320+
321+
### [Scrape Configs using Config file](#tab/ConfigFileScrapeConfig)
179322
Currently, the supported methods of target discovery for a [scrape config](https://aka.ms/azureprometheus-promioconfig-scrape) are either [`static_configs`](https://aka.ms/azureprometheus-promioconfig-static) or [`kubernetes_sd_configs`](https://aka.ms/azureprometheus-promioconfig-sdk8s) for specifying or discovering targets.
180323

181324
#### Static config
@@ -313,6 +456,8 @@ metric_relabel_configs:
313456
regex: '.+'
314457
```
315458
459+
---
460+
316461
### TLS based scraping
317462
318463
If you have a Prometheus instance served with TLS and you want to scrape metrics from it, you need to set scheme to `https` and set the TLS settings in your configmap or respective CRD. You can use the `tls_config` configuration property inside a custom scrape job to configure the TLS settings either using a CRD or a configmap. You need to provide a CA certificate to validate API server certificate with. The CA certificate is used to verify the authenticity of the server's certificate when Prometheus connects to the target over TLS. It helps ensure that the server's certificate is signed by a trusted authority.
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
---
2+
title: Create and apply Pod and Service Monitors for Prometheus metrics in Azure Monitor
3+
description: Describes how to create and apply pod and service monitors to scrape Prometheus metrics in Azure Monitor to Kubernetes cluster.
4+
ms.topic: conceptual
5+
ms.date: 3/13/2024
6+
ms.reviewer: aul
7+
---
8+
# Custom Resource Definitions
9+
The enablement of managed prometheus automatically deploys the custom resource definitions (CRD) for [pod monitors](https://github.com/Azure/prometheus-collector/blob/main/otelcollector/deploy/addon-chart/azure-monitor-metrics-addon/templates/ama-metrics-podmonitor-crd.yaml) and [service monitors](https://github.com/Azure/prometheus-collector/blob/main/otelcollector/deploy/addon-chart/azure-monitor-metrics-addon/templates/ama-metrics-servicemonitor-crd.yaml). These custom resource definitions are the same custom resource definitions (CRD) as [OSS Pod monitors](https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#monitoring.coreos.com/v1.PodMonitor) and [OSS service monitors](https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#monitoring.coreos.com/v1.ServiceMonitor) for Prometheus, except for a change in the group name. If you have existing Prometheus CRDs and custom resources on your cluster, these CRDs won't conflict with the CRDs created by the add-on. At the same time, the managed Prometheus addon does not pick up the CRDs created for the OSS Prometheus. This separation is intentional for the purposes of isolation of scrape jobs.
10+
11+
### Create a Pod or Service Monitor
12+
Use the [Pod and Service Monitor templates](https://github.com/Azure/prometheus-collector/tree/main/otelcollector/customresources) and follow the API specification to create your custom resources([PodMonitor](https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#podmonitor) and [Service Monitor](https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#monitoring.coreos.com/v1.ServiceMonitor)). **Note** that the only change required to the existing OSS CRs(Custom Resources) for being picked up by the Managed Prometheus is the API group - **azmonitoring.coreos.com/v1**.
13+
>Note - Please make sure to use the **labelLimit, labelNameLengthLimit and labelValueLengthLimit** specified in the templates so that they are not dropped during processing.
14+
15+
Your pod and service monitors should look like the following examples:
16+
17+
#### Example Pod Monitor -
18+
19+
```yaml
20+
# Note the API version is azmonitoring.coreos.com/v1 instead of monitoring.coreos.com/v1
21+
apiVersion: azmonitoring.coreos.com/v1
22+
kind: PodMonitor
23+
24+
# Can be deployed in any namespace
25+
metadata:
26+
name: reference-app
27+
namespace: app-namespace
28+
spec:
29+
labelLimit: 63
30+
labelNameLengthLimit: 511
31+
labelValueLengthLimit: 1023
32+
33+
# The selector specifies which pods to filter for
34+
selector:
35+
36+
# Filter by pod labels
37+
matchLabels:
38+
environment: test
39+
matchExpressions:
40+
- key: app
41+
operator: In
42+
values: [app-frontend, app-backend]
43+
44+
# [Optional] Filter by pod namespace
45+
namespaceSelector:
46+
matchNames: [app-frontend, app-backend]
47+
48+
# [Optional] Labels on the pod with these keys will be added as labels to each metric scraped
49+
podTargetLabels: [app, region, environment]
50+
51+
# Multiple pod endpoints can be specified. Port requires a named port.
52+
podMetricsEndpoints:
53+
- port: metrics
54+
```
55+
#### Example Service Monitor -
56+
```yaml
57+
# Note the API version is azmonitoring.coreos.com/v1 instead of monitoring.coreos.com/v1
58+
apiVersion: azmonitoring.coreos.com/v1
59+
kind: ServiceMonitor
60+
61+
# Can be deployed in any namespace
62+
metadata:
63+
name: reference-app
64+
namespace: app-namespace
65+
spec:
66+
labelLimit: 63
67+
labelNameLengthLimit: 511
68+
labelValueLengthLimit: 1023
69+
70+
# The selector filters endpoints by service labels.
71+
selector:
72+
matchLabels:
73+
app: reference-app
74+
75+
# Multiple endpoints can be specified. Port requires a named port.
76+
endpoints:
77+
- port: metrics
78+
```
79+
80+
### Deploy a Pod or Service Monitor
81+
You can then deploy the pod or service monitor using kubectl apply.
82+
83+
84+
When applied, any errors in the custom resources should show up and the pod or service monitors should fail to apply.
85+
A successful pod monitor creation looks like the following -
86+
```bash
87+
podmonitor.azmonitoring.coreos.com/my-pod-monitor created
88+
```
89+
90+
### Examples
91+
#### Create a sample application
92+
Deploy a sample application exposing prometheus metrics to be configured by pod/service monitor.
93+
94+
```bash
95+
kubectl apply -f https://github.com/Azure/prometheus-collector/blob/main/internal/referenceapp/prometheus-reference-app.yaml
96+
```
97+
98+
#### Create a pod monitor and/or service monitor to scrape metrics
99+
Deploy a pod monitor that is configured to scrape metrics from the example application from the previous step.
100+
101+
##### Pod Monitor
102+
```bash
103+
kubectl apply -f https://github.com/Azure/prometheus-collector/blob/main/otelcollector/deploy/example-custom-resources/pod-monitor/pod-monitor-reference-app.yaml
104+
```
105+
106+
##### Service Monitor
107+
```bash
108+
kubectl apply -f https://github.com/Azure/prometheus-collector/blob/main/otelcollector/deploy/example-custom-resources/service-monitor/service-monitor-reference-app.yaml
109+
```
110+
111+
### Troubleshooting
112+
When the pod or service monitors are successfully applied, if you want to make sure that the pod or service monitor targets get picked up by the addon, follow the instructions [here](prometheus-metrics-troubleshoot.md#prometheus-interface) for general troubleshooting of custom resources and also to ensure the targets show up in 127.0.0.1/targets.
113+
114+
:::image type="content" source="media/prometheus-metrics-troubleshoot/image-pod-service-monitor.png" alt-text="Screenshot showing targets for pod/service monitor" lightbox="media/prometheus-metrics-troubleshoot/image-pod-service-monitor.png":::
115+
116+
## Next steps
117+
118+
- [Learn more about collecting Prometheus metrics](../essentials/prometheus-metrics-overview.md).

0 commit comments

Comments
 (0)