How to use Firebase within a Kuberentes cluster

11/7/2016

I am trying to access a Firebase database from inside of a Kubernetes cluster, the only I think of is using a k8s service of type externalName. So I tried with below configuration.

Kuberentes Service:

kind : Service
apiVersion: v1
metadata :
  name : firebase-database-service
  labels :
    app : firebase-database-service
  spec:
    type : ExternalName
    externalName : myapp.firebaseio.com
    ports:
      - port : 443
        targetPort: 443

And below is my simple node app for demonstration.

var firebase        = require('firebase');
var os              = require("os");
var co              = require('co');
var hostName        = os.hostname();

var config = {
    apiKey: "API_KEY",
    databaseURL: "https://firebase-database-service",
};

var dashboard   = firebase.initializeApp(config, 'Dashboard');
dashboard.auth().signInWithEmailAndPassword("username", "password");
let uid = hostName.split('.').join('_') + "_" + process.pid;
let ref = "/data/"+ uid;

setInterval(co.wrap(function* (){
    try {
        dashboard.database().ref(ref + '/loadavg').set(os.loadavg());
    } catch (e) {
        console.error(e);
    }
}), 3000);

and I ended up with the below error.

FIREBASE FATAL ERROR: Cannot parse Firebase url. Please use https://<YOUR FIREBASE>.firebaseio.com 
at Error (native)
at xc (/Users/AAravindan/dev/realtime-screener/projects/test-kube-firebase/node_modules/firebase/database-node.js:55:277)
at yc (/Users/AAravindan/dev/realtime-screener/projects/test-kube-firebase/node_modules/firebase/database-node.js:57:20)
at Object.firebase.INTERNAL.registerService.Reference [as database] (/Users/AAravindan/dev/realtime-screener/projects/test-kube-firebase/node_modules/firebase/database-node.js:255:241)
at P.N (/Users/AAravindan/dev/realtime-screener/projects/test-kube-firebase/node_modules/firebase/app-node.js:18:94)
at Timeout.<anonymous> (/Users/AAravindan/dev/realtime-screener/projects/test-kube-firebase/index.js:19:19)
at next (native)
at onFulfilled (/Users/AAravindan/dev/realtime-screener/projects/test-kube-firebase/node_modules/co/index.js:65:19)
at /Users/AAravindan/dev/realtime-screener/projects/test-kube-firebase/node_modules/co/index.js:54:5
at Timeout.co (/Users/AAravindan/dev/realtime-screener/projects/test-kube-firebase/node_modules/co/index.js:50:10)

Is there any way to access firebase from kubernetes cluster.

Here is a sample project if you want to take a look.

Sample Project

-- anandaravindan
firebase
firebase-realtime-database
kubernetes

2 Answers

11/11/2016

Try kubectl get services, you'll get your list of service with

NAME           CLUSTER-IP      EXTERNAL-IP   PORT(S)             AGE

use CLUSTER-IP instead of externalName

If you still can't connect to your service, run kubeclt describe <service_name_above> and check ENTRYPOINT of your service (If your ENTRYPOINT=<None>, seem like your service doesn't run normally) Try to use ENTRYPOINT instead of CLUSTER-IP (not recommend)

-- luanbuingoc
Source: StackOverflow

11/11/2016

Currently(8 Nov 2016) there is no way using the javascript SDK.

I created a bug and here is the reply from the firebase support.

Currently, there is no way to specify the database to be used with the JS SDK other than with the literal URL, with the form: "https://.firebaseio.com", as the error suggests.

Although, I have created a bug regarding this issue and have brought this to the attention of our engineers. I will let you know on this thread for any updates.

-- anandaravindan
Source: StackOverflow