Friday, 28 December 2018

Setting MongoDB Replica sets in ubuntu

MongoDB Replica Set:

Introduction:

A replica set in MongoDB is a group of mongod processes that maintain the same data set. Replica sets provide redundancy and high availability, and are the basis for all production deployments. MongoDB handles replication through an implementation called “replication sets”. Replication sets in their basic form are somewhat similar to nodes in a master-slave configuration. The primary member of the replica set can be used for read and write operations whereas the secondary members are available for read-only operations.

Requirements

  • Each server instance must allow TCP traffic from each replica set member over port 27017
  • Each server instance hostname must be resolvable from each replica set member

Setup Replica Sets:

Setup DNS Resolution:

MongoDB instances to communicate with each other effectively, we will need to configure our machines to resolve the proper hostname for each member.
Add all MongoDb Servers in /etc/hosts:
192.168.0.1    mongodb01.yourdomain.com
192.168.0.2    mongodb02.yourdomain.com 
192.168.0.3    mongodb03.yourdomain.com
Verify that all the nodes are able to connect each other using command:
mongo -host $hostname
Example :
mongo -host mongodb01.yourdomain.commongo -host mongodb02.yourdomain.com mongo -host mongodb03.yourdomain.com
run above commands from all the servers to verify all servers are able to connect to each other.
Create Replica Set:
Update /etc/mongod.conf in all servers
replication:
replSetName: ${replica set name}
Restart MongoDB in all servers:
systemctl restart mongod
Use the mongo shell to connect once the service is running again.
mongo

Configure Replica Set:

Initialize Replica Set: This will initalize the replica set where this commands get executed becomes primary
rs.initiate( {
    _id : “${replica set name}”,
     members: [
       { _id: 0, host: “mongodb01.yourdomain.com:27017” },
       { _id: 1, host: “mongodb02.yourdomain.com:27017” },
       { _id: 2, host: “mongodb03.yourdomain.com:27017” }
      ]
});

Verify all nodes are active and working fine:

rs.status()
O/P of the above command will be
It will display heartbeat , last sync time etc. and confirm all the nodes are connected.
{
 "set" : "set1",
 "date" : ISODate("2018-11-12T16:09:58Z"),
 "myState" : 2,
 "members" : [
  {
   "_id" : 0,
   "name" : "mongodb01.yourdomain.com:27018",
   "health" : 1,
   "state" : 2,
   "stateStr" : "SECONDARY",
   "self" : true
  },
  {
   "_id" : 1,
   "name" : "mongodb02.yourdomain.com:27017",
   "health" : 1,
   "state" : 1,
   "stateStr" : "PRIMARY",
   "uptime" : 770622,
   "optime" : {
    "t" : 1294848597000,
    "i" : 13
   },
   "optimeDate" : ISODate("2018-11-12T16:09:57Z"),
   "lastHeartbeat" : ISODate("2018-11-12T16:09:57Z")
  },
  {
   "_id" : 3,
   "name" : "mongodb03.yourdomain.com:27017",
   "health" : 1,
   "state" : 7,
   "stateStr" : "SECONDARY",
   "uptime" : 770622,
   "optime" : {
    "t" : 0,
    "i" : 0
   },
   "optimeDate" : ISODate("1970-01-01T00:00:00Z"),
   "lastHeartbeat" : ISODate("2018-11-12T16:09:58Z")
  }
  
 ],
 "ok" : 1
}

Wednesday, 26 December 2018

How to Install MongoDB in Ubuntu:


1) Add MongoDB official software packages repository
sudo apt-key adv — keyserver hkp://keyserver.ubuntu.com:80 — recv 0C49F3730359A14518585931BC711F9BA15703C6

2) Add MongoDB repository details so apt will know where to download the packages:
echo “deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

3) Update the packages list:
sudo apt-get update

4) Now Install MongoDB:
sudo apt-get install mongodb-org

PS : This installation is valid for mongoDb version 3.4 in ubuntu for other versions update 3.4 value.

Tuesday, 30 October 2018

Memory and CPU Usage for All Your Docker Containers on Ubuntu


Running docker stats command will return statistics of your running container. Stats like CPU, memory network and block i/o operations.

How to look stats of specific container?

For example: 

docker stats  ${container Id or container name}.

command: docker stats chat.






above command will continouslly update the stats

How to look stats of all containers?

Command:  docker stats 

above command will display stats of all running containers with continouslly update the stats.



In case if you don't want stats to be updated continously, then add --no-stream

in docker stats command this will display stats only once.


For example:  docker stats -no-stream or docker stats ${container name | id} --no-stream.


Monday, 29 October 2018

Increase ulimit in ubuntu and docker

What is ulimit?

It is a number of open file descriptors per process. They can all refer to the same file, or different files. It prevent single users from using too many system resources.

How can is check ulimit in current shell?

Run ulimit -a command  from terminal. 

Output of the above command:




How to set ulimit in current shell?

ulimit -n {$no of files}

For example ->  If  you want to set no of open files 65535 below is the command you need to run on terminal.    ulimit -n 65535 ; 

How to set ulimit permanent?

For the ulimits to persists across reboots we need to set the ulimit values in the configuration file
  /etc/security/limits.conf.

Format ->
#[domain]   [type]  [item]  [value]


domain  - > a user name or group name
type ->   can have the two values (soft| hard)
item -> the property to set
value -> value of the property.

For example ->                 to add limit of number of files for user root:



to set limit  of number of files valid for all users




How to set ulimit in  Docker?

Due to some security reason. docker currently not extends host ulimit property.

 There are couple of ways to set following are the ways:

  • The latest docker supports setting ulimits through the command line and the API. For instance, docker run takes --ulimit <type>=<soft>:<hard> .
  • set ulimit -n 65535 in the file /etc/init.d/docker



  




Generating Unique Id in Distributed Environment in high Scale:

Recently I was working on a project which requires unique id in a distributed environment which we used as a  primary  key to store in dat...