Support for Kubernetes Postgres Operators (CNPG) #31147
Replies: 2 comments 11 replies
-
I'm currently working through this. I have tinkered with it before, and I duplicated supabase's postgres Dockerfile, and added in the cnpg requirements. I got it working, but in the end didnt need it. So I'm going to try a simple Dockerfile that will build on the supabase/postgres docker image and install the barman packages (the minimum requirements for cnpg) and see how it goes. I have recently found this: I guess there are 3 ways of applying the migrations.
It would be amazing to have all the migrations built into a docker container that can run them against any postgres instance, as opposed to having them included in the supabase/postgres image. I'll see where I get to with just brute-forcing barman into the supabase/postgres image. If that works easily enough, then I think working on a community supported (or officially supported?) container that centralizes all the required bootstraping migrations in something. on a side note, there is a bug in supabase/realtime that prevents it from clustering correctly. |
Beta Was this translation helpful? Give feedback.
-
Here is an all-in-one update of my progress so far: CNPG+Supabase Compatible Dockerfile FROM supabase/postgres:15.8.1.017
# Import the repository signing key:
RUN apt install curl ca-certificates
RUN install -d /usr/share/postgresql-common/pgdg
RUN curl -o /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc --fail https://www.postgresql.org/media/keys/ACCC4CF8.asc
# Create the repository configuration file:
RUN sh -c 'echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
# Update the package lists:
RUN apt update && apt install -y --no-install-recommends \
barman \
barman-cli \
barman-cli-cloud \
&& rm -rf /var/lib/apt/lists/*
# Change the uid of postgres to 26
RUN usermod -u 26 postgres
USER 26 Make sure you tag the image according to CNPG requirements https://cloudnative-pg.io/documentation/1.24/container_images/ Example cluster manifest: apiVersion: postgresql.cnpg.io/v1
kind: Cluster
metadata:
name: testing
spec:
instances: 3
imageName: <<<<IMAGE NAME>>>>
imagePullPolicy: Always
storage:
size: 5Gi
enableSuperuserAccess: true
postgresql:
pg_hba:
# ripped from supabase/posgres/ansible/files/postgresql_config/pg_hba.conf
- local all supabase_admin scram-sha-256
- local all all peer map=supabase_map
- host all all 127.0.0.1/32 trust
- host all all ::1/128 trust
- host all all 10.0.0.0/8 scram-sha-256
- host all all 172.16.0.0/12 scram-sha-256
- host all all 192.168.0.0/16 scram-sha-256
- host all all 0.0.0.0/0 scram-sha-256
- host all all ::0/0 scram-sha-256
pg_ident:
# ripped from supabase/posgres/ansible/files/postgresql_config/pg_ident.conf
- supabase_map postgres postgres
- supabase_map gotrue supabase_auth_admin
- supabase_map postgrest authenticator
- supabase_map adminapi postgres
# ripped from supabase/posgres/ansible/files/postgresql_config/postgresql.conf
shared_preload_libraries:
[
pg_stat_statements,
pg_stat_monitor,
pgaudit,
plpgsql,
plpgsql_check,
pg_cron,
pg_net,
timescaledb,
auto_explain,
pg_tle,
supautils,
# pgsodium # pgsodium crashes cnpg db pods
]
bootstrap:
initdb:
database: supabase
# I think this has to be supabase_admin (due to migration hard coded strings), however I will investigate further
owner: supabase_admin Migrations:
NOTE: comment out the contents of Dockerfile: # it seems like supabase uses dbmate?
# https://github.com/supabase/postgres/tree/develop/migrations
# https://github.com/amacneil/dbmate
FROM ghcr.io/amacneil/dbmate:2.24.2@sha256:6e245cce5580567747cf440d84a7507121793c81564772fdd75f8ee0f45bbb79
COPY ./db ./db
COPY ./migrate.sh ./migrate.sh
RUN chmod +x ./migrate.sh
# Install
# gettext for the envsubst bin
# moreutils for the sponge bin (allows reading/writing from the same file for in-situ envsubst)
RUN apk add --no-cache gettext moreutils
ENTRYPOINT ["./migrate.sh"] migrate.sh: #!/usr/bin/env sh
echo "--------------------"
echo "Beginning migrations"
# in-situ env_var substitution
echo "Variable Substitution"
for migration in ./db/final/*.sql; do
envsubst '$POSTGRES_USER, $POSTGRES_PASSWORD, $POSTGRES_DB, $JWT_SECRET, $JWT_EXP' < $migration | sponge $migration
done
# build from parts, as the CNPG superuser targets "*" database.
# Superuser credentials, and the App DB
db_uri="postgresql://${SUPERUSER_USER}:${SUPERUSER_PASSWORD}@${SUPERUSER_HOST}:${SUPERUSER_PORT}/${POSTGRES_DB}"
echo "Running base migrations"
# MIGRATION_SCHEMA as first search_path so the dbmate migrations table gets created in a seperate schema (not polluting the public schema, and not interfering with Realtime's schema_migrations table)
dbmate_uri="${db_uri}?search_path=${MIGRATION_SCHEMA},public"
# https://github.com/amacneil/dbmate
dbmate --url $dbmate_uri --migrations-dir "./db/base" --wait --no-dump-schema up
echo "Running final migrations"
echo "_supabase.sql"
psql -d $db_uri -f "./db/final/_supabase.sql"
echo "webhooks.sql"
psql -d $db_uri -f "./db/final/webhooks.sql"
echo "jwt.sql"
psql -d $db_uri -f "./db/final/jwt.sql"
echo "logs.sql"
psql -d $db_uri -f "./db/final/logs.sql"
echo "realtime.sql"
psql -d $db_uri -f "./db/final/realtime.sql"
echo "poolers.sql"
psql -d $db_uri -f "./db/final/poolers.sql"
echo "roles.sql"
psql -d $db_uri -f "./db/final/roles.sql"
echo " Complete"
echo "--------------------" Job example manifest: apiVersion: batch/v1
kind: Job
metadata:
name: supabase-migration
spec:
template:
spec:
containers:
- name: supabase-migration
image: <<<<CONTAINER NAME>>>>
env:
- name: SUPERUSER_HOST
valueFrom:
secretKeyRef:
name: testing-superuser
key: host
# Use the Superuser CNPG secret for migration connection
- name: SUPERUSER_PORT
valueFrom:
secretKeyRef:
name: testing-superuser
key: port
# Use the Superuser CNPG secret for migration connection
- name: SUPERUSER_USER
valueFrom:
secretKeyRef:
name: testing-superuser
key: user
# Use the Superuser CNPG secret for migration connection
- name: SUPERUSER_PASSWORD
valueFrom:
secretKeyRef:
name: testing-superuser
key: password
# Schema where dbmate will store base migration details, so it doesnt pollute the `public` schema
- name: MIGRATION_SCHEMA
value: dbmate
# use the App secret, for variable substitution in supabase migrations
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: testing-app
key: password
# use the App secret, for variable substitution in supabase migrations
- name: POSTGRES_USER
valueFrom:
secretKeyRef:
name: testing-app
key: user
# use the App secret, for variable substitution in supabase migrations
- name: POSTGRES_DB
valueFrom:
secretKeyRef:
name: testing-app
key: dbname
# for variable substitution in supabase migrations
- name: JWT_SECRET
valueFrom:
secretKeyRef:
name: supabase-jwt
key: secret
# for variable substitution in supabase migrations
- name: JWT_EXP
valueFrom:
secretKeyRef:
name: supabase-jwt
key: expiry
restartPolicy: Never
backoffLimit: 4 the secrets Notes on migration: Any new migrations in supabase/postgres can be loaded into the Probably worth adding some sort of flag or check to disable the And finally: 24/12/27 UPDATEOk, I had some issues with migrations, schemas, connection strings etc. Changes:
I will work on getting the other services up. 04/01/25. |
Beta Was this translation helpful? Give feedback.
-
I have been running my Postgres database on Kubernetes with 'Cloud-Native Postgres Operator' https://cloudnative-pg.io/ with great success on the base postgres image. I'd like to try self hosting some subset of Supabase on Kubernetes, while still using the CNPG operator to manage my database, but haven't been able to figure it out.
Would be great to have some option on how to merge the supabase postgres image with Kubernetes operators like CNPG or Zalando, so a self-hosted Supabase, or really any Kubernetes Postgres implementattion, can take advantage of both the high availability management goodies of these operators, and also the baseline extensions and other functionality of this image.
Beta Was this translation helpful? Give feedback.
All reactions