Skip to content

Commit eb71069

Browse files
Merge pull request #280 from grafana/ha-prometheus
HA prometheus statefulset mixin
2 parents 21b638f + f60b888 commit eb71069

File tree

3 files changed

+59
-10
lines changed

3 files changed

+59
-10
lines changed

prometheus-ksonnet/lib/config.libsonnet

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
prometheus_path: '/prometheus/',
3939
prometheus_port: 9090,
4040
prometheus_web_route_prefix: $._config.prometheus_path,
41+
prometheus_config_file: '/etc/prometheus/prometheus.yml',
4142

4243
// Alertmanager config options.
4344
alertmanager_external_hostname: 'http://alertmanager.%(namespace)s.svc.%(cluster_dns_suffix)s' % self,
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
local k = import 'ksonnet-util/kausal.libsonnet';
2+
local container = k.core.v1.container;
3+
local statefulset = k.apps.v1.statefulSet;
4+
local configMap = k.core.v1.configMap;
5+
6+
{
7+
local root = self,
8+
9+
_config+:: {
10+
prometheus_config_file: '/etc/$(POD_NAME)/prometheus.yml',
11+
},
12+
13+
prometheus_zero+:: {
14+
config+:: root.prometheus_config,
15+
alerts+:: root.prometheusAlerts,
16+
rules+:: root.prometheusRules,
17+
},
18+
19+
prometheus_one+:: {
20+
config+:: root.prometheus_config,
21+
alerts+:: root.prometheusAlerts,
22+
rules+:: root.prometheusRules,
23+
},
24+
25+
prometheus_config_map:: {},
26+
27+
prometheus_config_maps: [
28+
configMap.new('%s-config-0' % self.name) +
29+
configMap.withData({
30+
'prometheus.yml': k.util.manifestYaml(root.prometheus_zero.config),
31+
'alerts.rules': k.util.manifestYaml(root.prometheus_zero.alerts),
32+
'recording.rules': k.util.manifestYaml(root.prometheus_zero.rules),
33+
}),
34+
configMap.new('%s-config-1' % self.name) +
35+
configMap.withData({
36+
'prometheus.yml': k.util.manifestYaml(root.prometheus_one.config),
37+
'alerts.rules': k.util.manifestYaml(root.prometheus_one.alerts),
38+
'recording.rules': k.util.manifestYaml(root.prometheus_one.rules),
39+
}),
40+
],
41+
42+
prometheus_config_mount:: {},
43+
44+
prometheus_container+:: container.withEnv([
45+
container.envType.fromFieldPath('POD_NAME', 'metadata.name'),
46+
]),
47+
48+
prometheus_statefulset+:
49+
k.util.configVolumeMount('%s-config-0' % self.name, '/etc/prometheus-0') +
50+
k.util.configVolumeMount('%s-config-1' % self.name, '/etc/prometheus-1') +
51+
statefulset.mixin.spec.withReplicas(2),
52+
}

prometheus-ksonnet/lib/prometheus.libsonnet

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
name:: error 'must specify name',
1414

1515
_config:: $._config,
16+
local _config = self._config,
1617

1718
local policyRule = $.rbac.v1beta1.policyRule,
1819

@@ -31,12 +32,10 @@
3132
local container = $.core.v1.container,
3233

3334
prometheus_container::
34-
local _config = self._config;
35-
3635
container.new('prometheus', $._images.prometheus) +
3736
container.withPorts($.core.v1.containerPort.new('http-metrics', _config.prometheus_port)) +
3837
container.withArgs([
39-
'--config.file=/etc/prometheus/prometheus.yml',
38+
'--config.file=' + _config.prometheus_config_file,
4039
'--web.listen-address=:%s' % _config.prometheus_port,
4140
'--web.external-url=%(prometheus_external_hostname)s%(prometheus_path)s' % _config,
4241
'--web.enable-admin-api',
@@ -49,8 +48,6 @@
4948
$.util.resourcesLimits('500m', '2Gi'),
5049

5150
prometheus_watch_container::
52-
local _config = self._config;
53-
5451
container.new('watch', $._images.watch) +
5552
container.withArgs([
5653
'-v',
@@ -77,16 +74,17 @@
7774
local statefulset = $.apps.v1.statefulSet,
7875
local volumeMount = $.core.v1.volumeMount,
7976

80-
prometheus_statefulset:
81-
local _config = self._config;
77+
prometheus_config_mount::
78+
$.util.configVolumeMount('%s-config' % self.name, '/etc/prometheus'),
8279

80+
prometheus_statefulset:
8381
statefulset.new(self.name, 1, [
8482
self.prometheus_container + container.withVolumeMountsMixin(
8583
volumeMount.new('%s-data' % self.name, '/prometheus')
8684
),
8785
self.prometheus_watch_container,
8886
], self.prometheus_pvc) +
89-
$.util.configVolumeMount('%s-config' % self.name, '/etc/prometheus') +
87+
self.prometheus_config_mount +
9088
statefulset.mixin.spec.withServiceName('prometheus') +
9189
statefulset.mixin.spec.template.metadata.withAnnotations({
9290
'prometheus.io.path': '%smetrics' % _config.prometheus_web_route_prefix,
@@ -101,8 +99,6 @@
10199
local servicePort = service.mixin.spec.portsType,
102100

103101
prometheus_service:
104-
local _config = self._config;
105-
106102
$.util.serviceFor(self.prometheus_statefulset) +
107103
service.mixin.spec.withPortsMixin([
108104
servicePort.newNamed(

0 commit comments

Comments
 (0)