sghorbal commited on
Commit
684d2f7
·
1 Parent(s): 88c10b9

add pgbouncer and use it to pool db connexions

Browse files
Files changed (2) hide show
  1. Dockerfile +6 -0
  2. entrypoint.sh +50 -5
Dockerfile CHANGED
@@ -12,6 +12,7 @@ RUN apt-get update && apt-get install -y \
12
  build-essential \
13
  default-libmysqlclient-dev \
14
  libpq-dev \
 
15
  git \
16
  curl \
17
  && apt-get clean && rm -rf /var/lib/apt/lists/*
@@ -28,6 +29,11 @@ COPY --chown=airflow:airflow requirements.txt /requirements.txt
28
  COPY --chown=airflow:airflow dags $AIRFLOW_HOME/dags
29
  COPY --chown=airflow:airflow entrypoint.sh /entrypoint.sh
30
 
 
 
 
 
 
31
  # Make entrypoint executable
32
  RUN chmod +x /entrypoint.sh
33
 
 
12
  build-essential \
13
  default-libmysqlclient-dev \
14
  libpq-dev \
15
+ pgbouncer \
16
  git \
17
  curl \
18
  && apt-get clean && rm -rf /var/lib/apt/lists/*
 
29
  COPY --chown=airflow:airflow dags $AIRFLOW_HOME/dags
30
  COPY --chown=airflow:airflow entrypoint.sh /entrypoint.sh
31
 
32
+ # Create the pgbouncer configuration directory
33
+ # and give ownership to airflow user
34
+ RUN mkdir -p /etc/pgbouncer \
35
+ && chown -R airflow:airflow /etc/pgbouncer
36
+
37
  # Make entrypoint executable
38
  RUN chmod +x /entrypoint.sh
39
 
entrypoint.sh CHANGED
@@ -1,15 +1,60 @@
1
  #!/bin/bash
2
  set -e
3
 
4
- # Charger les variables d'environnement
 
5
  if [ -f .env ]; then
6
  export $(grep -v '^#' .env | xargs)
7
  fi
8
 
9
- # Init DB si besoin
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  airflow db init
11
 
12
- # Créer un user admin si aucun n'existe
13
  airflow users create \
14
  --username admin \
15
  --firstname Admin \
@@ -18,8 +63,8 @@ airflow users create \
18
  --email [email protected] \
19
  --password admin || true
20
 
21
- # Lancer le scheduler en background
22
  airflow scheduler &
23
 
24
- # Lancer le webserver (en foreground, pour que le conteneur reste "vivant")
25
  exec airflow webserver --port 8088
 
1
  #!/bin/bash
2
  set -e
3
 
4
+ # Load environment variables from .env file if it exists
5
+ # This is useful for local development
6
  if [ -f .env ]; then
7
  export $(grep -v '^#' .env | xargs)
8
  fi
9
 
10
+ # Dynamically extract database connection details from environment variables
11
+ # airflow_db
12
+ AF_USER=$(echo "$AIRFLOW__CORE__SQL_ALCHEMY_CONN" | sed -E 's|postgresql\+psycopg2://([^:]+):.*|\1|')
13
+ AF_PASS=$(echo "$AIRFLOW__CORE__SQL_ALCHEMY_CONN" | sed -E 's|postgresql\+psycopg2://[^:]+:([^@]+)@.*|\1|')
14
+ AF_HOST=$(echo "$AIRFLOW__CORE__SQL_ALCHEMY_CONN" | sed -E 's|.*@([^:/]+):([0-9]+)/.*|\1|')
15
+ AF_PORT=$(echo "$AIRFLOW__CORE__SQL_ALCHEMY_CONN" | sed -E 's|.*@[^:/]+:([0-9]+)/.*|\1|')
16
+ AF_DB=$(echo "$AIRFLOW__CORE__SQL_ALCHEMY_CONN" | sed -E 's|.*/([^?]+).*|\1|')
17
+
18
+ # transaction_db
19
+ TX_USER=$(echo "$DATABASE_URL" | sed -E 's|postgresql\+psycopg2://([^:]+):.*|\1|')
20
+ TX_PASS=$(echo "$DATABASE_URL" | sed -E 's|postgresql\+psycopg2://[^:]+:([^@]+)@.*|\1|')
21
+ TX_DB=$(echo "$DATABASE_URL" | sed -E 's|.*/([^?]+).*|\1|')
22
+
23
+ # Generate pgbouncer.ini
24
+ cat <<EOF > /etc/pgbouncer/pgbouncer.ini
25
+ [databases]
26
+ airflow_db = host=${AF_HOST} port=${AF_PORT} dbname=${AF_DB} user=${AF_USER} password=${AF_PASS}
27
+ transaction_db = host=${AF_HOST} port=${AF_PORT} dbname=${TX_DB} user=${TX_USER} password=${TX_PASS}
28
+
29
+ [pgbouncer]
30
+ listen_addr = 127.0.0.1
31
+ listen_port = 6432
32
+ auth_type = trust
33
+ auth_file = /etc/pgbouncer/userlist.txt
34
+ pool_mode = transaction
35
+ server_tls_sslmode = require
36
+ ignore_startup_parameters = extra_float_digits
37
+ log_connections = 0
38
+ log_disconnections = 0
39
+ default_pool_size = 5
40
+ EOF
41
+
42
+ # Generate userlist.txt
43
+ cat <<EOF > /etc/pgbouncer/userlist.txt
44
+ "${AF_USER}" "${AF_PASS}"
45
+ "${TX_USER}" "${TX_PASS}"
46
+ EOF
47
+
48
+ # Launch PgBouncer
49
+ pgbouncer /etc/pgbouncer/pgbouncer.ini &
50
+
51
+ # Dynamically modify the URL to PgBouncer
52
+ export AIRFLOW__CORE__SQL_ALCHEMY_CONN=postgresql+psycopg2://${AF_USER}:${AF_PASS}@127.0.0.1:6432/airflow_db
53
+
54
+ # Init DB if needed
55
  airflow db init
56
 
57
+ # Create a superuser if it doesn't exist
58
  airflow users create \
59
  --username admin \
60
  --firstname Admin \
 
63
  --email [email protected] \
64
  --password admin || true
65
 
66
+ # Launch the scheduler
67
  airflow scheduler &
68
 
69
+ # Launch the webserver
70
  exec airflow webserver --port 8088