mongodb cleanup iptables

pull/63/head
bennojoy 11 years ago
parent bd1b4f0ca3
commit 9afa3c5a22
  1. 73
      mongodb/README.md
  2. 10
      mongodb/group_vars/all
  3. 26
      mongodb/hosts
  4. 2
      mongodb/playbooks/addnode.yml
  5. 5
      mongodb/playbooks/common.yml
  6. 2
      mongodb/playbooks/mongod.yml
  7. 18
      mongodb/roles/common/tasks/main.yml
  8. 11
      mongodb/roles/mongoc/tasks/main.yml
  9. 5
      mongodb/roles/mongod/handlers/main.yml
  10. 6
      mongodb/roles/mongod/tasks/addshard.yml
  11. 16
      mongodb/roles/mongod/tasks/main.yml
  12. 62
      mongodb/roles/mongod/templates/mongod.conf.j2
  13. 2
      mongodb/roles/mongod/templates/repset_init.j2
  14. 2
      mongodb/roles/mongod/templates/shard_init.j2
  15. 8
      mongodb/roles/mongos/tasks/main.yml

@ -25,13 +25,20 @@ The way Ansible configures the three nodes is as follows:
6) All the processes, mongod,mogos are secured using the keyfiles.
####Once the cluster is deployed, if we want to scale the cluster, Ansible configures it as follows:
Once the cluster is deployed, if we want to scale the cluster, Ansible configures it as follows:
1) Install the MongoDB application on the new node.
2) Configure the replication set with primary as the new node and the secondaries as listed in the inventory file [replicationservers]. ( don't forget to add the new node also in the replicationservers section]
3) Adds the new shard to the mongos service pointing to the new replication set.
3) Adds a new shard to the mongos service pointing to the new replication set.
#### Pre-Requesites
1) Update the group_vars/all file which contains site specific parmaters, especially the section which contains the mapping of the hostname's and the ports that it should use for the mongod process. Please do make sure the ansible hostname matches the same. Also dont forget to add the variable when adding a new node.
2) The default directory for storing data is /data, please do change it if requried, also make sure it has sufficient space 10G recommended.
###The following example deploys a three node MongoDB Cluster
@ -41,19 +48,19 @@ The inventory file looks as follows:
[mongoservers]
mongo1
mongo2
mongo3
mongo3
#The list of servers where replication should happen, by default include all servers
#The list of servers where replication should happen, including the master server.
[replicationservers]
mongo1
mongo2
mongo3
mongo3
#The list of mongodb configuration servers, make sure it is 1 or 3
[mongocservers]
mongo1
mongo2
mongo3
mongo3
#The list of servers where mongos servers would run.
[mongosservers]
@ -115,53 +122,17 @@ and issue the following command to get the status of the Shards.
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
We can also make sure the Sharding works by creating a database and collection and populate it with documents and check if the chunks of the collection are balanced equally across nodes.
The above mentioned steps can be tested as follows:
###We can also make sure the Sharding works by creating a database,collection and populate it with documents and check if the chunks of the collection are balanced equally across nodes.
1) Once the Sharded cluster is ready, create a new database and an admin user for the database. This is done from the mongos machine.
The above mentioned steps can be tested with an automated playbook.
/usr/bin/mongo localhost:8888/admin -u admin -p 123456
mongos> use test
switched to db test
Issue the following command to run the test. In variable passed make sure the servername is one of any mongos server.
mongos> db.addUser('admin','123456')
{
"user" : "admin",
"readOnly" : false,
"pwd" : "95ec4261124ba5951720b199908d892b",
"_id" : ObjectId("51519f349cd3a93ca7e17909")
}
2) Once the DB and the user is created, create a collection and poplulate documents, This deployment add a script to the /tmp location of the mongos server which adds a new collection and 100,000 documents.
ansible-playbook -i hosts playbooks/testsharding.yml -e servername=mongos
$/usr/bin/mongo localhost:8888/test -u admin -p 123456 /tmp/testsharding.js
3) After the document's are populated, we have to enable sharding on the database and the collection. which can be done as follows:
$/usr/bin/mongo localhost:8888/admin -u admin -p 123456
mongos> db.runCommand( { enableSharding : "test" } )
{ "ok" : 1 }
mongos> db.runCommand( { shardCollection : "test.test_collection", key : {"number":1} })
{ "collectionsharded" : "test.test_collection", "ok" : 1 }
mongos> sh.status()
--- Sharding Status ---
sharding version: { "_id" : 1, "version" : 3 }
shards:
{ "_id" : "bensible", "host" : "bensible/bensible:20103,web2:20103,web3:20103" }
{ "_id" : "web2", "host" : "web2/bensible:20102,web2:20102,web3:20102" }
{ "_id" : "web3", "host" : "web3/bensible:20101,web2:20101,web3:20101" }
databases:
{ "_id" : "admin", "partitioned" : false, "primary" : "config" }
{ "_id" : "test", "partitioned" : true, "primary" : "bensible" }
test.test_collection chunks:
web2 1
bensible 19
4) In the above example we can see the chunks being balanced across nodes. After a few minutes if we excute the same command 'sh.status()'
we will see the below output, which shows all the chunks being balanced across the three nodes.
Once the playbook completes, we check if the shadring has succeded by logging on to any mongos server and issuing the following command. The output display the number of chunks spread across the shards.
mongos> sh.status()
--- Sharding Status ---
@ -191,20 +162,20 @@ To add a new node to the configured MongoDb Cluster, setup the inventory file as
[mongoservers]
mongo1
mongo2
mongo3
mongo3
mongo4
#The list of servers where replication should happen, by default include all servers
#The list of servers where replication should happen, make sure the new node is listed here.
[replicationservers]
mongo4
mongo1
mongo2
mongo2
#The list of mongodb configuration servers, make sure it is 1 or 3
[mongocservers]
mongo1
mongo2
mongo3
mongo3
#The list of servers where mongos servers would run.
[mongosservers]

@ -9,9 +9,6 @@ mongos_port: 8888
#The port for mongo config server
mongoc_port: 7777
#The port prefix for mongod servers, the latter part is appended the playbook (the last octect of the ipaddress)
mongodb_port_prefix: 20
#The directory prefix where the database files would be stored
mongodb_datadir_prefix: /data/
@ -20,3 +17,10 @@ iface: eth1
#The password for admin user
mongo_admin_pass: 123456
mongod_ports:
bensible: 2700
web2: 2701
web3: 2702
web4: 2703

@ -1,23 +1,29 @@
#The site wide list of mongodb servers
[mongoservers]
mongo1
mongo2
mongo3
web2
web3
web4
bensible
#The list of servers where replication should happen, by default include all servers
[replicationservers]
mongo1
mongo2
mongo3
bensible
web2
web3
#The list of mongodb configuration servers, make sure it is 1 or 3
[mongocservers]
mongo1
mongo2
mongo3
web4
web2
web3
#The list of servers where mongos servers would run.
[mongosservers]
mongo1
web4
web3

@ -4,6 +4,8 @@
- hosts: all
tasks:
- include: ../roles/common/tasks/main.yml
handlers:
- include: ../roles/common/handlers/main.yml
- hosts: ${servername}
tasks:

@ -1,7 +1,10 @@
---
# Deploys all common plays for the site
- hosts: mongoservers
- hosts: all
user: root
tasks:
- include: ../roles/common/tasks/main.yml
handlers:
- include: ../roles/common/handlers/main.yml

@ -5,5 +5,3 @@
user: root
tasks:
- include: ../roles/mongod/tasks/main.yml
handlers:
- include: ../roles/mongod/handlers/main.yml

@ -7,9 +7,27 @@
- name: Creates the repository for 10Gen
template: src=../roles/common/templates/10gen.repo.j2 dest=/etc/yum.repos.d/10gen.repo
- name: Download the EPEL repository RPM
get_url: url=http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm dest=/tmp/ force=yes
- name: Install EPEL RPM
yum: name=/tmp/epel-release-6-8.noarch.rpm state=installed
- name: Clean up
command: rm -f /tmp/epel-release-6-8.noarch.rpm
- name: Install the mongodb package
yum: name=$item state=installed
with_items:
- mongo-10gen
- mongo-10gen-server
- bc
- python-pip
- name: Install the latest pymongo package
pip: name=pymongo state=latest use_mirrors=no
- name: Create the iptables file
template: src=../roles/common/templates/iptables.j2 dest=/etc/sysconfig/iptables
notify: restart iptables

@ -7,17 +7,10 @@
- name: Create the mongo configuration server startup file
template: src=../roles/mongoc/templates/mongoc.j2 dest=/etc/init.d/mongoc mode=0655
- name: Insert iptables rule for mongoc
lineinfile: dest=/etc/sysconfig/iptables state=present regexp="$mongoc_port" insertafter="^:OUTPUT " line="-A INPUT -p tcp --dport "$mongoc_port" -j ACCEPT"
- name: Apply iptable rule
service: name=iptables state=restarted
- name: Create the mongo configuration server file
template: src=../roles/mongoc/templates/mongoc.conf.j2 dest=/etc/mongoc.conf
- name: Create the script to add the admin user
template: src=../roles/mongoc/templates/adduser.j2 dest=/tmp/adduser.js
- name: Copy the keyfile for authentication
copy: src=../roles/mongod/files/secret dest=${mongodb_datadir_prefix}/secret owner=mongod group=mongod mode=0400
@ -28,5 +21,7 @@
- name: pause
pause: seconds=20
- name: add the admin user
shell: /usr/bin/mongo localhost:7777/admin /tmp/adduser.js
mongodb_user: database=admin name=admin password=${mongo_admin_pass} login_port=${mongoc_port} state=present
ignore_errors: yes

@ -1,5 +0,0 @@
---
# Handler for mongod
- name: restart iptables
service: name=iptables state=restarted

@ -1,10 +1,6 @@
---
#This Playbooks adds shards to the mongos servers once everythig is added
- name: get the port number for mongod processes
shell: ifconfig ${iface} | grep "inet addr" | cut -d':' -f2 | cut -d. -f4 | cut -d' ' -f1
register: result
- name: Create the file to initialize the mongod Shard
template: src=../roles/mongod/templates/shard_init.j2 dest=/tmp/shard_init_${inventory_hostname}.js
@ -12,7 +8,7 @@
with_items: ${groups.mongosservers}
- name: Add the shard to the mongos
shell: /usr/bin/mongo localhost:8888/admin -u admin -p ${mongo_admin_pass} /tmp/shard_init_${inventory_hostname}.js
shell: /usr/bin/mongo localhost:${mongos_port}/admin -u admin -p ${mongo_admin_pass} /tmp/shard_init_${inventory_hostname}.js
delegate_to: $item
with_items: ${groups.mongosservers}

@ -1,29 +1,17 @@
---
#This Playbook deploys the mongod processes and sets up the firewall rules and sets up the replication set.
#This Playbook deploys the mongod processes and sets up the replication set.
- name: create data directory for mongodb
file: path=${mongodb_datadir_prefix}/mongo-${inventory_hostname} state=directory owner=mongod group=mongod
delegate_to: $item
with_items: ${groups.replicationservers}
- name: Create a port number for mongod processes
shell: ifconfig ${iface} | grep "inet addr" | cut -d':' -f2 | cut -d. -f4 | cut -d' ' -f1
register: result
- name: Create the mongodb startup file
template: src=../roles/mongod/templates/mongod.j2 dest=/etc/init.d/mongod-${inventory_hostname} mode=0655
delegate_to: $item
with_items: ${groups.replicationservers}
- name: insert iptables rule for mongod
lineinfile: dest=/etc/sysconfig/iptables state=present regexp="$mongodb_port_prefix${result.stdout}" insertafter="^:OUTPUT " line="-A INPUT -p tcp --dport "$mongodb_port_prefix${result.stdout}" -j ACCEPT"
delegate_to: $item
with_items: ${groups.replicationservers}
- name: Add the iptable rule to allow traffice dynamically
shell: sleep `echo $RANDOM/1000 | bc`; iptables -I INPUT 1 -p tcp --dport ${mongodb_port_prefix}${result.stdout} -j ACCEPT
delegate_to: $item
with_items: ${groups.replicationservers}
- name: Create the mongodb configuration file
template: src=../roles/mongod/templates/mongod.conf.j2 dest=/etc/mongod-${inventory_hostname}.conf
@ -46,6 +34,6 @@
pause: seconds=20
- name: Initialize the replication set
shell: /usr/bin/mongo --port "$mongodb_port_prefix${result.stdout}" /tmp/repset_init.js
shell: /usr/bin/mongo --port "${mongod_ports.${inventory_hostname}}" /tmp/repset_init.js

@ -9,7 +9,7 @@ logappend=true
# fork and run in background
fork = true
port = {{ mongodb_port_prefix }}{{ result.stdout }}
port = {{ mongod_ports[inventory_hostname] }}
dbpath={{ mongodb_datadir_prefix }}mongo-{{ inventory_hostname }}
keyFile={{ mongodb_datadir_prefix }}/secret
@ -17,69 +17,9 @@ keyFile={{ mongodb_datadir_prefix }}/secret
# location of pidfile
pidfilepath = /var/run/mongod.pid
# Disables write-ahead journaling
# nojournal = true
# Enables periodic logging of CPU utilization and I/O wait
#cpu = true
# Turn on/off security. Off is currently the default
#noauth = true
#auth = true
# Verbose logging output.
#verbose = true
# Inspect all client data for validity on receipt (useful for
# developing drivers)
#objcheck = true
# Enable db quota management
#quota = true
# Set oplogging level where n is
# 0=off (default)
# 1=W
# 2=R
# 3=both
# 7=W+some reads
#diaglog = 0
# Ignore query hints
#nohints = true
# Disable the HTTP interface (Defaults to localhost:27018).
#nohttpinterface = true
# Turns off server-side scripting. This will result in greatly limited
# functionality
#noscripting = true
# Turns off table scans. Any query that would do a table scan fails.
#notablescan = true
# Disable data file preallocation.
#noprealloc = true
# Specify .ns file size for new databases.
# nssize = <size>
# Accout token for Mongo monitoring server.
#mms-token = <token>
# Server name for Mongo monitoring server.
#mms-name = <server-name>
# Ping interval for Mongo monitoring server.
#mms-interval = <seconds>
# Replication Options
replSet={{ inventory_hostname }}
# in replicated mongo databases, specify here whether this is a slave or master
#slave = true
#source = master.example.com
# Slave only: specify a single database to replicate
#only = master.example.com
# or
#master = true
#source = slave.example.com

@ -1,7 +1,7 @@
rs.initiate()
sleep(13000)
{% for host in groups['replicationservers'] %}
rs.add("{{ host }}:{{ mongodb_port_prefix }}{{ result.stdout }}")
rs.add("{{ host }}:{{ mongod_ports[inventory_hostname] }}")
sleep(8000)
{% endfor %}
printjson(rs.status())

@ -1,2 +1,2 @@
sh.addShard("{{ inventory_hostname}}/{{ inventory_hostname }}:{{ mongodb_port_prefix }}{{ result.stdout }}")
sh.addShard("{{ inventory_hostname}}/{{ inventory_hostname }}:{{ mongod_ports[inventory_hostname] }}")
printjson(rs.status())

@ -4,11 +4,6 @@
- name: Create the mongos startup file
template: src=../roles/mongos/templates/mongos.j2 dest=/etc/init.d/mongos mode=0655
- name: insert iptables rule for mongos
lineinfile: dest=/etc/sysconfig/iptables state=present regexp="$mongos_port" insertafter="^:OUTPUT " line="-A INPUT -p tcp --dport "$mongos_port" -j ACCEPT"
- name: Apply iptable rule on replication servers also
service: name=iptables state=restarted
- name: Create the mongos configuration file
template: src=../roles/mongos/templates/mongos.conf.j2 dest=/etc/mongos.conf
@ -23,3 +18,6 @@
- name: copy the file for shard test
template: src=../roles/mongos/templates/testsharding.j2 dest=/tmp/testsharding.js
- name: copy the file enable sharding
template: src=../roles/mongos/templates/enablesharding.j2 dest=/tmp/enablesharding.js