Search code examples
kubernetespersistent-storagepersistent-volumeskubernetes-pvc

How to configure pv and pvc for single pod with multiple containers in kubernetes


Need to create a single pod with multiple containers for MySQL, MongoDB, MySQL. My question is should I need to create persistence volume and persistence volume claim for each container and specify the volume in pod configuration or single PV & PVC is enough for all the containers in a single pod-like below configs.

Could you verify below configuration is enough or not?

PV:

    apiVersion: v1
kind: PersistentVolume
metadata:
  name: mypod-pv
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
---

PVC

    apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mypod-pvc
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 3Gi
---

Deployment:

    apiVersion: apps/v1
kind: Deployment
metadata:
  name: mypod
  labels:
    app: mypod
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mypod
  template:
    metadata:
      labels:
        app: mypod
    spec:
      volumes:
        - name: task-pv-storage
          persistentVolumeClaim:
            claimName: mypod-pvc
      containers:
        - name: mysql
          image: mysql/mysql-server:latest
          ports:
            - containerPort: 3306
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - mountPath: "/var/lib/mysql"
              name: task-pv-storage
        - name: mongodb
          image: openshift/mongodb-24-centos7
          ports:
            - containerPort: 27017
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - mountPath: "/var/lib/mongodb"
              name: task-pv-storage
        - name: mssql
          image: mcr.microsoft.com/mssql/server
          ports:
            - containerPort: 1433
          imagePullPolicy: IfNotPresent
          volumeMounts:
            - mountPath: "/var/opt/mssql"
              name: task-pv-storage
      imagePullSecrets:
        - name: devplat

Solution

  • You should not be running multiple database containers inside a single pod.

    Consider running each database in a separate statefulset.

    follow below reference for mysql

    https://kubernetes.io/docs/tasks/run-application/run-replicated-stateful-application/

    You need to adopt similar approach for mongodb or other databases as well.