Skip to main content
Discover Packs Jobs Registries Requests Docs About GitHub

lhaig / pack-mongodb

Published by Lance Haig | Raw HCL

job
0 stars
Description
MongoDB replicaset
License
MPL-2.0
Tags
#database #mongo #mongodb
Download Status
Pending (v0.0.2)
Versions
Select a version to see its documentation and run command.
Quick Run (v0.0.2)
ramble job run lhaig/pack-mongodb@v0.0.2
README
# MongoDB Replicaset Nomad Pack Deploy a [MongoDB](https://www.mongodb.com/) replicaset for high availability document storage to HashiCorp Nomad. ## Features - Automatic replicaset initialization via `rs.initiate()` - Nomad native service discovery for member coordination - Keyfile authentication between nodes - Configurable instance count (3, 5, 7 for proper elections) ## Prerequisites 1. **Host volume** configured on Nomad clients: ```hcl client { host_volume "mongodb-data" { path = "/var/lib/mongodb" read_only = false } } ``` 2. **Keyfile** generated for internal authentication: ```bash openssl rand -base64 756 ``` ## Usage ### Basic deployment (3-node replicaset) ```bash nomad-pack run mongodb \ --var root_password="secret" \ --var keyfile_content="$(openssl rand -base64 756)" ``` ### 5-node replicaset ```bash nomad-pack run mongodb \ --var root_password="secret" \ --var keyfile_content="$(openssl rand -base64 756)" \ --var count=5 ``` ### Custom replicaset name ```bash nomad-pack run mongodb \ --var root_password="secret" \ --var keyfile_content="$(openssl rand -base64 756)" \ --var replicaset_name="myapp-rs" ``` ## Connecting ```bash # Connection string format mongodb://admin:<password>@<host1>:<port1>,<host2>:<port2>,<host3>:<port3>/?replicaSet=rs0&authSource=admin # Check replicaset status nomad alloc exec <alloc-id> mongosh --eval "rs.status()" # Connect to primary nomad alloc exec <alloc-id> mongosh --eval "rs.isMaster()" ``` ## Variables | Variable | Description | Default | |----------|-------------|---------| | `job_name` | Override job name | `mongodb` | | `region` | Nomad region | `""` | | `datacenters` | Target datacenters | `["*"]` | | `count` | Number of instances (use odd: 3,5,7) | `3` | | `mongodb_version` | Docker image tag | `8` | | `replicaset_name` | Replicaset name | `rs0` | | `root_username` | Admin username | `admin` | | `root_password` | Admin password | **required** | | `keyfile_content` | Internal auth keyfile | **required** | | `port` | MongoDB port (0=dynamic) | `0` | | `cpu` | CPU allocation (MHz) | `500` | | `memory` | Memory allocation (MB) | `512` | | `register_service` | Register with Nomad SD | `true` | | `service_name` | Service name | `mongodb` | | `service_tags` | Service tags | `["db", "mongodb", "replicaset"]` | ## Architecture ``` ┌─────────────────┐ │ Nomad Service │ │ Discovery │ └────────┬────────┘ │ ┌─────────────────┼─────────────────┐ │ │ │ ▼ ▼ ▼ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ MongoDB 1 │◄──│ MongoDB 2 │──►│ MongoDB 3 │ │ (PRIMARY) │ │ (SECONDARY) │ │ (SECONDARY) │ └─────────────┘ └─────────────┘ └─────────────┘ │ │ │ ▼ ▼ ▼ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │ Host Volume │ │ Host Volume │ │ Host Volume │ │ /data/db │ │ /data/db │ │ /data/db │ └─────────────┘ └─────────────┘ └─────────────┘ ``` One node is elected PRIMARY (handles writes), others are SECONDARY (replicate data, can serve reads). ## Notes - Use odd numbers for count (3, 5, 7) to ensure proper primary election - The keyfile must be identical across all nodes - First deployment may take 30-60 seconds for replicaset initialization - Data is replicated automatically between nodes ## License MPL-2.0