diff --git a/mariadb/Chart.yaml b/mariadb/Chart.yaml new file mode 100644 index 0000000..2f4b020 --- /dev/null +++ b/mariadb/Chart.yaml @@ -0,0 +1,8 @@ +apiVersion: v2 +name: mariadb +description: A Helm chart for MariaDB +type: application +annotations: + version-source: dockerhub-tags:library/mariadb +version: 0.1.0 +appVersion: "11.4.5" diff --git a/mariadb/templates/_helpers.tpl b/mariadb/templates/_helpers.tpl new file mode 100644 index 0000000..5a5172e --- /dev/null +++ b/mariadb/templates/_helpers.tpl @@ -0,0 +1,34 @@ +{{- define "mariadb.name" -}} +{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{- define "mariadb.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 }} + +{{- define "mariadb.chart" -}} +{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" }} +{{- end }} + +{{- define "mariadb.labels" -}} +helm.sh/chart: {{ include "mariadb.chart" . }} +{{ include "mariadb.selectorLabels" . }} +{{- if .Chart.AppVersion }} +app.kubernetes.io/version: {{ .Chart.AppVersion | quote }} +{{- end }} +app.kubernetes.io/managed-by: {{ .Release.Service }} +{{- end }} + +{{- define "mariadb.selectorLabels" -}} +app.kubernetes.io/name: {{ include "mariadb.name" . }} +app.kubernetes.io/instance: {{ .Release.Name }} +{{- end }} diff --git a/mariadb/templates/backup-cronjob.yaml b/mariadb/templates/backup-cronjob.yaml new file mode 100644 index 0000000..92e0d85 --- /dev/null +++ b/mariadb/templates/backup-cronjob.yaml @@ -0,0 +1,45 @@ +{{- if .Values.backup.enabled }} +apiVersion: batch/v1 +kind: CronJob +metadata: + name: {{ include "mariadb.fullname" . }}-backup + labels: + {{- include "mariadb.labels" . | nindent 4 }} +spec: + schedule: {{ .Values.backup.schedule | quote }} + jobTemplate: + spec: + template: + spec: + containers: + - name: mariadb-backup + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + command: + - /bin/sh + - -c + - | + mariadb-dump -h {{ include "mariadb.fullname" . }} -u root -p"$MARIADB_ROOT_PASSWORD" --all-databases \ + | gzip > /backup/mariadb-all-$(date +%Y%m%d-%H%M%S).sql.gz + find /backup -name "mariadb-all-*.sql.gz" -mtime +$RETENTION_DAYS -delete + env: + - name: MARIADB_ROOT_PASSWORD + valueFrom: + secretKeyRef: + name: {{ .Values.auth.existingSecret }} + key: {{ .Values.auth.rootPasswordKey }} + - name: RETENTION_DAYS + value: {{ .Values.backup.retentionDays | quote }} + volumeMounts: + - name: backup-storage + mountPath: /backup + volumes: + - name: backup-storage + hostPath: + path: {{ .Values.backup.hostPath }} + type: DirectoryOrCreate + {{- if .Values.backup.node }} + nodeSelector: + kubernetes.io/hostname: {{ .Values.backup.node }} + {{- end }} + restartPolicy: OnFailure +{{- end }} diff --git a/mariadb/templates/deployment.yaml b/mariadb/templates/deployment.yaml new file mode 100644 index 0000000..9f472a4 --- /dev/null +++ b/mariadb/templates/deployment.yaml @@ -0,0 +1,90 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{ include "mariadb.fullname" . }} + labels: + {{- include "mariadb.labels" . | nindent 4 }} + annotations: + version-source: dockerhub-tags:library/mariadb + kustomize.toolkit.fluxcd.io/prune: disabled +spec: + replicas: {{ .Values.replicaCount }} + strategy: + type: Recreate + selector: + matchLabels: + {{- include "mariadb.selectorLabels" . | nindent 6 }} + template: + metadata: + labels: + {{- include "mariadb.selectorLabels" . | nindent 8 }} + spec: + containers: + - name: mariadb + image: "{{ .Values.image.repository }}:{{ .Values.image.tag | default .Chart.AppVersion }}" + imagePullPolicy: {{ .Values.image.pullPolicy }} + ports: + - containerPort: {{ .Values.service.port }} + env: + - name: MARIADB_ROOT_PASSWORD + valueFrom: + secretKeyRef: + name: {{ .Values.auth.existingSecret }} + key: {{ .Values.auth.rootPasswordKey }} + - name: MARIADB_DATABASE + value: {{ .Values.auth.database | quote }} + {{- if .Values.auth.username }} + - name: MARIADB_USER + value: {{ .Values.auth.username | quote }} + - name: MARIADB_PASSWORD + valueFrom: + secretKeyRef: + name: {{ .Values.auth.existingSecret }} + key: {{ .Values.auth.passwordKey }} + {{- end }} + resources: + {{- toYaml .Values.resources | nindent 12 }} + volumeMounts: + - name: data + mountPath: /var/lib/mysql + readinessProbe: + exec: + command: + - mariadb-admin + - ping + - -h + - localhost + initialDelaySeconds: 15 + periodSeconds: 10 + livenessProbe: + exec: + command: + - mariadb-admin + - ping + - -h + - localhost + initialDelaySeconds: 30 + periodSeconds: 10 + volumes: + - name: data + {{- if .Values.persistence.existingClaim }} + persistentVolumeClaim: + claimName: {{ .Values.persistence.existingClaim }} + {{- else if .Values.persistence.enabled }} + persistentVolumeClaim: + claimName: {{ include "mariadb.fullname" . }}-data + {{- else }} + emptyDir: {} + {{- end }} + {{- 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/mariadb/templates/pvc.yaml b/mariadb/templates/pvc.yaml new file mode 100644 index 0000000..6d5eaae --- /dev/null +++ b/mariadb/templates/pvc.yaml @@ -0,0 +1,17 @@ +{{- if and .Values.persistence.enabled (not .Values.persistence.existingClaim) }} +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: {{ include "mariadb.fullname" . }}-data + labels: + {{- include "mariadb.labels" . | nindent 4 }} + annotations: + kustomize.toolkit.fluxcd.io/prune: disabled +spec: + accessModes: + - ReadWriteOnce + storageClassName: {{ .Values.persistence.storageClass }} + resources: + requests: + storage: {{ .Values.persistence.size }} +{{- end }} diff --git a/mariadb/templates/service.yaml b/mariadb/templates/service.yaml new file mode 100644 index 0000000..17c0eb4 --- /dev/null +++ b/mariadb/templates/service.yaml @@ -0,0 +1,17 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{ include "mariadb.fullname" . }} + labels: + {{- include "mariadb.labels" . | nindent 4 }} +spec: + type: {{ .Values.service.type }} + selector: + {{- include "mariadb.selectorLabels" . | nindent 4 }} + ports: + - port: {{ .Values.service.port }} + targetPort: {{ .Values.service.port }} + protocol: TCP + {{- if and (eq .Values.service.type "NodePort") .Values.service.nodePort }} + nodePort: {{ .Values.service.nodePort }} + {{- end }} diff --git a/mariadb/values.yaml b/mariadb/values.yaml new file mode 100644 index 0000000..27c414f --- /dev/null +++ b/mariadb/values.yaml @@ -0,0 +1,46 @@ +replicaCount: 1 + +image: + repository: mariadb + pullPolicy: IfNotPresent + tag: "" # defaults to Chart.AppVersion + +nameOverride: "" +fullnameOverride: "" + +auth: + database: mydatabase + username: "" + existingSecret: "" + rootPasswordKey: mariadb-root-password + passwordKey: mariadb-password + +persistence: + enabled: true + existingClaim: "" + storageClass: local-path + size: 5Gi + +service: + type: ClusterIP + port: 3306 + nodePort: "" + +backup: + enabled: false + schedule: "0 5 * * *" + retentionDays: 7 + hostPath: /storage/sdb/backups/mariadb + node: "" + +resources: + requests: + memory: 256Mi + cpu: 100m + limits: + memory: 1Gi + cpu: 500m + +nodeSelector: {} +affinity: {} +tolerations: []