mockserver
by RADAR-Base
Chart to deploy MockServer to Kubernetes. MockServer enables easy mocking of any system you integrate with via HTTP or HTTPS with clients written in Java, JavaScript and Ruby. MockServer also includes a proxy that introspects all proxied traffic including encrypted SSL traffic and supports Port Forwarding, Web Proxying (i.e. HTTP proxy), HTTPS Tunneling Proxying (using HTTP CONNECT) and SOCKS Proxying (i.e. dynamic port forwarding).
About this chart
Installing MockServer
Prerequisites
- Kubernetes (i.e. minikube or Docker for Desktop)
- Helm
Helm Install
To run MockServer in Kubernetes the easiest way is to use the existing MockServer helm chart.
This is available by using www.mock-server.com as a chart repo, with the following command:
helm upgrade --install --create-namespace --namespace mockserver mockserver http://www.mock-server.com/mockserver-5.15.0.tgz
OR
If you have helm chart source folder (i.e. you have the repository cloned):
helm upgrade --install --create-namespace --namespace mockserver mockserver helm/mockserver
The two commands above will install MockServer into a namespace called mockserver with default configuration (as per the embedded values.yaml).
MockServer will then be available on domain name mockserver.mockserver.svc.cluster.local, as long as the namespace you are calling from isn't prevented (by network policy) to call the mockserver namespace.
THEN
To view the logs:
kubectl -n mockserver logs --tail=100 -l app=mockserver,release=mockserver
To wait until the deployment is complete run:
kubectl -n mockserver rollout status deployments mockserver
To check the status of the deployment without waiting, run the following command and confirm the mockserver has the Running status:
kubectl -n mockserver get po -l release=mockserver
Basic MockServer Configuration
Modify the arguments used to start the docker container by setting values explicitly using --set, as follows:
helm upgrade --install --create-namespace --namespace mockserver --set app.serverPort=1080 --set app.logLevel=INFO mockserver http://www.mock-server.com/mockserver-5.15.0.tgz
The following values are supported:
app.serverPort(default: 1080)app.logLevel(default: INFO)app.proxyRemoteHost(no default)app.proxyRemotePort(no default)app.jvmOptions(no default)image.snapshot(default: false) - settrueto use latest snapshot version
For example configure a proxyRemoteHost and proxyRemotePort, as follows:
helm upgrade --install --create-namespace --namespace mockserver --set app.serverPort=1080 --set app.proxyRemoteHost=www.mock-server.com --set app.proxyRemotePort=443 mockserver http://www.mock-server.com/mockserver-5.15.0.tgz
Double check the correct arguments have been passed to the pod, as follows:
kubectl -n mockserver logs -l app=mockserver,release=mockserver
Detailed MockServer Configuration
If a configmap called mockserver-config exists in the same namespace this will be mapped into the MockServer container under the mountPath /config.
This configmap can be used to configure MockServer by containing a mockserver.properties file and other related configuration files such as a:
- json expectation initialization, or
- custom TLS CA, X.509 Certificate or Private Key
The
mockserver.propertiesfile should load these additional files from the directory/configwhich is themountPathfor the configmap.
See MockServer Configuration for details of all configuration options.
The mapping of the configuration configmap can be configured as follows:
app.mountedConfigMapName(default: mockserver-config) - name of the configuration configmap (in the same namespace) to mountapp.propertiesFileName(default: mockserver.properties) - path of the property file in the configmap
For example:
helm upgrade --install --create-namespace --namespace mockserver --set app.mountedConfigMapName=other-mockserver-config --set app.propertiesFileName=other-mockserver.properties mockserver helm/mockserver
An example of a helm chart to configure MockServer is helm/mockserver-config
Extending MockServer Classpath
To use class callbacks or an expectation initializer class the classpath for MockServer must include the specified classes.
To support adding classes to the classpath if a configmap called mockserver-config exists in the same namespace any jar files contained in this configmap will be added into MockServer classpath.
The mapping of the libs configmap can be configured as follows:
app.mountedLibsConfigMapName(default: mockserver-config) - name of the libs configmap (in the same namespace) to mount
For example:
helm upgrade --install --create-namespace --namespace mockserver --set app.mountedLibsConfigMapName=mockserver-libs mockserver helm/mockserver
MockServer URL
Local Kubernetes Cluster (i.e. minikube, microk8s)
If the service type hasn't been modified the following will provide the MockServer URL from outside the cluster.
export NODE_PORT=$(kubectl get -n mockserver -o jsonpath="{.spec.ports[0].nodePort}" services mockserver)
export NODE_IP=$(kubectl get nodes -n mockserver -o jsonpath="{.items[0].status.addresses[0].address}")
export MOCKSERVER_HOST=$NODE_IP:$NODE_PORT
echo http://$MOCKSERVER_HOST
To test the installation the following curl command should return the ports MockServer is bound to:
curl -v -X PUT http://$MOCKSERVER_HOST/status
Docker for Desktop
Docker for Desktop automatically exposes LoadBalancer services.
On MacOS Docker for Desktop runs inside Hyperkit so the node IP address is not reachable, therefore the only way to call services is via the exposed LoadBalancer service added by Docker for Desktop.
To ensure that Docker for Desktop exposes MockServer update the service type to LoadBalancer using --set service.type=LoadBalancer and set the exposed port using --set service.port=1080, as follows:
helm upgrade --install --namespace mockserver --set service.type=LoadBalancer --set service.port=1080 mockserver http://www.mock-server.com/mockserver-5.15.0.tgz
MockServer will then be reachable on http://localhost:1080
For LoadBalancer services it is possible to query kubernetes to programmatically determine the MockServer base URL as follows:
export SERVICE_IP=$(kubectl get svc --namespace mockserver mockserver -o jsonpath='{.status.loadBalancer.ingress[0].hostname}')
export MOCKSERVER_HOST=$SERVICE_IP:1081
echo http://$MOCKSERVER_HOST
Outside Remote Kubernetes Cluster (i.e. Azure AKS, AWS EKS, etc)
kubectl -n mockserver port-forward svc/mockserver 1080:1080 &
export MOCKSERVER_HOST=127.0.0.1:1080
echo http://$MOCKSERVER_HOST
Inside Kubernetes Cluster
If a DNS server has been installed in the Kubernetes cluster the following DNS name should be available mockserver.<namespace>.svc.cluster.local, i.e. mockserver.mockserver.svc.cluster.local
Helm Delete
To completely remove the chart:
helm delete mockserver --purge
Deploy mockserver with OpenTenant
Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has mockserver on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.
- Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
- Place the chartmockserver is pinned at v5.15.0 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
- ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below opentenant validate opentenant.yaml terraform init && terraform apply