#mongo.yal --- apiVersion:mongodbcommunity.mongodb.com/v1 kind:MongoDBCommunity metadata: name:mongodb spec: members:3 type:ReplicaSet version:"4.2.6" security: authentication: modes: ["SCRAM"] users: -name:root-user db:admin passwordSecretRef:# a reference to the secret that will be used to generate the user's password name:mongo-creds roles: -name:clusterAdmin db:admin -name:userAdminAnyDatabase db:admin scramCredentialsSecretName:my-scram additionalMongodConfig: storage.wiredTiger.engineConfig.journalCompressor:zlib
# the user credentials will be generated from this secret # once the credentials are generated, this secret is no longer required --- apiVersion:v1 kind:Secret metadata: name:mongo-creds type:Opaque stringData: password:rtfTTUIgu7890ggt#ioo123
1 2 3
kubectl apply -f mongo.yaml --namespace $KUBE_NAMESPACE kubectl get mdbc --namespace $KUBE_NAMESPACE kubectl get pods -n $KUBE_NAMESPACE
Output:
1 2 3 4 5 6 7
NAME PHASE VERSION mongodb Running 4.2.6 NAME READY STATUS RESTARTS AGE mongodb-0 2/2 Running 0 6m29s mongodb-1 2/2 Running 0 87s mongodb-2 2/2 Running 0 46s mongodb-kubernetes-operator-6465d9cd7-qpkzs 1/1 Running 0 7m3s
Create a new user on the primary replica
Create a new user to avoid the following error MongoDB - admin user not authorized
ssh into the 3 MongoDB replicas
1 2 3
kubectl exec -it mongodb-0 --namespace $KUBE_NAMESPACE -- sh kubectl exec -it mongodb-1 --namespace $KUBE_NAMESPACE -- sh kubectl exec -it mongodb-1 --namespace $KUBE_NAMESPACE -- sh
When the # prompt appears, type: mongo
Look out for the primary replica because this is the only member in the replica set that receives write operations.
1 2 3 4 5 6
MongoDB shell version v4.2.6 connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("ccdd585d-2212-451c-b58f-9a42b4a52894") } MongoDB server version: 4.2.6 ........ mongodb:PRIMARY>
I will terminate other 2 sessions and use kubectl exec -it mongodb-1 --namespace $KUBE_NAMESPACE -- sh, that is the primary replica
initiate the replica set with rs.initiate()
Switch to the test database: use admin
Authenticate with the username and password specified in mongo.yaml file : db.auth('root-user','rtfTTUIgu7890ggt#ioo123')
Create a new user : db.createUser({ user:"oluchi", pwd: "oluchitest", roles: [{role: "readWrite", db: "admin"}] })