简体   繁体   中英

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

  • 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

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.

Yes you can create as much databas as you want on the same Persistent Volume. You have to change the path value to store different database. See the example below.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: ...
  namespace: ...
  labels:
    type: ...
spec:
  storageClassName: ...
  capacity:
    storage: ...
  accessModes:
    - ...
  hostPath:
    path: "/mnt/data/DIFFERENT_PATH_FOR_EACH_DATABASE"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM