Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add initContainer to wait for Postgres to be ready #95

Merged
merged 2 commits into from
Sep 26, 2024

Conversation

mitchnielsen
Copy link
Contributor

@mitchnielsen mitchnielsen commented Sep 25, 2024

Summary

  • Adds an initContainer definition that waits until a given Postgres server is ready to accept connections using pg_isready
  • Uses this initContainer in the Prefect migrations Job and the Prefect Server Deployment

This ensures that workloads depending on the database first check to see if the database is ready. This should help avoid crash loop backoffs and can, in certain cases, improve overall spin-up time.

This is mostly effective in scenarios where the database is not running yet (fresh instances or database upgrades), but still worth pursuing - especially because it's clear what a Pod's dependencies are and helps us avoid CrashLoopBackOff problems.

Related to https://linear.app/prefect/issue/PLA-358/optimize-the-time-it-takes-for-the-prefect-operator-to-create-a-new

Testing

First, confirm the unit tests still pass. Additionally, you can manually check the logs for the new initContainer:

$ kubectl logs -f prefect-postgres-migration-3232e736-cmh79 -c wait-for-database
Waiting for PostgreSQL...
postgres:5432 - no response
Waiting for PostgreSQL...
postgres:5432 - no response
Waiting for PostgreSQL...
postgres:5432 - no response
Waiting for PostgreSQL...
postgres:5432 - accepting connections

I also ran a fairly unscientific test to compare how long the total time it took for the PrefectServer to become Ready:

#!/bin/bash

# usage:
#   time ./test.sh

# Create instance
kubectl apply -f deploy/samples/v1_prefectserver_postgres.yaml

# Wait for the instance to be ready
kubectl wait --for=jsonpath='{.status.ready}'=true prefectserver/prefect-postgres

# Clean up
kubectl delete -f deploy/samples/v1_prefectserver_postgres.yaml
kubectl delete pvc postgres-database-postgres-0

Results:

  • from main branch: 31.268 total
  • from PR branch: 16.666 total

Pretty significant difference here - mostly because the Prefect Server and Migrations Pods aren't crash looping while the database comes up.

Copy link

@jakekaplan jakekaplan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔥 🔥 🔥 🔥

- Adds an initContainer definition that waits until a given Postgres
  server is ready to accept connections using `pg_isready`
- Uses this initContainer in the Prefect migrations Job and the Prefect
  Server Deployment

This ensures that workloads depending on the database first check to see
if the database is ready. This should help avoid crash loop backoffs and
can, in certain cases, improve overall spin-up time.

Related to https://linear.app/prefect/issue/PLA-358/optimize-the-time-it-takes-for-the-prefect-operator-to-create-a-new
@mitchnielsen mitchnielsen force-pushed the reconcile-loop-speed-optimizations branch from 5e1cce1 to a182a21 Compare September 26, 2024 21:25
Copy link
Collaborator

@chrisguidry chrisguidry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice solution! It also makes it a little more obvious what's holding up the works: you'd see it in Init:1/1 in kubectl

@mitchnielsen mitchnielsen merged commit 46cd7e3 into main Sep 26, 2024
4 checks passed
@mitchnielsen mitchnielsen deleted the reconcile-loop-speed-optimizations branch September 26, 2024 21:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants