Skip to main content

Set up On-Premises API Mocking with Traefik Hub and Microcks

API Mocking allows developers and platform engineers to create simulated API responses without requiring a live backend. We've partnered with Microcks, an open-source API mocking tool that enables creating and managing simulated APIs based on OpenAPI specifications (OAS).

Traefik Hub provides a secure and controlled way to expose these mock APIs through the API Gateway. It allows adding authentication, rate-limiting, observability, and more, effectively treating mock APIs as first-class citizens like real production APIs.

Key benefits

  • Centralized API management: Register and manage mock and real production APIs from a single unified UI.
  • Rapid prototyping: Instantly generate live API mocks from your OpenAPI specs.
  • Flexible consumption: Use API Plans and Managed Subscriptions to define quotas and rate limits that suit your business model.
  • Developer empowerment: Make your mock APIs visible on the Traefik Hub Developer Portal.

This guide walks through setting up Microcks as a provider in Traefik Hub for On-prem API Mocking, covering the complete workflow from installing Microcks to exposing mock APIs via the Traefik API Gateway and Portal.

The intention is to give you a quick start, from zero to hero. For a production-ready installation, you can use the relevant installation guides.

Prerequisites

Before proceeding, ensure the following tools are installed:

  • k3d
  • kubectl
  • Helm
  • A valid Traefik Hub license token.
  • OpenAPI Specification (OAS) files for your APIs.

Set up a local Kubernetes cluster

First, create a cluster with two agent nodes to host both Microcks and Traefik Hub. For this guide, we'll use k3d

k3d cluster create --k3s-arg "--disable=traefik@server:*" \
--agents="2" \
--port 80:80@loadbalancer \
--port 443:443@loadbalancer \
--port 8080:8080@loadbalancer

This command creates a cluster with two agent nodes and exposes the necessary ports so that both Traefik Hub and your API endpoints are reachable externally.

Deploy Microcks

Create a namespace for Microcks

kubectl create namespace microcks

Deploy Microcks using the Microcks Kubernetes Operator

Install the operator CRD by running the following commands:

kubectl apply -f https://raw.githubusercontent.com/microcks/microcks-operator/refs/heads/main/deploy/crd/microckses.microcks.io-v1.yml

kubectl apply -f https://raw.githubusercontent.com/microcks/microcks-operator/refs/heads/main/deploy/crd/apisources.microcks.io-v1.yml

kubectl apply -f https://raw.githubusercontent.com/microcks/microcks-operator/refs/heads/main/deploy/crd/secretsources.microcks.io-v1.yml

Install the operator in the microcks namespace by running the following commands:

kubectl apply -f https://raw.githubusercontent.com/microcks/microcks-operator/refs/heads/main/deploy/operator-jvm.yaml -n microcks

Install Microcks by running the following command:

cat <<EOF | kubectl apply -n microcks -f -
apiVersion: microcks.io/v1alpha1
kind: Microcks
metadata:
name: microcks
spec:
# Check Microcks' release notes for the latest version
version: 1.11.0
microcks:
url: microcks.docker.localhost
env:
- name: KEYCLOAK_ENABLED
value: "false"
keycloak:
install: false
url: keycloak.docker.localhost
EOF

Verify the installation:

❯ kubectl get pods -n microcks
NAME READY STATUS RESTARTS AGE
microcks-6d9d56c9c9-gr7pt 1/1 Running 0 10s
microcks-mongodb-858b6b7f4d-74q2c 1/1 Running 0 10s
microcks-operator-f786c97cf-tl874 1/1 Running 0 10s
microcks-postman-runtime-85f49f5fc6-ssqxc 1/1 Running 0 10s

❯ kubectl get ingresses -n microcks
NAME CLASS HOSTS ADDRESS PORTS AGE
microcks <none> microcks.docker.localhost 192.168.107.2,192.168.107.3,192.168.107.4 80, 443 5s
microcks-grpc traefik microcks-grpc.docker.localhost 192.168.107.2,192.168.107.3,192.168.107.4 80, 443 5s

Install and configure Traefik Hub

Next, we deploy Traefik Hub API management and configure it to use Microcks as a provider:

Create a namespace for Traefik Hub

kubectl create namespace traefik

Add the Traefik Helm repository

helm repo add --force-update traefik https://traefik.github.io/charts

Install Traefik Hub with Microcks provider

export TRAEFIK_HUB_TOKEN=<YOUR_TRAEFIK_HUB_TOKEN>

kubectl create secret generic traefik-hub-license --namespace traefik \
--from-literal=token=${TRAEFIK_HUB_TOKEN}

helm upgrade --install traefik traefik/traefik \
--namespace traefik \
--set hub.apimanagement.enabled=true \
--set hub.providers.microcks.enabled=true \
--set hub.providers.microcks.endpoint="http://microcks.microcks:8080" \
--set hub.token=traefik-hub-license \
--set ingressRoute.dashboard.enabled=true \
--set ingressRoute.dashboard.matchRule='Host(`dashboard.docker.localhost`)' \
--set ingressRoute.dashboard.entryPoints={web} \
--set image.registry=ghcr.io \
--set image.repository=traefik/traefik-hub \
--set image.tag=v3.10.1 \
--set providers.kubernetesCRD.allowCrossNamespace=true \
--set logs.general.level=INFO
tip
  • Replace YOUR_TRAEFIK_HUB_TOKEN with your Traefik hub license token
  • Make sure to check the release notes for the latest version of Traefik Hub to use in image.tag
  • hub.providers.microcks.enabled=true enables the Microcks provider
  • hub.providers.microcks.endpoint="http://microcks.microcks:8080" defines the endpoint for the Microcks provider

Navigate to microcks.docker.localhost to see the Microcks UI

microcks.docker.localhost

Microcks UI)

info

Traefik Hub API Gateway dashboard is available at http://dashboard.docker.localhost.

Feed an OpenAPI Specification (OAS) to Microcks

To create mock APIs, we must import API definitions into Microcks.

apiVersion: microcks.io/v1alpha1
kind: APISource
metadata:
name: mock-apis
namespace: microcks
annotations:
microcks.io/instance: microcks
spec:
artifacts:
- url: https://doc.traefik.io/traefik-hub/assets/files/pastry-open-api-8809d128776b1935ebd8c37bcae99670.yaml # link to your OpenAPI spec
mainArtifact: true
info

You can download the OpenAPI spec for the Pastry API here

Apply the API source:

kubectl apply -f api-source.yaml -n microcks

Once the APISource has been applied, the OpenAPI spec will be exposed in Microcks. You can verify this via the Microcks UI

http://localhost:3000

Microck UI With Open API Spec Imported

Deploy the sample API

Deploy a sample Pastry API:

apiVersion: apps/v1
kind: Deployment
metadata:
name: pastry-api
namespace: default
spec:
replicas: 1
selector:
matchLabels:
app: pastry-api
template:
metadata:
labels:
app: pastry-api
spec:
containers:
- name: pastry-api
image: mmatur/pastry-api:latest
imagePullPolicy: Always

Expose this API on the gateway with an IngressRoute:

apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: pastry-ingress
namespace: default
annotations:
# The API will be created in the use cases page
hub.traefik.io/api: local-pastry@default
spec:
entryPoints:
- web
routes:
- match: Host(`pastry.docker.localhost`)
kind: Rule
services:
- name: mock-api-pastry-2-0@microcks
kind: TraefikService
middlewares:
- name: cors-middleware
info

The mock-api-pastry-2-0@microcks service referenced in the IngressRoute is an internal service generated by the Microcks provider to represent the Microcks mock API. Traefik Hub automatically creates a new service for every new APISource added to Microcks. Always reference the generated service in your IngressRoute configuration to ensure Traefik Hub can correctly reach your mock API.

Deploy an API Portal

We can provide a centralized developer experience to manage our APIs via the Traefik Hub API Portal:

apiVersion: hub.traefik.io/v1alpha1
kind: APIPortal
metadata:
name: my-portal
namespace: default
spec:
title: "My Portal"
description: "API documentation"
trustedUrls:
- "http://portal.docker.localhost"

The API Portal will be available at http://portal.docker.localhost.

info

Make sure to create a user on the Traefik Hub Online Dashboard and assign the user to a group called admin for the APIPortal, if you haven't already.

Conclusion

In this guide, you have learned how to:

  • Deploy Microcks: Enable automatic API mocking from your OpenAPI Specification.
  • Install Traefik Hub: Leverage our centralized API gateway for secure exposure, routing, and policy enforcement.
  • Publish Your API Mocks: Expose the Microcks-generated mocks on the gateway via IngressRoutes.
  • Integrate with the API Portal

In the use cases page, you'll learn how Traefik Hub API Mocking can help you deliver a scalable, secure, and efficient API delivery platform.