Gateway API tutorials
Enable the Gateway API
Available since
version 1.10
In this section, you will learn how to enable Gateway API in by installing GatewayClasses, Gateways, and Routes.
The Gateway API is a new way to define network routing in Kubernetes. HAProxy Kubernetes Ingress Controller implements this API alongside its support for the older and established Ingress API.
With objects meant to promote a separation of concerns between platform engineers, cluster operators, and application developers, Gateway API can improve workflows and access control, whereas those lines are more blurred in Ingress API.
For example, in Ingress API, the same person will often choose which ingress controllers to use and then deploy them into the cluster. In Gateway API, one person in the organization can handle choosing which Gateway API implementations to make available, and then someone else can pick which ones to use from that list.
Deploy Gateway API resources Jump to heading
Install the resources that enable Gateway API functionality in your cluster:
-
Deploy the Gateway API custom resource definitions:
nixkubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v0.5.1/experimental-install.yamlnixkubectl apply -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v0.5.1/experimental-install.yaml
Update your ingress controller Jump to heading
Update your HAProxy Kubernetes Ingress Controller deployment to support Gateway API.
- Set the
--gateway-controller-name
startup argument. - Add any TCP ports at which you will accept traffic to the ingress controller’s Service definition.
Update with Helm Jump to heading
If using Helm, create an override.yaml
file that creates the necessary ClusterRoleBinding object and update the ingress controller Deployment to have the --gateway-controller-name
argument. You will also need to add any ports at which you will listen for TCP traffic.
-
Create an
override.yaml
file that enables Gateway API and sets TCP ports where the controller should listen.override.yamlyamlcontroller:kubernetesGateway:enabled: truegatewayControllerName: haproxy.org/gateway-controllerservice:tcpPorts:- name: listener1protocol: TCPport: 8000targetPort: 8000override.yamlyamlcontroller:kubernetesGateway:enabled: truegatewayControllerName: haproxy.org/gateway-controllerservice:tcpPorts:- name: listener1protocol: TCPport: 8000targetPort: 8000 -
Use
helm upgrade
to make the changes.nixhelm upgrade haproxy-kubernetes-ingress haproxytech/kubernetes-ingress \--namespace haproxy-controller \-f override.yamlnixhelm upgrade haproxy-kubernetes-ingress haproxytech/kubernetes-ingress \--namespace haproxy-controller \-f override.yamlnixhelm upgrade haproxy-kubernetes-ingress haproxytech/kubernetes-ingress \--create-namespace \--namespace haproxy-controller \--set controller.imageCredentials.registry=kubernetes-registry.haproxy.com \--set controller.imageCredentials.username=<KEY> \--set controller.imageCredentials.password=<KEY> \--set controller.image.repository=kubernetes-registry.haproxy.com/hapee-ingress \--set controller.image.tag=v1.11 \-f override.yamlnixhelm upgrade haproxy-kubernetes-ingress haproxytech/kubernetes-ingress \--create-namespace \--namespace haproxy-controller \--set controller.imageCredentials.registry=kubernetes-registry.haproxy.com \--set controller.imageCredentials.username=<KEY> \--set controller.imageCredentials.password=<KEY> \--set controller.image.repository=kubernetes-registry.haproxy.com/hapee-ingress \--set controller.image.tag=v1.11 \-f override.yaml
Update with kubectl Jump to heading
To enable Gateway API features with kubectl
:
-
Download the deployment YAML file.
Product Link Community version Deployment YAML (v3.0) Enterprise version Deployment YAML file (v1.11) -
Edit the YAML file’s
Deployment
object to have the--gateway-controller-name
argument:haproxy-ingress.yamlyamlargs:- --gateway-controller-name=haproxy.org/gateway-controllerhaproxy-ingress.yamlyamlargs:- --gateway-controller-name=haproxy.org/gateway-controller -
Also update the
haproxy-kubernetes-ingress
Service
object to list listening ports. In the example below, we add listener1:haproxy-ingress.yamlyamlspec:selector:run: haproxy-ingresstype: NodePortports:- name: httpport: 80protocol: TCPtargetPort: 80- name: httpsport: 443protocol: TCPtargetPort: 443- name: statport: 1024protocol: TCPtargetPort: 1024- name: listener1protocol: TCPport: 8000targetPort: 8000haproxy-ingress.yamlyamlspec:selector:run: haproxy-ingresstype: NodePortports:- name: httpport: 80protocol: TCPtargetPort: 80- name: httpsport: 443protocol: TCPtargetPort: 443- name: statport: 1024protocol: TCPtargetPort: 1024- name: listener1protocol: TCPport: 8000targetPort: 8000Apply the changes with
kubectl apply
:nixkubectl apply -f haproxy-ingress.yamlnixkubectl apply -f haproxy-ingress.yaml -
Deploy the HAProxy Kubernetes Ingress Controller RBAC resources, which give the
haproxy-kubernetes-ingress
ServiceAccount permissions to use the Gateway API resource types:nixkubectl apply -f https://raw.githubusercontent.com/haproxytech/kubernetes-ingress/master/deploy/tests/config/experimental/gwapi-rbac.yamlnixkubectl apply -f https://raw.githubusercontent.com/haproxytech/kubernetes-ingress/master/deploy/tests/config/experimental/gwapi-rbac.yaml
Define a GatewayClass Jump to heading
A GatewayClass makes a Gateway API implementation available so that cluster operators can use it. Platform engineers can be responsible for this, defining GatewayClass objects that are available in the cluster.
To deploy a GatewayClass for HAProxy Kubernetes Ingress Controller:
-
Create a file named
haproxy-ingress-gatewayclass.yaml
to define a GatewayClass:haproxy-ingress-gatewayclass.yamlyamlapiVersion: gateway.networking.k8s.io/v1alpha2kind: GatewayClassmetadata:namespace: defaultname: haproxy-ingress-gatewayclassspec:controllerName: haproxy.org/gateway-controllerhaproxy-ingress-gatewayclass.yamlyamlapiVersion: gateway.networking.k8s.io/v1alpha2kind: GatewayClassmetadata:namespace: defaultname: haproxy-ingress-gatewayclassspec:controllerName: haproxy.org/gateway-controllerFrom this snippet:
- The
name
attribute will uniquely identify this GatewayClass in the cluster. - The
controllerName
attribute refers to the name you set with--gateway-controller-name
.
Apply the changes with
kubectl
:nixkubectl apply -f haproxy-ingress-gatewayclass.yamlnixkubectl apply -f haproxy-ingress-gatewayclass.yaml - The
Define a Gateway Jump to heading
With Gateway
objects, cluster operators can choose which Gateway API implementations to use.
To create a Gateway that listens on port 8000 and handles routing for applications in the default namespace:
-
Create a file that defines a Gateway object.
Below, in a file named
example-gateway.yaml
, we define a Gateway that uses the ingress controller referred to by thehaproxy-ingress-gatewayclass
GatewayClass:example-gateway.yamlyamlapiVersion: gateway.networking.k8s.io/v1alpha2kind: Gatewaymetadata:name: example-gatewaynamespace: defaultspec:gatewayClassName: haproxy-ingress-gatewayclasslisteners:- allowedRoutes:kinds:- group: gateway.networking.k8s.iokind: TCPRoutenamespaces:from: Allname: listener1port: 8000protocol: TCPexample-gateway.yamlyamlapiVersion: gateway.networking.k8s.io/v1alpha2kind: Gatewaymetadata:name: example-gatewaynamespace: defaultspec:gatewayClassName: haproxy-ingress-gatewayclasslisteners:- allowedRoutes:kinds:- group: gateway.networking.k8s.iokind: TCPRoutenamespaces:from: Allname: listener1port: 8000protocol: TCPIn this example, the Gateway is deployed to the default namespace, and it will accept routes from all namespaces. Cluster operators can also deploy Gateways that accept routes from only the same namespace or specific namespaces by changing the
allowedRoutes.namespaces
section to have afrom
attribute of either:Value 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.Apply the changes with
kubectl
:nixkubectl apply -f example-gateway.yamlnixkubectl apply -f example-gateway.yaml
Define routes Jump to heading
After platform engineers have deployed the Gateway API resources, HAProxy Kubernetes Ingress Controller, and a GatewayClass, and after cluster operators have defined a Gateway that uses that GatewayClass, then application developers can define routes that make use of the Gateway.
Setting allowed routes
Earlier when defining the Gateway, we set allowedRoutes
to accept routes of kind TCPRoute
. This means that only those types of routes will be handled by that Gateway.
To define routing for TCP traffic to an application named example-service:
-
Create a file named
example-route.yaml
with the following contents:example-route.yamlyamlapiVersion: gateway.networking.k8s.io/v1alpha2kind: TCPRoutemetadata:name: example-routenamespace: defaultspec:parentRefs:- group: gateway.networking.k8s.iokind: Gatewayname: example-gatewaynamespace: defaultrules:- backendRefs:- group: ''kind: Servicename: example-serviceport: 80weight: 10example-route.yamlyamlapiVersion: gateway.networking.k8s.io/v1alpha2kind: TCPRoutemetadata:name: example-routenamespace: defaultspec:parentRefs:- group: gateway.networking.k8s.iokind: Gatewayname: example-gatewaynamespace: defaultrules:- backendRefs:- group: ''kind: Servicename: example-serviceport: 80weight: 10Apply the changes with
kubectl
:nixkubectl apply -f example-route.yamlnixkubectl apply -f example-route.yaml -
Create a file named
example-service.yaml
to define an example application for the route:example-service.yamlyamlapiVersion: apps/v1kind: Deploymentmetadata:namespace: defaultlabels:run: appname: appspec:replicas: 1selector:matchLabels:run: apptemplate:metadata:labels:run: appspec:containers:- name: appimage: jmalloc/echo-serverports:- containerPort: 8080readinessProbe:httpGet:path: /port: 8080initialDelaySeconds: 5periodSeconds: 5successThreshold: 1---apiVersion: v1kind: Servicemetadata:namespace: defaultname: example-servicespec:selector:run: appports:- name: httpprotocol: TCPport: 80targetPort: 8080example-service.yamlyamlapiVersion: apps/v1kind: Deploymentmetadata:namespace: defaultlabels:run: appname: appspec:replicas: 1selector:matchLabels:run: apptemplate:metadata:labels:run: appspec:containers:- name: appimage: jmalloc/echo-serverports:- containerPort: 8080readinessProbe:httpGet:path: /port: 8080initialDelaySeconds: 5periodSeconds: 5successThreshold: 1---apiVersion: v1kind: Servicemetadata:namespace: defaultname: example-servicespec:selector:run: appports:- name: httpprotocol: TCPport: 80targetPort: 8080Apply the changes with
kubectl
:nixkubectl apply -f example-service.yamlnixkubectl apply -f example-service.yaml
Do you have any suggestions on how we can improve the content of this page?