Created
February 4, 2016 00:49
-
-
Save thesandlord/a87c78cf01d44a7761b5 to your computer and use it in GitHub Desktop.
Sample Code showing how you can use the kubernetes-pod-ip-finder to connect to a MongoDB Replica Set
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2016, Google, Inc. | |
Licensed under the Apache License, Version 2.0 (the "License"); | |
you may not use this file except in compliance with the License. | |
You may obtain a copy of the License at | |
http://www.apache.org/licenses/LICENSE-2.0 | |
Unless required by applicable law or agreed to in writing, software | |
distributed under the License is distributed on an "AS IS" BASIS, | |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
See the License for the specific language governing permissions and | |
limitations under the License. | |
*/ | |
var MongoClient = require('mongodb').MongoClient; | |
var express = require('express'); | |
var request = require('request'); | |
var app = express(); | |
var url = 'localhost'; | |
var errorMsg = function(db, res, txt){ | |
res.write(txt); | |
res.end(); | |
} | |
var insertDoc = function(db, res) { | |
var date = new Date(); | |
db.collection('test').insertOne( {"time":date.toDateString()}, function(err, result) { | |
if(err != null) errorMsg(db, res, " - Failed to write Data - " + err); | |
else{res.write(" - Wrote Data"); findDocs(db, res);} | |
}); | |
}; | |
var findDocs = function(db, res) { | |
var cursor =db.collection('test').find( ); | |
cursor.each(function(err, doc) { | |
if(err != null) errorMsg(db, res, " - Failed to read Data - " + err); | |
else if (doc != null) res.write(" - " + JSON.stringify(doc)); | |
else {db.close();res.end();} | |
}); | |
}; | |
var drop = function(db, callback) { | |
db.collection('test').drop( function(err, response) { | |
callback(); | |
}); | |
}; | |
var options = { | |
db: { | |
readPreference: 'nearest', | |
slaveOk: true | |
}, | |
replSet: { | |
replicaSet: 'rs0' | |
}, | |
server: { | |
w: 2, | |
autoReconnect: true | |
} | |
}; | |
//Ping Cluster for IP addresses of MongoDB Replica Sets | |
setInterval(function(){ | |
request('http://kubernetes-pod-ip-finder?role=mongo', function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
url = 'mongodb://'+JSON.parse(body).join()+'/test_?'; | |
} | |
}) | |
}, 5000); | |
app.get('/', function(req, res){ | |
res.writeHead(200, { 'Content-Type': 'application/json' }); | |
MongoClient.connect(url, options, function(err, db) { | |
if(err != null) errorMsg(db, res, "Mongo DOWN"); | |
else{res.write("Mongo UP");insertDoc(db, res);} | |
}); | |
}); | |
app.get('/clear', function(req, res){ | |
res.writeHead(200, { 'Content-Type': 'application/json' }); | |
MongoClient.connect(url, options, function(err, db) { | |
if(err != null) errorMsg(db, res, "Mongo DOWN"); | |
else drop(db, function(){errorMsg(db, res, "Database Dropped");}); | |
}); | |
}); | |
app.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment