Usage
Define Gateways for HAProxy Unified Gateway
Gateways are Kubernetes resources that receive traffic from outside the cluster and, working in conjunction with Route resources, direct the traffic to the correct services running inside the cluster. A Gateway is backed by a proxy and when you set the gatewayClassName field to haproxy, it will be backed by the HAProxy Unified Gateway controller as the proxy technology.
Prerequisites checklist Jump to heading
Before continuing, ensure that you’ve met these prerequisites:
Add listeners Jump to heading
When you define a Gateway, you set the types of traffic it can receive. To do so, configure one or more listeners, each specifying the application protocol it accepts. For example, here’s a partial snippet where we define listeners for HTTP, HTTPS, and TLS-protected TCP traffic. Later, we’ll see how to define these sections fully.
gateway.yamlyamlapiVersion: gateway.networking.k8s.io/v1kind: Gatewaymetadata:name: example-haproxy-gatewaynamespace: defaultspec:gatewayClassName: haproxylisteners:- name: http-listenerprotocol: HTTP...- name: https-listenerprotocol: HTTPS...- name: tls-listenerprotocol: TLS...
gateway.yamlyamlapiVersion: gateway.networking.k8s.io/v1kind: Gatewaymetadata:name: example-haproxy-gatewaynamespace: defaultspec:gatewayClassName: haproxylisteners:- name: http-listenerprotocol: HTTP...- name: https-listenerprotocol: HTTPS...- name: tls-listenerprotocol: TLS...
Let’s briefly consider these fields:
| Field | Description |
|---|---|
apiVersion |
The version of the Gateway API schema to use. |
kind |
The kind of resource to instantiate. Here, it’s Gateway. |
name |
The name of the Gateway, which Route resources will use to refer to this Gateway. |
namespace |
The Kubernetes namespace to deploy this Gateway into. |
gatewayClassName |
The Gateway’s class. The class maps to the HAProxy Unified Gateway controller. |
listeners |
A list of endpoints that accept traffic. Each sets a protocol, address, port, and other attributes. |
name |
A name for the listener, which will appear in logs. |
protocol |
A protocol type for the listener. |
The next sections describe how to define each type of listener.
Listen for HTTP traffic Jump to heading
When you add an HTTP listener, HAProxy Unified Gateway creates a proxy (in HAProxy terminology, a frontend) that accepts HTTP traffic. To define a listener that accepts HTTP traffic:
-
Add a listener that has its
protocolset toHTTP.gateway.yamlyamlapiVersion: gateway.networking.k8s.io/v1kind: Gatewaymetadata:name: example-haproxy-gatewaynamespace: defaultspec:gatewayClassName: haproxylisteners:- name: http-listenerprotocol: HTTPport: 31080allowedRoutes:kinds:- group: gateway.networking.k8s.iokind: HTTPRoutenamespaces:from: Samegateway.yamlyamlapiVersion: gateway.networking.k8s.io/v1kind: Gatewaymetadata:name: example-haproxy-gatewaynamespace: defaultspec:gatewayClassName: haproxylisteners:- name: http-listenerprotocol: HTTPport: 31080allowedRoutes:kinds:- group: gateway.networking.k8s.iokind: HTTPRoutenamespaces:from: SameIn this example, the listener named
http-listenersets these fields:Field Description protocolHTTPmeans that it will accept cleartext HTTP/1.1 traffic.portSets the HTTP port to 31080. HAProxy Unified Gateway will dynamically publish this port on its Service resource. If you set the Service’s type to be NodePort, then a random NodePort value will be assigned. allowedRoutesRestricts the kinds of Route resources that can connect to this listener. Here, we’ve configured it to accept only HTTPRoute resources that are defined in the same namespace. You can also allow routes from other namespaces. -
Apply the changes with
kubectl:nixkubectl apply -f gateway.yamlnixkubectl apply -f gateway.yamloutputtextgateway.gateway.networking.k8s.io/example-haproxy-gateway createdoutputtextgateway.gateway.networking.k8s.io/example-haproxy-gateway created -
You should now see that the HAProxy Unified Gateway service has published port 31080.
nixkubectl get svc -n haproxy-unified-gateway haproxy-unified-gatewaynixkubectl get svc -n haproxy-unified-gateway haproxy-unified-gatewayoutputtextNAME PORT(S) AGEhaproxy-unified-gateway ... 31024:31103/TCP,31060:32387/TCP,31080:31722/TCP 5m54soutputtextNAME PORT(S) AGEhaproxy-unified-gateway ... 31024:31103/TCP,31060:32387/TCP,31080:31722/TCP 5m54s -
Define one or more HTTPRoute resources that utilize the listener.
Listen for HTTPS traffic Jump to heading
When you add an HTTPS listener, HAProxy Unified Gateway creates a proxy (in HAProxy terminology, a frontend) that accepts HTTPS traffic. Clients connect via HTTPS; HAProxy Unified Gateway will terminate the TLS connection and then relay the request to the backend service over plaintext HTTP.
To define a listener that accepts HTTPS traffic:
-
On Linux, you can generate a self-signed TLS certificate for the domain
example.comwith this command:nixopenssl req -x509 -nodes -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=example.com" -days 365nixopenssl req -x509 -nodes -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=example.com" -days 365 -
Use the
kubectl create secretcommand to store your TLS certificate and key in the Kubernetes cluster:nixkubectl create secret tls example-tls --cert=tls.crt --key=tls.keynixkubectl create secret tls example-tls --cert=tls.crt --key=tls.key -
Add a listener that has its
protocolset toHTTPS. Under thetlssection, setmodetoTerminateand specify the TLS secret to terminate the TLS connection on the frontend and relay the request to the backend service as decrypted HTTP.gateway.yamlyamlapiVersion: gateway.networking.k8s.io/v1kind: Gatewaymetadata:name: example-haproxy-gatewaynamespace: defaultspec:gatewayClassName: haproxylisteners:- name: https-listenerprotocol: HTTPSport: 31443allowedRoutes:kinds:- group: gateway.networking.k8s.iokind: HTTPRoutenamespaces:from: Sametls:mode: TerminatecertificateRefs:- name: example-tlsgateway.yamlyamlapiVersion: gateway.networking.k8s.io/v1kind: Gatewaymetadata:name: example-haproxy-gatewaynamespace: defaultspec:gatewayClassName: haproxylisteners:- name: https-listenerprotocol: HTTPSport: 31443allowedRoutes:kinds:- group: gateway.networking.k8s.iokind: HTTPRoutenamespaces:from: Sametls:mode: TerminatecertificateRefs:- name: example-tlsIn this example, the listener named
https-listenersets these fields:Field Description protocolHTTPSmeans that it will accept HTTP/2 traffic protected by TLS.portSets the HTTPS port to 31443. HAProxy Unified Gateway will dynamically publish this port on its Service resource. If you set the Service’s type to be NodePort, then a random NodePort value will be assigned. allowedRoutesRestricts the kinds of Route resources that can connect to this listener. Here, we’ve configured it to accept only HTTPRoute resources that are defined in the same namespace. You can also allow routes from other namespaces. tlsSets modetoTerminateand references a TLS secret namedexample-tls. Note that you cannot use amodeofPassthroughwhenprotocolisHTTPS. -
Apply the changes with
kubectl:nixkubectl apply -f gateway.yamlnixkubectl apply -f gateway.yamloutputtextgateway.gateway.networking.k8s.io/example-haproxy-gateway configuredoutputtextgateway.gateway.networking.k8s.io/example-haproxy-gateway configured -
You should now see that the HAProxy Unified Gateway service has published port 31443.
nixkubectl get svc -n haproxy-unified-gateway haproxy-unified-gatewaynixkubectl get svc -n haproxy-unified-gateway haproxy-unified-gatewayoutputtextNAME PORT(S) AGEhaproxy-unified-gateway ... 31024:31103/TCP,31060:32387/TCP,31443:31038/TCP 12moutputtextNAME PORT(S) AGEhaproxy-unified-gateway ... 31024:31103/TCP,31060:32387/TCP,31443:31038/TCP 12m -
Define one or more HTTPRoute resources that utilize the listener.
Listen for TLS traffic Jump to heading
When you add a TLS listener, HAProxy Unified Gateway creates a proxy (in HAProxy terminology, a frontend) that accepts TCP traffic encrypted by TLS. HAProxy Unified Gateway will relay the encrypted TCP stream to the backend service without decrypting it, preserving end-to-end encryption to the backend service.
To define a listener that accepts TLS-encrypted TCP traffic:
-
Add a listener that has its
protocolset toTLS. Under thetlssection, setmodetoPassthrough.gateway.yamlyamlapiVersion: gateway.networking.k8s.io/v1kind: Gatewaymetadata:name: example-haproxy-gatewaynamespace: defaultspec:gatewayClassName: haproxylisteners:- name: tls-listenerprotocol: TLSport: 31444allowedRoutes:kinds:- group: gateway.networking.k8s.iokind: TLSRoutenamespaces:from: Sametls:mode: Passthroughgateway.yamlyamlapiVersion: gateway.networking.k8s.io/v1kind: Gatewaymetadata:name: example-haproxy-gatewaynamespace: defaultspec:gatewayClassName: haproxylisteners:- name: tls-listenerprotocol: TLSport: 31444allowedRoutes:kinds:- group: gateway.networking.k8s.iokind: TLSRoutenamespaces:from: Sametls:mode: PassthroughIn this example, the listener named
tls-listenersets these fields:Field Description protocolTLSmeans that it will accept TLS sessions over TCP.portSets the TLS port to 31444. HAProxy Unified Gateway will dynamically publish this port on its Service resource. If you set the Service’s type to be NodePort, then a random NodePort value will be assigned. allowedRoutesRestricts the kinds of Route resources that can connect to this listener. Here, we’ve configured it to accept only TLSRoute resources that are defined in the same namespace. You can also allow routes from other namespaces. tlsSets modetoPassthroughto indicate that the TLS session isn’t terminated by the Gateway. -
Apply the changes with
kubectl:nixkubectl apply -f gateway.yamlnixkubectl apply -f gateway.yamloutputtextgateway.gateway.networking.k8s.io/example-haproxy-gateway configuredoutputtextgateway.gateway.networking.k8s.io/example-haproxy-gateway configured -
You should now see that the HAProxy Unified Gateway service has published port 31444.
nixkubectl get svc -n haproxy-unified-gateway haproxy-unified-gatewaynixkubectl get svc -n haproxy-unified-gateway haproxy-unified-gatewayoutputtextNAME PORT(S) AGEhaproxy-unified-gateway ... 31024:31103/TCP,31060:32387/TCP,31444:32190/TCP 15moutputtextNAME PORT(S) AGEhaproxy-unified-gateway ... 31024:31103/TCP,31060:32387/TCP,31444:32190/TCP 15m -
Define one or more TLSRoute resources that utilize the listener.
Allow routes from other namespaces Jump to heading
When you set allowedRoutes.namespaces.from to Same, as we’ve done in previous examples, then the Gateway will accept only Route resources defined in the same namespace as the Gateway. It will ignore others. This is best when you want to deploy a Gateway to a namespace and have it handle traffic for services in that namespace only. However, you can also deploy Gateways that set from to any of these values:
| From | Description |
|---|---|
All |
Matches routes from any namespace. |
Same |
Matches routes from the same namespace where the Gateway is deployed. |
Selector |
Matches routes from namespaces matching the selector attribute. In this case, add a selector attribute to define the match criteria. |
To match route resources from other namespaces, use a selector:
-
When defining your namespace, add a label. In this example, we set a label named
shared-gateway-access.namespace.yamlyamlapiVersion: v1kind: Namespacemetadata:name: example-namespacelabels:shared-gateway-access: "true"namespace.yamlyamlapiVersion: v1kind: Namespacemetadata:name: example-namespacelabels:shared-gateway-access: "true" -
On the Gateway, set
namespaces.fromtoSelector. Also, define theselector. In the example below, the selector usesmatchLabelsto indicate that only HTTPRoute resources defined in namespaces with the labelshared-gateway-accessset totruewill be used.gateway.yamlyamlapiVersion: gateway.networking.k8s.io/v1kind: Gatewaymetadata:name: example-haproxy-gatewaynamespace: defaultspec:gatewayClassName: haproxylisteners:- name: httpprotocol: HTTPport: 31080allowedRoutes:kinds:- group: gateway.networking.k8s.iokind: HTTPRoutenamespaces:from: Selectorselector:matchLabels:shared-gateway-access: "true"gateway.yamlyamlapiVersion: gateway.networking.k8s.io/v1kind: Gatewaymetadata:name: example-haproxy-gatewaynamespace: defaultspec:gatewayClassName: haproxylisteners:- name: httpprotocol: HTTPport: 31080allowedRoutes:kinds:- group: gateway.networking.k8s.iokind: HTTPRoutenamespaces:from: Selectorselector:matchLabels:shared-gateway-access: "true"
See also Jump to heading
- Refer to the Gateway API’s Gateway documentation.
- Learn more about cross-namespace routing in the Gateway API guide, Cross-namespace Route Attachment.