Imagine you have a postgresql-ephemeral database set up via the following command:
oc new-app --name my-test-db --template postgresql-persistent -p POSTGRESQL_USER=mytestuser -p POSTGRESQL_PASSWORD=mytestpassword -p POSTGRESQL_DATABASE=mytestdb -p DATABASE_SERVICE_NAME=my-test-db
If the persistent volume is lost or corrupted all your data is gone. Furthermore all reads and writes must go through that single instance which can mean poor performance.
You could use a different database template like crunchy but you would need to migrate your config and data over.
Alternatively you could set up your existing database to support slave replication, add one or more slaves and be all set!
First, we need a master user set up (this would happen automatically for a new cluster, but it doesn't for an existing one). Run the following:
oc rsh dc/my-test-db bash -c "createuser master"
Next, we need to deploy our new template. Since OpenShift/Kubernetes does either create or replace we do this in two steps.
Create new secrets and our slaves:
oc new-app --name my-test-db -f postgresql_replica.json -p POSTGRESQL_USER=mytestuser -p POSTGRESQL_PASSWORD=mytestpassword -p POSTGRESQL_DATABASE=mytestdb -p POSTGRESQL_MASTER_SERVICE_NAME=my-test-db -p POSTGRESQL_SLAVE_SERVICE_NAME=my-test-db-slaves
Patch the existing deployment:
oc new-app --name my-test-db -f postgresql_replica.json -p POSTGRESQL_USER=mytestuser -p POSTGRESQL_PASSWORD=mytestpassword -p POSTGRESQL_DATABASE=mytestdb -p POSTGRESQL_MASTER_SERVICE_NAME=my-test-db -p POSTGRESQL_SLAVE_SERVICE_NAME=my-test-db-slaves -o json | oc replace -f -
All done! You should see your database replicated to our new slave, and any future changes should be available there as well. If your application supports it, you can send reads to the slave service. It will be set up the same as the existing connection, but with a different host name (in this case my-test-db-slaves
).