Firebase with Spring Boot Kubernetes Deployment configuration

Firebase setup with spring boot is pretty straight forward and so is configuring it for kubernetes. Do check out my previous post on firebase for spring boot setup https://thepro.io/post/firebase-authentication-for-spring-boot-rest-api-5V

In local development environment placing firebase service account file somewhere on file system and mapping it to environment variable GOOGLE_APPLICATION_CREDENTIALS is sufficient to get it working.

Doing the same on cloud environment such as kubernetes requires a bit more work

To accomplish this we will,

  1. Create Secret
  2. Create Deployment with Config Map Volume

Create Service Account file as Secret

Get base64 encoded string of the service account file. use following command to do this.

cat file_path | base64

Create a secret file using this base64 string

## generate base64 encoded string of firebase admin config file and replace here
## cat file_path | base64
apiVersion: v1
kind: Secret
metadata:
  name: firebase-admin-config
data:
  firebase-service-account.json: REPLACE_WITH_BASE64_ENCODED_FIREBASE_ADMIN_CONFIG_FILE

Create Deployment with Secret Volume

Follow this template to create a deployment file. We are going to attach the secret created from previous section as a volume to our deployment.

The Volume is attached with a mountPath “/app/thepro-firebase-config.json” , this is then passed to the pod as environment variable GOOGLE_APPLICATION_CREDENTIALS .

apiVersion: apps/v1
kind: Deployment
metadata:
  name: firebase-middleware-deployment
  labels:
    app: firebase-middleware
spec:
  replicas: 1
  selector:
    matchLabels:
      app: firebase-middleware
  template:
    metadata:
      labels:
        app: firebase-middleware
    spec:
      containers:
        - name: firebase-middleware
          image: firebase-middleware:latest
          env:
            - name: GOOGLE_APPLICATION_CREDENTIALS
              value: "/app/thepro-firebase-config.json"
          ports:
            - containerPort: 8080
          volumeMounts:
            - name: config-volume
              mountPath: /app/firebase-service-account.json
      volumes:
        - name: firebase-config-volume
          secret:
            secretName: firebase-admin-config

thats it !