For the last couple of days I have tried to make this work. Just for testing purposes, I have three small gear nodejs apps on Openshift, and I want to have a load balancer on one that will distribute the load between the other two apps through a round-robin strategy.
I want to test if it is possible to do this with different Openshift accounts. On one account, we have the load balancer, and on the other two accounts is the app hosted.
So , let's say we have these three apps hosted at the following addresses:
http://loadbalancer-hostname0.rhcloud.com/
http://appone-hostname1.rhcloud.com/
http://apptwo-hostname2.rhcloud.com/
The code for the load balancer is:
var http = require('http'),
var httpProxy = require('http-proxy');
var proxy = httpProxy.createProxyServer({});
var addresses = [
'http://appone-hostname1.rhcloud.com:8000',
'http://apptwo-hostname2.rhcloud.com:8000'
];
var i = 0;
var server = http.createServer(function(req, res) {
proxy.web(req, res, { target: addresses[i] });
i = (i + 1) % addresses.length;
});
server.listen(8080,process.env.OPENSHIFT_NODEJS_IP);
And the code for the apps is:
var http = require('http');
var express = require('express')
app = express();
app.get('/', function (req, res) {
res.writeHead(200, {'Content-Type' : 'text/plain'});
res.end('Pi: ' + estimatePi());
});
app.listen(8080, process.env.OPENSHIFT_NODEJS_IP, function () {
console.log( "Listening on port 8080" )
});
function estimatePi() {
var n = 100000000, inside = 0, i, x, y;
for ( i = 0; i < n; i++ ) {
x = Math.random();
y = Math.random();
if ( Math.sqrt(x * x + y * y) <= 1 )
inside++;
}
return 4 * inside / n;
}
It should work as follows, when someone accesses https://loadbalancer-hostname.rhcloud.com/
the server should proxy the user to one of the apps and display the result of Pi. However, when accessing the load balancer, the user is being redirected to http://openshift.com
Note: when testing on my own computer, it works flawlessly.
Can somebody please tell me what I am doing wrong and what I should do to make this work?
EDIT: If port 80 is used instead of port 8000, the load balancer server redirects to http://loadbalancer-hostname.rhcloud.com/app