Can we create Multiple databases in Same Persistent Volume in kubernetes ?

6/9/2017
  • I have Azure Machine (kubernetes) who have Agent of 2 core, 1 GB. My two services are running on this Machine each have its own Postgres (Deplyment, Service, Pv, PVC).
  • I want to host my third service too on same machine.
  • So when I tried to create Postgres Deployment (this too have its own service, PV, PVC) but Pod was stuck in status=ContainerCreating .
  • After some digging I got to know that my VM only Supports data-disks.

So i thought why not use PVC of earlier deployment in current service like:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: third-postgres
  labels:
    name: third-postgres
spec:
  replicas: 1
  template:
    metadata:
      labels:
         name: third-postgres
    spec:
      containers:
      - name: third-postgres
        image: postgres
        env:
          - name: PGDATA
            value: /var/lib/postgresql/data/pgdata
          - name: POSTGRES_USER
            value: third-user
          - name: POSTGRES_PASSWORD
            value: <password>
          - name: POSTGRES_DB
            value: third_service_db
        ports:
          - containerPort: 5432
        volumeMounts:
        - name: third-postgresdata
      mountPath: /var/lib/postgresql/data
  volumes:
  - name: third-postgresdata
    persistentVolumeClaim:
      claimName: <second-postgres-data>
  • Now this Deployment was successfully running but it doesn't create new database third_service_db
  • May be because second PVC was already exists so it skips the Db create part ?
  • So is their any way I can use same PVC for my all services and same PVC can have multiple databases. So that when I run kubectl create -f <path-to-thirst-postgres.yaml> it takes name Database configuration from env Variables and create DB in same PVC
-- user5594493
azure
kubectl
kubernetes
persistent-storage
postgresql

1 Answer

6/28/2017

You have to create one PVC per Deployment. Once a PVC has been claimed, it must be released before it can be used again.

In the case of AzureDisk, the auto-created volumes can only be mounted by a single node (ReadWriteOnce access mode) so there's one more constraint: each of your Deployments can have at most 1 replica.

-- Antoine Cotten
Source: StackOverflow