|
| 1 | +--- |
| 2 | +layout: page |
| 3 | +title: Configure Ruler and Alertmanager with Configmap |
| 4 | +parent: Guides |
| 5 | +has_children: false |
| 6 | +has_toc: false |
| 7 | +--- |
| 8 | +# Configure Ruler and Alertmanager with Configmap |
| 9 | +{: .no_toc } |
| 10 | + |
| 11 | +## Table of contents |
| 12 | +{: .no_toc .text-delta } |
| 13 | + |
| 14 | +1. TOC |
| 15 | +{:toc} |
| 16 | + |
| 17 | +--- |
| 18 | +# Preface |
| 19 | + |
| 20 | +Cortex's Ruler and Alertmanager can be setup to use ConfigMaps to provide themselves with a configuration that can be dynamically updated. |
| 21 | +We simply put ConfigMaps into the same namespace where cortex is running, and they are automatically detected and mounted as files to the Ruler and/or AlertManager containers. |
| 22 | +The containers are also constantly polling for changes to the ConfigMaps. If any changes are found, the configuration file on the container will be updated. |
| 23 | + |
| 24 | +It should be noted that this guide is assuming that multi-tenancy is enabled : |
| 25 | +```yaml |
| 26 | +auth_enabled: true |
| 27 | +``` |
| 28 | +# Setup |
| 29 | +
|
| 30 | +In order to get this working, we must do a couple things. |
| 31 | +
|
| 32 | +## Create Ruler ConfigMap |
| 33 | +
|
| 34 | +To provide the Ruler with the *rules* to alert on, we must create a ConfigMap with the desired ruleset. How you maintain and deploy the ConfigMap is completely in your control, the only requirement is that the ConfigMap be on the same namespace as the Ruler. |
| 35 | +
|
| 36 | +**IMPORTANT** things to note: |
| 37 | +
|
| 38 | +- This ruleset is ONLY for tenantfoo (We will explicitly map it to tenantfoo later) |
| 39 | +- The section under `tenantfoo.yaml` is validated and interpreted exactly as a [Prometheus rules configuration](https://prometheus.io/docs/prometheus/latest/configuration/recording_rules/) would be. How you configure this is completely up to you. |
| 40 | + |
| 41 | +```yaml |
| 42 | +kind: ConfigMap |
| 43 | +metadata: |
| 44 | + name: tenantfoo-ruler-config |
| 45 | + namespace: cortex |
| 46 | +apiVersion: v1 |
| 47 | +data: |
| 48 | + tenantfoo.yaml: |- |
| 49 | + groups: |
| 50 | + - name: <group name> |
| 51 | + rules: |
| 52 | + - alert: <alert name> |
| 53 | + expr: <promql expression> |
| 54 | + for: <duration> |
| 55 | + labels: |
| 56 | + severity: <severity> |
| 57 | + annotations: |
| 58 | + description: <description> |
| 59 | + <remainder of alerts...> |
| 60 | +``` |
| 61 | + |
| 62 | +## Create Alertmanager ConfigMap |
| 63 | + |
| 64 | +To provide the Alertmanager with the information needed to *route* notifications to stakeholders, we must create a ConfigMap with the desired routing rules. How you maintain and deploy the ConfigMap is completely in your control, the only requirement is that the ConfigMap be on the same namespace as the Alertmanager. |
| 65 | + |
| 66 | +**IMPORTANT** things to note: |
| 67 | + |
| 68 | +- These routing rules are ONLY for tenantfoo. The Alertmanager takes the key under `data` (excluding `.yaml`) and uses it as the tenant name for that specific config. |
| 69 | +- The section under `tenantfoo.yaml` is validated and interpreted exactly as a [Prometheus alerting rules configuration](https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/) would be. How you configure this is completely up to you. |
| 70 | + |
| 71 | +```yaml |
| 72 | +kind: ConfigMap |
| 73 | +metadata: |
| 74 | + name: tenantfoo-alertmanager-config |
| 75 | + namespace: cortex |
| 76 | +apiVersion: v1 |
| 77 | +data: |
| 78 | + tenantfoo.yaml: | |
| 79 | + global: |
| 80 | + resolve_timeout: <duration> |
| 81 | + http_config: {} |
| 82 | + smtp_from: ExamplePerson <[email protected]> |
| 83 | + smtp_hello: example.com |
| 84 | + smtp_smarthost: example-smtp.example.com |
| 85 | + route: |
| 86 | + receiver: team-X-mails |
| 87 | + group_by: ['alertname'] |
| 88 | + group_wait: <duration> |
| 89 | + group_interval: <duration> |
| 90 | + repeat_interval: <duration> |
| 91 | + receivers: |
| 92 | + - name: 'team-X-mails' |
| 93 | + email_configs: |
| 94 | + |
| 95 | +``` |
| 96 | + |
| 97 | +## Helm Chart Configuration |
| 98 | + |
| 99 | +Now that the ConfigMaps are created, we can tinker the helm chart to achieve our goal. |
| 100 | + |
| 101 | +### Configuration for Ruler and Alertmanager Config |
| 102 | +Add this to the `values.yaml` under the `config` section as shown. |
| 103 | + |
| 104 | +```yaml |
| 105 | +config: |
| 106 | + ruler: |
| 107 | + enable_api: true |
| 108 | + # already set-up temporary emptyDir volume. Cortex will parse rules from /data/rules |
| 109 | + # and copy them here for prometheus rule evaluation |
| 110 | + rule_path: /rules |
| 111 | +
|
| 112 | + ruler_storage: |
| 113 | + backend: "local" |
| 114 | + local: |
| 115 | + # where your mounted configmap data will be persistently stored |
| 116 | + directory: /data/rules |
| 117 | +
|
| 118 | + alertmanager: |
| 119 | + enable_api: true |
| 120 | + # temporary volume if not using stateful alertmanagers |
| 121 | + data_dir: /data |
| 122 | +
|
| 123 | + alertmanager_storage: |
| 124 | + backend: "local" |
| 125 | + local: |
| 126 | + # temporary volume if not using stateful alertmanagers |
| 127 | + path: /data |
| 128 | +``` |
| 129 | + |
| 130 | +### Configuration for Ruler and Alertmanager |
| 131 | +Add this to the `values.yaml`. |
| 132 | +Note that when we mount `tenantfoo-ruler-config`, we mount it under a folder which should be named after the tenant it corresponds to. In this situation, since we want `tenantfoo-ruler-config` to be the alerting rules for `tenantfoo`, we mount it under `/data/rules/tenantfoo`. |
| 133 | +Also note that for `tenantfoo-alertmanager-config`, we similarily mount it under a folder specific to that tenant. Although it will not use the folder name as the tenant name (it instead uses the key name under `data` in the ConfigMap), it simplifies things if you do so. |
| 134 | + |
| 135 | +```yaml |
| 136 | +ruler: |
| 137 | + extraVolumes: |
| 138 | + - configMap: |
| 139 | + defaultMode: 420 |
| 140 | + name: tenantfoo-ruler-config |
| 141 | + name: tenantfoo-ruler-config |
| 142 | + extraVolumeMounts: |
| 143 | + - name: tenantfoo-ruler-config |
| 144 | + mountPath: /data/rules/tenantfoo |
| 145 | +alertmanager: |
| 146 | + extraVolumes: |
| 147 | + - configMap: |
| 148 | + defaultMode: 420 |
| 149 | + name: tenantfoo-alertmanager-config |
| 150 | + name: tenantfoo-alertmanager-config |
| 151 | + extraVolumeMounts: |
| 152 | + - name: tenantfoo-alertmanager-config |
| 153 | + mountPath: /data/tenantfoo |
| 154 | +``` |
| 155 | + |
| 156 | +# Result |
| 157 | + |
| 158 | +You can now install the helm chart. |
| 159 | + |
| 160 | +You may use the section below to verify the configuration went according to plan. |
| 161 | + |
| 162 | +### Verify Alertmanager |
| 163 | + |
| 164 | +1. Port-forward the alertmanager pod |
| 165 | +2. Run `curl -X GET -H "X-Scope-OrgID: <name of tenant>" localhost:8080/api/v1/alerts` |
| 166 | +3. Verify the correct configuration shows up |
| 167 | + |
| 168 | +### Verify Ruler |
| 169 | + |
| 170 | +1. Port-forward the ruler pod |
| 171 | +2. Run `curl -X GET -H "X-Scope-OrgID: <name of tenant>" localhost:8080/api/v1/rules` |
| 172 | +3. Verify the correct configuration shows up |
| 173 | + |
| 174 | +# Moving forward |
| 175 | + |
| 176 | +You can also follow this guide if you don't have multi-tenancy enabled. However, in this case, you will not get alerting/routing rules specific to each tenant. |
| 177 | + |
| 178 | +Defining fine-grained alerting/routing rules per tenant is when you really get to eat the fruits of your labour when deploying multi-tenant alertmanager/ruler. For example, lets say you currently only have routing rules for tenantfoo. If you wanted to add specific routing rules for another tenant, tenantB, you will do the following: |
| 179 | + |
| 180 | +1. Create another set of ConfigMaps for the new tenants, being sure to adhere to the naming policies stated in this guide |
| 181 | + |
| 182 | +2. Add entries under `extraVolumes` and `extraVolumeMounts` for the Ruler and Alertmanager. All while being sure to adhere to the naming policies stated in this guide |
| 183 | + |
| 184 | +3. [Verify the new tenant is configured](#result) |
0 commit comments