Running Data API Builder on Kubernetes

2023, May 03

The Data API Builder is currently on Public Preview and is enabling businesses to quickly expose Data through APIs, to be consumed by Modern Apps. When it comes to how to host it, you can either leverage Static Web Apps to bind UI and API together, or you can get it running as API in containers.

Because I have been exploring the tool I decided to run it on Kubernetes for development purposes.

The Docker image

Microsoft has made it available on https://mcr.microsoft.com/product/azure-databases/data-api-builder/about.

Note that you can run it locally with docker run:

docker run -it -v <host-directory>:/App/<container-directory> -p 5000:5000 mcr.microsoft.com/azure-databases/data-api-builder:<tag> --ConfigFileName <configuration-file>

It requires the ConfigFileName as a parameter, and this is what I would like to talk about it.

The Config file

This post intends to show how to host the Container on Kubernetes, but we need the Configuration File!

This config file is created when the DAB CLI is used, by running a few commands. Data API Builder can be run against Azure Databases like Azure Database/SQL Server, PostgreSQL, MySQL, CosmosDB.

In my case I am running it against a SQL database:

dab init --database-type mssql --connection-string "Server=localhost;Database=Library;"

This will generate the file dab-config.json in the directory where you run the command. On Kubernetes, we can mount a volume and use a secret for it.

Using Kubernetes Secret

Hosting the config file as a secret works like a charm, if you notice, there is a connection string on the file. Having it as a secret prevents us from exposing any sensitive information.

It is simple to setup, run the kubectl command:

kubectl create secret generic dab-config --from-file=dab-config.json -n demo

With that, we have the file as a secret on namespace demo.

Now we just need to launch our deployment.

Running a deployment

We need to create a YAML file to do that for us. This will have a Deployment to initialize our replicas by using the container image with the secret that it was just created.

File: deployment-dab.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: data-api-builder
  namespace: demo
  labels:
    app: data-api-builder
spec:
  replicas: 3
  selector:
    matchLabels:
      app: data-api-builder
  template:
    metadata:
      labels:
        app: data-api-builder
    spec:
      containers:
      - name: data-api-builder
        image: mcr.microsoft.com/azure-databases/data-api-builder:latest
        ports:
        - containerPort: 5000
        volumeMounts:
        - name: config-volume
          mountPath: /App/dab-config.json
          subPath: dab-config.json
      volumes:
      - name: config-volume
        secret:
          secretName: dab-config

---
apiVersion: v1
kind: Service
metadata:
  name: data-api-builder
  namespace: demo
  labels:
    app: data-api-builder
spec:
  selector:
    app: data-api-builder
  ports:
  - name: http
    port: 80
    targetPort: 5000
  type: LoadBalancer

NOTE: A volume mount was created with the mountPath: /App/dab-config.json and subPath dab-config.json. If that is not mapped, the file is not attached to the running container, and if you don't specify the subPath, the /App/dab-config.json is created as a folder! So don't forget to specify the subPath to attach the secret as a file.

Run the command:

kubectl apply -f deployment-dab.yaml

Check the number of replicas! You should have 3 pods:

K9s

If you reach the public ip address you should see "Healthy" on browser. This means that the Data API is running!

From here there are lots of possibilities. The Data API Builder is a great tool, I hope to write more posts about it soon.

Learn more about the Data API Builder.