Hands-On Kubernetes on Windows
上QQ阅读APP看书,第一时间看更新

Writing and reading test data

Follow these steps to write and read test data:

  1. First, in the mongo shell for the ReplicaSet primary node (as a prompt, you will see replSet0:PRIMARY> ), let's add 1,000 sample documents in the demo collection:
for (var i = 1; i <= 1000; i++) {
db.demo.insert( { exampleValue : i } )
}
  1. You can quickly verify the inserted documents by using the find() method on the demo collection:
db.demo.find()
  1. Now, we will create a minimalistic .NET Core 3.0 console application running in a Docker container. This will connect to the ReplicaSet running in our Docker container, query our demo collection, and write a value of exampleValue for each document to standard output.

You can find the source code and Dockerfile for this in this book's GitHub repository: https://github.com/PacktPublishing/Hands-On-Kubernetes-on-Windows/tree/master/Chapter02/04_MongoDB_dotnet.

If, during the execution of this scenario, you experience any instability issues with MongoDB, consider upgrading the mongo-1903 Dockerfile to the latest MongoDB version.

To read our test data, we need to build the application Docker image and create a container that's running in the mongo-cluster network. Perform the following steps to do so:

  1. Clone the repository and navigate to the Chapter02/04_MongoDB_dotnet directory in PowerShell.
  2. Execute docker build in the current directory in order to create the mongo-dotnet-sample Docker image:
docker build -t mongo-dotnet-sample:latest .
  1. Run the sample container. This needs to be connected to the mongo-cluster network:
docker run --isolation=process `
--rm `
--net mongo-cluster `
mongo-dotnet-sample:latest

In the output, you should see an increasing sequence of numbers, which is the values of exampleValue in our test documents:

If you are curious, you can check what the SMB share contains on Azure Portal (https://portal.azure.com/):

Congratulations! You have successfully created and tested a MongoDB ReplicaSet running in a Windows container with Azure Files SMB share being used as a bind mount for storing data. Let's quickly summarize what we have learned in this chapter.