This is not an officially supported Google product
SqlDB operator is a basic Kubernetes stateful operator that automates creation, backup, and restore workflows of SQL instances. This operator leverages CustomResourceDefinitions to represent each workflow declaratively:
SqlDB
: for instance creation and restore workflowsSqlBackup
: for backup workflow
kubernetes
: 1.11.0
and above
kubectl
: 1.11.0
and above
All example manifests are located under config/samples
.
Before starting any of the workflows below, run following commands:
-
Run the operator:
make run
Note: You can also build an image of the operator and deploy it using a pod. -
Run a NFS server (for storing backup file):
kubectl apply -f config/samples/nfs.yaml
-
Run an
alpine
pod and get a shell to its container:
kubectl run --generator=run-pod/v1 --image=alpine:latest -it alpine -- /bin/sh
-
Install the container with
psql
terminal:
apk add --update postgresql-client
To Detech from alpine-shell: CTRL-P, CTRL-Q
To reattach to an existing alpine session: kubectl attach -it alpine-shell
-
Create a
SqlDB
resource nameddb1
to bring up a PostgreSQL cluster:
kubectl apply -f config/samples/db1.yaml
-
Wait for the instance to become ready:
kubectl get sqldb/db1
VerifySTATUS
field becomesServerReady
eventually. -
Within the
alpine
container, connect to the PostgreSQL instance usingpsql
terminal via Kubernetes Service namedsqldb-db1-svc
(username isjohn
and password isabc
):
psql -h sqldb-db1-svc -U john
-
In the
psql
terminal, execute following commands:
CREATE TABLE account(user_id serial PRIMARY KEY);
INSERT INTO account(user_id) VALUES (1234);
SELECT * FROM account;
- Verify following output is shown:
user_id
---------
1234
(1 row)
-
Create a
SqlBackup
resource nameddb1-backup
:
kubectl apply -f config/samples/db1-backup.yaml
-
Wait for the backup to become ready:
kubectl get sqlbackup/db1-backup
VerifySTATUS
field becomesBackupSucceeded
eventually.
-
Create another
SqlDB
resource nameddb2
to bring upanother
PostgreSQL cluster:
kubectl apply -f config/samples/db2.yaml
Note:.spec.backupName
field has been specified to differentiate between instance creation and restore. -
Wait for the restore to complete:
kubectl get sqldb/db2
VerifySTATUS
field becomesServerRestored
eventually. -
Within the
alpine
container, connect to the instance usingpsql
terminal via Kubernetes Service namedsqldb-db2-svc
(again, username isjohn
and password isabc
):
psql -h sqldb-db2-svc -U john
-
In the
psql
terminal, execute following command:
SELECT * FROM account;
Verify that user ID1234
is found.