- Hands-On Kubernetes on Windows
- Piotr Tylenda
- 779字
- 2021-06-24 16:54:02
Creating a MongoDB ReplicaSet
Follow these steps to create the ReplicaSet:
- First, let's create a Docker network called mongo-cluster for the new cluster using the docker network create command:
docker network create --driver nat mongo-cluster
For Windows-specific documentation, please go to https://docs.microsoft.com/en-us/virtualization/windowscontainers/container-networking/network-drivers-topologies.
We will use Azure Files SMB share (globally mapped to the G: drive), which we created in the previous section, in order to store MongoDB's state using bind mounts.
- We need to create new directories in our SMB share, two for each MongoDB node:
New-Item -ItemType Directory -Force -Path G:\MongoData1\db
New-Item -ItemType Directory -Force -Path G:\MongoData1\configdb
New-Item -ItemType Directory -Force -Path G:\MongoData2\db
New-Item -ItemType Directory -Force -Path G:\MongoData2\configdb
New-Item -ItemType Directory -Force -Path G:\MongoData3\db
New-Item -ItemType Directory -Force -Path G:\MongoData3\configdb
Currently, the official MongoDB image for Windows only exists in Windows Server Core 1803, which means we would have to use Hyper-V isolation to run such containers on Windows 1903. This means that we can't leverage SMB Global Mappings, so we need to create our own MongoDB image based on Windows Server Core 1903. This will make it possible for us to use process isolation. The image we are going to build is based on the official MongoDB image for the 4.2.0 RC8 version, which can be found here: https://github.com/docker-library/mongo/blob/a3a213fd2b4b2c26c71408761534fc7eaafe517f/4.2-rc/windows/windowsservercore-1803/Dockerfile. To perform the build, follow these steps:
- Download the Dockerfile from this book's GitHub repository: https://github.com/PacktPublishing/Hands-On-Kubernetes-on-Windows/blob/master/Chapter02/03_MongoDB_1903/Dockerfile.
- In PowerShell, navigate to the location where you downloaded the Dockerfile (using a new, separate directory is recommended).
- Execute the docker build command in order to create a custom MongoDB image named mongo-1903 in your local image registry:
docker build -t mongo-1903:latest .
The build process will take a few minutes as MongoDB has to be downloaded and installed in the build container.
This image also exposes the MongoDB data as volumes under C:\data\db and C:\data\configdb inside the container (https://github.com/PacktPublishing/Hands-On-Kubernetes-on-Windows/blob/master/Chapter02/03_MongoDB_1903/Dockerfile#L44). Taking all of these into account, let's create our first MongoDB process-isolated container named mongo-node1, which will be running in the background (using the -d option):
docker run -d `
--isolation=process `
--volume G:\MongoData1\db:C:\data\db `
--volume G:\MongoData1\configdb:C:\data\configdb `
--name mongo-node1 `
--net mongo-cluster `
mongo-1903:latest `
mongod --bind_ip_all --replSet replSet0
When running this container, we are providing a custom command to run the container process, that is, mongod --bind_ip_all --replSet replSet0. The --bind_ip_all argument instructs MongoDB to bind to all the network interfaces that are available in the container. For our use case, the --replSet replSet0 argument ensures that the daemon runs in ReplicaSet mode, expecting to be in a ReplicaSet named replSet0.
After the successful creation of the first node, repeat this process for the next two nodes, changing their name and volume mount points appropriately:
docker run -d `
--isolation=process `
--volume G:\MongoData2\db:C:\data\db `
--volume G:\MongoData2\configdb:C:\data\configdb `
--name mongo-node2 `
--net mongo-cluster `
mongo-1903:latest `
mongod --bind_ip_all --replSet replSet0
docker run -d `
--isolation=process `
--volume G:\MongoData3\db:C:\data\db `
--volume G:\MongoData3\configdb:C:\data\configdb `
--name mongo-node3 `
--net mongo-cluster `
mongo-1903:latest `
mongod --bind_ip_all --replSet replSet0
After the creation process has finished, you can verify that the containers are running properly using the docker ps command:
The next stage is to configure the ReplicaSet. We will do this using the mongo shell. Follow these steps:
- Create an instance of the mongo shell. If you're already running a MongoDB container (for example,mongo-node1), the easiest way to do this is to exec into an existing container and run themongo process:
docker exec -it mongo-node1 mongo
- After a few seconds, you should see the mongo shell console prompt, >. You can initialize the ReplicaSet using the rs.initiate() method:
rs.initiate(
{
"_id" : "replSet0",
"members" : [
{ "_id" : 0, "host" : "mongo-node1:27017" },
{ "_id" : 1, "host" : "mongo-node2:27017" },
{ "_id" : 2, "host" : "mongo-node3:27017" }
]
}
)
The preceding command creates a ReplicaSet called replSet0 using our three nodes. These can be identified by their DNS names in the mongo-cluster Docker network.
- You can also verify the state of initialization using the rs.status() command in the mongo shell. After a short time, when the ReplicaSet is fully initialized, in the command JSON output, you should be able to see the ReplicaSet in one node with "stateStr": "PRIMARY" and two other nodes with "stateStr": "SECONDARY" in the command's output.
In the next subsection, we will quickly verify our ReplicaSet by generating test data and reading it in another container.
- Advanced Quantitative Finance with C++
- 潮流:UI設計必修課
- Computer Vision for the Web
- 小創客玩轉圖形化編程
- Functional Programming in JavaScript
- VMware虛擬化技術
- 青少年學Python(第1冊)
- Python Data Science Cookbook
- Scala編程(第5版)
- QPanda量子計算編程
- 人人都能開發RPA機器人:UiPath從入門到實戰
- Mastering ASP.NET Core 2.0
- Learning iOS Penetration Testing
- Python人工智能項目實戰
- Linux Networking Cookbook