Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[das#91] Evaluate the impact of sharding in MongoDB indexes #16

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
76442b8
feat: add deb package server script
levisingularity Aug 20, 2024
fbc5933
feat: add standalone server script
levisingularity Aug 20, 2024
7798592
feat: remove unused script
levisingularity Aug 20, 2024
53b9d2f
feat: template path
levisingularity Aug 20, 2024
8d30be5
cluster module
levisingularity Aug 22, 2024
ea3ba39
config set
levisingularity Aug 23, 2024
cd37cee
fix single quotes
levisingularity Aug 23, 2024
ae58b53
set cluster
levisingularity Aug 23, 2024
73496fe
fix name
levisingularity Aug 23, 2024
1da201c
change cluster
levisingularity Aug 27, 2024
3f8db8d
init replica
levisingularity Aug 29, 2024
da8fb71
gen logs
levisingularity Aug 29, 2024
2bf0201
config shard
levisingularity Aug 29, 2024
35ac23a
fix special caracters
levisingularity Aug 29, 2024
c65b6a8
Fix config repl
levisingularity Aug 29, 2024
ee253be
create shard
levisingularity Aug 30, 2024
eec13e2
remove init replica
levisingularity Aug 31, 2024
a0ffc7a
add init config set script
levisingularity Aug 31, 2024
013cf43
init config shard
levisingularity Aug 31, 2024
e10fe7b
update main references
levisingularity Aug 31, 2024
6f0ef50
fix: config-set and init-config-set scripts
levisingularity Sep 2, 2024
6ef727c
fix shard scripts
levisingularity Sep 2, 2024
5668571
config mongos
levisingularity Sep 3, 2024
56e800d
start and set mongos
levisingularity Sep 3, 2024
fee44d0
create ssh key to access instances
levisingularity Sep 3, 2024
ebd5952
fix dependencies and use variables as config
levisingularity Sep 3, 2024
6483d1b
refactor cluster config
levisingularity Sep 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ VULTR_SSH_PRIVATE_KEY
VULTR_SSH_PUBLIC_KEY
vultr_*.json
tfplan-destroy
*.pem
28 changes: 26 additions & 2 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ locals {
}
}
data "template_file" "mongodb_user_data" {
template = file("install-server.sh")
template = file("user-data/default-server.sh")

vars = merge(local.instance_user_data, {
environment_type = "toolbox"
Expand All @@ -69,8 +69,32 @@ module "das_apt_repository" {
create_resource = true
name = "das-apt-repository"
environment = local.environment
user_data_file = file("install-apt-repository.sh")
user_data_file = file("user-data/deb-package-server.sh")
ssh_key_ids = var.ssh_key_ids
region = var.region
plan = "vc2-1c-2gb"
}

module "mongodb_shard" {
source = "./mongodb-shards"
region = var.region
environment = local.environment

shard = {
clusters = 1
nodes_per_cluster = 2,
instance_type = "vc2-1c-2gb"
}

config_set = {
clusters = 1
nodes_per_cluster = 2,
instance_type = "vc2-1c-2gb"
}

mongos = {
nodes = 1
instance_type = "vc2-1c-2gb"
}

}
50 changes: 50 additions & 0 deletions mongodb-shards/config-set.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module "mongodb_cluster_config_set" {
source = "../instance"
create_resource = true
count = var.config_set.clusters * var.config_set.nodes_per_cluster
name = "configrepl${count.index}-${random_string.random[count.index].id}"
environment = var.environment
user_data_file = file("mongodb-shards/user-data/config-set.sh")
ssh_key_ids = [vultr_ssh_key.mongodb_ssh_key.id]
region = var.region
plan = var.config_set.instance_type
}

resource "null_resource" "mongo_init_config_set" {
depends_on = [
module.mongodb_cluster_config_set
]

count = var.config_set.clusters
triggers = {
cluster_instance_ids = join(",", module.mongodb_cluster_config_set[*].instance_ip)
}

provisioner "file" {
source = "mongodb-shards/user-data/init-config-set.sh"
destination = "/tmp/bootstrap-cluster.sh"


connection {
type = "ssh"
user = "root"
private_key = file(local_file.mongodb_private_key.filename)
host = slice(module.mongodb_cluster_config_set[*].instance_ip, count.index * var.config_set.nodes_per_cluster, (count.index + 1) * var.config_set.nodes_per_cluster)[0]
}
}

provisioner "remote-exec" {
inline = [
"chmod +x /tmp/bootstrap-cluster.sh",
"/tmp/bootstrap-cluster.sh ${join(" ",
slice(module.mongodb_cluster_config_set[*].instance_ip, count.index * var.config_set.nodes_per_cluster, (count.index + 1) * var.config_set.nodes_per_cluster))}"
]

connection {
type = "ssh"
user = "root"
private_key = file(local_file.mongodb_private_key.filename)
host = slice(module.mongodb_cluster_config_set[*].instance_ip, count.index * var.config_set.nodes_per_cluster, (count.index + 1) * var.config_set.nodes_per_cluster)[0]
}
}
}
62 changes: 62 additions & 0 deletions mongodb-shards/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
variable "region" {
type = string
}

variable "environment" {
type = string
}

variable "shard" {
type = object({
clusters = number
nodes_per_cluster = number
instance_type = string
})
}


variable "config_set" {
type = object({
clusters = number
nodes_per_cluster = number
instance_type = string
})
}

variable "mongos" {
type = object({
nodes = number
instance_type = string
})
}

terraform {
required_providers {
vultr = {
source = "vultr/vultr"
version = "2.15.1"
}
}
}

resource "random_string" "random" {
length = 16
special = false
count = var.config_set.clusters * var.config_set.nodes_per_cluster
}

resource "tls_private_key" "mongodb_ssh_key" {
algorithm = "RSA"
rsa_bits = 4096
}

resource "vultr_ssh_key" "mongodb_ssh_key" {
name = "mongodb-shard-${formatdate("YYYYMMDDhhmmss", timestamp())}"
ssh_key = tls_private_key.mongodb_ssh_key.public_key_openssh
}

resource "local_file" "mongodb_private_key" {
content = tls_private_key.mongodb_ssh_key.private_key_pem
filename = "${vultr_ssh_key.mongodb_ssh_key.name}.pem"
}

57 changes: 57 additions & 0 deletions mongodb-shards/mongos.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module "mongodb_cluster_mongos" {
source = "../instance"
create_resource = true
count = var.mongos.nodes
name = "mongos${count.index}-${random_string.random[count.index].id}"
environment = var.environment
user_data_file = file("mongodb-shards/user-data/config-mongos.sh")
ssh_key_ids = [vultr_ssh_key.mongodb_ssh_key.id]
region = var.region
plan = var.mongos.instance_type

depends_on = [
module.mongodb_cluster_config_set,
module.mongodb_cluster_shard,
null_resource.mongo_init_config_set,
null_resource.mongo_init_shard,
]
}


resource "null_resource" "mongo_init_mongos" {
depends_on = [
module.mongodb_cluster_mongos
]

count = var.mongos.nodes
triggers = {
cluster_instance_ids = join(",", module.mongodb_cluster_mongos[*].instance_ip)
}

provisioner "file" {
source = "mongodb-shards/user-data/init-mongos.sh"
destination = "/tmp/bootstrap-cluster.sh"


connection {
type = "ssh"
user = "root"
private_key = file(local_file.mongodb_private_key.filename)
host = module.mongodb_cluster_mongos[count.index].instance_ip
}
}

provisioner "remote-exec" {
inline = [
"chmod +x /tmp/bootstrap-cluster.sh",
"/tmp/bootstrap-cluster.sh --config-set ${join(",", module.mongodb_cluster_config_set[*].instance_ip)} --shards ${join(",", module.mongodb_cluster_shard[*].instance_ip)} --shard-clusters ${var.config_set.clusters}"
]

connection {
type = "ssh"
user = "root"
private_key = file(local_file.mongodb_private_key.filename)
host = module.mongodb_cluster_mongos[count.index].instance_ip
}
}
}
55 changes: 55 additions & 0 deletions mongodb-shards/shard.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module "mongodb_cluster_shard" {
source = "../instance"
create_resource = true
count = var.shard.nodes_per_cluster * var.shard.clusters
name = "shard${count.index % var.shard.nodes_per_cluster == 0 ? var.shard.nodes_per_cluster : count.index % var.shard.nodes_per_cluster}node${count.index}-${random_string.random[count.index].id}"
environment = var.environment
user_data_file = file("mongodb-shards/user-data/config-shard.sh")
ssh_key_ids = [vultr_ssh_key.mongodb_ssh_key.id]
region = var.region
plan = var.shard.instance_type

depends_on = [
module.mongodb_cluster_config_set,
null_resource.mongo_init_config_set
]
}

resource "null_resource" "mongo_init_shard" {
depends_on = [
module.mongodb_cluster_shard
]

count = var.shard.clusters
triggers = {
cluster_instance_ids = join(",", module.mongodb_cluster_shard[*].instance_ip)
}

provisioner "file" {
source = "mongodb-shards/user-data/init-shard-set.sh"
destination = "/tmp/bootstrap-cluster.sh"


connection {
type = "ssh"
user = "root"
private_key = file(local_file.mongodb_private_key.filename)
host = slice(module.mongodb_cluster_shard[*].instance_ip, count.index * var.shard.nodes_per_cluster, (count.index + 1) * var.shard.nodes_per_cluster)[0]
}
}

provisioner "remote-exec" {
inline = [
"chmod +x /tmp/bootstrap-cluster.sh", var.shard.clusters,
"/tmp/bootstrap-cluster.sh ${join(" ",
slice(module.mongodb_cluster_shard[*].instance_ip, count.index * var.shard.nodes_per_cluster, (count.index + 1) * var.shard.nodes_per_cluster))}"
]

connection {
type = "ssh"
user = "root"
private_key = file(local_file.mongodb_private_key.filename)
host = slice(module.mongodb_cluster_shard[*].instance_ip, count.index * var.shard.nodes_per_cluster, (count.index + 1) * var.shard.nodes_per_cluster)[0]
}
}
}
64 changes: 64 additions & 0 deletions mongodb-shards/user-data/config-mongos.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#!/bin/bash

# Global Variables
PID_FILE="/run/terraform-mongos-manager.pid"
CURRENT_SCRIPT_LOGS="/tmp/mongos-setup.log"

exec >$CURRENT_SCRIPT_LOGS 2>&1

function raise_command_not_found() {
local cmd="$1"

if ! command -v "$cmd" &> /dev/null; then
echo "$cmd is required"
exit 1
fi
}

function enable_firewall() {
if ! command -v ufw &>/dev/null; then
apt-get update
apt-get install ufw
fi

ufw enable
ufw allow ssh
ufw allow 28041
}

function cleanup() {
if [ -f "$PID_FILE" ]; then
rm -f "$PID_FILE"
fi
}

function create_pid_file() {
if [ -f "$PID_FILE" ]; then
echo "Script is already running with PID $(cat "$PID_FILE"). Waiting process to end..."
exit 1
fi

echo $$ > "$PID_FILE"
trap cleanup EXIT
}

function install_mongodb() {
if command -v mongod; then
echo "Skipping mongodb installation because it's already installed."
return 0
fi

wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
apt update
apt install -y mongodb-org
systemctl start mongod
systemctl enable mongod
systemctl status mongod

raise_command_not_found "mongod"
}

create_pid_file
install_mongodb
enable_firewall
Loading