0

我已经在 kubernetes 上部署了 mongodb pod,并且能够在数据库中连接和插入数据。但是当吊舱重新启动时,我的所有数据都将丢失。我也为 mongodb 创建了 persistentvolume,但数据仍然丢失。下面是我的yamls。有人可以请有人解释我在这里做错了什么。

Statefulset.yaml

---
  apiVersion: "apps/v1"
  kind: "StatefulSet"
  metadata:
     name: "mongo-development"
     namespace: "development"
  spec:
    serviceName: "mongo-development"
    replicas: 1
    selector:
      matchLabels:
        app: "mongo-development"
    template:
      metadata:
        labels:
           app: "mongo-development"
      spec:
        containers:
          -
            name: "mongo-development"
            image: "mongo"
            imagePullPolicy: "Always"
            env:
             -
                name: "MONGO_INITDB_ROOT_USERNAME"
                value: "xxxx"

             -
                name: "MONGO_INITDB_ROOT_PASSWORD"
                value: "xxxx"

            ports:
              -
                containerPort: 27017
                name: "mongodb"

            volumeMounts: 
              - 
                name: "mongodb-persistent-storage"
                mountPath: "/var/lib/mongodb"
        volumes: 
          - 
            name: "mongodb-persistent-storage"
            persistentVolumeClaim: 
              claimName: "mongodb-pvc-development"

pvc.yaml

---
  apiVersion: "v1"
  kind: "PersistentVolumeClaim"
  metadata: 
    name: "mongodb-pvc-development"
    namespace: "development"
    labels: 
      app: "mongo-development"
  spec: 
    accessModes: 
      - ReadWriteOnce
    resources: 
      requests: 
        storage: 5Gi
    storageClassName: gp2

pv.yaml

---
apiVersion: "v1"
kind: "PersistentVolume"
metadata:
 name: "mongo-pv-development"
 namespace: "development"
 labels:
  type: local
  app: "mongo-development"
spec:
  storageClassName: "local-storage"
  capacity:
     storage: 5Gi
  accessModes:
     - ReadWriteOnce
  persistentVolumeReclaimPolicy: Retain
  claimRef:
     namespace: "development"
     name: "mongodb-pvc-development"
  nfs:
     path: /mnt
     server: xxxx

服务.yaml

---
  apiVersion: "v1"
  kind: "Service"
  metadata: 
    name: "mongo-development"
    namespace: "development"
    labels: 
      app: "mongo-development"
  spec: 
    ports: 
      - 
        name: "mongodb"
        port: 27017
        targetPort: 27017
    clusterIP: "None"
    selector: 
      app: "mongo-development"

4

1 回答 1

0

问题已解决我定义的挂载路径不正确,它应该是 statefulset.yaml 文件中的 /data/db

于 2021-09-02T14:00:14.817 回答