HTTP
Create an HTTP listener on your gateway proxy. Your proxy listens for HTTP traffic on the specified port and hostname that you configure.
Before you begin
-
Deploy the httpbin sample app.
-
Decide whether to set up a listener inline on the Gateway resource or as a separate ListenerSet resource. For more information, see the Listener overview.
ListenerSets: To use ListenerSets, you must install the experimental channel of the Kubernetes Gateway API.
kubectl apply --server-side -f https://github.com/kubernetes-sigs/gateway-api/releases/download/v1.4.0/experimental-install.yamlYou must also ensure that you installed agentgateway with the
--set controller.extraEnv.KGW_ENABLE_GATEWAY_API_EXPERIMENTAL_FEATURES=trueHelm flag to use experimental Kubernetes Gateway API features. For an example, see the Get started guide.
Set up an HTTP listener
Set up an HTTP listener on your Gateway.
-
Create a Gateway resource with an HTTP listener.
kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: agentgateway-proxy-http namespace: agentgateway-system spec: gatewayClassName: agentgateway listeners: - protocol: HTTP port: 80 name: http allowedRoutes: namespaces: from: All EOFReview the following table to understand this configuration.
Setting Description spec.gatewayClassNameThe name of the Kubernetes GatewayClass that you want to use to configure the Gateway. When you set up agentgateway, a default GatewayClass is set up for you. spec.listenersConfigure the listeners for this Gateway. In this example, you configure an HTTP Gateway that listens for incoming traffic on port 80. The Gateway can serve HTTPRoutes from any namespace. -
Create a Gateway that enables the attachment of ListenerSets.
kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: Gateway metadata: name: agentgateway-proxy-http namespace: agentgateway-system spec: gatewayClassName: agentgateway allowedListeners: namespaces: from: All listeners: - protocol: HTTP port: 8080 name: http-mock allowedRoutes: namespaces: from: All EOFReview the following table to understand this configuration.
Setting Description spec.gatewayClassNameThe name of the Kubernetes GatewayClass that you want to use to configure the Gateway. When you set up agentgateway, a default GatewayClass is set up for you. spec.allowedListenersEnable the attachment of ListenerSets to this Gateway. The example allows listeners from any namespace, which is helpful in multitenant environments. You can also limit the allowed listeners. To limit to listeners in the same namespace as the Gateway, set this value to Same. To limit to listeners with a particular label, set this value toSelector.spec.listenersOptionally, you can configure a listener that is specific to the Gateway. Note that due to a Gateway API limitation, you must configure at least one listener on the Gateway resource, even if the listener is not used and is a generic, “mock” listener. This generic listener cannot conflict with the listener that you configure in the ListenerSet, such as using the same port or name. In this example, the generic listener is configured on port 8080, which differs from port 80 in the ListenerSet that you create later. -
Create a ListenerSet that configures an HTTP listener for the Gateway.
kubectl apply -f- <<EOF apiVersion: gateway.networking.x-k8s.io/v1alpha1 kind: XListenerSet metadata: name: http-listenerset namespace: httpbin spec: parentRef: name: agentgateway-proxy-http namespace: agentgateway-system kind: Gateway group: gateway.networking.k8s.io listeners: - protocol: HTTP port: 80 name: http allowedRoutes: namespaces: from: All EOFReview the following table to understand this configuration.
Setting Description spec.parentRefThe name of the Gateway to attach the ListenerSet to. spec.listenersConfigure the listeners for this ListenerSet. In this example, you configure an HTTP gateway that listens for incoming traffic on port 80. The Gateway can serve HTTPRoutes from any namespace.
-
-
Check the status of the Gateway to make sure that your configuration is accepted. Note that in the output, a
NoConflictsstatus ofFalseindicates that the Gateway is accepted and does not conflict with other Gateway configuration.kubectl get gateway agentgateway-proxy-http -n agentgateway-system -o yaml -
Create an HTTPRoute resource for the httpbin app that is served by the Gateway that you created.
kubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-route namespace: httpbin spec: hostnames: - listener.example parentRefs: - name: agentgateway-proxy-http namespace: agentgateway-system rules: - backendRefs: - name: httpbin port: 8000 EOFkubectl apply -f- <<EOF apiVersion: gateway.networking.k8s.io/v1 kind: HTTPRoute metadata: name: httpbin-route namespace: httpbin spec: parentRefs: - name: http-listenerset namespace: httpbin kind: XListenerSet group: gateway.networking.x-k8s.io rules: - backendRefs: - name: httpbin port: 8000 EOF -
Verify that the HTTPRoute is applied successfully.
kubectl get httproute/httpbin-route -n httpbin -o yamlExample output: Notice in the
statussection that theparentRefis either the Gateway or the ListenerSet, depending on how you attached the HTTPRoute.... status: parents: - conditions: ... parentRef: group: gateway.networking.k8s.io kind: Gateway name: agentgateway-proxy-http namespace: agentgateway-system -
Verify that the listener now has a route attached.
kubectl get gateway -n agentgateway-system agentgateway-proxy-http -o yamlExample output:
... listeners: - attachedRoutes: 1kubectl get xlistenerset -n httpbin http-listenerset -o yamlExample output:
... listeners: - attachedRoutes: 1Note that because the HTTPRoute is attached to the ListenerSet, the Gateway does not show the route in its status.
kubectl get gateway -n agentgateway-system agentgateway-proxy-http -o yamlExample output:
... listeners: - attachedRoutes: 0If you create another HTTPRoute that attaches to the Gateway and uses the same listener as the ListenerSet, then the route is reported in the status of both the Gateway (attachedRoutes: 1) and the ListenerSet (attachedRoutes: 2).
-
Get the external address of the gateway and save it in an environment variable.
export INGRESS_GW_ADDRESS=$(kubectl get svc -n agentgateway-system agentgateway-proxy-http -o jsonpath="{.status.loadBalancer.ingress[0]['hostname','ip']}") echo $INGRESS_GW_ADDRESSkubectl port-forward deployment/agentgateway-proxy-http -n agentgateway-system 8080:80 -
Send a request to the httpbin app and verify that you get back a 200 HTTP response code.
curl -vi http://$INGRESS_GW_ADDRESS:80/status/200 -H "host: listener.example"curl -vi localhost:8080/status/200 -H "host: listener.example"Example output:
* Mark bundle as not supporting multiuse < HTTP/1.1 200 OK HTTP/1.1 200 OK < access-control-allow-credentials: true access-control-allow-credentials: true < access-control-allow-origin: * access-control-allow-origin: * < date: Fri, 03 Nov 2023 20:02:48 GMT date: Fri, 03 Nov 2023 20:02:48 GMT < content-length: 0 content-length: 0 < x-envoy-upstream-service-time: 1 x-envoy-upstream-service-time: 1 < server: envoy server: envoy
Cleanup
You can remove the resources that you created in this guide.kubectl delete gateway agentgateway-proxy-http -n agentgateway-system
kubectl delete httproute httpbin-route -n httpbinkubectl delete gateway agentgateway-proxy-http -n agentgateway-system
kubectl delete httproute httpbin-route -n httpbin
kubectl delete xlistenersets http-listenerset -n httpbin