From 3e01bab89289dedab4301aeb511c8e43ff33a326 Mon Sep 17 00:00:00 2001 From: Rakesh Patnaik Date: Mon, 21 May 2018 17:16:10 +0000 Subject: [PATCH] postgresql prometheus exporter Change-Id: I1559b77e7e9e980f14ed1322aaa1d61797747230 --- .../_create-postgresql-exporter-user.sh.tpl | 25 ++++++ .../prometheus/exporter-configmap-bin.yaml | 27 +++++++ .../prometheus/exporter-deployment.yaml | 54 +++++++++++++ .../prometheus/exporter-job-create-user.yaml | 78 +++++++++++++++++++ .../prometheus/exporter-secrets-etc.yaml | 33 ++++++++ .../prometheus/exporter-service.yaml | 37 +++++++++ postgresql/values.yaml | 75 ++++++++++++++++++ 7 files changed, 329 insertions(+) create mode 100644 postgresql/templates/monitoring/prometheus/bin/_create-postgresql-exporter-user.sh.tpl create mode 100644 postgresql/templates/monitoring/prometheus/exporter-configmap-bin.yaml create mode 100644 postgresql/templates/monitoring/prometheus/exporter-deployment.yaml create mode 100644 postgresql/templates/monitoring/prometheus/exporter-job-create-user.yaml create mode 100644 postgresql/templates/monitoring/prometheus/exporter-secrets-etc.yaml create mode 100644 postgresql/templates/monitoring/prometheus/exporter-service.yaml diff --git a/postgresql/templates/monitoring/prometheus/bin/_create-postgresql-exporter-user.sh.tpl b/postgresql/templates/monitoring/prometheus/bin/_create-postgresql-exporter-user.sh.tpl new file mode 100644 index 0000000000..a797b34674 --- /dev/null +++ b/postgresql/templates/monitoring/prometheus/bin/_create-postgresql-exporter-user.sh.tpl @@ -0,0 +1,25 @@ +#!/bin/sh + +{{/* +Copyright 2017 The Openstack-Helm Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/}} + +set -ex + +psql "postgresql://${ADMIN_USER}:${ADMIN_PASSWORD}@${POSTGRESQL_HOST_PORT}?sslmode=disable" << EOF +CREATE USER ${EXPORTER_USER} WITH PASSWORD '${EXPORTER_PASSWORD}'; +ALTER USER ${EXPORTER_USER} SET SEARCH_PATH TO postgres_exporter,pg_catalog; +GRANT SELECT ON pg_stat_database TO ${EXPORTER_USER}; +EOF diff --git a/postgresql/templates/monitoring/prometheus/exporter-configmap-bin.yaml b/postgresql/templates/monitoring/prometheus/exporter-configmap-bin.yaml new file mode 100644 index 0000000000..90d4311a57 --- /dev/null +++ b/postgresql/templates/monitoring/prometheus/exporter-configmap-bin.yaml @@ -0,0 +1,27 @@ +{{/* +Copyright 2017 The Openstack-Helm Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/}} + +{{- if and .Values.manifests.monitoring.prometheus.configmap_bin .Values.monitoring.prometheus.enabled }} +{{- $envAll := . }} +--- +apiVersion: v1 +kind: ConfigMap +metadata: + name: postgresql-exporter-bin +data: + create-postgresql-exporter-user.sh: | +{{ tuple "bin/_create-postgresql-exporter-user.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }} +{{- end }} diff --git a/postgresql/templates/monitoring/prometheus/exporter-deployment.yaml b/postgresql/templates/monitoring/prometheus/exporter-deployment.yaml new file mode 100644 index 0000000000..c875a12cb6 --- /dev/null +++ b/postgresql/templates/monitoring/prometheus/exporter-deployment.yaml @@ -0,0 +1,54 @@ +{{/* +Copyright 2017 The Openstack-Helm Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/}} + +{{- if and .Values.manifests.monitoring.prometheus.deployment_exporter .Values.monitoring.prometheus.enabled }} +{{- $envAll := . }} +{{- $serviceAccountName := "prometheus-postgresql-exporter" }} +{{ tuple $envAll "prometheus_postgresql_exporter" $serviceAccountName | include "helm-toolkit.snippets.kubernetes_pod_rbac_serviceaccount" }} +--- +apiVersion: extensions/v1beta1 +kind: Deployment +metadata: + name: prometheus-postgresql-exporter +spec: + replicas: {{ .Values.pod.replicas.prometheus_postgresql_exporter }} +{{ tuple $envAll | include "helm-toolkit.snippets.kubernetes_upgrades_deployment" | indent 2 }} + template: + metadata: + labels: +{{ tuple $envAll "prometheus_postgresql_exporter" "exporter" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 8 }} + namespace: {{ .Values.endpoints.prometheus_postgresql_exporter.namespace }} + spec: + serviceAccountName: {{ $serviceAccountName }} + nodeSelector: + {{ .Values.labels.prometheus_postgresql_exporter.node_selector_key }}: {{ .Values.labels.prometheus_postgresql_exporter.node_selector_value }} + terminationGracePeriodSeconds: {{ .Values.pod.lifecycle.termination_grace_period.prometheus_postgresql_exporter.timeout | default "30" }} + initContainers: +{{ tuple $envAll "prometheus_postgresql_exporter" list | include "helm-toolkit.snippets.kubernetes_entrypoint_init_container" | indent 8 }} + containers: + - name: postgresql-exporter +{{ tuple $envAll "prometheus_postgresql_exporter" | include "helm-toolkit.snippets.image" | indent 10 }} +{{ tuple $envAll $envAll.Values.pod.resources.prometheus_postgresql_exporter | include "helm-toolkit.snippets.kubernetes_resources" | indent 10 }} + ports: + - name: metrics + containerPort: {{ tuple "prometheus_postgresql_exporter" "internal" "metrics" . | include "helm-toolkit.endpoints.endpoint_port_lookup" }} + env: + - name: DATA_SOURCE_NAME + valueFrom: + secretKeyRef: + name: {{ .Values.secrets.postgresql.exporter }} + key: DATA_SOURCE_NAME +{{- end }} diff --git a/postgresql/templates/monitoring/prometheus/exporter-job-create-user.yaml b/postgresql/templates/monitoring/prometheus/exporter-job-create-user.yaml new file mode 100644 index 0000000000..73fabca3a7 --- /dev/null +++ b/postgresql/templates/monitoring/prometheus/exporter-job-create-user.yaml @@ -0,0 +1,78 @@ +{{/* +Copyright 2017 The Openstack-Helm Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/}} + +{{- if and .Values.manifests.monitoring.prometheus.job_user_create .Values.monitoring.prometheus.enabled }} +{{- $envAll := . }} + +{{- $serviceAccountName := "prometheus-postgresql-exporter-create-user" }} +{{ tuple $envAll "prometheus_postgresql_exporter_create_user" $serviceAccountName | include "helm-toolkit.snippets.kubernetes_pod_rbac_serviceaccount" }} +--- +apiVersion: batch/v1 +kind: Job +metadata: + name: prometheus-postgresql-exporter-create-user +spec: + template: + metadata: + labels: +{{ tuple $envAll "prometheus_postgresql_exporter" "create_user" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 8 }} + spec: + serviceAccountName: {{ $serviceAccountName }} + restartPolicy: OnFailure + nodeSelector: + {{ .Values.labels.prometheus_postgresql_exporter.node_selector_key }}: {{ .Values.labels.prometheus_postgresql_exporter.node_selector_value }} + initContainers: +{{ tuple $envAll "prometheus_postgresql_exporter_create_user" list | include "helm-toolkit.snippets.kubernetes_entrypoint_init_container" | indent 8 }} + containers: + - name: prometheus-postgresql-exporter-create-user +{{ tuple $envAll "prometheus_postgresql_exporter_create_user" | include "helm-toolkit.snippets.image" | indent 10 }} +{{ tuple $envAll $envAll.Values.pod.resources.jobs.prometheus_postgresql_exporter_create_user | include "helm-toolkit.snippets.kubernetes_resources" | indent 10 }} + command: + - /tmp/create-postgresql-exporter-user.sh + env: + - name: EXPORTER_USER + valueFrom: + secretKeyRef: + name: {{ .Values.secrets.postgresql.exporter }} + key: EXPORTER_USER + - name: EXPORTER_PASSWORD + valueFrom: + secretKeyRef: + name: {{ .Values.secrets.postgresql.exporter }} + key: EXPORTER_PASSWORD + - name: ADMIN_USER + valueFrom: + secretKeyRef: + name: {{ .Values.secrets.postgresql.admin }} + key: POSTGRES_USER + - name: ADMIN_PASSWORD + valueFrom: + secretKeyRef: + name: {{ .Values.secrets.postgresql.admin }} + key: POSTGRES_PASSWORD + - name: POSTGRESQL_HOST_PORT + value: {{ tuple "postgresql" "internal" "postgresql" $envAll | include "helm-toolkit.endpoints.host_and_port_endpoint_uri_lookup" }} + volumeMounts: + - name: postgresql-exporter-bin + mountPath: /tmp/create-postgresql-exporter-user.sh + subPath: create-postgresql-exporter-user.sh + readOnly: true + volumes: + - name: postgresql-exporter-bin + configMap: + name: postgresql-exporter-bin + defaultMode: 0555 +{{- end }} diff --git a/postgresql/templates/monitoring/prometheus/exporter-secrets-etc.yaml b/postgresql/templates/monitoring/prometheus/exporter-secrets-etc.yaml new file mode 100644 index 0000000000..31f7d8cd1c --- /dev/null +++ b/postgresql/templates/monitoring/prometheus/exporter-secrets-etc.yaml @@ -0,0 +1,33 @@ +{{/* +Copyright 2017 The Openstack-Helm Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/}} + +{{- if and .Values.manifests.monitoring.prometheus.secret_etc .Values.monitoring.prometheus.enabled }} +{{- $envAll := . }} +{{- $exporter_user := .Values.endpoints.postgresql.auth.exporter.username }} +{{- $exporter_password := .Values.endpoints.postgresql.auth.exporter.password }} +{{- $db_host := tuple "postgresql" "internal" "postgresql" $envAll | include "helm-toolkit.endpoints.host_and_port_endpoint_uri_lookup" }} +{{- $data_source_name := printf "postgresql://%s:%s@%s/postgres?sslmode=disable" $exporter_user $exporter_password $db_host }} +--- +apiVersion: v1 +kind: Secret +metadata: + name: {{ .Values.secrets.postgresql.exporter }} +type: Opaque +data: + DATA_SOURCE_NAME: {{ $data_source_name | b64enc }} + EXPORTER_USER: {{ $exporter_user | b64enc }} + EXPORTER_PASSWORD: {{ $exporter_password | b64enc }} +{{- end }} diff --git a/postgresql/templates/monitoring/prometheus/exporter-service.yaml b/postgresql/templates/monitoring/prometheus/exporter-service.yaml new file mode 100644 index 0000000000..fc2c54b1d6 --- /dev/null +++ b/postgresql/templates/monitoring/prometheus/exporter-service.yaml @@ -0,0 +1,37 @@ +{{/* +Copyright 2017 The Openstack-Helm Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/}} + +{{- if and .Values.manifests.monitoring.prometheus.service_exporter .Values.monitoring.prometheus.enabled }} +{{- $envAll := . }} +{{- $prometheus_annotations := $envAll.Values.monitoring.prometheus.postgresql_exporter }} +--- +apiVersion: v1 +kind: Service +metadata: + name: {{ tuple "prometheus_postgresql_exporter" "internal" . | include "helm-toolkit.endpoints.hostname_short_endpoint_lookup" }} + labels: +{{ tuple $envAll "prometheus_postgresql_exporter" "metrics" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 4 }} + annotations: +{{- if .Values.monitoring.prometheus.enabled }} +{{ tuple $prometheus_annotations | include "helm-toolkit.snippets.prometheus_service_annotations" | indent 4 }} +{{- end }} +spec: + ports: + - name: metrics + port: {{ tuple "prometheus_postgresql_exporter" "internal" "metrics" . | include "helm-toolkit.endpoints.endpoint_port_lookup" }} + selector: +{{ tuple $envAll "prometheus_postgresql_exporter" "exporter" | include "helm-toolkit.snippets.kubernetes_metadata_labels" | indent 4 }} +{{- end }} diff --git a/postgresql/values.yaml b/postgresql/values.yaml index f059fcde6b..2a52b0571f 100644 --- a/postgresql/values.yaml +++ b/postgresql/values.yaml @@ -28,6 +28,18 @@ pod: replicas: #only 1 replica currently supported server: 1 + prometheus_postgresql_exporter: 1 + lifecycle: + upgrades: + deployments: + revision_history: 3 + pod_replacement_strategy: RollingUpdate + rolling_update: + max_unavailable: 1 + max_surge: 3 + termination_grace_period: + prometheus_postgresql_exporter: + timeout: 30 resources: enabled: false server: @@ -37,6 +49,13 @@ pod: limits: memory: "1024Mi" cpu: "2000m" + prometheus_postgresql_exporter: + limits: + memory: "1024Mi" + cpu: "2000m" + requests: + memory: "128Mi" + cpu: "500m" jobs: image_repo_sync: requests: @@ -45,6 +64,13 @@ pod: limits: memory: "1024Mi" cpu: "2000m" + prometheus_postgresql_exporter_create_user: + limits: + memory: "1024Mi" + cpu: "2000m" + requests: + memory: "128Mi" + cpu: "100m" # using dockerhub postgresql: https://hub.docker.com/r/library/postgres/tags/ images: @@ -52,6 +78,8 @@ images: postgresql: "docker.io/postgres:9.5" dep_check: quay.io/stackanetes/kubernetes-entrypoint:v0.3.1 image_repo_sync: docker.io/docker:17.07.0 + prometheus_postgresql_exporter: docker.io/wrouesnel/postgres_exporter:v0.4.6 + prometheus_postgresql_exporter_create_user: "docker.io/postgres:9.5" pull_policy: "IfNotPresent" local_registry: active: false @@ -75,6 +103,9 @@ labels: server: node_selector_key: openstack-control-plane node_selector_value: enabled + prometheus_postgresql_exporter: + node_selector_key: openstack-control-plane + node_selector_value: enabled dependencies: dynamic: @@ -92,6 +123,22 @@ dependencies: services: - endpoint: internal service: local_image_registry + prometheus_postgresql_exporter_create_user: + services: + - endpoint: internal + service: postgresql + prometheus_postgresql_exporter: + services: + - endpoint: internal + service: postgresql + jobs: + - prometheus-postgresql-exporter-create-user + +monitoring: + prometheus: + enabled: false + postgresql_exporter: + scrape: true conf: postgresql: @@ -101,6 +148,7 @@ conf: secrets: postgresql: admin: postgresql-admin + exporter: postgresql-exporter endpoints: cluster_domain_suffix: cluster.local @@ -121,6 +169,9 @@ endpoints: admin: username: postgres password: password + exporter: + username: psql_exporter + password: psql_exp_pass hosts: default: postgresql host_fqdn_override: @@ -130,6 +181,23 @@ endpoints: port: postgresql: default: 5432 + prometheus_postgresql_exporter: + namespace: null + auth: + user: + username: exporter + password: password + hosts: + default: postgresql-exporter + host_fqdn_override: + default: null + path: + default: /metrics + scheme: + default: 'http' + port: + metrics: + default: 9187 manifests: configmap_bin: true @@ -137,3 +205,10 @@ manifests: secret_admin: true service: true statefulset: true + monitoring: + prometheus: + configmap_bin: true + deployment_exporter: true + job_user_create: true + secret_etc: true + service_exporter: true