How-to guide / Platform capabilities
Expose a service with Gateway API
These docs describe Kix's main branch, which can include changes not yet released. Read the docs for the latest release.
Use kix.expose to create an HTTPRoute for a package Service when the cluster
uses Gateway API.
This guide assumes:
- Your cluster declares a
gateway-apiinstance to install the Gateway API CRDs and anenvoy-gateway-controllerinstance to run Envoy Gateway (see Install Envoy Gateway). - You have a local package whose returned parts include a Service named
service.
The envoy-gateway package used below creates a Gateway managed by
envoy-gateway-controller.
Make the package exposable
Section titled “Make the package exposable”Add the standard ingress options to the package:
21 options.ingress = kix.options.ingress;The option name is ingress for both supported exposure resources. Set
config.ingress.host on an instance to choose its hostname.
Add kix.expose to the package’s build list and name the Service part it
should route to:
56 (kix.expose { service = "service"; })kix.expose reads the Service name and port from that part.
Create the Gateway
Section titled “Create the Gateway”Add a Gateway instance with an HTTP listener:
32 instances.gateway-system.gateway = {33 package = packages.experimental.envoy-gateway;34 config.listeners = [35 {36 name = "http";37 port = 80;38 protocol = "HTTP";39 allowedRoutes.namespaces.from = "All";40 }41 ];42 };The Gateway is in gateway-system, while the application in the next step is
in another namespace. allowedRoutes.namespaces.from = "All" permits that
HTTPRoute to attach to this listener.
The listener’s protocol, hostname, and allowedRoutes also decide the
application’s public URL (self.exposure.url): an HTTP listener on port
80 gives http://<host>. If no listener accepts the host from the
application’s namespace, evaluation fails and names each listener.
To use Traefik instead of Envoy Gateway, set package = packages.experimental.traefik;
on the Gateway instance and remove the envoy-gateway-controller instance.
The application configuration stays the same.
Set the application hostname
Section titled “Set the application hostname”Add the application instance and set the host clients will request:
46 instances.gateway-example.web = {47 package = exposureApp;48 config.ingress.host = "web.example.test";49 };Kix resolves the gateway dependency and creates an HTTPRoute for the
application’s Service.
Check the result
Section titled “Check the result”Evaluate the cluster before deploying it:
❱ kix check how-to-platform-gateway
TOOL RESULT DETAILS
eval pass 55 manifests evaluated
scorecard pass 0 errors, 9 warnings, 1 info
governance.hasOwner (warning), 1:
gateway-example/web: package 'web' has no meta.owner
reliability.boundedResources (warning), 2:
gateway-example/web/web: container 'nginx' is missing resource settings: requests.cpu, requests.memory, limits.cpu, limits.memory. Set CPU and memory requests and limits. Without a memory limit, a workload can exhaust the node's memory.
gateway-system/envoy-gateway-controller/envoy-gateway: container 'envoy-gateway' is missing resource settings: limits.cpu. Set CPU and memory requests and limits.
reliability.hasProbes (warning), 2:
gateway-example/web/web: container 'nginx' has no livenessProbe
gateway-example/web/web: container 'nginx' has no readinessProbe
security.hasNetworkPolicy (warning), 2:
gateway-example: namespace 'gateway-example' has workloads but no network policy
gateway-system: namespace 'gateway-system' has workloads but no network policy
security.nonRoot (warning), 1:
gateway-example/web/web: container 'nginx' may run as root: neither it nor the pod sets securityContext.runAsNonRoot, and no non-zero runAsUser resolves for it
security.readOnlyRootfs (warning), 1:
gateway-example/web/web: container 'nginx' does not set readOnlyRootFilesystem
1 info finding(s) not listed; run with -v to list them. Inspect the generated HTTPRoute:
❱ kix build how-to-platform-gateway --output json
{
"apiVersion": "gateway.networking.k8s.io/v1",
"kind": "HTTPRoute",
"metadata": {
"name": "web",
"namespace": "gateway-example"
},
"spec": {
"hostnames": [
"web.example.test"
],
"parentRefs": [
{
"name": "gateway",
"namespace": "gateway-system"
}
],
"rules": [
{
"backendRefs": [
{
"name": "web",
"port": 80
}
],
"matches": [
{
"path": {
"type": "PathPrefix",
"value": "/"
}
}
]
}
]
}
} The route attaches to the gateway-system/gateway Gateway and sends requests
for web.example.test to port 80 of the web Service.
Route several paths
Section titled “Route several paths”Pass paths instead of service when one hostname fans out to several
Service parts. Each entry names a path and the Service part behind it;
pathType defaults to Prefix and port to the port the Service part
publishes through out.port:
1(kix.expose {2 paths = [3 { path = "/"; service = "frontend"; }4 { path = "/api"; pathType = "Exact"; service = "api"; }5 ];6})Kix renders one HTTPRoute with a rule per entry. In a cluster that routes
through ingress-nginx, the same paths list renders one Ingress with a path
entry per element.