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.
Create a new namespace named httpbin
with the following command:
Deploy the httpbin
sample service with this command:
Create a file named httpbin-k8s-gw.yaml
that defines a Kubernetes Gateway
resource. This will configure gateway proxies to expose port 80 (HTTP) for the httpbin.example.com
host.
By default, each Gateway
will automatically provision a Service
and Deployment
. These will be named <Gateway name>-<GatewayClass name>
(with the exception of the istio-waypoint
GatewayClass
, which does not append a suffix). These configurations will be updated automatically if the Gateway
changes (for example, if a new port is added).
Example gateway resource file
Service
type; defaults to LoadBalancer
.Apply the YAML file with this command:
Create a YAML file named httpbin-hr.yaml
that defines an HTTPRoute
resource. This resource specifies the rules for routing traffic from the gateway proxy to the httpbin
service.
Example HTTPRoute file
HTTPROUTE
resource to the Kubernetes Gateway
created previously by adding the gateway's name to the list of gateways.httpbin
service by defining a backendRefs
entry that includes the name and port of the httpbin
Service
.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:
Create a namespace for a curl
client by executing this command:
Deploy a curl
client using the command below:
Set a CURL_POD
variable with the name of the curl
pod with this command:
From the curl
client, send a request to the /headers
endpoint of the httpbin
application via the ingress gateway Service
. Set the Host
header to httpbin.example.com
to match the host specified in the Kubernetes Gateway
and HTTPROUTE
resources. Run the following curl
command:
The response should show a 200 OK
HTTP status, indicating a successful request.
Example output
Send a curl
request to an endpoint without a matching Uniform Resource Identifier (URI) prefix in the httpbin
HTTPROUTE
by running this command:
The response will be a 404 Not Found
status. This is expected, as the /get
endpoint does not have a matching URI prefix defined in the httpbin
HTTPROUTE
resource.
Example output
Expose the gateway proxy to external traffic by setting its Service
type to the default LoadBalancer
. Run this command:
Verify that the httpbin
service is accessible from outside the cluster by using the external hostname or IP address of the gateway Service
. Ensure the INGRESS_HOST
variable is set appropriately for your cluster's environment.
Set the INGRESS_HOST
variable by running this command:
Set the INGRESS_PORT
variable by running this command:
Using the gateway host, send a curl
request to the httpbin
service with this command:
Verify that the response shows the HTTP/1.1 200 OK
status, which confirms the request was successful.