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
- 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 providerhub.providers.microcks.endpoint="http://microcks.microcks:8080"
defines the endpoint for the Microcks provider
Navigate to microcks.docker.localhost to see the Microcks UI
)
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
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
Deploy the sample API
Deploy a sample Pastry API:
- Deployment
- Service
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
apiVersion: v1
kind: Service
metadata:
name: pastry-api
namespace: default
labels:
app: pastry-api
spec:
type: ClusterIP
ports:
- port: 8080
name: pastry-api
selector:
app: pastry-api
Expose this API on the gateway with an IngressRoute:
- IngressRoute
- CORS Middleware
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
apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
name: cors-middleware
namespace: default
spec:
headers:
accessControlAllowOriginList:
- "http://portal.docker.localhost"
accessControlAllowMethods:
- "GET"
- "OPTIONS"
- "POST"
- "PUT"
- "DELETE"
accessControlAllowHeaders:
- "Authorization"
- "Content-Type"
accessControlAllowCredentials: true
accessControlMaxAge: 100
addVaryHeader: true
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:
- API Portal
- IngressRoute
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"
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: portal
namespace: default
annotations:
hub.traefik.io/api-portal: my-portal@default # references the API portal created
spec:
entryPoints:
- web
routes:
- match: Host(`portal.docker.localhost`)
kind: Rule
services:
- name: apiportal
port: 9903
namespace: traefik
The API Portal will be available at http://portal.docker.localhost.
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.