From e353d032944004c1e28713836ee6ba4fb1146f41 Mon Sep 17 00:00:00 2001 From: Ganey Date: Fri, 15 May 2026 22:06:28 +0100 Subject: [PATCH] feat(kubernetes): add kubernetes deployment example with initContainer --- examples/kubernetes/README.md | 25 +++++++++++++++ examples/kubernetes/deployment.yaml | 50 +++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 examples/kubernetes/README.md create mode 100644 examples/kubernetes/deployment.yaml diff --git a/examples/kubernetes/README.md b/examples/kubernetes/README.md new file mode 100644 index 0000000..946dab7 --- /dev/null +++ b/examples/kubernetes/README.md @@ -0,0 +1,25 @@ +# Kubernetes Example: SQLite to PostgreSQL Hook + +This example demonstrates how to use `postgres-sqlite` in a Kubernetes environment to redirect an application`s SQLite calls to a PostgreSQL database without modifying the application image. + +## How it works + +1. **Init Container**: Downloads the `sqlite_hook.so` library and installs `libpq5` (the PostgreSQL client library). It then copies the hook and `libpq.so.5` to a shared `emptyDir` volume. +2. **Environment Variables**: + * `LD_PRELOAD`: Tells the dynamic linker to load our hook before any other libraries. + * `LD_LIBRARY_PATH`: Ensures the linker can find `libpq.so.5` inside the shared volume. + * `PG_CONNINFO`: Provides the PostgreSQL connection string used by the hook. +3. **Volume Sharing**: Both the init container and the main container mount the `patch-libs` volume to share the library files. + +## Files + +- `deployment.yaml`: A sample Kubernetes Deployment manifest. + +## Usage + +1. Customize the `PG_CONNINFO` in `deployment.yaml`. +2. Ensure your main container image is based on an OS compatible with the downloaded `.so` (e.g., Ubuntu 24.04). +3. Apply the manifest: + ```bash + kubectl apply -f deployment.yaml + ``` diff --git a/examples/kubernetes/deployment.yaml b/examples/kubernetes/deployment.yaml new file mode 100644 index 0000000..480a575 --- /dev/null +++ b/examples/kubernetes/deployment.yaml @@ -0,0 +1,50 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: sqlite-app-postgres +spec: + replicas: 1 + selector: + matchLabels: + app: sqlite-app + template: + metadata: + labels: + app: sqlite-app + spec: + initContainers: + - name: download-sqlite-hook + # Use an image matching your target OS (e.g., Ubuntu 24.04 for Noble based apps) + image: ubuntu:24.04 + command: + - /bin/sh + - -c + - | + apt-get update && apt-get install -y curl libpq5 + # Fetch the pre-compiled hook for your architecture/OS + curl -L -o /patch/sqlite_hook.so https://github.com/ganey/postgres-sqlite/releases/download/v1.0.0/sqlite_hook-ubuntu.so + # Copy libpq dependency to the shared volume so the hook can find it + cp /usr/lib/x86_64-linux-gnu/libpq.so.5* /patch/ + chmod 644 /patch/sqlite_hook.so /patch/libpq.so.5* + volumeMounts: + - name: patch-libs + mountPath: /patch + containers: + - name: main + image: your-sqlite-app-image:latest + env: + # PostgreSQL connection details + - name: PG_CONNINFO + value: "postgresql://user:password@postgres-host:5432/dbname" + # Ensure the hook can find its dependencies in the shared volume + - name: LD_LIBRARY_PATH + value: /patch + # Preload the hook to intercept SQLite calls + - name: LD_PRELOAD + value: /patch/sqlite_hook.so + volumeMounts: + - name: patch-libs + mountPath: /patch + volumes: + - name: patch-libs + emptyDir: {}