<p>I'm trying to set up mongodb locally in a container using minikube, following this example repository here:
<a href="https://github.com/pkdone/minikube-mongodb-demo" rel="nofollow noreferrer">https://github.com/pkdone/minikube-mongodb-demo</a></p>

<p>I get the error:</p>

<pre><code>The StatefulSet "mongod" is invalid:
* spec.selector: Required value
* spec.template.metadata.labels: Invalid value: map[string]string{"environment":"test", "replicaset":"MainRepSet", "role":"mongo"}: `selector` does not match template `labels`
</code></pre>

<p>Here is my full yaml file:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: mongodb-service
  labels:
    name: mongo
spec:
  ports:
  - port: 27017
    targetPort: 27017
  clusterIP: None
  selector:
    role: mongo
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongod
spec:
  serviceName: mongodb-service
  replicas: 3
  template:
    metadata:
      labels:
        role: mongo
        environment: test
        replicaset: MainRepSet
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: replicaset
                  operator: In
                  values:
                  - MainRepSet
              topologyKey: kubernetes.io/hostname
      terminationGracePeriodSeconds: 10
      volumes:
        - name: secrets-volume
          secret:
            secretName: shared-bootstrap-data
            defaultMode: 256
      containers:
        - name: mongod-container
          #image: pkdone/mongo-ent:3.4
          image: mongo
          command:
            - "numactl"
            - "--interleave=all"
            - "mongod"
            - "--wiredTigerCacheSizeGB"
            - "0.1"
            - "--bind_ip"
            - "0.0.0.0"
            - "--replSet"
            - "MainRepSet"
            - "--auth"
            - "--clusterAuthMode"
            - "keyFile"
            - "--keyFile"
            - "/etc/secrets-volume/internal-auth-mongodb-keyfile"
            - "--setParameter"
            - "authenticationMechanisms=SCRAM-SHA-1"
          resources:
            requests:
              cpu: 0.2
              memory: 200Mi
          ports:
            - containerPort: 27017
          volumeMounts:
            - name: secrets-volume
              readOnly: true
              mountPath: /etc/secrets-volume
            - name: mongodb-persistent-storage-claim
              mountPath: /data/db
  volumeClaimTemplates:
  - metadata:
      name: mongodb-persistent-storage-claim
      annotations:
        volume.beta.kubernetes.io/storage-class: "standard"
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 1Gi
</code></pre>

<p>The <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.16/#statefulset-v1-apps" rel="nofollow noreferrer"><code>spec.selector</code></a> field is missing on your Statefulset template.</p>

<blockquote>
  <p>selector is a label query over pods that should match the replica
  count. <strong>It must match the pod template's labels</strong>. More info:
  <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors</a></p>
</blockquote>

<p>You need to add the <code>spec.selector</code> to match one or more labels. Example:</p>

<pre><code>apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongod
spec:
  serviceName: mongodb-service
  replicas: 3
  selector:
    matchLabels:
      role: mongo
  template:
    metadata:
      labels:
        role: mongo
        environment: test
        replicaset: MainRepSet
...
</code></pre>

<hr>

<p>The example that you are using is probably outdated. The <a href="https://kubernetes.io/docs/reference/generated/kubernetes-api/v1.14/#statefulset-v1beta1-apps" rel="nofollow noreferrer">former <code>apps/v1beta1</code></a> added a default value for <code>spec.selector</code> if empty, which is not the case anymore.</p>

<blockquote>
  <p>selector is a label query over pods that should match the replica
  count. <strong>If empty, defaulted to labels on the pod template</strong>. More info:
  <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors" rel="nofollow noreferrer">https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/#label-selectors</a></p>
</blockquote>

<p>In <code>apps/v1beta2</code> and  <code>apps/v1</code> it should be explicitly set.</p>
