|
| 1 | += Monitoring |
| 2 | +:description: Use Prometheus to monitor OpenSearch |
| 3 | + |
| 4 | +OpenSearch clusters can be monitored with Prometheus, see also the general xref:operators:monitoring.adoc[] page. |
| 5 | +The Prometheus metrics are exposed on the HTTP port 9200 at the path `/_prometheus/metrics`. |
| 6 | + |
| 7 | +The role group services contain the corresponding labels and annotations: |
| 8 | + |
| 9 | +[source,yaml] |
| 10 | +---- |
| 11 | +--- |
| 12 | +apiVersion: v1 |
| 13 | +kind: Service |
| 14 | +metadata: |
| 15 | + name: opensearch-nodes-default-headless |
| 16 | + labels: |
| 17 | + prometheus.io/scrape: "true" |
| 18 | + annotations: |
| 19 | + prometheus.io/path: /_prometheus/metrics |
| 20 | + prometheus.io/port: "9200" |
| 21 | + prometheus.io/scheme: https |
| 22 | + prometheus.io/scrape: "true" |
| 23 | +---- |
| 24 | + |
| 25 | +If authentication is enabled in the OpenSearch security plugin, then the metrics endpoint is also secured. |
| 26 | +To make the metrics accessible for all users, especially Prometheus, anonymous authentication can be enabled and access to the monitoring statistics can be allowed for the role of the anonymous user: |
| 27 | + |
| 28 | +[source,yaml] |
| 29 | +---- |
| 30 | +--- |
| 31 | +apiVersion: v1 |
| 32 | +kind: Secret |
| 33 | +metadata: |
| 34 | + name: opensearch-security-config |
| 35 | +stringData: |
| 36 | + config.yml: | |
| 37 | + --- |
| 38 | + _meta: |
| 39 | + type: config |
| 40 | + config_version: 2 |
| 41 | + config: |
| 42 | + dynamic: |
| 43 | + authc: |
| 44 | + basic_internal_auth_domain: |
| 45 | + description: Authenticate via HTTP Basic against internal users database |
| 46 | + http_enabled: true |
| 47 | + transport_enabled: true |
| 48 | + order: 1 |
| 49 | + http_authenticator: |
| 50 | + type: basic |
| 51 | + challenge: false # <1> |
| 52 | + authentication_backend: |
| 53 | + type: intern |
| 54 | + authz: {} |
| 55 | + http: |
| 56 | + anonymous_auth_enabled: true # <2> |
| 57 | + roles.yml: | |
| 58 | + --- |
| 59 | + _meta: |
| 60 | + type: roles |
| 61 | + config_version: 2 |
| 62 | + monitoring: # <3> |
| 63 | + reserved: true |
| 64 | + cluster_permissions: |
| 65 | + - cluster:monitor/health |
| 66 | + - cluster:monitor/nodes/info |
| 67 | + - cluster:monitor/nodes/stats |
| 68 | + - cluster:monitor/prometheus/metrics |
| 69 | + - cluster:monitor/state |
| 70 | + index_permissions: |
| 71 | + - index_patterns: |
| 72 | + - "*" |
| 73 | + allowed_actions: |
| 74 | + - indices:monitor/health |
| 75 | + - indices:monitor/stats |
| 76 | + roles_mapping.yml: | |
| 77 | + --- |
| 78 | + _meta: |
| 79 | + type: rolesmapping |
| 80 | + config_version: 2 |
| 81 | + monitoring: # <4> |
| 82 | + backend_roles: |
| 83 | + - opendistro_security_anonymous_backendrole |
| 84 | +---- |
| 85 | +<1> If anonymous authentication is enabled, then all defined HTTP authenticators are non-challenging. |
| 86 | +<2> Enable https://docs.opensearch.org/latest/security/access-control/anonymous-authentication/[anonymous authentication] |
| 87 | +<3> Create a role "monitoring" with the required permissions for the Prometheus endpoint |
| 88 | +<4> Map the role "monitoring" to the backend role "opendistro_security_anonymous_backendrole" that is assigned to the anonymous user |
| 89 | + |
| 90 | +If you use the https://prometheus-operator.dev/[Prometheus Operator] to install Prometheus, then you can define a https://prometheus-operator.dev/docs/api-reference/api/#monitoring.coreos.com/v1.ServiceMonitor[ServiceMonitor] to collect the metrics: |
| 91 | + |
| 92 | +[source,yaml] |
| 93 | +---- |
| 94 | +--- |
| 95 | +apiVersion: monitoring.coreos.com/v1 |
| 96 | +kind: ServiceMonitor |
| 97 | +metadata: |
| 98 | + name: stackable-opensearch |
| 99 | + labels: |
| 100 | + release: prometheus-stack # <1> |
| 101 | +spec: |
| 102 | + selector: |
| 103 | + matchLabels: # <2> |
| 104 | + prometheus.io/scrape: "true" |
| 105 | + endpoints: |
| 106 | + - relabelings: |
| 107 | + - sourceLabels: # <3> |
| 108 | + - __meta_kubernetes_service_annotation_prometheus_io_scheme |
| 109 | + action: replace |
| 110 | + targetLabel: __scheme__ |
| 111 | + regex: (https?) |
| 112 | + - sourceLabels: # <4> |
| 113 | + - __meta_kubernetes_service_annotation_prometheus_io_path |
| 114 | + action: replace |
| 115 | + targetLabel: __metrics_path__ |
| 116 | + regex: (.+) |
| 117 | + - sourceLabels: # <5> |
| 118 | + - __meta_kubernetes_pod_name |
| 119 | + - __meta_kubernetes_service_name |
| 120 | + - __meta_kubernetes_namespace |
| 121 | + - __meta_kubernetes_service_annotation_prometheus_io_port |
| 122 | + action: replace |
| 123 | + targetLabel: __address__ |
| 124 | + regex: (.+);(.+);(.+);(\d+) |
| 125 | + replacement: $1.$2.$3.svc.cluster.local:$4 |
| 126 | + tlsConfig: # <6> |
| 127 | + ca: |
| 128 | + configMap: |
| 129 | + name: truststore |
| 130 | + key: ca.crt |
| 131 | +--- |
| 132 | +apiVersion: secrets.stackable.tech/v1alpha1 |
| 133 | +kind: TrustStore |
| 134 | +metadata: |
| 135 | + name: truststore |
| 136 | +spec: |
| 137 | + secretClassName: tls |
| 138 | + format: tls-pem |
| 139 | +---- |
| 140 | +<1> The `release` label must match the Helm release name. |
| 141 | + This Helm release was installed with `helm install prometheus-stack oci://ghcr.io/prometheus-community/charts/kube-prometheus-stack ...`. |
| 142 | +<2> Label selector to select the Kubernetes `Endpoints` objects to scrape metrics from. |
| 143 | + The Endpoints inherit the labels from their Service. |
| 144 | +<3> Use the schema (`http` or `https`) from the Service annotation `prometheus.io/scheme` |
| 145 | +<4> Use the path (`/_prometheus/metrics`) from the Service annotation `prometheus.io/path`. |
| 146 | + These values could also be hard-coded in the ServiceMonitor but it is better to use the ones provided by the operator if they change in the future. |
| 147 | +<5> Use the FQDN instead of the IP address because the IP address is not contained in the certificate. |
| 148 | + The FQDN is constructed from the pod name, service name, namespace and the HTTP port provided in the Service annotation `prometheus.io/port`, e.g. `opensearch-nodes-default-0.opensearch-nodes-default-headless.my-namespace.svc.cluster.local:9200`. |
| 149 | +<6> If TLS is used and the CA is not already provided to Prometheus in another way, then it can be taken from a xref:secret-operator:truststore.adoc[] ConfigMap. |
| 150 | + The TrustStore ConfigMap is updated whenever the CA is rotated. |
| 151 | + In this case, Prometheus takes over the new certificate. |
0 commit comments