-
Notifications
You must be signed in to change notification settings - Fork 12
/
prometheus.tf
211 lines (193 loc) · 5.72 KB
/
prometheus.tf
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
resource "kubernetes_namespace" "prometheus" {
metadata {
name = "prometheus"
}
}
resource "helm_release" "prometheus" {
repository = "https://prometheus-community.github.io/helm-charts"
chart = "kube-prometheus-stack"
version = var.prometheus-stack-chart-version
name = "prometheus"
namespace = kubernetes_namespace.prometheus.metadata.0.name
create_namespace = false
wait = false
values = [yamlencode({
alertmanager = {
alertmanagerSpec = {
// Create a 10GiB volume for alertmanager
storage = {
volumeClaimTemplate = {
spec = {
accessModes = ["ReadWriteOnce"]
resources = {
requests = {
storage = "10Gi"
}
}
storageClassName = "standard-block"
}
}
}
}
// Make alertmanager available via a loadbalancer
service = {
type = "LoadBalancer"
}
}
grafana = {
// Enable persistence using Persistent Volume Claims
persistence = {
enabled = true
storageClassName = "standard-block"
}
// Make Grafana available via a loadbalancer
service = {
type = "LoadBalancer"
}
additionalDataSources = [
{
// Automatically add Loki as a datasource to Grafana
name = "Loki"
type = "loki"
access = "proxy"
orgId = 1
url = "http://loki.loki.svc.cluster.local:3100/"
version = 1
editable = false
}
]
}
// This serviceMonitor is enabled by default, but let's add a selector so it works a bit better
kubeControllerManager = {
service = {
selector = {
k8s-app = "kube-controller-manager"
}
}
}
// Since etcd isn't running inside the cluster, we have to tell Prometheus to use the control plane node IPs directly
kubeEtcd = {
enabled = var.etcd-ca.enabled
endpoints = var.control-plane-nodes
serviceMonitor = {
scheme = "https"
caFile = "/etc/prometheus/secrets/etcd-client-cert/ca.crt"
certFile = "/etc/prometheus/secrets/etcd-client-cert/tls.crt"
keyFile = "/etc/prometheus/secrets/etcd-client-cert/tls.key"
}
}
// This is also enabled by default, but the selector makes things work a bit better
kubeProxy = {
service = {
selector = {
k8s-app = "kube-proxy"
}
}
}
// This is also enabled by default, but the selector makes things work a bit better
kubeScheduler = {
service = {
selector = {
k8s-app = "kube-scheduler"
}
}
}
prometheus = {
prometheusSpec = {
// By unsetting these selectors Prometheus will check cluster-wide for monitors and rules
ruleSelectorNilUsesHelmValues = false
serviceMonitorSelectorNilUsesHelmValues = false
podMonitorSelectorNilUsesHelmValues = false
probeSelectorNilUsesHelmValues = false
// Create a 10GiB volume for prometheus
storageSpec = {
volumeClaimTemplate = {
spec = {
accessModes = ["ReadWriteOnce"]
resources = {
requests = {
storage = "10Gi"
}
}
storageClassName = "standard-block"
}
}
}
// We need to make sure the etcd-client-cert secret is added to the pod (if etcd monitoring is enabled)
secrets = var.etcd-ca.enabled ? ["etcd-client-cert"] : null
}
// Make prometheus available via a loadbalancer
service = {
type = "LoadBalancer"
}
}
})]
depends_on = [helm_release.rook-ceph]
}
// Create a secret with the CA cert and key for etcd, so that we can generate client certs for scraping metrics
resource "kubernetes_secret" "etcd-ca-key-pair" {
count = var.etcd-ca.enabled ? 1 : 0
metadata {
name = "etcd-ca-key-pair"
namespace = kubernetes_namespace.prometheus.metadata.0.name
}
data = {
"tls.crt" = base64decode(var.etcd-ca.cert)
"tls.key" = base64decode(var.etcd-ca.key)
}
}
// Create the cert-manager Issuer that will sign client certs using the etcd CA key
resource "kubernetes_manifest" "etcd-ca-issuer" {
count = var.etcd-ca.enabled ? 1 : 0
provider = kubernetes-alpha
manifest = {
apiVersion = "cert-manager.io/v1"
kind = "Issuer"
metadata = {
name = "etcd-ca-issuer"
namespace = kubernetes_namespace.prometheus.metadata.0.name
}
spec = {
ca = {
secretName = kubernetes_secret.etcd-ca-key-pair[0].metadata.0.name
}
}
}
}
// Generate a client cert for scraping etcd metrics
resource "kubernetes_manifest" "etcd-client-cert" {
count = var.etcd-ca.enabled ? 1 : 0
provider = kubernetes-alpha
manifest = {
apiVersion = "cert-manager.io/v1"
kind = "Certificate"
metadata = {
name = "etcd-client-cert"
namespace = kubernetes_namespace.prometheus.metadata.0.name
}
spec = {
secretName = "etcd-client-cert"
duration = "2160h0m0s" # 90d
renewBefore = "720h0m0s" # 30d
subject = {
organizations = [
"prometheus"
]
}
privateKey = {
algorithm = "ECDSA"
size = 256
}
usages = [
"client auth"
]
ipAddresses = [
"127.0.0.1"
]
issuerRef = {
name = kubernetes_manifest.etcd-ca-issuer[0].manifest.metadata.name
kind = kubernetes_manifest.etcd-ca-issuer[0].manifest.kind
}
}
}
}