Initialise and Populate MongoDB ReplicaSet
Start course
1h 42m

OpenShift is a rock solid platform engineered for the enterprise. It's built on top of Kubernetes and provides many value add features, tools, and services which help to streamline the complete end-to-end container development and deployment lifecycle.  

This introductory level training course is designed to bring you quickly up to speed with the key features that OpenShift provides. You'll then get to observe first hand how to launch a new OpenShift Container Platform 4.2 cluster on AWS and then deploy a real world cloud native application into it.



We’d love to get your feedback on this course, so please give it a rating when you’re finished. If you have any queries or suggestions, please contact us at

Learning Objectives

By completing this course, you will:

  • Learn and understand what OpenShift is and what it brings to the table
  • Learn and understand how to provision a brand new OpenShift 4.2 cluster on AWS
  • Learn and understand the basic principles of deploying a cloud native application into OpenShift
  • Understand how to work with and configure many of the key OpenShift value add cluster resources
  • Learn how to work with the OpenShift web administration console to manage and administer OpenShift deployments
  • Learn how to work with the oc command line tool to manage and administer OpenShift deployments
  • And finally, you’ll learn how to manage deployments and OpenShift resources through their full lifecycle

Intended Audience

This course is intended for:

  • Anyone interested in learning OpenShift
  • Software Developers interested in OpenShift containerisation, orchestration, and scheduling
  • DevOps practitioners looking to learn how to provision and manage and maintain applications on OpenShift


To get the most from this course, you should have at least:

  • A basic understanding of containers and containerisation
  • A basic understanding of Kubernetes - and container orchestration and scheduling
  • A basic understanding of software development and the software development life cycle
  • A basic understanding of networks and networking

Source Code

This course references the following CloudAcademy GitHub hosted repos:



- Okay, welcome back, in this demo, I'm going to show you how to initialize the MongoDB replicaset and then populate it with data. Establishing and configuring a replicaset gives us a layer of data redundancy which is always helpful in production systems since each and every data write to the MongoDB primary will then be replicated to both MongoDB secondaries within the replicaset. Okay, let's begin, let's jump back into the runsheet and this time copy the first code block from Step 13. I'll use this to write out a configuration file into our local directory. 

A couple things to highlight before we execute it. Firstly, we are configuring the replicaset to use the stable DNS network names that I highlighted and walked you through in the previous demonstration. This is a key requirement to allow the underlying pods to float across the cluster worker nodes should the cluster orchestrator decide to reschedule and relaunch them on differing nodes. The replicaset configuration has a 5 second pause included to ensure that MongoDB has enough time to perform the replicaset management before we go and attempt to use it. This is merely a precaution to enough time to the MongoDB Replicaset to reorganize itself.

Okay, we are ready to execute it now. I'll jump back into the runsheet and copy the following command which will establish a remote shell connection to the first Mongo pod and execute the Mongo client on it passing in the contents of the db.init file. Ignoring the warning about not being able to save history, everything looks good and the MongoDB replicaset has been established successfully. Let's confirm this by running the following command rs.status *​ Here we can see that Mongo-1 and Mongo-2 have been registered as the secondaries, and Mongo-0 has been registered as the primary. 

This looks good and the database is ready to be populated. Let's do this now, jumping back into the runsheet, copy the database population script and run it within the terminal. Again, this will create a new script file on the file system in the current local directory. I'll now copy and run the next command which will actually connect to the MongoDB database and populate it using the same technique as used to configure the replicaset. Okay, finally execute the last command within Step 14 to test that indeed we have successfully populated it with documents one per programming language with their respective votes counterset to zero. Brilliant, that looks good. 

Okay, that completes Steps 13 and 14. We've now successfully populated our MongoDB database and we are all good to go. That concludes this demo We're now ready to setup and deploy the API which will connect against our MongoDB database and perform read and writes against it.

About the Author
Learning Paths

Jeremy is a Content Lead Architect and DevOps SME here at Cloud Academy where he specializes in developing DevOps technical training documentation.

He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 25+ years. In recent times, Jeremy has been focused on DevOps, Cloud (AWS, Azure, GCP), Security, Kubernetes, and Machine Learning.

Jeremy holds professional certifications for AWS, Azure, GCP, Terraform, Kubernetes (CKA, CKAD, CKS).