I'm trying to deploy mongodb on Kubernetes but I'm getting the error:
error parsing stateful.yaml: error converting YAML to JSON: yaml: line 35: mapping values are not allowed in this context
the line 35 is the container name: - name: uat-mongo-primary
I get this error when i try to create the pods but if i comment the lines :
mongo --eval rs.initiate({_id: "rs0", version: 1, members: [{ _id: 0, host : "uat-mongo-primary-rc-0:27017" }]}); 53 mongo --eval "db.getSiblingDB('admin').createUser({user : \"$MONGO_USER\", pwd : \"$MONGO_PASSWORD\", roles: [ { role: 'root', db: 'admin' } ] })";
the pods being create normally but i need to initiate the cluster also create an user and password.
this is the full yaml file I'm using, i will appreciate any help:
apiVersion: v1
kind: Service
metadata:
name: uat-mongo-primary
labels:
name: uat-mongo-primary
spec:
type: NodePort
ports:
- port: 27017
targetPort: 27017
protocol: TCP
name: uat-mongo-primary
selector:
name: uat-mongo-primary
---
apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
name: uat-mongo-primary-rc
labels:
name: uat-mongo-primary-rc
spec:
serviceName: uat-mongo-primary
replicas: 1
template:
metadata:
labels:
role: mongo
environment: test
spec:
containers:
- name: uat-mongo-primary
image: mongo
env:
- name: "MONGO_DATA_DIR"
value: "/data/db"
- name: "MONGO_LOG_DIR"
value: "/data/logs"
- name: MONGO_USER
value: "admin"
- name: MONGO_PASSWORD
value: "password"
command: ["/bin/sh", "-c"]
args:
- echo starting;
ulimit -a;
mongod --replSet rs0 --bind_ip_all;
mongo --eval rs.initiate({_id: "rs0", version: 1, members: [{ _id: 0, host : "uat-mongo-primary-rc-0:27017" }]});
mongo --eval "db.getSiblingDB('admin').createUser({user : \"$MONGO_USER\", pwd : \"$MONGO_PASSWORD\", roles: [ { role: 'root', db: 'admin' } ] })";
ports:
- containerPort: 27017
volumes:
- name: uat-mongo-primary-db
persistentVolumeClaim:
claimName: uat-mongo-primary-pvc
Alignment for ports in your yml file is incorrect:
ports: - containerPort: 27017
it must be aligned on same label as image name.
Firstly, there is no need to use double quotes unless you want to force value for "string". There is great explanation in this post
Also, what you are doing, it is not best practice, and hard to manage such long complicated commands in yaml files. I would suggest you to build your own Dockerfile by adding those commands inside Dockerfile and build your own image.
Other option is as you check mongo docker documentation, for initializing new instance
When a container is started for the first time it will execute files with extensions .sh and .js that are found in /docker-entrypoint-initdb.d. Files will be executed in alphabetical order. .js files will be executed by mongo using the database specified by the MONGO_INITDB_DATABASE variable, if it is present, or test otherwise. You may also switch databases within the .js script.
So, you can attach PV to this directory, and add your scripts there, then when container will start, it will run your scripts.
To check that your yaml file renders correctly try any YAML to JSON convertor online tool. So it will indicate which fields are not valid.
It is not possible to use YAML strings containing colons (at least with kubectl). You should convert your YAML to JSON (using this tool for example) and apply that JSON with kubectl.