Multiple databases in postgres with Kubernetes?

10/9/2017

I have four java apps (that make up one large java app) that use two postgres servers with about 8 different databases (four on each postgres server).

Since we're going with kubernetes I'm not shure what to do that would yield a good result? Can I make in a consistent way just a single postgres replica with 8 databases in it? Is that even possible in Kubernetes? Maybe there is a better way of handling that situation?

Tnx, Tom

-- Tomislav Mikulin
java
kubernetes
postgresql

1 Answer

10/9/2017

If you don't need your 8 databases on 2 different servers(because of proximity or otherwise), then I would put all 8 databases in a single instance. One less thing to manage.

You can run your postgres database in a single replica(or more than one replica) within Kubernetes. Running a database in a container means you would also need to setup persistent storage, otherwise you will lose data when your container is stopped.

To run a database in Kubernetes with persistent storage, you need to create a deployment, a persistent volume, and a persistent volume claim. I'm using NFS in this example, but there are plenty of other storage options available.

postgres-deployment.yml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-database
  labels:
    app: my-database
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      labels:
        app: my-database
    spec:
      containers:
      - image: postgres:9.6.3
        name: my-database
        ports:
        - containerPort: 5432
        resources: {}
        volumeMounts:
        - mountPath: "/var/lib/postgresql/data:Z"
          name: postgresdb-storage
      restartPolicy: Always
      volumes:
      - name: postgresdb-storage
        persistentVolumeClaim:
          claimName: postgresdb-volume-claim
status: {}

postgres-pvolume.yml

apiVersion: v1
kind: PersistentVolume
metadata:
  name: postgresdb-volume
spec:
  storageClassName: postgresdb-class
  accessModes:
    - ReadWriteMany
  capacity:
    storage: 100Gi
  nfs:
    server: <NFS_SERVER_IP_ADDRESS>
    path: /var/nfs/data/my-database-data

postgres-pvolume-claim.yml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: postgresdb-volume-claim
spec:
  storageClassName: postgresdb-class
  accessModes:
  - ReadWriteMany
  resources:
    requests:
      storage: 100Gi
status: {}

Then you can add these to your Kubernetes cluster with the following commands:

kubectl create -f postgres-pvolume.yml 
kubectl create -f postgres-pvolume-claim.yml 
kubectl create -f postgres-deployment.yml

One word of caution though. Persistent storage in the containerized world is still in its infancy, and can be problematic in high traffic environments when running more than one replica. If you plan on scaling your database(i.e. running more than one postgres replica), you should research this further, before doing anything in production. There are lots of articles on the web about this.

A lot of people still run their databases in VM's, or on bare metal, and only run databases in containers for local development only.

-- grizzthedj
Source: StackOverflow