How to scale a cluster¶
Replication in PostgreSQL is the process of creating copies of the stored data. This provides redundancy, which means the application can provide self-healing capabilities in case one replica fails. In this context, each replica is equivalent to one juju unit.
This guide will show you how to establish and change the amount of Juju units used to replicate your data.
Deploy PostgreSQL with n replicas¶
To deploy PostgreSQL with multiple replicas, specify the number of desired units with the -n option.
juju deploy postgresql --channel 16/stable -n <number_of_replicas>
juju deploy postgresql-k8s --channel 16/stable -n <number_of_replicas> --trust
It is recommended to deploy an odd number of replicas
This helps prevent a split-brain scenario.
Scale replicas on an existing application¶
The amount of replicas (Juju units) can also be modified after deployment.
Data loss warning
Removing the last unit will destroy your data permanently!
To scale up the cluster, use juju add-unit:
juju add-unit postgresql --num-units <amount_of_units_to_add>
To scale down the cluster, use juju remove-unit:
juju remove-unit postgresql/<unit_id_to_remove>
In Kubernetes, scaling operations are performed using juju scale-application and specifying the total amount of units you want to have in the cluster:
juju scale-application postgresql-k8s <total number of units>
See also: PostgreSQL units