To run mock endpoints in your own infrastructure using standard Kubernetes resources, deploy a self-hosted mock server to Kubernetes. This approach lets you manage availability, scaling, and networking using the same deployment and operational practices you already use for other services.
Self-hosted mock servers require Redis to store mock data at runtime. When you deploy the mock server to Kubernetes, you must configure the Redis connection using environment variables. At a minimum, you must provide the Redis host so that the container can connect to your Redis service.
Set the following environment variable in the container spec:
-
MOCKBIN_REDIS: The Redis connection URL. For example, redis://redis.mock.svc:6379.
For a full list of supported environment variables in the mock server, review the Docker Compose configuration.
Run the following command to create a deployment for your mock server:
echo "
apiVersion: apps/v1
kind: Deployment
metadata:
name: insomnia-mock
namespace: mock
spec:
replicas: 1
selector:
matchLabels:
app: insomnia-mock
template:
metadata:
labels:
app: insomnia-mock
spec:
containers:
- name: insomnia-mock
image: ghcr.io/kong/insomnia-mockbin-self-hosted:latest
ports:
- containerPort: 9080
env:
- name: MOCKBIN_PORT
value: '9080'
- name: MOCKBIN_REDIS
value: "redis://redis:6379"
" | kubectl apply -f -
When you run a self-hosted mock server in Kubernetes, Redis connectivity depends on both the mock server configuration and the Redis image that you deploy. If your mock server can’t connect to Redis even though Redis appears healthy and reachable with port-forwarding, use a different Redis base image.
To create a service that exposes the Mockbin internally to the cluster, run the following command:
echo "
apiVersion: v1
kind: Service
metadata:
name: insomnia-mock
namespace: mock
spec:
type: ClusterIP
ports:
- name: mock
port: 9080
targetPort: 9080
selector:
app: insomnia-mock
" | kubectl apply -f -
To configure the Ingress to manage external access, first set your domain and TLS secret, and then apply the manifest. Your domain and TLS settings determine the host and secret of the configuration.
To export a domain and a TLS secret name as environment variables, run the following command:
export DOMAIN='your-domain'
export SECRET_NAME='your-tls-secret-name'
To apply the Ingress manifest, run the following command:
echo "
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: insomnia-mock-ingress
namespace: mock
spec:
ingressClassName: nginx
rules:
- host: $DOMAIN
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: insomnia-mock
port:
number: 9080
tls:
- hosts:
- $DOMAIN
secretName: $SECRET_NAME
" | kubectl apply -f -
Use the following commands to check the status of your resources:
kubectl get deployments -n mock
kubectl get services -n mock
kubectl get ingress -n mock
Once you deploy your mock, point your front-end application to the mock URL.