Search code examples
mongodbkubernetesamazon-eksamazon-efs

deploy MongoDB in EKS/EFS with Mongo Operator


I'm trying to deploy mongodb with the kubernetes operator on AWS EKS with EFS for the storage class. Just following the documentation examples here:

https://github.com/mongodb/mongodb-kubernetes-operator

I don't understand how to define the PVC naming properly. I've looked through the Github issues and Stack Overflow. Just not finding the example to resolve what seems to be a simple issue.


apiVersion: mongodb.com/v1
kind: MongoDB
metadata:
  name: mongodb
spec:
  members: 1
  type: ReplicaSet
  version: "4.2.6"
  security:
    authentication:
      modes: ["SCRAM"]
  users:
    - name: my-user
      db: admin
      passwordSecretRef: # a reference to the secret that will be used to generate the user's password
        name: my-user-password
      roles:
        - name: clusterAdmin
          db: admin
        - name: userAdminAnyDatabase
          db: admin
      scramCredentialsSecretName: my-scram
  statefulSet:
    spec:
     volumeClaimTemplates:
       - metadata:
         name: data-volume
         spec:
          accessModes: [ "ReadWriteOnce" ]
          resources:
            requests:
              storage: 1Gi
            storageClassName: "efs-sc"

Events:

create Pod mongodb-0 in StatefulSet mongodb failed error: failed to create PVC -mongodb-0: PersistentVolumeClaim "-mongodb-0" is invalid: metadata.name: Invalid value: "-mongodb-0": a DNS-1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*') 

Solution

  • I replicated this error and I found an error in your yaml file. There is a missing indentation in the voluemClaimTemplates name.

    This is your yaml file:

         volumeClaimTemplates:
           - metadata:
        👉   name: data-volume
             spec:
              accessModes: [ "ReadWriteOnce" ]
              resources:
                requests:
                  storage: 1Gi
                storageClassName: "efs-sc"
    

    And this is the correct part with fixed indentation:

         volumeClaimTemplates:
           - metadata:
        👉     name: data-volume
             spec:
              accessModes: [ "ReadWriteOnce" ]
              resources:
                requests:
                  storage: 1Gi
                storageClassName: "efs-sc"
    

    It appears that because of this error the operator could not get the name correctly and he was trying to create volumes with its default template. You can verify that yourself with kubectl get sts mongodb -o yaml:

      volumeClaimTemplates:
      - apiVersion: v1
        kind: PersistentVolumeClaim
        metadata:
        spec:
          accessModes:
          - ReadWriteOnce
          resources:
            requests:
              storage: 1Gi
          volumeMode: Filesystem
      - apiVersion: v1
        kind: PersistentVolumeClaim
        metadata:
          name: data-volume
        spec:
          accessModes:
          - ReadWriteOnce
          resources:
            requests:
              storage: 10G
          volumeMode: Filesystem