Error: error parsing mongodb-deployment.yaml: error converting YAML to JSON: yaml: line 27: did not find expected key

I am doing a lab about kubernetes Can you help me?

I have create the YAML file, but when I am trying to deploy it a shell shows me this error:

> error: error parsing mongodb-deployment.yaml: error converting YAML to JSON: yaml: line 27: did not find expected key

This is the yaml file for MongoDB for Kubernetes

apiVersion: apps/v1
kind: Deployment
metadata:
 name: mongodb-deployment
 labels:
   app: mongodb
spec:
  replicas: 1
  selector:
   matchLabels:
     app: mongodb
   template:
     metadata:
       labels:
         app: mongodb
     spec:
       containers:
       - name: mongodb
         image: mongo
         ports:
         - containerPort: 27017
         env:
         - name: MONGO_INITDB_ROOT_USERNAME
           valueFROM:
             secretKeyRef:
                name: mongodb-secret
                key: mongo-root-username
         - name: MONGO_INITDB_ROOT_PASSWORD
           valueFROM:
             secretKeyRef: 
                name: mongodb-secret
                key: mongo-root-password

valueFROM should be valueFrom and the tabbings is wrong all over the place. YAML is really picky about how far things get indented.

The reason I could spot this is because of the lines I see in vscode:

Here’s a corrected version:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongodb-deployment
  labels:
    app: mongodb
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mongodb
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      containers:
      - name: mongodb
        image: mongo
        ports:
        - containerPort: 27017
        env:
        - name: MONGO_INITDB_ROOT_USERNAME
          valueFrom:
            secretKeyRef:
               name: mongodb-secret
               key: mongo-root-username
        - name: MONGO_INITDB_ROOT_PASSWORD
          valueFrom:
            secretKeyRef: 
               name: mongodb-secret
               key: mongo-root-password

thank you very much!!!

Even I’m getting error in this yaml file
" error: error parsing deployment.yaml: error converting YAML to JSON: yaml: line 8: did not find expected key"
I have created yaml file for demokube in kubernates

apiVersion: apps/v1
kind: Deployment
metadata:
name: demokube
spec:
selector:
matchLabels:
app: demokube
replicas: 2
template:
metadata:
labels:
app: demokube
spec:
containers:
- name: demosupcrregistry
image: Azure Container Registry | Microsoft Azure
imagePullPolicy: IfNotPresent
ports:
- containerPort: 7000

apiVersion: v1
kind: Service
metadata:
name: demokube
spec:
selector:
app: demokube
ports:
- protocol: “TCP”
port: 7000
targetPort: 7000
name: http
type: LoadBalancer

Hi, @Supriya_Singh!

I have the same issue did you manage to find a solution?

Thank you for the solution. But why is it not working when the key is “mongo-user” .