Skip to content

Instantly share code, notes, and snippets.

@manuerumx
Created April 13, 2016 15:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manuerumx/c386dd360f49f9fb1cac04d54a328642 to your computer and use it in GitHub Desktop.
Save manuerumx/c386dd360f49f9fb1cac04d54a328642 to your computer and use it in GitHub Desktop.

MongoDB Replicaset with Vagrant

This will generate a 3 virtual machines running Debian/Jessie 64 bits with MongoDB 3.2

If you want to change the box to use another OS, you should change the script mongoInitScript which install MongoDB and configure it.

Requirements

  • Virtualbox
  • Vagrant

Versions tested

vagrant --version 
Vagrant 1.8.1

VBoxManage --version 
5.0.16r105871

Execute

Run in terminal

vagrant up && sh setup.sh
mongo --host 192.168.42.100 << 'EOF'
config = { _id: "ap-Replica", members:[
{ _id : 0, host : "192.168.42.100:27017"},
{ _id : 1, host : "192.168.42.101:27017"},
{ _id : 2, host : "192.168.42.102:27017"} ]
};
rs.initiate(config);
rs.status();
EOF
# -*- mode: ruby -*-
# vi: set ft=ruby :
$mongoInitScript = <<-"SHELL"
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
echo "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.2 main" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
apt-get update
sudo apt-get install -y mongodb-org iptables
MONGOD_CONF_FILE="/etc/mongod.conf"
tee -a $MONGOD_CONF_FILE <<-"EOF"
replication:
oplogSizeMB: 64
replSetName: ap-Replica
EOF
iptables -I INPUT -p tcp -m state --state NEW -m tcp --dport 27017 -j ACCEPT
iptables-save > /home/vagrant/iptables.rule
perl -pi -e 's/bindIp: 127.0.0.1/#bind_ip=127.0.0.1/g' /etc/mongod.conf
service mongod restart
SHELL
Vagrant.configure(2) do |config|
config.vm.box = "debian/jessie64"
# config.vm.provider "virtualbox" do |v|
# v.customize ["modifyvm", :id, "--cpus", "2"]
# end
config.vm.define :mongo1 do |mongo1|
mongo1.vm.network :private_network, ip: "192.168.42.100"
mongo1.vm.provision "shell", inline: $mongoInitScript
end
config.vm.define :mongo2 do |mongo2|
mongo2.vm.network :private_network, ip: "192.168.42.101"
mongo2.vm.provision "shell", inline: $mongoInitScript
end
config.vm.define :mongo3 do |mongo3|
mongo3.vm.network :private_network, ip: "192.168.42.102"
mongo3.vm.provision "shell", inline: $mongoInitScript
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment