如何在kubernetes中为具有多个容器的单个pod配置pv和pvc

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

需要为 MySQL、MongoDB、MySQL 创建一个包含多个容器的 pod。我的问题是我是否需要为每个容器创建持久卷和持久卷声明并在 pod 配置中指定卷 单个 PV 和 PVC 足以用于单个 pod 中的所有容器-像下面的配置。

您能否验证以下配置是否足够?

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
---

部署:

    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

您不应该 运行 在单个 pod 中使用多个数据库容器。

考虑 运行 每个数据库在一个单独的状态集中。

遵循以下参考 mysql

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

您需要对 mongodb 或其他数据库采用类似的方法。