feat: add easy-dataset and mongodb-replicaset-single

This commit is contained in:
Sun-ZhenXing
2025-10-22 10:05:17 +08:00
parent 37b52545a2
commit 84e8b85990
15 changed files with 736 additions and 39 deletions

View File

@@ -20,24 +20,16 @@ This service sets up a MongoDB replica set with three members.
docker compose up -d
```
2. Connect to the primary node:
The services will automatically initialize the replica set through the `mongo-init` init container. This container:
- Waits for all MongoDB nodes to be healthy
- Connects to the primary node
- Initializes the replica set with internal container names
- Uses container-based networking for communication
2. Verify the replica set status:
```bash
docker exec -it mongodb-replicaset-mongo1-1 mongosh
```
3. Initialize the replica set. **Remember to replace the host IP with your actual host IP.**
```js
config = {
_id: "rs0",
members: [
{_id: 0, host: "192.168.31.38:27017"},
{_id: 1, host: "192.168.31.38:27018"},
{_id: 2, host: "192.168.31.38:27019"},
]
}
rs.initiate(config)
docker exec -it mongodb-replicaset-mongo1-1 mongosh -u root -p password --authenticationDatabase admin --eval "rs.status()"
```
## Services

View File

@@ -20,24 +20,16 @@
docker compose up -d
```
2. 连接到主节点
这些服务将通过 `mongo-init` init 容器自动初始化副本集。该容器会
- 等待所有 MongoDB 节点就绪
- 连接到主节点
- 使用容器名初始化副本集
- 通过容器网络进行通信
2. 验证副本集状态:
```bash
docker exec -it mongodb-replicaset-mongo1-1 mongosh
```
3. 初始化副本集。**请记得将 host IP 替换为你的实际主机 IP。**
```js
config = {
_id: "rs0",
members: [
{_id: 0, host: "192.168.31.38:27017"},
{_id: 1, host: "192.168.31.38:27018"},
{_id: 2, host: "192.168.31.38:27019"},
]
}
rs.initiate(config)
docker exec -it mongodb-replicaset-mongo1-1 mongosh -u root -p password --authenticationDatabase admin --eval "rs.status()"
```
## 服务

View File

@@ -24,6 +24,12 @@ x-mongo: &mongo
chown 999:999 /tmp/rs0.key
export MONGO_INITDB_ROOT_USERNAME MONGO_INITDB_ROOT_PASSWORD MONGO_INITDB_DATABASE
exec docker-entrypoint.sh mongod --replSet ${MONGO_REPLICA_SET_NAME:-rs0} --keyFile /tmp/rs0.key
healthcheck:
test: mongosh --eval "db.adminCommand('ping')"
interval: 5s
timeout: 3s
retries: 10
start_period: 30s
deploy:
resources:
limits:
@@ -38,11 +44,75 @@ services:
<<: *mongo
ports:
- "${MONGO_PORT_OVERRIDE_1:-27017}:27017"
mongo2:
<<: *mongo
ports:
- "${MONGO_PORT_OVERRIDE_2:-27018}:27017"
mongo3:
<<: *mongo
ports:
- "${MONGO_PORT_OVERRIDE_3:-27019}:27017"
mongo-init:
<<: *default
image: mongo:${MONGO_VERSION:-8.0.13}
depends_on:
mongo1:
condition: service_healthy
mongo2:
condition: service_healthy
mongo3:
condition: service_healthy
environment:
TZ: ${TZ:-UTC}
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME:-root}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD:-password}
MONGO_REPLICA_SET_NAME: ${MONGO_REPLICA_SET_NAME:-rs0}
MONGO_PORT_1: ${MONGO_PORT_OVERRIDE_1:-27017}
MONGO_PORT_2: ${MONGO_PORT_OVERRIDE_2:-27018}
MONGO_PORT_3: ${MONGO_PORT_OVERRIDE_3:-27019}
MONGO_HOST: ${MONGO_HOST:-mongo1}
volumes:
- ./secrets/rs0.key:/data/rs0.key:ro
entrypoint:
- bash
- -c
- |
set -e
echo "Waiting for MongoDB nodes to be ready..."
sleep 5
mongosh \
--host "mongodb://$${MONGO_INITDB_ROOT_USERNAME}:$${MONGO_INITDB_ROOT_PASSWORD}@$${MONGO_HOST}:$${MONGO_PORT_1}" \
--authenticationDatabase admin \
--eval "
const config = {
_id: '$${MONGO_REPLICA_SET_NAME}',
members: [
{ _id: 0, host: 'mongo1:27017' },
{ _id: 1, host: 'mongo2:27017' },
{ _id: 2, host: 'mongo3:27017' }
]
};
try {
const result = rs.status();
print('Replica set already initialized');
} catch (e) {
print('Initializing replica set...');
rs.initiate(config);
print('Replica set initialized successfully');
}
"
echo "Init container completed successfully"
deploy:
resources:
limits:
cpus: '0.25'
memory: 256M
reservations:
cpus: '0.10'
memory: 128M