6.12.18

Bootstrapping Postgres in a kube cluster via kubernetes configmaps

Postgres databases can run several init scripts when they start, specifically, the docker containers are set up to run from the docker-entrypoint-initdb/ directory.

So, when you run a postgres container, if you mount your schema bootstrap files into the container

Its ridiculously easy, you dont need a managed database or kubedb, or anything else.

Just do this :).

Bootstrapping postgres in a cluster, with a schema, by injecting a configmap 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-bootstrapped
  labels:
    tier: postgres-bootstrapped
spec:
  selector:
    matchLabels:
      tier: postgres-bootstrapped
  template:
    metadata:
      labels:
        tier: postgres
    spec:
      containers:
      - image: postgres
        name: postgres
        ports:
        - containerPort: 5432
          name: postgres
        volumeMounts:
            - name: postgres-storage
              mountPath: /var/lib/postgresql/db-data
            - name: postgres-initdb-configmap
              mountPath: /docker-entrypoint-initdb.d/
      volumes:
        - name: postgres-storage
          emptyDir: {}
        - name: postgres-initdb-configmap
          configMap:
            name: postgres-initdb-configmap
            items:
              - key: 0-bootstrap
                path: 0-bootstrap.sql
---
apiVersion: v1
data:
  0-bootstrap: |
    ALTER USER postgres WITH PASSWORD '1234';
    CREATE USER jay WITH PASSWORD 'jay';
    CREATE DATABASE jaysDb OWNER jay ;
kind: ConfigMap
metadata:
  name: postgres-initdb-configmap

No comments:

Post a Comment