Sub topic 18
Securing and scaling the monitoring stack
This chapter assumes you have a working stack from the earlier chapters: node_exporter on each server, a Prometheus server, Alertmanager, and Grafana. You should be comfortable editing systemd units and YAML, and know your firewall tool. The goal is to reduce what the stack exposes, then decide how to grow it without a rewrite.
Why the default setup is open
Prometheus, its exporters, and Alertmanager start with no authentication and no encryption. Anyone who can reach the port can read the data. Metrics reveal hostnames, kernel versions, mount points, and load patterns, which is useful reconnaissance for an attacker.
| Component | Default port | Risk if exposed |
|---|---|---|
| node_exporter | 9100 | Reveals host details |
| Prometheus | 9090 | Reveals all metrics and runs expensive queries |
| Alertmanager | 9093 | Lets anyone create silences that hide real alerts |
| Grafana | 3000 | Has a login, but a default password is a known weakness |
Reduce the exposure first
Start with network controls, because they cost nothing and work for every component.
- Bind services to the interface they need. If Prometheus and Alertmanager run on the same host, bind Alertmanager to
127.0.0.1:9093. - Allow port 9100 only from the Prometheus server's address.
- Put only Grafana, behind a TLS reverse proxy, in front of users.
This firewall rule uses ufw and assumes Prometheus is at 10.0.0.5:
sudo ufw allow from 10.0.0.5 to any port 9100 proto tcp
sudo ufw deny 9100/tcpufw evaluates rules in order, so keep the allow rule before the deny rule.
To bind node_exporter to one address, change the ExecStart line of the unit from the earlier chapter:
ExecStart=/usr/local/bin/node_exporter --web.listen-address=10.0.1.21:9100TLS and basic authentication
Prometheus, node_exporter, and Alertmanager all accept a web configuration file through --web.config.file. It enables TLS and basic authentication without a proxy. Generate a bcrypt hash first:
htpasswd -nBC 10 "" | tr -d ':\n'Then write the file, replacing the placeholder hash with your output:
tls_server_config:
cert_file: /etc/node_exporter/tls.crt
key_file: /etc/node_exporter/tls.key
basic_auth_users:
prometheus: $2y$10$REPLACE_WITH_YOUR_BCRYPT_HASHAdd --web.config.file=/etc/node_exporter/web.yml to the exporter's ExecStart. The scrape job must now present the credentials and trust the certificate:
scrape_configs:
- job_name: node
scheme: https
basic_auth:
username: prometheus
password_file: /etc/prometheus/node_password
tls_config:
ca_file: /etc/prometheus/ca.crt
static_configs:
- targets: ['server1.example.com:9100', 'server2.example.com:9100']Use password_file instead of an inline password. The file can have mode 0640 and stay out of version control. The certificate must contain the name in the target, such as server1.example.com. A certificate for another name makes the scrape fail with an x509 error, and the target shows up == 0.
Protect Grafana
Grafana holds your dashboards and, through its data source, can query everything in Prometheus. Settings in /etc/grafana/grafana.ini:
[server]
http_addr = 127.0.0.1
root_url = https://grafana.example.com/
[security]
admin_user = admin
cookie_secure = true
[auth.anonymous]
enabled = falseBinding to 127.0.0.1 forces all traffic through your reverse proxy, which terminates TLS. Change the initial admin password on first login, or set it from a secrets file with the GF_SECURITY_ADMIN_PASSWORD__FILE environment variable. Give most people the Viewer role, give dashboard authors Editor, and keep Admin to a very small group. For scripts, use service accounts with tokens. Do not share a person's login.
Two Prometheus flags need care. --web.enable-admin-api allows deleting series and creating snapshots. --web.enable-lifecycle allows remote reload and shutdown through HTTP. Both are off by default. Leave the admin API off unless you need it, and if you enable the lifecycle API, make sure only trusted hosts can reach Prometheus.
Know your limits before scaling
A single Prometheus server is more capable than most people expect. A modest machine can handle hundreds of thousands of active series. The limit is almost always cardinality, the number of distinct label combinations. One label with unbounded values, such as a user ID or a full URL, can multiply the series count. Measure first:
prometheus_tsdb_head_series
topk(10, count by (__name__) ({__name__=~".+"}))The second query is expensive. Run it occasionally, not on a dashboard. Memory use grows with active series, so this count tells you when to act.
Limit the damage from a bad target with a per-scrape sample limit and drop labels or metrics you never use:
scrape_configs:
- job_name: node
sample_limit: 20000
static_configs:
- targets: ['server1.example.com:9100']
metric_relabel_configs:
- source_labels: [__name__]
regex: 'node_scrape_collector_.*'
action: dropIf a scrape returns more than sample_limit samples, Prometheus rejects the whole scrape and the target is marked as failed. That is a useful alarm, but set the limit with headroom.
Scaling options
Choose the simplest option that fits your problem.
| Approach | What it solves | Cost |
|---|---|---|
| Bigger machine, faster disk | More series and longer queries | Single point of failure |
| Split by function, one Prometheus per team or environment | Isolation and smaller servers | Cross-server queries are harder |
| Two identical replicas | Availability | Double the scrape load and storage |
| Remote write to long-term storage (Thanos, Mimir, VictoriaMetrics) | Long retention and a global view | More components to run |
| Federation | Pulling selected series into a higher-level server | Only suitable for aggregated series |
For a small production setup, run two identical Prometheus replicas with a distinguishing external label. Both scrape the same targets and evaluate the same rules:
global:
scrape_interval: 15s
external_labels:
cluster: prod
replica: a
remote_write:
- url: https://metrics.example.com/api/v1/write
basic_auth:
username: prom
password_file: /etc/prometheus/remote_passwordThe replica label must differ between the two servers (a and b). Long-term storage backends use it to deduplicate data.
Alertmanager has its own clustering. Start each instance with --cluster.peer pointing at the others, and list every Alertmanager address in each Prometheus alerting block. Do not put a load balancer in front of them. Prometheus must send each alert to every instance, and the cluster then deduplicates notifications.
Common mistakes
- Opening 9090 to the internet. Anyone can run heavy queries against it. Put it behind the proxy or a firewall.
- Sending alerts through an unsecured webhook. Alert text includes hostnames and thresholds. Use HTTPS receivers.
- Putting a load balancer in front of Alertmanager. This can cause missed or duplicated notifications.
- Adding labels with unbounded values. Watch
prometheus_tsdb_head_seriesafter each change. - Reusing one password everywhere. If the scrape credential leaks, only metrics reads are at risk. Do not reuse it for Grafana or any admin account.
Exercise
Prometheus scrapes https://server1.example.com:9100 with basic authentication, and the target shows up == 0 with the error "x509: certificate is valid for server01.internal, not server1.example.com". What is the correct fix, and why should you not use insecure_skip_verify: true?
Show answer
Issue a certificate whose subject alternative names include server1.example.com (or change the target to the name the certificate covers). insecure_skip_verify disables the check that the server is who it claims to be, so a man-in-the-middle could intercept the credentials and the metrics.
Exercise
Your two Prometheus replicas both run with external_labels of cluster: prod and no replica label. Why is this a problem when you add remote write, and what do you change?
Show answer
Both replicas send identical series, and the backend cannot tell them apart, so it cannot deduplicate them. Add a replica label with a different value on each server, such as a and b.