Using Onboardbase in Kubernetes Deployments

📘

REQUIREMENT

This is a continuation of our previous article on integrating Onboardbase with Docker, we will be working with the same setup from the article.

It is therefore required that you walk through the Docker Integration tutorial and setup an Onboardbase Project as described there if you want to follow along here.

In the previous example, we showed how we can use Onboardbase to inject environment variables into our Docker applications.

This tutorial will take that example further and show how we can use Onboardbase to manage Secrets in a Kubernetes Deployment. It is important that you create an Onboardbase Project with the environment and variables created in the previous post before you can follow along here.

After you have set up the Onboardbase Project and Environment variables as described in the previous post, you can clone the repository containing the final code in that post here:

git clone https://github.com/Onboardbase/tutorials-simple-express-dockerized

After cloning the repository, you will need to change the Dockerfile to this:

FROM node:16

# Create app directory
WORKDIR /usr/src/app


COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3600

CMD [ "yarn", "start" ]

I have removed the integration to Onboardbase we added to the Dockerfile in Docker Setup tutorial, we are going to be injecting those variables via Kubernetes Secrets.

We will also update the index.js file to include a compulsory DB_URL environment variable, the updated file is shown below:

const express = require("express");
const app = express();

app.get("/", (req, res) => {
  res.send("GET request to the homepage");
});

const port = process.env.PORT;
if (!port) {
  throw Error("You must set a Port for the App to Run");
}

const DB_URL = process.env.DB_URL;
if (!DB_URL) {
  throw Error("You must set a DB_URL for the App to Run");
}

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`);
});

In a real application, you will be using your Database URL to connect to a database, but this is a very simple example so we wouldn't go into those details.

Adding the Database URL to Onboardbase

Since we have another environment variable and during the Docker Setup, we only set a PORT variable, we have to add the variable into our Onboardbase Project.

Login into your Onboardbase account and navigate to the Onboardbase Project that you created from theDocker Setup tutorial. Click on the + icon to add a new environment variable

We add the DB_URL variable and save the changes

Back to the code editor, we will create a folder to contain the configuration files for Kubernetes.

mkdir k8s 

cd k8s/

touch deployment.yaml

The content of deployment.yaml is as follows

apiVersion: apps/v1
kind: Deployment
metadata:
  name:  simple-express
  labels:
    app:  simple-express
spec:
  selector:
    matchLabels:
      app: simple-express
  replicas: 1
  template:
    metadata:
      labels:
        app:  simple-express
    spec:
      containers:
      - name:  simple-express
        image:  onboardbasetest/simple-express
        ports:
        - containerPort:  8080

The Docker image is hosted on Docker Hub here

Creating Secrets with Onboardbase

We will be putting the environment variables into a Kubernetes Secret, before then, we need to set up Onboardbase in the project folder.

Preferably, execute onboardbase setup in the k8s/ folder and select the project and environment to configure Onboardbase locally.

To generate secrets with Onboardbase, run the following command:

kubectl create secret generic onboardbase-env-vars --from-env-file <(onboardbase secrets --docker)

You can view the details of the created secret by running:

kubectl describe secret onboardbase-env-vars

The data section contains the environment variables that we have set on Onboardbase

Now we will update the deployment.yaml to include the environment variables needed for the app to start

apiVersion: apps/v1
kind: Deployment
metadata:
  name:  simple-express
  labels:
    app:  simple-express
spec:
  selector:
    matchLabels:
      app: simple-express
  replicas: 1
  template:
    metadata:
      labels:
        app:  simple-express
    spec:
      containers:
      - name:  simple-express
        image:  onboardbasetest/simple-express
        args: ["printenv"]
        ports:
        - containerPort:  8080
        env:
        - name: PORT
          valueFrom:
            secretKeyRef:
              name: onboardbase-env-vars
              key: PORT
        - name: DB_URL
          valueFrom:
            secretKeyRef:
              name: onboardbase-env-vars
              key: DB_URL

Notice that I have also added args: ["printenv"] to the container specification so that we can print out the environment variables that are set when the Pod is created. I have also referenced the onboardbase-env-vars secret from the env.

Still in the k8s/ folder, apply the deployment configuration:

kubectl apply -f deployment.yaml

Get the name of the pod that was created by the Deployment by running kubectl get pods. The name of the pod will start with simple-express-*. After getting the name of the pod, we can get the logs from it by executing

#simple-express-749d89dd64-sfc8j this is name of the pod on my machine, it will be different on yours

kubectl logs simple-express-749d89dd64-sfc8j

You should see all the default environment variables in the pod, including the DB_URL and PORT that we set on Onboardbase

Conclusion

We have shown how we can use Onboardbase to inject Environment Variables and Secrets in a Kubernetes Deployment.

We hope you found this helpful.