Skip to content

Commit 2f89711

Browse files
Add Documentation for Custom Cluster Configuration
This commit documents the method for updating the configuration for a PostgreSQL cluster created by the Operator.
1 parent 3f862ac commit 2f89711

File tree

1 file changed

+225
-16
lines changed

1 file changed

+225
-16
lines changed

docs/content/advanced/custom-config.md

Lines changed: 225 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ draft: false
55
weight: 4
66
---
77

8-
## Custom Postgres Configurations
8+
## Custom PostgreSQL Configuration
99

1010
Users and administrators can specify a
11-
custom set of Postgres configuration files be used when creating
12-
a new Postgres cluster. The configuration files you can
11+
custom set of PostgreSQL configuration files to be used when creating
12+
a new PostgreSQL cluster. The configuration files you can
1313
change include -
1414

15-
* postgresql.conf
16-
* pg_hba.conf
15+
* postgres-ha.yaml
1716
* setup.sql
1817

1918
Different configurations for PostgreSQL might be defined for
@@ -45,16 +44,18 @@ that will include the PostgreSQL configuration files within that directory.
4544

4645
#### Config Files Purpose
4746

48-
The *postgresql.conf* file is the main Postgresql configuration file that allows
49-
the definition of a wide variety of tuning parameters and features.
47+
The *postgres-ha.yaml* file is the main configuration file that allows for the
48+
configuration of a wide variety of tuning parameters for you PostgreSQL cluster.
49+
This includes various PostgreSQL settings, e.g. those that should be applied to
50+
files such as `postgresql.conf`, `pg_hba.conf` and `pg_ident.conf`, as well as
51+
tuning paramters for the High Availability features inlcuded in each cluster.
52+
The various configuration settings available can be
53+
[found here](https://access.crunchydata.com/documentation/patroni/latest/settings/index.html#settings)
5054

51-
The *pg_hba.conf* file is the way Postgresql secures client access.
52-
53-
The *setup.sql* file is a Crunchy Container Suite configuration
54-
file used to initially populate the database after the initial *initdb*
55-
is run when the database is first created. Changes would be made
56-
to this if you wanted to define which database objects are created by
57-
default.
55+
The *setup.sql* file is a SQL file that is executed following the initialization
56+
of a new PostgreSQL cluster, specifically after *initdb* is run when the database
57+
is first created. Changes would be made to this if you wanted to define which
58+
database objects are created by default.
5859

5960
#### Granular Config Maps
6061

@@ -69,10 +70,218 @@ clusters.
6970

7071
#### Defaults
7172

72-
If there is no reason to change the default PostgreSQL configuration
73-
files that ship with the Crunchy Postgres container, there is no
73+
If there is no reason to change the default PostgreSQL configuration
74+
files that ship with the Crunchy Postgres container, there is no
7475
requirement to. In this event, continue using the Operator as usual
7576
and avoid defining a global configMap.
7677

7778

79+
## Modifying PostgreSQL Cluster Configuration
80+
81+
Once a PostgreSQL cluster has been initialized, its configuration settings
82+
can be updated and modified as needed. This done by modifying the
83+
`<clusterName>-pgha-config` ConfigMap that is created for each individual
84+
PostgreSQL cluster.
85+
86+
The `<clusterName>-pgha-config` ConfigMap is populated following cluster
87+
initializtion, specifically using the baseline configuration settings used to
88+
bootstrap the cluster. Therefore, any customiztions applied using a custom
89+
`postgres-ha.yaml` file as described in the **Custom PostgreSQL Configuration**
90+
section above will also be included when the ConfigMap is populated.
91+
92+
The various configuration settings available for modifying and updating
93+
and cluster's configuration can be
94+
[found here](https://access.crunchydata.com/documentation/patroni/latest/settings/index.html#settings).
95+
Please proceed with caution when modiying configuration, especially those settings
96+
applied by default by Operator. Certain settings are required for normal operation
97+
of the Operator and the PostgreSQL clusters it creates, and altering these
98+
settings could result in expected behavior.
99+
100+
### Types of Configuration
101+
102+
Within the `<clusterName>-pgha-config` ConfigMap are two forms of configuration:
103+
104+
- **Distributed Configuration Store (DCS):** Cluster-wide
105+
configuration settings that are applied to all database servers in the PostgreSQL
106+
cluster
107+
- **Local Database:** Configuration settings that are applied
108+
individually to each database server (i.e. the primary and each replica) within
109+
the cluster.
110+
111+
The DCS configuration settings are stored within the `<clusterName>-pgha-config`
112+
ConfigMap in a configuration named `<clusterName>-dcs-config`, while the local
113+
database configurations are stored in one or more configurations named
114+
`<severName>-dcs-config` (with one local configuration for the primary and each
115+
replica within the cluster). Please note that
116+
[as described here](https://access.crunchydata.com/documentation/patroni/latest/dynamic_configuration/),
117+
certain settings can only be applied via the DCS to ensure they are uniform among
118+
the primary and all replicas within the cluster.
119+
120+
The following is an example of the both the DCS and primary configuration settings
121+
as stored in the `<clusterName>-pgha-config` ConfigMap for a cluster named `mycluster`.
122+
Please note the `mycluster-dcs-config` configuration defining the DCS configuration
123+
for `mycluster`, along with the `mycluster-local-config` configuration defining the
124+
local configuration for the database server named `mycluster`, which is the current
125+
primary within the PostgreSQL cluster.
126+
127+
```bash
128+
$ kubectl describe cm mycluster-pgha-config
129+
Name: mycluster-pgha-config
130+
Namespace: pgouser1
131+
Labels: pg-cluster=mycluster
132+
pgha-config=true
133+
vendor=crunchydata
134+
Annotations: <none>
135+
136+
Data
137+
====
138+
mycluster-dcs-config:
139+
----
140+
postgresql:
141+
parameters:
142+
archive_command: source /opt/cpm/bin/pgbackrest/pgbackrest-set-env.sh && pgbackrest
143+
archive-push "%p"
144+
archive_mode: true
145+
archive_timeout: 60
146+
log_directory: pg_log
147+
log_min_duration_statement: 60000
148+
log_statement: none
149+
max_wal_senders: 6
150+
shared_buffers: 128MB
151+
shared_preload_libraries: pgaudit.so,pg_stat_statements.so
152+
temp_buffers: 8MB
153+
unix_socket_directories: /tmp,/crunchyadm
154+
wal_level: logical
155+
work_mem: 4MB
156+
recovery_conf:
157+
restore_command: source /opt/cpm/bin/pgbackrest/pgbackrest-set-env.sh && pgbackrest
158+
archive-get %f "%p"
159+
use_pg_rewind: true
160+
161+
mycluster-local-config:
162+
----
163+
postgresql:
164+
callbacks:
165+
on_role_change: /opt/cpm/bin/callbacks/pgha-on-role-change.sh
166+
create_replica_methods:
167+
- pgbackrest
168+
- basebackup
169+
pg_hba:
170+
- local all postgres peer
171+
- local all crunchyadm peer
172+
- host replication primaryuser 0.0.0.0/0 md5
173+
- host all primaryuser 0.0.0.0/0 reject
174+
- host all all 0.0.0.0/0 md5
175+
pgbackrest:
176+
command: /opt/cpm/bin/pgbackrest/pgbackrest-create-replica.sh
177+
keep_data: true
178+
no_params: true
179+
pgbackrest_standby:
180+
command: /opt/cpm/bin/pgbackrest/pgbackrest-create-replica.sh
181+
keep_data: true
182+
no_master: 1
183+
no_params: true
184+
pgpass: /tmp/.pgpass
185+
remove_data_directory_on_rewind_failure: true
186+
use_unix_socket: true
187+
```
188+
189+
### Updating Configuration Settings
190+
191+
In order to update a cluster's configuration settings and then apply
192+
those settings (e.g. to the DCS and/or any individual database servers), the
193+
DCS and local configuration settings within the `<clusterName>-pgha-config`
194+
ConfigMap can be modified. This can be done using the various commands
195+
available using the `kubectl` client (or the `oc` client if using OpenShift)
196+
for modifying Kubernetes resources. For instance, the following command can be
197+
utilized to open the ConfigMap in a local text editor, and then update the
198+
various cluster configurations as needed:
199+
200+
```bash
201+
kubectl edit configmap mycluster-pgha-config
202+
```
203+
204+
Once the `<clusterName>-pgha-config` ConfigMap has been updated, any
205+
changes made will be detected by the Operator, and then applied to the
206+
DCS and/or any individual database servers within the cluster.
207+
208+
#### PostgreSQL Configuration
209+
210+
In order to update the `postgresql.conf` file for a one of more database servers, the
211+
`parameters` section of either the DCS and/or a local database configuration can be
212+
updated, e.g.:
213+
214+
```yaml
215+
----
216+
postgresql:
217+
parameters:
218+
max_wal_senders: 10
219+
```
220+
221+
The various key/value pairs provided within the `paramters` section result in the
222+
configuration of the same settings within the `postgresql.conf` file. Please note that
223+
settings applied locally to a database server take precendence over those set via the DCS (with the
224+
exception being those that must be set via the DCS, as
225+
[described here](https://access.crunchydata.com/documentation/patroni/latest/dynamic_configuration/)).
226+
227+
Also, please note that `pg_hba` and `pg_ident` sections exist to update both the `pg_hba.conf` and
228+
`pg_ident.conf` PostgreSQL configuration files as needed.
229+
230+
#### Restarting Database Servers
231+
232+
Changes to certain settings may require a restart of a PostgreSQL database.
233+
This can be accomplished using the `patronictl` utility included wihtin
234+
each PostgreSQL database container in the cluster, specifically
235+
using the `patronictl restart` command. For example, to detect if a
236+
restart is needed for a server in a cluster called `mycluster`, the
237+
`kubectl exec` command can be utilized to access the database container for
238+
the primary PostgreSQL database server, and run the `patronictl list`
239+
command:
240+
241+
```bash
242+
$ kubectl exec -it mycluster-6f89d8bb85-pnlwz -- patronictl list
243+
+ Cluster: mycluster (6821144425371877525) -------+---------+----+-----------+-----------------+
244+
| Member | Host | Role | State | TL | Lag in MB | Pending restart |
245+
+-----------------------------+-----------+--------+---------+----+-----------+-----------------+
246+
| mycluster-6f89d8bb85-pnlwz | 10.44.0.6 | Leader | running | 1 | | * |
247+
+-----------------------------+-----------+--------+---------+----+-----------+-----------------+
248+
```
249+
250+
Here we can see that the ` mycluster-6f89d8bb85-pnlwz` server is pending a restart,
251+
which can then be accomplished as follows:
252+
253+
```bash
254+
$ kubectl exec -it mycluster-6f89d8bb85-pnlwz -- patronictl restart mycluster mycluster-6f89d8bb85-pnlwz
255+
+ Cluster: mycluster (6821144425371877525) -------+---------+----+-----------+
256+
| Member | Host | Role | State | TL | Lag in MB |
257+
+-----------------------------+-----------+--------+---------+----+-----------+
258+
| mycluster-6f89d8bb85-pnlwz | 10.44.0.6 | Leader | running | 1 | |
259+
+-----------------------------+-----------+--------+---------+----+-----------+
260+
When should the restart take place (e.g. 2020-04-29T17:23) [now]: now
261+
Are you sure you want to restart members mycluster-6f89d8bb85-pnlwz? [y/N]: y
262+
Restart if the PostgreSQL version is less than provided (e.g. 9.5.2) []:
263+
Success: restart on member mycluster-6f89d8bb85-pnlwz
264+
```
265+
266+
Please note that these commands can be run from the primary or any replica
267+
database container within the PostgreSQL cluster being updated.
78268

269+
### Refreshing Configuration Settings
270+
271+
If necessary, it is possible to refresh the configuration stored within the
272+
`<clusterName>-pgha-config` ConfigMap with a fresh copy of either the DCS
273+
configuration and/or the configuration for one or more local database servers.
274+
This is specifically done by fully deleting a configuration from the
275+
`<clusterName>-pgha-config` ConfigMap. Once a configuration has been deleted,
276+
the Operator will detect this and refresh the ConfigMap with a fresh copy of
277+
that specific configuration.
278+
279+
For instance, the following `kubectl patch` command can be utilized to
280+
remove the `mycluster-dcs-config` configuration from the example above,
281+
causing that specific configuration to be refreshed with a fresh copy of
282+
the DCS configuration settings for `mycluster`:
283+
284+
```bash
285+
kubectl patch configmap mycluster-pgha-config \
286+
--type='json' -p='[{"op": "remove", "path": "/data/mycluster-dcs-config"}]'
287+
```

0 commit comments

Comments
 (0)