From 9be152e66729ff0824ac693a716fa86c832b8fac Mon Sep 17 00:00:00 2001 From: Patrick Domnick Date: Tue, 4 Aug 2020 15:09:03 +0200 Subject: [PATCH] add helm template example --- helm/.helmignore | 26 ++++++++ helm/Chart.yaml | 17 ++++++ helm/README.md | 59 ++++++++++++++++++ helm/templates/_helpers.tpl | 98 ++++++++++++++++++++++++++++++ helm/templates/alert.yaml | 25 ++++++++ helm/templates/configmap.yaml | 13 ++++ helm/templates/deployment.yaml | 76 +++++++++++++++++++++++ helm/templates/secrets.yaml | 14 +++++ helm/templates/service.yaml | 20 ++++++ helm/templates/servicemonitor.yaml | 38 ++++++++++++ helm/values.yaml | 86 ++++++++++++++++++++++++++ 11 files changed, 472 insertions(+) create mode 100644 helm/.helmignore create mode 100644 helm/Chart.yaml create mode 100644 helm/README.md create mode 100644 helm/templates/_helpers.tpl create mode 100644 helm/templates/alert.yaml create mode 100644 helm/templates/configmap.yaml create mode 100644 helm/templates/deployment.yaml create mode 100644 helm/templates/secrets.yaml create mode 100644 helm/templates/service.yaml create mode 100644 helm/templates/servicemonitor.yaml create mode 100644 helm/values.yaml diff --git a/helm/.helmignore b/helm/.helmignore new file mode 100644 index 00000000..a1e771e3 --- /dev/null +++ b/helm/.helmignore @@ -0,0 +1,26 @@ +# Patterns to ignore when building packages. +# This supports shell glob matching, relative path matching, and +# negation (prefixed with !). Only one pattern per line. +.DS_Store +# Common VCS dirs +.git/ +.gitignore +.bzr/ +.bzrignore +.hg/ +.hgignore +.svn/ +# Common backup files +*.swp +*.bak +*.tmp +*.orig +*~ +# Various IDEs +.project +.idea/ +*.tmproj +.vscode/ +# Gitlab CI +.gitlab-ci.yml +examples/ \ No newline at end of file diff --git a/helm/Chart.yaml b/helm/Chart.yaml new file mode 100644 index 00000000..940f8eb0 --- /dev/null +++ b/helm/Chart.yaml @@ -0,0 +1,17 @@ +apiVersion: v2 +name: sql-exporter +description: A Helm chart for SQL Metrics +type: application +version: 1.0.0 +appVersion: 1.0.0 +keywords: + - exporter + - servicemonitor + - sql + - metrics +home: https://github.com/free/sql_exporter +sources: + - https://github.com/free/sql_exporter +maintainers: +- name: Patrick Domnick + email: patrickfdomnick@gmail.com diff --git a/helm/README.md b/helm/README.md new file mode 100644 index 00000000..d97e4e0a --- /dev/null +++ b/helm/README.md @@ -0,0 +1,59 @@ +# sql-exporter + +![Version: 1.0.0](https://img.shields.io/badge/Version-1.0.0-informational?style=flat-square) ![Type: application](https://img.shields.io/badge/Type-application-informational?style=flat-square) ![AppVersion: 1.0.0](https://img.shields.io/badge/AppVersion-1.0.0-informational?style=flat-square) + +A Helm chart for SQL Metrics + +**Homepage:** + +## Maintainers + +| Name | Email | Url | +| ---- | ------ | --- | +| Patrick Domnick | patrickfdomnick@gmail.com | | + +## Source Code + +* + +## Values + +| Key | Type | Default | Description | +|-----|------|---------|-------------| +| affinity | object | `{}` | | +| database.database | string | `""` | | +| database.host | string | `""` | | +| database.metrics | list | `[]` | | +| database.password | string | `""` | | +| database.port | string | `""` | | +| database.protocol | string | `"mysql"` | | +| database.user | string | `""` | | +| deployment.port | int | `9399` | | +| fullnameOverride | string | `""` | | +| image.pullPolicy | string | `"IfNotPresent"` | | +| image.repository | string | `"githubfree/sql_exporter"` | | +| image.tag | float | `0.5` | | +| imagePullSecrets | object | `{}` | | +| livenessProbe.failureThreshold | int | `6` | | +| livenessProbe.initialDelaySeconds | int | `30` | | +| livenessProbe.tcpSocket.port | string | `"http"` | | +| livenessProbe.timeoutSeconds | int | `5` | | +| nameOverride | string | `""` | | +| nodeSelector | object | `{}` | | +| podSecurityContext | object | `{}` | | +| readinessProbe.initialDelaySeconds | int | `5` | | +| readinessProbe.periodSeconds | int | `5` | | +| readinessProbe.tcpSocket.port | string | `"http"` | | +| readinessProbe.timeoutSeconds | int | `3` | | +| replicaCount | int | `1` | | +| resources.limits | object | `{}` | | +| securityContext | object | `{}` | | +| service.port | int | `9399` | | +| service.type | string | `"ClusterIP"` | | +| serviceMonitor.alerts.enabled | bool | `false` | | +| serviceMonitor.alerts.rules | list | `[]` | | +| serviceMonitor.enabled | bool | `false` | | +| serviceMonitor.namespace | string | `"monitoring"` | | +| serviceMonitor.path | string | `"/metrics"` | | +| serviceMonitor.selector | object | `{}` | | +| tolerations | list | `[]` | | diff --git a/helm/templates/_helpers.tpl b/helm/templates/_helpers.tpl new file mode 100644 index 00000000..79d2e838 --- /dev/null +++ b/helm/templates/_helpers.tpl @@ -0,0 +1,98 @@ +{{/* vim: set filetype=mustache: */}} +{{/* +Expand the name of the chart. +*/}} +{{- define "sql-exporter.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{/* +Create a default fully qualified app name. +We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec). +If release name contains chart name it will be used as a full name. +*/}} +{{- define "sql-exporter.fullname" -}} +{{- if .Values.fullnameOverride -}} +{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- $name := default .Chart.Name .Values.nameOverride -}} +{{- if contains $name .Release.Name -}} +{{- .Release.Name | trunc 63 | trimSuffix "-" -}} +{{- else -}} +{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}} +{{- end -}} +{{- end -}} +{{- end -}} + +{{/* +Create chart name and version as used by the chart label. +*/}} +{{- define "sql-exporter.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}} +{{- end -}} + +{{/* +Common labels +*/}} +{{- define "sql-exporter.labels" -}} +helm.sh/chart: {{ include "sql-exporter.chart" . }} +{{ include "sql-exporter.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end -}} + +{{/* +Selector labels +*/}} +{{- define "sql-exporter.selectorLabels" -}} +app.kubernetes.io/name: {{ include "sql-exporter.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end -}} + +{{/* +Create the sql_export.yml file +*/}} +{{- define "sql-exporter.expoter" -}} +{{- print "" -}} +{{- print "global:" | nindent 4 -}} +{{- print "scrape_timeout_offset: 500ms" | nindent 6 -}} +{{- print "min_interval: 0s" | nindent 6 -}} +{{- print "max_connections: 3" | nindent 6 -}} +{{- print "max_idle_connections: 3" | nindent 6 -}} +{{- print "target:" | nindent 4 -}} +{{- if eq .Values.database.protocol "mysql" -}} +{{- printf "data_source_name: %s://%s:%s@(%s:%s)/%s" .Values.database.protocol .Values.database.user .Values.database.password .Values.database.host .Values.database.port .Values.database.database | nindent 6 -}} +{{- end -}} +{{- if or (eq .Values.database.protocol "postgres") (eq .Values.database.protocol "sqlserver") -}} +{{- printf "data_source_name: %s://%s:%s@%s:%s/%s" .Values.database.protocol .Values.database.user .Values.database.password .Values.database.host .Values.database.port .Values.database.database | nindent 6 -}} +{{- end -}} +{{- if eq .Values.database.protocol "clickhouse" -}} +{{- printf "data_source_name: %s://%s:%s?username=%s&password=%s&dbname=%s" .Values.database.protocol .Values.database.host .Values.database.port .Values.database.user .Values.database.password .Values.database.database | nindent 6 -}} +{{- end -}} +{{- print "collectors: [sql_standard]" | nindent 6 -}} +{{- print "collector_files:" | nindent 4 -}} +{{- print "- /bin/config/standard.collector.yml" | nindent 6 -}} +{{- end -}} + +{{/* +Create the standard.collector.yml file +*/}} +{{- define "sql-exporter.collector" -}} +{{- print "" -}} +{{- print "collector_name: sql_standard" | nindent 4 -}} +{{- print "metrics: " | nindent 4 -}} +{{ .Values.database.metrics | toYaml | nindent 6}} +{{- end -}} + +{{/* +Create the name of the service account to use +*/}} +{{- define "sql-exporter.serviceAccountName" -}} +{{- if .Values.serviceAccount.create -}} + {{ default (include "sql-exporter.fullname" .) .Values.serviceAccount.name }} +{{- else -}} + {{ default "default" .Values.serviceAccount.name }} +{{- end -}} +{{- end -}} diff --git a/helm/templates/alert.yaml b/helm/templates/alert.yaml new file mode 100644 index 00000000..e4b8965f --- /dev/null +++ b/helm/templates/alert.yaml @@ -0,0 +1,25 @@ +{{- if .Values.serviceMonitor.enabled }} +{{- if .Values.serviceMonitor.alerts.enabled }} +apiVersion: monitoring.coreos.com/v1 +kind: PrometheusRule +metadata: + name: {{ template "sql-exporter.fullname" . }} + {{- if .Values.serviceMonitor.namespace }} + namespace: {{ .Values.serviceMonitor.namespace }} + {{- else }} + namespace: {{ .Release.Namespace }} + {{- end }} + labels: + app: {{ include "sql-exporter.fullname" . }} + chart: {{ template "sql-exporter.chart" . }} + release: {{ .Release.Name | quote }} + heritage: {{ .Release.Service | quote }} + {{- range $key, $value := .Values.serviceMonitor.selector }} + {{ $key }}: {{ $value | quote }} + {{- end }} +spec: + groups: + - name: {{ include "sql-exporter.fullname" . }} + rules: {{- toYaml .Values.serviceMonitor.alerts.rules | nindent 6 }} +{{- end }} +{{- end }} \ No newline at end of file diff --git a/helm/templates/configmap.yaml b/helm/templates/configmap.yaml new file mode 100644 index 00000000..a8ed9214 --- /dev/null +++ b/helm/templates/configmap.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: ConfigMap +metadata: + name: {{ include "sql-exporter.fullname" . }} + labels: + app: {{ include "sql-exporter.fullname" . }} + chart: {{ template "sql-exporter.chart" . }} + release: {{ .Release.Name }} + heritage: {{ .Release.Service }} + {{- include "sql-exporter.labels" . | nindent 4 }} +data: + standard.collector.yml: |- + {{- include "sql-exporter.collector" . }} diff --git a/helm/templates/deployment.yaml b/helm/templates/deployment.yaml new file mode 100644 index 00000000..1d3b26bf --- /dev/null +++ b/helm/templates/deployment.yaml @@ -0,0 +1,76 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "sql-exporter.fullname" . }} + labels: + app: {{ include "sql-exporter.fullname" . }} + chart: {{ template "sql-exporter.chart" . }} + release: {{ .Release.Name }} + heritage: {{ .Release.Service }} + {{- include "sql-exporter.labels" . | nindent 4 }} +spec: + replicas: {{ .Values.replicaCount }} + selector: + matchLabels: + {{- include "sql-exporter.selectorLabels" . | nindent 6 }} + template: + metadata: + annotations: + rollme: {{ randAlphaNum 5 | quote }} + labels: + app: {{ include "sql-exporter.fullname" . }} + chart: {{ template "sql-exporter.chart" . }} + release: {{ .Release.Name }} + {{- include "sql-exporter.selectorLabels" . | nindent 8 }} + spec: + {{- with .Values.imagePullSecrets }} + imagePullSecrets: + {{- toYaml . | nindent 8 }} + {{- end }} + securityContext: + {{- toYaml .Values.podSecurityContext | nindent 8 }} + volumes: + - name: sql-exporter + secret: + secretName: {{ include "sql-exporter.fullname" . }} + - name: sql-collector + configMap: + name: {{ include "sql-exporter.fullname" . }} + containers: + - name: {{ .Chart.Name }} + securityContext: + {{- toYaml .Values.securityContext | nindent 12 }} + image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + args: ["-config.file=/bin/target/sql_exporter.yml"] + volumeMounts: + - name: sql-exporter + readOnly: true + mountPath: "/bin/target" + - name: sql-collector + readOnly: true + mountPath: "/bin/config" + {{- if .Values.livenessProbe }} + livenessProbe: {{- toYaml .Values.livenessProbe | nindent 12 }} + {{- end }} + {{- if .Values.readinessProbe }} + readinessProbe: {{- toYaml .Values.readinessProbe | nindent 12 }} + {{- end }} + ports: + - name: http + containerPort: {{ .Values.deployment.port }} + protocol: TCP + resources: + {{- toYaml .Values.resources | nindent 12 }} + {{- with .Values.nodeSelector }} + nodeSelector: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.affinity }} + affinity: + {{- toYaml . | nindent 8 }} + {{- end }} + {{- with .Values.tolerations }} + tolerations: + {{- toYaml . | nindent 8 }} + {{- end }} diff --git a/helm/templates/secrets.yaml b/helm/templates/secrets.yaml new file mode 100644 index 00000000..0359ac94 --- /dev/null +++ b/helm/templates/secrets.yaml @@ -0,0 +1,14 @@ +apiVersion: v1 +kind: Secret +metadata: + name: {{ include "sql-exporter.fullname" . }} + labels: + app: {{ include "sql-exporter.fullname" . }} + chart: {{ template "sql-exporter.chart" . }} + release: {{ .Release.Name }} + heritage: {{ .Release.Service }} + {{- include "sql-exporter.labels" . | nindent 4 }} +type: Opaque +stringData: + sql_exporter.yml: |- + {{- include "sql-exporter.expoter" . }} \ No newline at end of file diff --git a/helm/templates/service.yaml b/helm/templates/service.yaml new file mode 100644 index 00000000..b651c744 --- /dev/null +++ b/helm/templates/service.yaml @@ -0,0 +1,20 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "sql-exporter.fullname" . }} + labels: + {{- include "sql-exporter.labels" . | nindent 4 }} + app: {{ include "sql-exporter.fullname" . }} + chart: {{ template "sql-exporter.chart" . }} + release: {{ .Release.Name }} + heritage: {{ .Release.Service }} +spec: + type: {{ .Values.service.type }} + ports: + - port: {{ .Values.service.port }} + targetPort: http + protocol: TCP + name: http + selector: + {{- include "sql-exporter.selectorLabels" . | nindent 4 }} + app: {{ include "sql-exporter.fullname" . }} diff --git a/helm/templates/servicemonitor.yaml b/helm/templates/servicemonitor.yaml new file mode 100644 index 00000000..f659c970 --- /dev/null +++ b/helm/templates/servicemonitor.yaml @@ -0,0 +1,38 @@ +{{- if .Values.serviceMonitor.enabled }} +apiVersion: monitoring.coreos.com/v1 +kind: ServiceMonitor +metadata: + name: {{ template "sql-exporter.fullname" . }} + {{- if .Values.serviceMonitor.namespace }} + namespace: {{ .Values.serviceMonitor.namespace }} + {{- else }} + namespace: {{ .Release.Namespace }} + {{- end }} + labels: + app: {{ include "sql-exporter.fullname" . }} + chart: {{ template "sql-exporter.chart" . }} + release: {{ .Release.Name | quote }} + heritage: {{ .Release.Service | quote }} + {{- range $key, $value := .Values.serviceMonitor.selector }} + {{ $key }}: {{ $value | quote }} + {{- end }} +spec: + selector: + matchLabels: + app: {{ include "sql-exporter.fullname" . }} + release: {{ .Release.Name | quote }} + endpoints: + - port: http + {{- if .Values.serviceMonitor.path }} + path: {{ .Values.serviceMonitor.path }} + {{- end }} + {{- if .Values.serviceMonitor.interval }} + interval: {{ .Values.serviceMonitor.interval }} + {{- end }} + {{- if .Values.serviceMonitor.scrapeTimeout }} + scrapeTimeout: {{ .Values.serviceMonitor.scrapeTimeout }} + {{- end }} + namespaceSelector: + matchNames: + - {{ .Release.Namespace }} +{{- end }} diff --git a/helm/values.yaml b/helm/values.yaml new file mode 100644 index 00000000..30c09a62 --- /dev/null +++ b/helm/values.yaml @@ -0,0 +1,86 @@ +replicaCount: 1 + +image: + repository: githubfree/sql_exporter + tag: 0.5 + pullPolicy: IfNotPresent + +imagePullSecrets: {} +nameOverride: "" +fullnameOverride: "" + +podSecurityContext: {} + +securityContext: {} + +## Containers' liveness and readiness probes +## ref: https://kubernetes.io/docs/concepts/workloads/pods/pod-lifecycle/#container-probes +## +livenessProbe: + tcpSocket: + port: http + initialDelaySeconds: 30 + timeoutSeconds: 5 + failureThreshold: 6 +readinessProbe: + tcpSocket: + port: http + initialDelaySeconds: 5 + timeoutSeconds: 3 + periodSeconds: 5 + +deployment: + port: 9399 + +service: + type: ClusterIP + port: 9399 + +resources: + limits: {} + # cpu: 50m + # memory: 64Mi + +nodeSelector: {} + +tolerations: [] + +affinity: {} + +database: + protocol: "mysql" + user: "" + password: "" + host: "" + port: "" + database: "" + metrics: [] + # - metric_name: mariadb_database_locks + # type: gauge + # help: 'Shows the locks of a database' + # values: [LOCKS] + # query: SELECT COUNT(1) AS LOCKS FROM INFORMATION_SCHEMA.PROCESSLIST WHERE Info IS NOT NULL AND State LIKE 'Waiting for table level lock' AND TIME > 60 + +# Enable this if you're using https://github.com/coreos/prometheus-operator +serviceMonitor: + enabled: false + ## The Path of where the metrics are exposed + path: /metrics + ## Specify a namespace if needed + namespace: monitoring + # fallback to the prometheus default unless specified + # interval: 10s + # scrapeTimeout: 10s + selector: {} + # namespace: "monitoring" + alerts: + enabled: false + rules: [] + # - alert: DatabaseLocks + # annotations: + # summary: Mysql/MariaDB Database Locks + # expr: mariadb_database_locks > + # 0 + # for: 1m + # labels: {} +