prometheus 配置postgres作为远程读写数据库

当对于prometheus持久化指标时间要求较高和对prometheus有集群部署的需求时,我们就需要对prometheus配置远程读写库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# prometheus 2.11支持远程读写的数据库
AppOptics: write
Chronix: write
Cortex: read and write
CrateDB: read and write
Elasticsearch: write
Gnocchi: write
Graphite: write
InfluxDB: read and write
IRONdb: read and write
Kafka: write
M3DB: read and write
OpenTSDB: write
PostgreSQL/TimescaleDB: read and write
SignalFx: write
Splunk: read and write
TiKV: read and write
Thanos: write
VictoriaMetrics: write
Wavefront: write

使用PostgreSQL/TimescaleDB 作为远程读写库

创建pg-prometheus

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: pg-prometheus
namespace: default
spec:
serviceName: "pg-prometheus"
selector:
matchLabels:
app: pg-prometheus
replicas: 1
template:
metadata:
labels:
app: pg-prometheus
spec:
initContainers:
- name: "remove-lost-found"
image: "registry.c2cloud.cn/c2cloud/busybox:latest"
command:
- "rm"
- "-fr"
- "/var/lib/postgresql/data/lost+found"
volumeMounts:
- name: prometheusdatabase-vol
mountPath: /var/lib/postgresql/data
securityContext:
fsGroup: 1001
containers:
- name: pg-prometheus
image: registry.c2cloud.cn/library/pg_prometheus:0.2.1
ports:
- containerPort: 5432
env:
- name: POSTGRES_PASSWORD
value: '888888'
args:
- "postgres"
- "-csynchronous_commit=off"
volumeMounts:
- name: prometheusdatabase-vol
mountPath: /var/lib/postgresql/data
volumes:
- name: prometheusdatabase-vol
hostPath:
path: /var/prometheus-database-data
# persistentVolumeClaim:
# claimName: prometheusdatabase

---
apiVersion: v1
kind: Service
metadata:
name: pg-prometheus
namespace: default
labels:
app: pg-prometheus
spec:
type: NodePort
ports:
- port: 5432
targetPort: 5432
protocol: TCP
name: "postgresql"
# label keys and values that must match in order to receive traffic for this service
selector:
app: pg-prometheus

prometheus与数据库间的adapter

创建 pg-prometheus-adapter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: StatefulSet
metadata:
name: pg-prometheus-adapter
namespace: default
spec:
serviceName: "pg-prometheus-adapter"
selector:
matchLabels:
app: pg-prometheus-adapter
replicas: 1 # tells deployment to run 2 pods matching the template
template:
metadata:
labels:
app: pg-prometheus-adapter
spec:
containers:
- name: pg-prometheus-adapter
image: registry.c2cloud.cn/library/prometheus-postgresql-adapter:0.4.1
ports:
- containerPort: 9201
args:
- "-pg.host=pg-prometheus"
- "-pg.password=888888"
- "-pg.prometheus-log-samples"

---
apiVersion: v1
kind: Service
metadata:
name: pg-prometheus-adapter
namespace: default
labels:
app: pg-prometheus-adapter
spec:
type: NodePort
ports:
- port: 9201
targetPort: 9201
protocol: TCP
name: "prometheus-adapter"
# label keys and values that must match in order to receive traffic for this service
selector:
app: pg-prometheus-adapter

在prometheus中配置远程读写

1
2
3
4
remoteRead:
- url: "http://172.17.0.1:9201/read"
remoteWrite:
- url: "http://172.17.0.1:9201/write"

运行一段时间后数据库中的指标

数据库中写入的指标数据量非常大,需要考虑对写入指标进行过滤和数据库集群高可用化部署
mark