Skip to content

Commit 924171e

Browse files
committed
Add a cluster chart and documentation of how to use it
1 parent 959692b commit 924171e

File tree

9 files changed

+266
-6
lines changed

9 files changed

+266
-6
lines changed

.drone.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,12 @@ pipeline:
109109
- helm package mysql-operator
110110
- CHART="$(basename *.tgz)" ; MESSAGE="Publish $(basename $CHART .tgz)"
111111
- /usr/local/bin/gh put --skip-existing -m "$MESSAGE" "$CHART" "presslabs/charts/docs/"
112+
- rm *.tgz
113+
# publish cluster chart
114+
- (cd mysql-cluster && helm dep build)
115+
- helm package mysql-cluster
116+
- CHART="$(basename *.tgz)" ; MESSAGE="Publish $(basename $CHART .tgz)"
117+
- /usr/local/bin/gh put --skip-existing -m "$MESSAGE" "$CHART" "presslabs/charts/docs/"
112118
secrets:
113119
- GH_PASSWORD
114120
when:

docs/integrate-operator.md

Lines changed: 78 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Integration of the MySQL Operator
2+
title: Integrating mysql clusters into your own helm charts
33
linktitle: MySQL Operator Integration
44
description: How to integrate the MySQL operator with your application.
55
categories: [mysql operator]
@@ -13,14 +13,86 @@ aliases: []
1313
toc: true
1414
---
1515

16-
After cluster creation, you can update the provided secret with a new field named `DB_CONNECT_URL` that contains a [DSN](https://en.wikipedia.org/wiki/Data_source_name) to connect to the writable cluster endpoint. You can check the `_helper.tpl` file for more insights.
16+
After cluster creation, you can update the provided secret with a new field named `DB_CONNECT_URL`
17+
that contains a [DSN](https://en.wikipedia.org/wiki/Data_source_name) to connect to the writable
18+
cluster endpoint. You can check the `_helper.tpl` file for more insights.
1719

1820
The MySQL operator provides 3 services to access the nodes:
1921

20-
* `<cluster_name>-mysql-master` is the service that points to the master node and this endpoint should be used for writes. This service is usually used to construct the DSN.
22+
* `<cluster_name>-mysql-master` is the service that points to the master node and this endpoint
23+
should be used for writes. This service is usually used to construct the DSN.
2124

22-
* `<cluster_name>-mysql` is the service that routes traffic to all the _healthy_ nodes from the cluster. You should use this endpoint for reads.
25+
* `<cluster_name>-mysql` is the service that routes traffic to all the _healthy_ nodes from the
26+
cluster. You should use this endpoint for reads.
2327

24-
* `<cluster_name>-mysql-nodes` is the service used internally to access nodes. You can use this service to access a specific node (e.g. `<cluster_name>-mysql-0.<cluster-name>-mysql-nodes.default`)
28+
* `<cluster_name>-mysql-nodes` is the service used internally to access nodes. You can use this
29+
service to access a specific node (e.g.
30+
`<cluster_name>-mysql-0.<cluster-name>-mysql-nodes.default`)
2531

26-
We use helm to deploy our application into Kubernetes, so we updated our charts to use the MySQL operator to provide one cluster per application.
32+
We use helm to deploy our application into Kubernetes, so we updated our charts to use the MySQL
33+
operator to provide one cluster per application.
34+
35+
36+
## Using Helm
37+
38+
Usually a cluster of MySQL is needed alongside with an application, that's why we provide a Helm
39+
chart for easy deployment of a MySQL cluster. The chart can be found in `presslabs` chart repository and
40+
installed like the operator. Below is illustrated how this chart can be integrated with your
41+
application to provision a MySQL cluster.
42+
43+
44+
### Add MySQL Cluster chart as dependency
45+
In your chart add in `requirements.yaml` under `dependencies` section the following:
46+
```yaml
47+
dependencies:
48+
- name: mysql-cluster
49+
version: 0.1.0
50+
repository: https://presslabs.github.io/charts
51+
condition: mysql.enabled
52+
alias: mysql
53+
54+
```
55+
56+
Once dependencies are configured run `helm dependency update` to fetch related charts.
57+
58+
More information about chart requirements can be found in the official
59+
[documentation](https://docs.helm.sh/developing_charts/#managing-dependencies-with-requirements-yaml).
60+
61+
### Configure your chart's values.yaml file
62+
You can configure the cluster by providing values under the `mysql` key. A comprehensive description
63+
can be found in the chart
64+
[`values.yaml`](https://github.com/presslabs/mysql-operator/blob/master/hack/charts/mysql-cluster/values.yaml)
65+
file.
66+
67+
```yaml
68+
mysql:
69+
enabled: true
70+
rootPassword: <secure>
71+
appUser: <user name>
72+
appPassword: <user password>
73+
appDatabase: <app database>
74+
```
75+
76+
### Use into your application
77+
In your deployment add an environment variable that point to the `DB_CONNECT_URL` field from cluster
78+
secret named `{{ include "mysql-cluster.secretName" . }}`.
79+
80+
For example in the `deployment.yaml`:
81+
```yaml
82+
spec:
83+
replicas: {{ .Values.replicaCount }}
84+
template:
85+
spec:
86+
containers:
87+
- name: {{ .Chart.Name }}
88+
...
89+
env:
90+
- name: DB_CONNECT_URL
91+
valueFrom:
92+
secretKeyRef:
93+
name: {{ include "mysql-cluster.secretName" . }}
94+
key: DB_CONNECT_URL
95+
```
96+
97+
Now just modify your app to connect to the DSN that is provided into `DB_CONNECT_URL` environment
98+
variable.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Patterns to ignore when building packages.
2+
# This supports shell glob matching, relative path matching, and
3+
# negation (prefixed with !). Only one pattern per line.
4+
.DS_Store
5+
# Common VCS dirs
6+
.git/
7+
.gitignore
8+
.bzr/
9+
.bzrignore
10+
.hg/
11+
.hgignore
12+
.svn/
13+
# Common backup files
14+
*.swp
15+
*.bak
16+
*.tmp
17+
*~
18+
# Various IDEs
19+
.project
20+
.idea/
21+
*.tmproj
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
apiVersion: v1
2+
appVersion: "1.0"
3+
description: A Helm chart for easy deployment of a MySQL cluster with MySQL operator.
4+
name: mysql-cluster
5+
version: 0.1.0
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{{/* vim: set filetype=mustache: */}}
2+
{{/*
3+
Expand the name of the chart.
4+
*/}}
5+
{{- define "mysql-cluster.name" -}}
6+
{{- default .Chart.Name .Values.nameOverride | trunc 63 | trimSuffix "-" -}}
7+
{{- end -}}
8+
9+
{{/*
10+
Create a default fully qualified app name.
11+
We truncate at 63 chars because some Kubernetes name fields are limited to this (by the DNS naming spec).
12+
If release name contains chart name it will be used as a full name.
13+
*/}}
14+
{{- define "mysql-cluster.fullname" -}}
15+
{{- if .Values.fullnameOverride -}}
16+
{{- .Values.fullnameOverride | trunc 63 | trimSuffix "-" -}}
17+
{{- else -}}
18+
{{- $name := default .Chart.Name .Values.nameOverride -}}
19+
{{- if contains $name .Release.Name -}}
20+
{{- .Release.Name | trunc 63 | trimSuffix "-" -}}
21+
{{- else -}}
22+
{{- printf "%s-%s" .Release.Name $name | trunc 63 | trimSuffix "-" -}}
23+
{{- end -}}
24+
{{- end -}}
25+
{{- end -}}
26+
27+
{{/*
28+
Create chart name and version as used by the chart label.
29+
*/}}
30+
{{- define "mysql-cluster.chart" -}}
31+
{{- printf "%s-%s" .Chart.Name .Chart.Version | replace "+" "_" | trunc 63 | trimSuffix "-" -}}
32+
{{- end -}}
33+
34+
{{- define "mysql-cluster.dbConnectURL" -}}
35+
mysql://{{- urlquery .Values.appUser -}}:{{- urlquery .Values.appPassword -}}@
36+
{{- include "mysql-cluster.clusterName" . -}}-mysql-master:3306/{{- .Values.appDatabase -}}
37+
{{- end -}}
38+
39+
{{- define "mysql-cluster.clusterName" -}}
40+
{{- printf "%s-db" (include "mysql-cluster.fullname" .) | trunc 63 | trimSuffix "-" -}}
41+
{{- end -}}
42+
43+
{{- define "mysql-cluster.secretName" -}}
44+
{{- printf "%s-db" (include "mysql-cluster.fullname" .) | trunc 63 | trimSuffix "-" -}}
45+
{{- end -}}
46+
47+
{{- define "mysql-cluster.backupSecretName" -}}
48+
{{- printf "%s-db-backup" (include "mysql-cluster.fullname" .) | trunc 63 | trimSuffix "-" -}}
49+
{{- end -}}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{{- if .Values.backupCredentials }}
2+
apiVersion: v1
3+
kind: Secret
4+
metadata:
5+
name: {{ template "mysql-cluster.backupSecretName" . }}
6+
labels:
7+
app: {{ template "mysql-cluster.name" . }}
8+
chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
9+
release: {{ .Release.Name }}
10+
heritage: {{ .Release.Service }}
11+
type: Opaque
12+
data:
13+
{{- range $key, $value := .Values.backupCredentials }}
14+
{{ $key | upper }}: {{ $value | b64enc | quote }}
15+
{{ end }}
16+
{{- end -}}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
apiVersion: mysql.presslabs.org/v1alpha1
2+
kind: MysqlCluster
3+
metadata:
4+
name: {{ include "mysql-cluster.clusterName" . }}
5+
labels:
6+
app: {{ template "mysql-cluster.name" . }}
7+
chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
8+
release: {{ .Release.Name }}
9+
heritage: {{ .Release.Service }}
10+
spec:
11+
replicas: {{ .Values.replicas }}
12+
secretName: {{ include "mysql-cluster.secretName" . }}
13+
14+
{{- if .Values.backupSecretName }}
15+
backupSecretName: {{ .Values.backupSecretName }}
16+
{{- else if .Values.backupCredentials }}
17+
backupSecretName: {{ include "mysql-cluster.backupSecretName" . }}
18+
{{- else if .Values.backupSchedule }}
19+
{{ required "One of .mysql.backupBucketSecretName and .mysql.backupCredentails should be specified" "" }}
20+
{{- end }}
21+
22+
{{- if .Values.backupSchedule }}
23+
backupSchedule: {{ .Values.backupSchedule }}
24+
backupURL: {{ required ".mysql.backupURL is missing" .Values.backupURL }}
25+
{{- end }}
26+
27+
{{- if .Values.mysqlConfig }}
28+
mysqlConf:
29+
{{ toYaml .Values.mysqlConf | nindent 4 }}
30+
{{- end }}
31+
32+
{{- if .Values.podSpec }}
33+
podSpec:
34+
{{ toYaml .Values.podSpec | nindent 4 }}
35+
{{- end }}
36+
37+
{{- if .Values.volumeSpec }}
38+
volumeSpec:
39+
{{ toYaml .Values.volumeSpec | nindent 4 }}
40+
{{- end }}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: v1
2+
kind: Secret
3+
metadata:
4+
name: {{ include "mysql-cluster.secretName" . }}
5+
labels:
6+
app: {{ template "mysql-cluster.name" . }}
7+
chart: {{ .Chart.Name }}-{{ .Chart.Version | replace "+" "_" }}
8+
release: {{ .Release.Name }}
9+
heritage: {{ .Release.Service }}
10+
type: Opaque
11+
data:
12+
ROOT_PASSWORD: {{ required ".rootPassword is missing" .Values.rootPassword | b64enc | quote }}
13+
USER: {{ required ".appUser is missing" .Values.appUser | b64enc | quote }}
14+
PASSWORD: {{ required ".appPassword is missing" .Values.appPassword | b64enc | quote }}
15+
DATABASE: {{ required ".appDatabase is missing" .Values.appDatabase | b64enc | quote }}
16+
DB_CONNECT_URL: {{ include "mysql-cluster.dbConnectURL" . | b64enc | quote }}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Default values for mysql-cluster.
2+
# This is a YAML-formatted file.
3+
# Declare variables to be passed into your templates.
4+
5+
## The cluster number of nodes
6+
replicas: 1
7+
8+
## MySQL connect credentials, thoses credentials will be provisioned in the cluster
9+
# rootPassword:
10+
# appUser:
11+
# appPassword:
12+
# appDatabase:
13+
14+
podSpec:
15+
mysqlConf:
16+
volumeSpec:
17+
18+
backupSchedule:
19+
backupURL:
20+
backupSecretName:
21+
backupCredentials:
22+
# AWS_ACCESS_KEY_ID: ?
23+
# AWS_SECRET_KEY: ?
24+
# AWS_REGION: us-east-1
25+
# AWS_ACL: ?
26+
27+
# GCS_SERVICE_ACCOUNT_JSON_KEY: ?
28+
# GCS_PROJECT_ID: ?
29+
# GCS_OBJECT_ACL: ?
30+
# GCS_BUCKET_ACL: ?
31+
# GCS_LOCATION: ?
32+
# GCS_STORAGE_CLASS: MULTI_REGIONAL
33+
34+
# HTTP_URL: ?
35+

0 commit comments

Comments
 (0)