<p>I am trying to create a mongodb user along with a stateful set. Here is my .yaml file:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
 name: mongo
 labels:
  name: mongo
spec:
 type: NodePort
 ports:
  - port: 27017
    targetPort: 27017
 selector:
  name: mongo
---
apiVersion: v1
kind: Secret
metadata:
  name: admin-secret
  # corresponds to user.spec.passwordSecretKeyRef.name
type: Opaque
stringData:
  password: pass1
  # corresponds to user.spec.passwordSecretKeyRef.key
---
apiVersion: mongodb.com/v1
kind: MongoDBUser
metadata:
  name: admin
spec:
  passwordSecretKeyRef:
    name: admin-secret
    # Match to metadata.name of the User Secret
    key: password
  username: admin
  db: "admin" #
  mongodbResourceRef:
    name: mongo
    # Match to MongoDB resource using authenticaiton
  roles:
    - db: "admin"
      name: "clusterAdmin"
    - db: "admin"
      name: "userAdminAnyDatabase"
    - db: "admin"
      name: "readWrite"
    - db: "admin"
      name: "userAdminAnyDatabase"
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongo
spec:
  serviceName: "mongo"
  replicas: 2
  selector:
    matchLabels:
      name: mongo
  template:
    metadata:
      labels:
        name: mongo
    spec:
      terminationGracePeriodSeconds: 10
      containers:
      # - envFrom:
      #   - secretRef:
      #       name: mongo-secret
      - image: mongo
        name: mongodb
        command:
          - mongod
          - "--replSet"
          - rs0
          - "--bind_ip" 
          - 0.0.0.0
        ports:
        - containerPort: 27017
</code></pre>

<p>Earlier I used the secret to create a mongo user:</p>

<pre><code>...
spec:
  containers:
  - envFrom:
    - secretRef:
      name: mongo-secret
...
</code></pre>

<p>but once I added spec.template.spec.containers.command to the StatefulSet this approach is no longer working. Then I added Secret and MongoDBUser but I started getting this error:</p>

<pre><code>unable to recognize "mongo.yaml": no matches for kind "MongoDBUser" in version "mongodb.com/v1"
</code></pre>

<p>How to automatically create a mongodb user when creating StatefulSet with few replicas in kubernetes?</p>

<p>One of the resources in your yaml file refers to a <code>kind</code> that doesn't exist in your cluster.</p>

<p>You can check this by running the command <code>kubectl api-resources | grep mongo -i</code></p>

<p>Specifically it's the resource of kind <code>MongoDBUser</code>. This API resource type is part of <a href="https://www.mongodb.com/kubernetes" rel="nofollow noreferrer">MongoDB Enterprise Kubernetes Operator</a>. </p>

<p>You haven't indicated whether you are using this in your cluster, but the error you're getting implies the CRD's for the operator are not installed and so cannot be used.</p>

<p>MongoDB Kubernetes Operator is a paid enterprise package for Kubernetes. If you don't have access to this enterprise package from MongoDB you can also install the community edition yourself by either setting up all the resources yourself or using <a href="https://helm.sh/" rel="nofollow noreferrer">Helm</a> to install it as a package. Using Helm makes managing the resources significantly easier, especially with regards to configuration, upgrades, re-installation or unistalling. The existing Helm charts are open source and also allow for running MongDB as a standalone instance, replica set or a sharded cluster.</p>

<p>For reference, Bitnami <a href="https://github.com/bitnami/charts/tree/master/bitnami/mongodb" rel="nofollow noreferrer">provides a MongoDB Standalone or replica set helm chart</a> which seems to be on the latest MongoDB version and is maintained regularly. There is also <a href="https://github.com/helm/charts/tree/master/stable/mongodb-replicaset" rel="nofollow noreferrer">this one</a>, but it's on an older version of MongoDB and doesn't seem to be getting much attention.</p>
