Skip to content

Instantly share code, notes, and snippets.

View sarjarapu's full-sized avatar

sarjarapu

  • Amazon Web Services
  • Austin, TX
View GitHub Profile
@sarjarapu
sarjarapu / elections-environment.sh
Created June 15, 2018 05:53
A bash script with download MongoDB v3.6.5 and start mongod on port 27000
# Run these commands on all the 3 servers of yours.
# download v3.6
curl -O https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.5.tgz
BIN_NAME="mongodb-linux-x86_64-rhel70-3.6.5"
BIN_VERSION="v3.6.5"
# create data directory and untar the binaries
tar -xvzf "$BIN_NAME.tgz"
rm "$BIN_NAME.tgz"
mv $BIN_NAME $BIN_VERSION
rm -rf data
@sarjarapu
sarjarapu / elections-initiate.sh
Created June 15, 2018 05:56
A bash script to initiate a replica set with 3 hosts we created earlier
$BIN_VERSION/bin/mongo --port 27000 <<EOF
rs.initiate({
_id: 'rs0',
members: [
{ _id: 0, host : 'mon01:27000' },
{ _id: 1, host : 'mon02:27000' },
{ _id: 2, host : 'mon03:27000' }
] })
EOF
# MongoDB shell version v3.6.5
@sarjarapu
sarjarapu / elections-rs.config.js
Last active June 15, 2018 06:03
A JavaScript function call showing the replica set configuration settings
rs.config()
/*
{
"_id": "rs0",
"version": 1,
"protocolVersion": NumberLong(1),
"members": [{
"_id": 0,
"host": "mon01:27000",
"arbiterOnly": false,
@sarjarapu
sarjarapu / elections-rs.status.js
Last active June 15, 2018 06:02
A JavaScript function call showing the replica set status information
rs.status()
/*
{
"set": "rs0",
"date": ISODate("2018-06-15T00:57:24.708Z"),
"myState": 1,
"term": NumberLong(1),
"heartbeatIntervalMillis": NumberLong(2000),
"optimes": {
"lastCommittedOpTime": {
@sarjarapu
sarjarapu / elections-printSlaveReplicationInfo.js
Last active June 15, 2018 06:02
A JavaScript function calls showing the database printSlaveReplicationInfo command output
db.printSlaveReplicationInfo()
/*
source: mon02:27000
syncedTo: Fri Jun 15 2018 01:29:11 GMT+0000 (UTC)
0 secs (0 hrs) behind the primary
source: mon03:27000
syncedTo: Fri Jun 15 2018 01:29:11 GMT+0000 (UTC)
0 secs (0 hrs) behind the primary
*/
@sarjarapu
sarjarapu / elections-freeze.js
Created June 15, 2018 06:05
A JavaScript method invoking rs.freeze to make the replica set member ineligible to become primary
// Freeze mon03
rs.isMaster().me
// mon03:27000
rs.freeze(60)
/*
{
"ok" : 1,
"operationTime" : Timestamp(1529033711, 1),
"$clusterTime" : {
@sarjarapu
sarjarapu / elections-electionTimeoutMillis.js
Created June 15, 2018 06:10
A JavaScript code to set the electionTimeoutMillis to 2 seconds and stepDown the primary
// # rs0:PRIMARY>
rs.isMaster().me
// mon01:27000
var conf = rs.conf()
conf.settings.electionTimeoutMillis=2000
rs.reconfig(conf)
/*
{
"ok": 1,
@sarjarapu
sarjarapu / elections-mongod.sh
Created June 15, 2018 06:13
A bash script to show the transition of mon02 from Secondary to Primary
# Server mon02
tail -100 data/mongod.log | grep REPL
# 2018-06-15T03:52:42.304+0000 I REPL [rsBackgroundSync] could not find member to sync from
# 2018-06-15T03:52:42.305+0000 I REPL [replexec-28] Member mon01:27000 is now in state SECONDARY
# 2018-06-15T03:52:43.306+0000 I REPL [SyncSourceFeedback] SyncSourceFeedback error sending update to mon01:27000: InvalidSyncSource: Sync source was cleared. Was mon01:27000
# 2018-06-15T03:52:43.360+0000 I REPL [replexec-26] Starting an election, since weve seen no PRIMARY in the past 2000ms
# 2018-06-15T03:52:43.360+0000 I REPL [replexec-26] conducting a dry run election to see if we could be elected. current term: 4
# 2018-06-15T03:52:43.360+0000 I REPL [replexec-24] VoteRequester(term 4 dry run) received a yes vote from mon01:27000; response message: { term: 4, voteGranted: true, reason: "", ok: 1.0, operationTime: Timestamp(1529034758, 1), $clusterTime: { clusterTime: Timestamp(1529034758, 1), signature: { hash: BinData(0, 0000000000000000000
@sarjarapu
sarjarapu / elections-reset.electionTimeoutMillis.js
Created June 15, 2018 06:15
A JavaScript code to reset the electionTimeoutMillis back to its default value
rs.isMaster().me
// mon02:27000
// rs0:PRIMARY>
// on the new primary
var conf = rs.conf()
conf.settings.electionTimeoutMillis=10000
/*
rs.reconfig(conf)
{
"ok": 1,
@sarjarapu
sarjarapu / kubernetes-helper.scripts.sh
Created July 11, 2018 20:23
Helper scripts to deploy the MongoDB in Kubernetes
# Download the helper scripts from GitHub repo
wget -O k8-mongo.zip https://github.com/sarjarapu/k8-mongo/archive/master.zip
unzip k8-mongo.zip
cd k8-mongo-master