Skip to content

Commit aa3b197

Browse files
committed
First version of the 'getting started' page
Signed-off-by: Nikolai Rodionov <allanger@badhouseplants.net>
1 parent 40cdf9c commit aa3b197

9 files changed

Lines changed: 279 additions & 1 deletion

File tree

docs/assets/logo.png

184 KB
Loading

docs/database.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Database
2+
3+
WIP

docs/dbinstances.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# DbInstance
2+
3+
WIP

docs/dbuser.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# DbUser

docs/development/contribution.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# How to contribute
2+
3+
## Workflow
4+
5+
## How to become a maintainer

docs/development/helm-charts.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,3 @@ yq -i '.appVersion = "${GIT_SHA}"' ./charts/db-operator/Chart.yaml
2525
```
2626

2727
After that, CRDs will be up-to-date with the operator repo and you can test your changes.
28-
##

docs/index.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,159 @@
11
# Getting Started
22

3+
DB Operator is a Kubernetes operator for managing **MySQL** and **PostgreSQL** databases through **CRDs**.
34

5+
This operator doesn't launch database servers, instead it should be connected to the running ones, that's why it doesn't nececeraly require that the database is running in Kubernetes, and can be used with any server that can be accessed from inside the cluster.
6+
7+
After it is connected to a server, you can start managing databases and users through CRDs. When a user or a database is created, db-operator will add a `Secret` and a `ConfigMap` to the namespace where CR is deployed, they can be used by a pod to establish a connection with a database.
8+
9+
### Example
10+
11+
Let's imagine, you have an application that requires a connection to a Postgres DB, it receives credentials from the environment variable `POSTGRES_DATASOURCE`, and it needs to be in a following format: `postgresql://${USER}:${PASSWORD}@${HOSTNAME}:${PORT}/${DATABASE}?search_path=myapp`
12+
13+
We will talk about how to install the operator and connect it to a server later, now let's focus on the main logic. We need to create a `Database` resource:
14+
```yaml
15+
apiVersion: kinda.rocks/v1beta1
16+
kind: Database
17+
metadata:
18+
name: my-app
19+
namespace: my-namespace
20+
spec:
21+
backup:
22+
enable: false
23+
credentials:
24+
templates:
25+
# - This template will be used by the operator to add POSTGRES_DATASOURCE to the secret.
26+
- name: POSTGRES_DATASOURCE
27+
secret: true
28+
template: '{{ .Protocol }}://{{ .Username }}:{{ .Password }}@{{ .Hostname }}:{{ .Port }}/{{ .Database }}?search_path=my-app'
29+
deletionProtected: true
30+
instance: some-postgres
31+
postgres:
32+
dropPublicSchema: true
33+
schemas:
34+
- my-app
35+
secretName: my-app-db-creds
36+
```
37+
38+
[Read more about templates here](templates.md)
39+
40+
41+
> (@allanger): This logic might be changed when the `Database` resource will be upgrade to the version `v1`
42+
After the reconciliation you should be able to find a `ConfigMap` and a `Secret` in the namespace `my-namespace`. They both will be called `my-app-db-creds`, and you can use them to connect your application to the database. No manual interactions are needed, everything is managed within a Kubernetes cluster.
443

544
## Install db-operator
645

46+
We distribute DB Operator as a `helm` chart. You don't have to use it, but if you want to be able to get support, it's would be easier for us if you use the chart.
47+
48+
The charts is released as s simple help repo as well as a an OCI artifact.
49+
50+
To install the repo, run the following
51+
52+
```sh
53+
$ helm repo add db-operator https://db-operator.github.io/charts
54+
$ helm search repo db-operator
55+
```
56+
57+
OCI artifacts are available under `ghcr.io/db-operator/charts/`
58+
59+
To install the chart, run the following:
60+
61+
```sh
62+
$ helm install db-operator/db-operator
63+
# -- Or OCI
64+
$ helm install ghcr.io/db-operator/charts/db-operator:${CHART_VERSION}
65+
```
66+
67+
More info about the db-operator chart can be found in the [README.md](https://github.com/db-operator/charts/tree/main/charts/db-operator)
68+
769
## Create a DbInstance
870

71+
After the operator is installed, you need to connect it to a database. For this the `db-instances` chart can be used.
72+
73+
Create a following `values.yaml` file:
74+
```yaml
75+
# -- values.yaml
76+
dbinstances:
77+
instance1:
78+
engine: postgres
79+
monitoring:
80+
enabled: false
81+
secrets:
82+
adminUser: admin # Root username
83+
adminPassword: password # Root password
84+
generic:
85+
host: postgres.databases.svc.cluster.local # Host
86+
port: 5432
87+
```
88+
89+
And install a helm chart like this:
90+
91+
```sh
92+
$ helm install db-operator/db-instances -f ./values.yaml
93+
```
94+
95+
To check the DbInstance status, run:
96+
```sh
97+
kubectl get dbinstance instance1
98+
99+
NAME PHASE STATUS
100+
instance1 Running true
101+
```
102+
103+
If `.status.Status` is `true`, it means that you can create Databases on this instance.
104+
105+
You can read more about DbInstances [here](dbinstances.md)
106+
9107
## Create a Database
108+
109+
After the instancei is ready, you can start managing databases with the operator. Databases are not packaged in any helm chart, because they are supposed to be parts of the end applications, like an `Ingress` or a `PVC`, - something that your pod will need to run. Let's create a database:
110+
111+
```yaml
112+
# - db.yaml
113+
apiVersion: kinda.rocks/v1beta1
114+
kind: Database
115+
metadata:
116+
name: database-1
117+
spec:
118+
backup:
119+
enable: false
120+
deletionProtected: false
121+
instance: instance1
122+
secretName: database-1-creds
123+
```
124+
125+
```sh
126+
$ kubectl apply -f db.yaml
127+
$ kubectl get db
128+
NAME STATUS PROTECTED DBINSTANCE OPERATORVERSION AGE
129+
database-1 true false instance1 2.19.0 31s
130+
```
131+
132+
When `.status.Status` is `true`, it means that you can use your database.
133+
134+
You can read more about databases [here](database.md)
135+
136+
## F.A.Q.
137+
138+
### How to add ownerReferences to Secrets and ConfigMaps created by the operator?
139+
140+
DB Operator is designed in a way, that an application should be able to connect to a database, even if the operator was accidentally removed with all the CRDs. That's why by default Secrets and ConfigMaps are just created in the cluster without owner references. But if you would like these resources to be cleaned up after a databases is removed, or you need to see connection between them in ArgoCD, you can set the `.spec.cleanup` to `true`
141+
142+
If ArgoCD is used to manage Databases and the `cleanup` is set to `true`, please make sure that the `PrunePropagationPolicy` is not set to `foreground`, because db-operator is using secrets to understand which Database must be removed, and with the `foreground` policy the secret is removed before the Database, that makes it impossible for the operator to finish the reconciliation.
143+
144+
### How to connect the operator to an existing Database?
145+
146+
DB Operator is reading the secret using the `spec.secretName` entry. If this secret doesn't exist, operator will create it and read data out of it. But if a secret is found, it will try to get items that are required to connect to a database from there.
147+
148+
There are the keys, they a secret must contain:
149+
150+
```
151+
# for PosrgreSQL
152+
POSTGRES_DB: $DATABASE_NAME
153+
POSTGRES_PASSWORD: $PASSWORD
154+
POSTGRES_USER: $USERNAME
155+
# and for MySQL
156+
DB: $DATABASE_NAME
157+
PASSWORD: $PASSWORD
158+
USER: $USER
159+
```

docs/templates.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Templates
2+
3+
DB Operator is capable of producing custom templated entries in `Secrets` and `ConfigMaps` using the database data. This features is using **go templates** and can be used in a following way:
4+
5+
```yaml
6+
---
7+
# in Databases
8+
kind: Database
9+
spec:
10+
credentials:
11+
templates:
12+
- name: USER_AND_PASSWORD
13+
template: "{{ .Username }}-{{ .Password }}"
14+
secret: true
15+
---
16+
# or in DbUsers
17+
kind: DbUser
18+
spec:
19+
credentials:
20+
templates:
21+
- name: USER_AND_PASSWORD
22+
template: "{{ .Username }}-{{ .Password }}"
23+
secret: true
24+
```
25+
26+
## Available fields and functions
27+
28+
These fields are available for templating:
29+
30+
- **Protocol**: Depends on the db engine. Possible values are `mysql`/`postgresql`
31+
- **Hostname**: The same value as for db host in the connection configmap
32+
- **Port**: The same value as for db port in the connection configmap
33+
- **Database**: The same value as for db name in the creds secret
34+
- **Username**: The same value as for database user in the creds secret
35+
- **Password**: The same value as for password in the creds secret
36+
37+
So to create a full connection string, you could use a template like that:
38+
39+
```
40+
"{{ .Protocol }}://{{ .Username }}:{{ .Password }}@{{ .Hostname }}:{{ .Port }}/{{ .Database }}"
41+
```
42+
43+
It's also possible to use one of these functions to get data directly from data sources available to the operator:
44+
45+
- **Secret**: Query data from the Secret
46+
- **ConfigMap**: Query data from the ConfigMap
47+
- **Query**: Get data directly from the database
48+
49+
When using `Secret` and `ConfigMap` you can query the previously created secret to template a new one, e.g.:
50+
51+
```yaml
52+
spec:
53+
credentials:
54+
templates:
55+
- name: TMPL_1
56+
template: "test"
57+
secret: false
58+
- name: TMPL_2
59+
template: "{{ .ConfigMap \"TMPL_1\"}}"
60+
secret: true
61+
- name: TMPL_3
62+
template: "{{ .Secret \"TMPL_2\"}}"
63+
secret: false
64+
```
65+
66+
When using `Query` you need to make sure that you query returns only one value. For example:
67+
68+
```yaml
69+
...
70+
templates:
71+
- name: POSTGRES_VERSION
72+
secret: false
73+
template: "{{ .Query \"SHOW server_version;\" }}"
74+
```
75+
76+
For values that are shared between different databases on the same instance, it's possible to set instance variables. Once they are set on the instance, they can also be used in database/dbuser templates.
77+
78+
79+
```yaml
80+
kind: DbInstance
81+
spec:
82+
instanceVars:
83+
TEST_KEY: TEST_VALUE
84+
```
85+
86+
These variables can be also accessed by the Database/DbUser templates
87+
88+
```yaml
89+
...
90+
templates:
91+
- name: DB_INSTANCE_VAR
92+
secret: false
93+
template: "{{ .InstanceVar 'TEST_KEY' }}"
94+
...
95+
```
96+
97+
Then the secret that is created by the operator should contain the following entry: `DB_INSTANCE_VAR: TEST_VALUE`. When the value is changed on the instance level, it should also trigger reconciliation of the databases and hence the values should also be updated in the target secrets.

mkdocs.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,24 @@
11
site_name: DB Operator
22
theme:
3+
logo: assets/logo.png
4+
favicon: assets/logo.png
35
name: material
46
language: en
7+
features:
8+
- navigation.tabs
9+
10+
palette:
11+
12+
# Palette toggle for light mode
13+
- media: "(prefers-color-scheme: light)"
14+
scheme: default
15+
toggle:
16+
icon: material/brightness-7
17+
name: Switch to dark mode
18+
19+
# Palette toggle for dark mode
20+
- media: "(prefers-color-scheme: dark)"
21+
scheme: slate
22+
toggle:
23+
icon: material/brightness-4
24+
name: Switch to light mode

0 commit comments

Comments
 (0)