Prometheus
Prometheus
A time-series metrics collection/storage system. Queried with PromQL. Used with Grafana.
Prometheus
A time-series metrics collection/storage system. Queried with PromQL. Used with Grafana.
Prometheus is an open-source monitoring system that began development at SoundCloud in 2012 and became the second graduated project of the CNCF (Cloud Native Computing Foundation) in 2016. It is an all-in-one solution that has a built-in time series database (TSDB) to collect, store, and query metrics.
Its key feature is the Pull model. Prometheus accesses configured targets over HTTP and fetches metrics from the /metrics endpoint. This approach keeps coupling loose, because the monitored targets do not need to know about Prometheus. The default collection interval (scrape_interval) is 15 seconds.
PromQL (Prometheus Query Language) is a powerful metric query language. Functions such as rate(), increase(), and histogram_quantile() can express complex calculations. For example: rate(http_requests_total[5m]) calculates the number of requests per second over 5 minutes.
In practice, the combination of Prometheus + Grafana (visualization) + Alertmanager (alerting) is the standard. In Kubernetes environments, automatic target discovery is possible with the ServiceMonitor CRD, and in large-scale environments, long-term retention and high availability are achieved with Thanos or Mimir.
# prometheus.yml - Prometheus ์ค์ ํ์ผ
global:
scrape_interval: 15s # ๋ฉํธ๋ฆญ ์์ง ์ฃผ๊ธฐ
evaluation_interval: 15s # ๋ฃฐ ํ๊ฐ ์ฃผ๊ธฐ
external_labels:
cluster: 'production'
region: 'ap-northeast-2'
# ์๋ฆผ ๊ท์น ํ์ผ
rule_files:
- '/etc/prometheus/rules/*.yml'
# Alertmanager ์ค์
alerting:
alertmanagers:
- static_configs:
- targets: ['alertmanager:9093']
# ๋ฉํธ๋ฆญ ์์ง ๋์
scrape_configs:
# Prometheus ์์ฒด ๋ฉํธ๋ฆญ
- job_name: 'prometheus'
static_configs:
- targets: ['localhost:9090']
# Kubernetes ์๋น์ค ๋์ค์ปค๋ฒ๋ฆฌ
- job_name: 'kubernetes-pods'
kubernetes_sd_configs:
- role: pod
relabel_configs:
# prometheus.io/scrape: "true" ์ด๋
ธํ
์ด์
์ด ์๋ ํ๋๋ง ์์ง
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
action: keep
regex: true
- source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_port]
action: replace
target_label: __address__
regex: (.+)
replacement: ${1}:$1
# Node Exporter (์๋ฒ ๋ฉํธ๋ฆญ)
- job_name: 'node-exporter'
static_configs:
- targets: ['node-exporter:9100']
---
# alert-rules.yml - ์๋ฆผ ๊ท์น
groups:
- name: application
rules:
# ๋์ ์๋ฌ์จ ์๋ฆผ
- alert: HighErrorRate
expr: |
sum(rate(http_requests_total{status=~"5.."}[5m])) by (service)
/
sum(rate(http_requests_total[5m])) by (service)
> 0.05
for: 5m
labels:
severity: critical
annotations:
summary: "{{ $labels.service }} ์๋ฌ์จ 5% ์ด๊ณผ"
description: "์ต๊ทผ 5๋ถ๊ฐ ์๋ฌ์จ: {{ $value | humanizePercentage }}"
# ๋์ ๋ ์ดํด์ ์๋ฆผ
- alert: HighLatency
expr: |
histogram_quantile(0.95,
sum(rate(http_request_duration_seconds_bucket[5m])) by (le, service)
) > 1
for: 10m
labels:
severity: warning
annotations:
summary: "{{ $labels.service }} P95 ๋ ์ดํด์ 1์ด ์ด๊ณผ"
---
# ์ ์ฉํ PromQL ์ฟผ๋ฆฌ ์์
# 1. CPU ์ฌ์ฉ๋ฅ (%)
100 - (avg by(instance) (irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100)
# 2. ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋ฅ (%)
(1 - node_memory_MemAvailable_bytes / node_memory_MemTotal_bytes) * 100
# 3. ์ด๋น ์์ฒญ ์ (RPS)
sum(rate(http_requests_total[5m])) by (service)
# 4. P99 ์๋ต ์๊ฐ
histogram_quantile(0.99, sum(rate(http_request_duration_seconds_bucket[5m])) by (le))
# 5. ํ๋ ์ฌ์์ ํ์ (์ต๊ทผ 1์๊ฐ)
increase(kube_pod_container_status_restarts_total[1h])
Senior: "Prometheus metric retention is 15 days, but we need long-term retention to pull monthly reports. Have you looked into adopting Thanos?"
Junior: "Yes, if we store to S3 with the Thanos Sidecar, we can retain data without limit, and I think about $50 a month would be enough for the cost."
Interviewer: "What is the difference between Prometheus's Pull model and Push model?"
Candidate: "In the Pull model, Prometheus fetches metrics from targets, so problems are easy to detect when there is a network issue, while in the Push model, targets send the metrics, which suits ephemeral jobs. Prometheus uses Pull by default, but it also supports Push through the Pushgateway."
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free
Reviewer: "If the range in this rate() query is smaller than scrape_interval, you won't get accurate values. You need to raise [1m] to at least [2m]."
Developer: "Ah, right. Since scrape_interval is 15 seconds, I'll set it to at least 4 times that, which is 1 minute or more."