Run from a file, no database¶
How-to guideThe steps for one task.
apinx gateway run serves one gateway file and nothing else: no store, no console, no
control plane. The file is the configuration, read at start and watched for changes; the
process is stateless and identical on every instance. This is the file mode. The usual
mode, with the console and the store, is the console mode.
When to use it¶
- A Kubernetes or GitOps deployment where the configuration is a file in Git, rendered into a ConfigMap, and the pods must be interchangeable and disposable.
- An edge or sidecar gateway with a handful of routes, where a database is one more thing to run.
- CI: start the gateway with the file under test, run the suite, stop it.
Choose the console mode when people edit the configuration in a UI, when consumers hold API keys or subscriptions, or when you want traffic analytics and the request inspector.
The command¶
The file is the argument; without one, proxy.config_file from apinx.yaml, else
./gateway.yaml. apinx.yaml is read as usual (--config, flags, APINX_* variables):
console.listen is where the admin listener binds, and logging, access_log, redis,
proxy.cache_bytes, proxy.callouts, shutdown.drain_timeout and demo apply. The
storage, analytics, inspector, security, license, cluster and assistant
sections are ignored.
A bad file at start is a usage error: the process exits with code 2 and the validation
issues in the log, before any port is bound. apinx gateway validate gateway.yaml reports
the same issues without starting anything.
What runs and what does not¶
Everything on the request path is the same binary code as in the console mode: listeners
(HTTP/1.1, h2c, TLS, mTLS), upstreams with balancing, health checks, breakers and retries,
routing, WebSocket and gRPC, policies and shared flows, environment flow hooks, rate limits
and quotas (shared through Redis when redis.address is set), caching, JWT and JWKS, IP
filters, transformations, service callouts, mock responses, the access log, Prometheus
metrics, graceful shutdown.
The file is read exactly as an import is: each route is resolved and lists the policies
it runs (the reference describes the contract); the API's
hosts, base path and prefix stripping are folded into its routes, the environment's flow
hooks surround every route, and a route that leaves an inherited policy out of its list has
it switched off, as the console would show after importing the same file. What apinx
gateway run serves and what apinxctl import followed by a publish serves are the same
configuration.
| Not available | Why | In its place |
|---|---|---|
| The console and the Admin API | They edit a store | The file, and apinx gateway validate |
api-key and subscription policies |
Keys, applications and plans live in the store's consumer directory | jwt (keys issued by your identity provider), basic, ip-filter; a file that binds them is refused at start with the policy named |
| Products and plans | Subscriptions are consumer data | Rate limits and quotas bound directly on the API |
| Analytics and the request inspector | Both write to a store | The access log and /metrics |
| Secrets at rest, licence, cluster heartbeat, assistant | Nothing is stored | Secrets come from the environment (below); several instances need no coordination |
Environment variables in the file¶
A gateway file may reference environment variables, so one file serves every environment:
upstreams:
- name: orders
targets:
- url: ${ORDERS_URL} # required: unset stops the start
- url: ${ORDERS_URL_2:-http://orders-2:8080} # default when unset or empty
routes:
- name: orders
path: /orders/{rest...}
upstream: orders
policies:
- type: jwt
config:
jwks_url: ${IDP_URL:-https://idp.internal}/.well-known/jwks.json
- type: set-header
config:
request: { X-Caller: "${principal.name}" } # lower case: a per-request template, untouched
response: { X-Literal: "$${NOT_A_VARIABLE}" } # $${ gives a literal ${
Names are upper case: letters, digits and underscores, not starting with a digit
([A-Z_][A-Z0-9_]*). ${NAME:-default} uses the default when the variable is unset or
empty, as in a shell. Every ${NAME} without a value and without a default is an error:
APX-GW-0012 names all of them at once and nothing starts. Apinx's own settings are never
gateway material: a reference to an APINX_* variable (the connection strings and keys among
them would be copied into the stored revision) is refused with APX-GW-0016, whether or not
it is set. Lower-case references such as
${client_ip}, ${host}, ${principal.name} or ${secret.key} are the policies'
per-request templates and pass through unchanged. Comments are left as written, so a note such
as # set ${ORDERS_URL} first needs no variable; a # inside a quoted value or a block scalar
(|, >) belongs to the value, and references there are replaced.
Expansion happens whenever a file is read: apinx gateway run, apinx gateway validate,
apinxctl validate <file> and the seed of an empty store. A document stored in the console
mode is never expanded: a revision is what was applied, not a template.
Watch and reload¶
The file is polled every two seconds (modification time and size, so it works the same on Linux, Windows and network filesystems; a ConfigMap update is a rename, which is seen). On a change the file is read again, resolved, validated and compiled; only then is it swapped in, atomically, as a publish is in the console mode: requests in flight finish on the old configuration, listeners restart only when their own definition changed, and unchanged upstreams keep their connections and health state. The log says what happened:
{"level":"INFO","msg":"gateway file reloaded","path":"/etc/apinx/gateway.yaml","sequence":2,"listeners":1,"upstreams":2,"routes":7}
A file that does not load (a syntax error, a validation issue, an unset variable, a listener
that cannot bind) is logged with every issue and the previous configuration keeps
serving. Nothing is retried on a timer: fix the file, and the next change is picked up.
sequence on the status endpoint (below) tells which load is live.
Several instances, one file¶
Instances in file mode share nothing but the file, so run as many as the load needs behind a load balancer and give them all the same file. On Kubernetes:
apiVersion: v1
kind: ConfigMap
metadata: { name: apinx-gateway }
data:
gateway.yaml: |
apiVersion: apinx/v1
kind: Gateway
listeners: [{ name: http, address: ":8080" }]
upstreams: [{ name: orders, targets: [{ url: ${ORDERS_URL} }] }]
routes: [{ name: orders, path: /orders/{rest...}, upstream: orders }]
---
apiVersion: apps/v1
kind: Deployment
metadata: { name: apinx }
spec:
replicas: 3
selector: { matchLabels: { app: apinx } }
template:
metadata: { labels: { app: apinx } }
spec:
containers:
- name: apinx
image: your-registry/apinx:1.1.3
args: ["gateway", "run", "/etc/apinx/gateway.yaml"]
env:
- { name: ORDERS_URL, value: http://orders.default.svc:8080 }
- { name: APINX_CONSOLE_LISTEN, value: ":9000" }
- { name: APINX_REDIS_ADDRESS, value: redis:6379 } # rate limits counted across the replicas
ports:
- { containerPort: 8080, name: http }
- { containerPort: 9000, name: admin }
livenessProbe: { httpGet: { path: /healthz, port: admin } }
readinessProbe: { httpGet: { path: /readyz, port: admin } }
volumeMounts:
- { name: gateway, mountPath: /etc/apinx }
volumes:
- name: gateway
configMap: { name: apinx-gateway }
Editing the ConfigMap updates the mounted file on every pod within the kubelet's sync period, and each instance reloads on its own; a rolling restart is not needed. Without Redis each replica counts rate limits and quotas alone.
Health, readiness, metrics and status¶
The admin listener (console.listen, default :9000) answers exactly four routes and
nothing else; keep it off the interface that carries traffic.
| Route | Answer |
|---|---|
GET /healthz |
{"status":"ok"} while the process runs |
GET /readyz |
200 once the file is applied and every listener is bound, else 503 naming the check |
GET /metrics |
Prometheus: requests and latency per route, upstream health, policy decisions, telemetry drops |
GET / |
{"mode":"file","file":"/etc/apinx/gateway.yaml","sequence":2,"listeners":1,"upstreams":2,"routes":7} |
The metrics are the same series as in the console mode, so one dashboard serves both.
Moving to the console mode¶
The file mode is the console mode's data plane alone, and the file is the same document the
console exports and imports. To grow into the console, the store, API keys, products and
analytics, start apinx the usual way and load the file:
apinx --config apinx.yaml & # console mode: SQLite store, console on :9000
apinxctl import gateway.yaml # the same file becomes the draft
apinxctl publish -m "from file mode"
Or point proxy.config_file at it: an empty store is seeded from the file on first start.
From then on the store is the source of truth and the file is an export format
(apinxctl export); see Configuration as code.
Variables are expanded when the file is read, so the import holds the values, not the
references.