Prometheus-operator 采用动态PV持久化

持久化的重要性

Prometheus-operator 中的Prometheus需要进行持久化操作否则一旦重启就会导致所有性能数据丢失

持久化方式的选择

查找Prometheus-operator的 value.yaml文件 发现有个storageSpec配置 采用的是storageClass 动态PV持久化
关于是否支持其他形式的持久化需要进一步的研究(也可以使用local volume PVC)

value.yaml的storageSpec配置

1
2
3
4
5
6
7
8
9
storageSpec:
volumeClaimTemplate:
spec:
storageClassName: prometheus-data
accessModes: ["ReadWriteOnce"]
resources:
requests:
storage: 30Gi
# selector: {}

nfs-client-provisioner

如果要让prometheus可以使用动态PV进行持久化要先在集群中创建一个提供动态PV的服务
本集群采用的是nfs

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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: prometheus-data
provisioner: c2.pri/nfsshare

# ---
# #测试用的PVC
# apiVersion: v1
# kind: PersistentVolumeClaim
# metadata:
# labels:
# app: prometheus
# prometheus: monit-prometheus-operator-prometheus
# name: prometheus-data-pvc
# namespace: default
# spec:
# accessModes:
# - ReadWriteOnce
# resources:
# requests:
# storage: 10Gi
# storageClassName: prometheus-data

---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-provisioner-runner
rules:
- apiGroups: [""]
resources: ["persistentvolumes"]
verbs: ["get", "list", "watch", "create", "delete"]
- apiGroups: [""]
resources: ["persistentvolumeclaims"]
verbs: ["get", "list", "watch", "update"]
- apiGroups: ["storage.k8s.io"]
resources: ["storageclasses"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["events"]
verbs: ["create", "update", "patch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: run-nfs-provisioner
subjects:
- kind: ServiceAccount
name: nfs-provisioner
namespace: default
roleRef:
kind: ClusterRole
name: nfs-provisioner-runner
apiGroup: rbac.authorization.k8s.io
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-provisioner
rules:
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-provisioner
subjects:
- kind: ServiceAccount
name: nfs-provisioner
# replace with namespace where provisioner is deployed
namespace: default
roleRef:
kind: Role
name: leader-locking-nfs-provisioner
apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-provisioner
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
name: nfs-client-provisioner
spec:
replicas: 1
strategy:
type: Recreate
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceAccountName: nfs-provisioner
containers:
- name: nfs-client-provisioner
image: registry.c2cloud.cn/library/nfs-client-provisioner:v1
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: c2.pri/nfsshare
- name: NFS_SERVER
value: #nfsIP
- name: NFS_PATH
value: #nfsPath
volumes:
- name: nfs-client-root
nfs:
server: #nfsIP
path: #nfsPath

官方推荐的持久化方式

prometheus 支持把数据远程读写到远程时序数据库中
所以更加好的方法是配置prometheus的远程数据库,把相关数据推送到远程数据库中进行持久化