|
1 | 1 | # DbInstance |
2 | 2 |
|
3 | | -WIP |
| 3 | +Currently, DbInstances are more or less a meta resource that connects the operator to a database server. |
| 4 | + |
| 5 | +> There are actually two types of databases: generic and gsql, and the gsql one is supposed to bootstrap an sql instance in GCP, but they are going to be deprecated soon, and hence I don't feel like writing docs for them. The only important thing about them is that you **shouldn't use them** |
| 6 | +
|
| 7 | +Here we only talk about the generic instances |
| 8 | + |
| 9 | +## How to configure a DbInstance |
| 10 | + |
| 11 | +You need to have a **PostgreSQL** or a **MySQL** server running, and it has to be accessible by the operator. You also need a user with sufficient permissions, if it's fine in your environment, I would suggest to use an admin user. |
| 12 | + |
| 13 | +Now let's get started: |
| 14 | +```yaml |
| 15 | +apiVersion: kinda.rocks/v1beta1 |
| 16 | +kind: DbInstance |
| 17 | +metadata: |
| 18 | + name: cloudnative-pg |
| 19 | +spec: |
| 20 | + adminSecretRef: |
| 21 | + Name: cnpg17-admin-creds |
| 22 | + Namespace: databases |
| 23 | + # -- Legacy settings, please set it like this |
| 24 | + # -- it's going to be removed in the next api version |
| 25 | + backup: |
| 26 | + bucket: "" |
| 27 | + engine: postgres |
| 28 | + generic: |
| 29 | + hostFrom: |
| 30 | + key: host |
| 31 | + kind: ConfigMap |
| 32 | + name: cloudnative-pg-config |
| 33 | + namespace: databases |
| 34 | + portFrom: |
| 35 | + key: port |
| 36 | + kind: ConfigMap |
| 37 | + name: cloudnative-pg-config |
| 38 | + namespace: databases |
| 39 | + monitoring: |
| 40 | + enabled: false |
| 41 | +``` |
| 42 | +
|
| 43 | +Let's quickly go through the yaml |
| 44 | +
|
| 45 | +With `.adminSecretRef` you are pointing the operator to a secret, where the admin credentials are stored. |
| 46 | +They must be stored in a following format |
| 47 | + |
| 48 | +```yaml |
| 49 | +kind: Secret |
| 50 | +data: |
| 51 | + # -- user might be omitted, then the following values will be used |
| 52 | + # -- for PostgreSQL - postgres |
| 53 | + # -- for MySQL - root |
| 54 | + user: < base64 encoded admin username > |
| 55 | + password: < base64 encoded admin password > |
| 56 | +``` |
| 57 | + |
| 58 | +With `.engine` you let the operator know, if it should treat a server as a PostgreSQL or a MySQL one. Possible values are `postgres` and `mysql` |
| 59 | + |
| 60 | +Then you need to configure a URL and a port that the operator should try to connect to, there are two options to do that, you can set them directly in the manifest: |
| 61 | + |
| 62 | +```yaml |
| 63 | +spec: |
| 64 | + generic: |
| 65 | + host: ${HOST} |
| 66 | + port: ${PORT} |
| 67 | +``` |
| 68 | + |
| 69 | +Or you can read them from a ConfigMap or a Secret: |
| 70 | + |
| 71 | +```yaml |
| 72 | +spec: |
| 73 | + generic |
| 74 | + hostFrom: |
| 75 | + key: host |
| 76 | + kind: ConfigMap |
| 77 | + name: cloudnative-pg-config |
| 78 | + namespace: databases |
| 79 | + portFrom: |
| 80 | + key: port |
| 81 | + kind: ConfigMap |
| 82 | + name: cloudnative-pg-config |
| 83 | + namespace: databases |
| 84 | +``` |
| 85 | + |
| 86 | +## Additional configurations |
| 87 | + |
| 88 | +### Extra Grants |
| 89 | + |
| 90 | +To use the `.extraGrants` feature of `Databases`, you need to enabled it on the instance level. To do so, set the `.spec.allowExtraGrants` to `true` |
| 91 | + |
| 92 | +### Allowed Privileges |
| 93 | + |
| 94 | +To use the `.extraPrivileges` feature of `DbUsers`, you also need to enabled the privileges on the instance level. Extra privileges is a list of roles that can be granteed to `DbUsers`. For example: |
| 95 | + |
| 96 | +```yaml |
| 97 | +spec: |
| 98 | + generic: |
| 99 | + allowedPriveleges: |
| 100 | + - readOnlyAdmin |
| 101 | + - rds-iam |
| 102 | +``` |
| 103 | + |
| 104 | +Then you will be able to assigned these roles to DbUsers. The roles are not managed by the operator, they must be already on a server when a user is created. |
| 105 | + |
| 106 | +### Instance Vars |
| 107 | + |
| 108 | +It may happen that you need to share the same variable in the Database/DbUser [templates](templates.md). Let's say you have a **RW** and **RO** urls, and your application need two env variables to connect: one - for actibely writing, and another - only for reading. |
| 109 | + |
| 110 | +The **RW** URL will be available anyway, but how to set a **RO** one. |
| 111 | + |
| 112 | +Without the instance vars you could just create a template with a hardcoded string in it: |
| 113 | + |
| 114 | +```yaml |
| 115 | +templates: |
| 116 | + - name: PG_READONLYHOST |
| 117 | + secret: false |
| 118 | + template: "my-read-only-postgres-url.test" |
| 119 | +``` |
| 120 | + |
| 121 | +Or you can use the instance variables. |
| 122 | + |
| 123 | +```yaml |
| 124 | +kind: DbInstance |
| 125 | +spec: |
| 126 | + instanceVars: |
| 127 | + PG_READONLYHOST: my-read-only-postgres-url.test |
| 128 | +``` |
| 129 | + |
| 130 | +And then later use it in a template like that: |
| 131 | +```yaml |
| 132 | +templates: |
| 133 | + - name: PG_READONLYHOST |
| 134 | + secret: false |
| 135 | + template: '{{ .instanceVar "PG_READONLYHOST" }}' |
| 136 | +``` |
| 137 | + |
| 138 | +If a value of the variable is changed on the instance, it will be also syned for each Database and User. |
0 commit comments