Skip to content

Connecting

Every database has one user, app, one Postgres database, app, and a password Peat makes. Peat puts what a client needs beside the Database object, in the same namespace:

ObjectHolds
Secret <name>-loginusername, password, dbname, host, serviceHost, port and url (a full connection URL)
ConfigMap peat-caca.crt, the certificate authority that signed the database’s certificate

Every connection is over TLS and goes through the front door, which reads the database’s name from the connection and routes on it. So a client must connect by the database’s name, not by an IP address, and should check the certificate with sslmode=verify-full.

The name inside the cluster is <name>.peat-db.svc, port 5432. Give the pod the Secret and the authority:

env:
- name: DATABASE_URL
valueFrom: { secretKeyRef: { name: orders-login, key: url } }
- name: PGSSLROOTCERT
value: /peat/ca.crt
volumeMounts:
- { name: peat-ca, mountPath: /peat }
# and under the pod's volumes:
# - name: peat-ca
# configMap: { name: peat-ca }

PGSSLROOTCERT is read by libpq and the clients built on it. Other drivers have a setting of their own for the root certificate.

A port-forward to the front door works for a look around. The connection still names the database (host=) while going to your own machine (hostaddr=):

Terminal window
kubectl -n peat-gateway port-forward svc/pg-front 15432:5432 &
kubectl get configmap peat-ca -o jsonpath='{.data.ca\.crt}' > peat-ca.crt
host=$(kubectl get database orders -o jsonpath='{.status.host}')
PGPASSWORD=$(kubectl get secret orders-login -o jsonpath='{.data.password}' | base64 -d) \
psql "host=$host hostaddr=127.0.0.1 port=15432 user=app dbname=app sslmode=verify-full sslrootcert=peat-ca.crt"

kubectl ends a port-forward when its stream drops; start it again if a connection is refused.

Set a domain and a way in on the Platform:

spec:
gateways:
domain: db.example.com
service:
type: LoadBalancer # or NodePort
allowFrom: # optional: who may reach the front door
- 203.0.113.0/24

Then point a wildcard DNS name, *.db.example.com, at the front door’s address (kubectl -n peat-gateway get svc pg-front). Each database is <name>.db.example.com on port 5432:

Terminal window
peat ca > peat-ca.crt
psql "$(peat url orders)&sslrootcert=peat-ca.crt"

peat url prints the URL with the password in it. Use it at a terminal you are alone at, not in a log or on a shared screen.

allowFrom only sees a client’s own address where the way in keeps it (a Service with externalTrafficPolicy: Local, or a load balancer that does). Through one that does not, every client appears to come from a machine of the cluster.

A database nobody has used for a while is asleep, and a connection wakes it. That first connection waits while the compute starts, usually a few seconds, so give clients a connect timeout of a minute and let them retry once. Sleep and wake says more.

By default Peat signs with an authority of its own, the one in peat-ca. To use cert-manager instead, name an issuer in the Platform’s certificates.issuer; clients then trust whatever authority that issuer chains to.