Skip to content
Install the CLI

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-api instance to install the Gateway API CRDs and an envoy-gateway-controller instance 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.

Add the standard ingress options to the package:

how-to/platform/exposure-app.nix
options.ingress = kix.options.ingress;

View source on GitHub ↗

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:

how-to/platform/exposure-app.nix
(kix.expose { service = "service"; })

View source on GitHub ↗

kix.expose reads the Service name and port from that part.

Add a Gateway instance with an HTTP listener:

how-to/platform/gateway-cluster.nix
instances.gateway-system.gateway = {
package = packages.experimental.envoy-gateway;
config.listeners = [
{
name = "http";
port = 80;
protocol = "HTTP";
allowedRoutes.namespaces.from = "All";
}
];
};

View source on GitHub ↗

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.

Add the application instance and set the host clients will request:

how-to/platform/gateway-cluster.nix
instances.gateway-example.web = {
package = exposureApp;
config.ingress.host = "web.example.test";
};

View source on GitHub ↗

Kix resolves the gateway dependency and creates an HTTPRoute for the application’s Service.

Evaluate the cluster before deploying it:

kix-examples/
❱ 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-examples/ output excerpt
❱ 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.

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:

(kix.expose {
paths = [
{ path = "/"; service = "frontend"; }
{ path = "/api"; pathType = "Exact"; service = "api"; }
];
})

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.