Simple http API backend for testing purpose using docker/kubernetes

At work we are responsible for an API Gateway offering. Testing of that API gateway is often done by using an API backend. This post shows how to create a static API backend to which you can easily add responses.

While looking for solutions I did not want to maintain another image, so I tried using an existing image and add config to mold it into something we can use.

I’m going to show both docker-compose and kubernetes yaml, as I’ve used both for testing.

Note: Create a local directory html for static files. These files contain the ‘responses’ for the backend.

Big thanks to my colleague Mahesh, who showed me a lot of cool tricks with K8s and OpenShift! Not only that, he helps me a lot with my simple questions and it’s been a lot of fun to figure out our (work related) challenges!
If you’d like then take a look at his blog over here: Mahesh Chinthaka – Medium

Setup

First up is the docker compose file which we can use for testing our backend locally. This file was the basis forwhat I converted to the k8s yamls.

Docker compose

version: '3.8'

services:
  nginxbackend:
    image: nginxinc/nginx-unprivileged
    container_name: nginxapibackend

    ports:
      - "8080:8080/tcp"
    volumes:
      - ./html:/usr/share/nginx/html
    restart: unless-stopped

Run docker compose up -d to start the nginx backend.

You can test the backend by opening this endpoint in your browser:

http://localhost:8080/yourfile.json

Note that yourfile.json should be present in the html directory

Kubernetes

Note: I’m not going to expose the NGINX backend outside of our namespace as I only want it accessible from the API Gateway, which resides in the same namespace.

If you want to test the backend, use docker or change the service to your liking.

The setup changes slightly because I don’t want to use local storage in my kubernetes cluster. In this setup I am using configmaps for the static response files.

K8s step 1: Create the configmap

kubectl create configmap nginxhtml --from-file=./html/

In the next steps we’ll mount the configmap for use in the pod.

K8s step 2: Create the deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginxapibackend
  name: nginxapibackend
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginxapibackend
  strategy: {}
  template:
    metadata:
      labels:
        app: nginxapibackend
    spec:
      containers:
      - image: nginxinc/nginx-unprivileged
        name: nginx
        volumeMounts:
          - name: nginxhtml
            mountPath: /usr/share/nginx/html
        ports:
          - containerPort: 8080
      volumes:
        - name: nginxhtml
          configMap:
            name: nginxhtml

Create the service

apiVersion: v1
kind: Service
metadata:
  name: nginxapibackend-service
  namespace: apim-dev
spec:
  selector:
    app: nginxapibackend
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080

Extra; Update the HTML configmap in kubernetes

You cannot update a configmap, so delete the current one and create a new one. Then recreate your pod so it uses the updated html directory.

kubectl delete configmap nginxhtml
kubectl create configmap nginxhtml --from-file=./html/
kubectl delete pod nginxapibackend-abcdefghij-abcde

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.