|
2 | 2 | title: Deploying Prometheus in Kubernetes |
3 | 3 | short: Kubernetes |
4 | 4 | --- |
| 5 | + |
| 6 | +import { Steps, Tabs, Tab } from "nextra/components" |
| 7 | + |
5 | 8 | # Deploying Prometheus in Kubernetes |
6 | 9 |
|
| 10 | +Prometheus is the most popular open-source monitoring solution for Kubernetes, therefore deploying and configuring it is pretty straightforward. |
| 11 | + |
7 | 12 | Prometheus supports service discovery, so it can automatically find Kubernetes |
8 | 13 | pods to scrape. |
9 | 14 |
|
10 | | -If you have Prometheus installed using the [helm |
11 | | -chart](https://github.com/prometheus-community/helm-charts), service discovery |
12 | | -should be enabled by default. |
| 15 | +## Requirements |
| 16 | + |
| 17 | +- Kubernetes cluster running with `kubectl` configured to access it |
| 18 | +- Make sure you have [Helm](https://helm.sh/docs/intro/install) installed |
| 19 | + |
| 20 | + |
| 21 | +## Instructions |
| 22 | + |
| 23 | +<Steps> |
| 24 | + |
| 25 | +### Create a `monitoring` namespace |
| 26 | + |
| 27 | +It is a good practice to keep all monitoring resources in a separate namespace. |
| 28 | + |
| 29 | +```bash |
| 30 | +kubectl create namespace monitoring |
| 31 | +``` |
| 32 | + |
| 33 | +### Deploy Prometheus to Kubernetes |
| 34 | + |
| 35 | + |
| 36 | +Run the following command to install Prometheus using the Helm chart: |
| 37 | + |
| 38 | +```bash |
| 39 | +helm install prometheus prometheus-community/kube-prometheus-stack --namespace monitoring |
| 40 | +``` |
| 41 | + |
| 42 | +This will install Prometheus in your cluster along with Alertmanager, Node Exporter, and a few other components. |
| 43 | + |
| 44 | + |
| 45 | +### Make Autometrics discoverable |
13 | 46 |
|
14 | | -Add the following annotations to your app's deployment: |
| 47 | +If you have installed Prometheus using the [helm chart](https://github.com/prometheus-community/helm-charts), service discovery should be enabled by default. |
| 48 | + |
| 49 | +Service discovery uses Kubernetes labels to tell Prometheus which ports to look for. In order to make sure that your application metrics are available add the following annotations to your app's deployment: |
15 | 50 |
|
16 | 51 | ```yaml filename="deployment.yaml" |
17 | 52 | apiVersion: apps/v1 |
|
22 | 57 | annotations: |
23 | 58 | prometheus.io/scrape: "true" |
24 | 59 | prometheus.io/path: "/metrics" |
25 | | - prometheus.io/port: "9091" |
| 60 | + prometheus.io/port: "9464" # this can be different depending on which library you have used for instrumentation |
| 61 | +``` |
| 62 | +
|
| 63 | +### (Optional) Add Autometrics ruleset to Prometheus |
| 64 | +
|
| 65 | +If you want to make use of the SLO alerting features in Autometrics, you need to add the Autometrics alerting rules to your Prometheus instance. |
| 66 | +
|
| 67 | +You can find the ruleset in the [Autometrics repository](https://github.com/autometrics-dev/autometrics-shared/blob/main/autometrics.rules.yml). |
| 68 | +
|
| 69 | +#### Save the ruleset to a file |
| 70 | +
|
| 71 | +```bash |
| 72 | +curl https://raw.githubusercontent.com/autometrics-dev/autometrics-shared/main/autometrics.rules.yml > autometrics.rules.yml |
| 73 | +``` |
| 74 | + |
| 75 | +#### Create a ConfigMap from the ruleset |
| 76 | + |
| 77 | +```bash |
| 78 | +kubectl create configmap autometrics-rules --from-file=autometrics.rules.yml --namespace monitoring |
26 | 79 | ``` |
| 80 | + |
| 81 | +#### Create a `values.yaml` file for the Prometheus Helm chart |
| 82 | + |
| 83 | +The `values.yaml` file is used to add extra configuration to the Prometheus Helm chart. In this case we will mount the extra configuration as a ConfigMap and modify Prometheus configuration to include the Autometrics ruleset. |
| 84 | + |
| 85 | +```yaml filename="values.yaml" |
| 86 | +server: |
| 87 | + extraConfigmapMounts: |
| 88 | + - name: autometrics-rules |
| 89 | + mountPath: /autometrics-alerts/ |
| 90 | + configMap: autometrics-rules |
| 91 | + readOnly: true |
| 92 | +serverFiles: |
| 93 | + prometheus.yml: |
| 94 | + rule_files: |
| 95 | + - /etc/config/recording_rules.yml # leave whichever rules you have already |
| 96 | + - /etc/config/alerting_rules.yml # |
| 97 | + - /autometrics-alerts/autometrics.rules.yml # <- add the autometrics ruleset here |
| 98 | +``` |
| 99 | +
|
| 100 | +#### Upgrade Prometheus using Helm |
| 101 | +
|
| 102 | +Upgrade the Prometheus installation using the `helm upgrade` command, passing in the `values.yaml` file. |
| 103 | + |
| 104 | +```bash |
| 105 | +helm upgrade prometheus prometheus-community/kube-prometheus-stack --namespace monitoring -f values.yaml |
| 106 | +``` |
| 107 | + |
| 108 | +</Steps> |
0 commit comments