官术网_书友最值得收藏!

Creating a MongoDB ReplicaSet

Follow these steps to create the ReplicaSet:

  1. 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
If you would like to learn more about Docker networks, please refer to the official documentation: https://docs.docker.com/network/.
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.

  1. 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:

  1. 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.

 

  1. In PowerShell, navigate to the location where you downloaded the Dockerfile (using a new, separate directory is recommended).
  2. 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 preceding steps have also been provided as a PowerShell script in this book's GitHub repository: https://github.com/PacktPublishing/Hands-On-Kubernetes-on-Windows/blob/master/Chapter02/02_InitializeMongoDBReplicaSet.ps1.

The next stage is to configure the ReplicaSet. We will do this using the mongo shell. Follow these steps:

  1. 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
  1. 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.

For more details regarding the initialization of ReplicaSets, please refer to the official documentation: https://docs.mongodb.com/manual/reference/method/rs.initiate/.
  1. 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.

主站蜘蛛池模板: 伊通| 衡东县| 大足县| 河间市| 汝阳县| 舞阳县| 上饶县| 大洼县| 固安县| 京山县| 突泉县| 和林格尔县| 曲水县| 永清县| 正定县| 霍州市| 杂多县| 北川| 沾益县| 四会市| 遂溪县| 来安县| 桐乡市| 景宁| 金平| 通山县| 常州市| 榆树市| 遵义市| 长春市| 富源县| 东阳市| 克拉玛依市| 东兰县| 双城市| 江津市| 台湾省| 信丰县| 固始县| 日喀则市| 美姑县|