Sub topic 2
Three roles, three resources
This chapter assumes you know Pods, Services, Namespaces, and labels. You should also be able to apply YAML with kubectl and know what an HTTP host, path, and header are.
Why the model has three resources
Ingress puts everything into one object. The same manifest holds the controller choice, the listening port, the TLS settings, and the application routing rules. In a shared cluster, that forces one team to own all of it, or forces teams to edit each other's files.
The Gateway API splits the work along the lines where responsibility usually changes hands. Each resource has a typical owner.
| Role | Typical owner | Resource | Scope |
|---|---|---|---|
| Infrastructure provider | The platform vendor or cloud team | GatewayClass | Cluster |
| Cluster operator | The platform or networking team | Gateway | Namespace |
| Application developer | The team that ships a service | HTTPRoute | Namespace |
These roles are a convention. One person can fill all three. The split matters because Kubernetes RBAC (role-based access control) can grant each resource separately. Developers can edit HTTPRoutes without being able to change ports or certificates on a Gateway.
How the resources relate
A GatewayClass names a controller. A Gateway refers to a GatewayClass by name. An HTTPRoute refers to a Gateway through parentRefs. Each link points from the more specific object to the more general one.
The resources are only declarations. A controller that implements the API watches them and configures the real proxy or load balancer. Without an installed controller, the objects exist but nothing handles traffic.
The three resources in YAML
The infrastructure provider supplies the GatewayClass. It is cluster-scoped, so it has no namespace. The controllerName value is defined by the implementation you install. The value below is a placeholder.
apiVersion: gateway.networking.k8s.io/v1
kind: GatewayClass
metadata:
name: example-class
spec:
controllerName: example.com/gateway-controllerThe cluster operator creates a Gateway that uses that class. It lives in the infra namespace and listens for HTTP on port 80.
apiVersion: gateway.networking.k8s.io/v1
kind: Gateway
metadata:
name: web-gateway
namespace: infra
spec:
gatewayClassName: example-class
listeners:
- name: http
protocol: HTTP
port: 80
allowedRoutes:
namespaces:
from: Selector
selector:
matchLabels:
shared-gateway-access: "true"The allowedRoutes block limits which namespaces may attach routes. A later chapter covers it. For now, note that the operator controls this, not the developer.
The application developer then writes an HTTPRoute in their own namespace. It attaches to the Gateway and sends /cart requests to a Service.
apiVersion: gateway.networking.k8s.io/v1
kind: HTTPRoute
metadata:
name: shop
namespace: shop-team
spec:
parentRefs:
- name: web-gateway
namespace: infra
hostnames:
- shop.example.com
rules:
- matches:
- path:
type: PathPrefix
value: /cart
backendRefs:
- name: cart-svc
port: 8080For this route to work, the shop-team namespace needs the label shared-gateway-access=true. Otherwise the Gateway refuses the route.
Common mistakes
- Expecting a GatewayClass to do work on its own. It only names a controller. A matching controller must be running.
- Putting a namespace on a GatewayClass. It is cluster-scoped.
- Forgetting the namespace in
parentRefs. If the Gateway is in another namespace, the route must name it. Otherwise the route looks in its own namespace. - Mixing up ownership. Listeners belong in the Gateway. Matching rules belong in the HTTPRoute.
Exercise
A developer in the shop-team namespace can create HTTPRoutes but not Gateways. They need HTTP traffic on a new port, 8081. Which resource must change, who should change it, and why?
Show answer
The Gateway must change, because ports and protocols are defined by its listeners. The cluster operator should add a listener for port 8081. The developer's HTTPRoute then attaches to it, subject to allowedRoutes.