Exposing a service via Kubernetes Gateway API
You can use the Kubernetes Gateway API to create Gateway and HTTPRoute resources to deploy a gateway. These resources configure the gateway to make a service inside the mesh accessible to traffic from outside. You can then change the gateway's Service to LoadBalancer to expose it to traffic external to the cluster.
TOC
Prerequisites
- Alauda Service Mesh v2 Operator is installed.
- An Istio control plane is deployed.
Procedure
-
Create a new namespace named
httpbinwith the following command: -
Deploy the
httpbinsample service with this command: -
Create a file named
httpbin-k8s-gw.yamlthat defines a KubernetesGatewayresource. This will configure gateway proxies to expose port 80 (HTTP) for thehttpbin.example.comhost.Automated deploymentBy default, each
Gatewaywill automatically provision aServiceandDeployment. These will be named<Gateway name>-<GatewayClass name>(with the exception of theistio-waypointGatewayClass, which does not append a suffix). These configurations will be updated automatically if theGatewaychanges (for example, if a new port is added).Example gateway resource file
- Specifies the gateway's
Servicetype; defaults toLoadBalancer. - Specifies the gateway's name.
- Specifies the gateway's namespace.
- Specifies the virtual hostname for clients to use when accessing a mesh service on this port.
- Specifies the gateway's
-
Apply the YAML file with this command:
-
Create a YAML file named
httpbin-hr.yamlthat defines anHTTPRouteresource. This resource specifies the rules for routing traffic from the gateway proxy to thehttpbinservice.Example HTTPRoute file
- Links the
HTTPROUTEresource to the KubernetesGatewaycreated previously by adding the gateway's name to the list of gateways. - Directs matching traffic to the
httpbinservice by defining abackendRefsentry that includes the name and port of thehttpbinService.
- Links the
-
Apply the YAML file by executing this command:
-
Confirm that the Gateway API service is ready and has an allocated address by running this command:
Verification
-
Create a namespace for a
curlclient by executing this command: -
Deploy a
curlclient using the command below: -
Set a
CURL_PODvariable with the name of thecurlpod with this command: -
From the
curlclient, send a request to the/headersendpoint of thehttpbinapplication via the ingress gatewayService. Set theHostheader tohttpbin.example.comto match the host specified in the KubernetesGatewayandHTTPROUTEresources. Run the followingcurlcommand:The response should show a
200 OKHTTP status, indicating a successful request.Example output
-
Send a
curlrequest to an endpoint without a matching Uniform Resource Identifier (URI) prefix in thehttpbinHTTPROUTEby running this command:The response will be a
404 Not Foundstatus. This is expected, as the/getendpoint does not have a matching URI prefix defined in thehttpbinHTTPROUTEresource.Example output
-
Expose the gateway proxy to external traffic by setting its
Servicetype to the defaultLoadBalancer. Run this command: -
Verify that the
httpbinservice is accessible from outside the cluster by using the external hostname or IP address of the gatewayService. Ensure theINGRESS_HOSTvariable is set appropriately for your cluster's environment.a. Set the
INGRESS_HOSTvariable by running this command:b. Set the
INGRESS_PORTvariable by running this command:c. Using the gateway host, send a
curlrequest to thehttpbinservice with this command: -
Verify that the response shows the
HTTP/1.1 200 OKstatus, which confirms the request was successful.